Skip to content

Commit

Permalink
chore: fixup
Browse files Browse the repository at this point in the history
Signed-off-by: moul <[email protected]>
  • Loading branch information
moul committed Nov 9, 2024
1 parent 9497156 commit f230fa3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
18 changes: 14 additions & 4 deletions examples/gno.land/p/moul/mdtable/mdtable.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
// table.Append([]string{"#2", "Change parameter", "timed out", "2024-01-02"})
// return table.String()
// }
//
// Output:
//
// | ID | Title | Status | Date |
// | --- | --- | --- | --- |
// | #1 | Add a new validator | succeed | 2024-01-01 |
// | #2 | Change parameter | timed out | 2024-01-02 |
package mdtable

import (
Expand All @@ -21,14 +28,17 @@ import (
type Table struct {
Headers []string
Rows [][]string
// XXX: Headers alignment option
// XXX: optional headers alignment.
}

func (t *Table) Append(row []string) {
t.Rows = append(t.Rows, row)
}

func (t Table) String() string {
// XXX: switch to using text/tabwriter when porting to Gno to support
// better-formatted raw Markdown output.

if len(t.Headers) == 0 && len(t.Rows) == 0 {
return ""
}
Expand All @@ -39,15 +49,15 @@ func (t Table) String() string {
t.Headers = make([]string, len(t.Rows[0]))
}

// Print header
// Print header.
sb.WriteString("| " + strings.Join(t.Headers, " | ") + " |\n")
sb.WriteString("|" + strings.Repeat(" --- |", len(t.Headers)) + "\n")

// Print rows
// Print rows.
for _, row := range t.Rows {
escapedRow := make([]string, len(row))
for i, cell := range row {
escapedRow[i] = strings.ReplaceAll(cell, "|", "&#124;") // Escape pipe characters
escapedRow[i] = strings.ReplaceAll(cell, "|", "&#124;") // Escape pipe characters.
}
sb.WriteString("| " + strings.Join(escapedRow, " | ") + " |\n")
}
Expand Down
2 changes: 1 addition & 1 deletion examples/gno.land/p/moul/mdtable/mdtable_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"gno.land/p/moul/mdtable"
)

// XXX: switch to `func Example() {}`
// XXX: switch to `func Example() {}` when supported.
func TestExample(t *testing.T) {
table := mdtable.Table{
Headers: []string{"ID", "Title", "Status"},
Expand Down

0 comments on commit f230fa3

Please sign in to comment.