Skip to content

Commit

Permalink
fix(table): fix rendering table in ascii-only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
andreynering committed Feb 25, 2025
1 parent b0776ab commit d394540
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
66 changes: 65 additions & 1 deletion glamour_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package glamour
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"regexp"
"strings"
"testing"

styles "github.com/charmbracelet/glamour/styles"
"github.com/charmbracelet/glamour/styles"
"github.com/charmbracelet/x/exp/golden"
)

Expand Down Expand Up @@ -218,3 +220,65 @@ func FuzzData(f *testing.F) {
}()
})
}

func TestTableAscii(t *testing.T) {
markdown := strings.TrimSpace(`
| Header A | Header B |
| --------- | --------- |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
| Cell 5 | Cell 6 |
`)

renderer, err := NewTermRenderer(
WithStyles(styles.ASCIIStyleConfig),
WithWordWrap(80),
)
if err != nil {
t.Fatal(err)
}

result, err := renderer.Render(markdown)
if err != nil {
t.Fatal(err)
}

nonAsciiRegexp := regexp.MustCompile(`[^\x00-\x7f]+`)
nonAsciiChars := nonAsciiRegexp.FindAllString(result, -1)
if len(nonAsciiChars) > 0 {
t.Errorf("Non-ASCII characters found in output: %v", nonAsciiChars)
}
}

func ExampleASCIIStyleConfig() {
markdown := strings.TrimSpace(`
| Header A | Header B |
| --------- | --------- |
| Cell 1 | Cell 2 |
| Cell 3 | Cell 4 |
| Cell 5 | Cell 6 |
`)

renderer, err := NewTermRenderer(
WithStyles(styles.ASCIIStyleConfig),
WithWordWrap(80),
)
if err != nil {
return
}

result, err := renderer.Render(markdown)
if err != nil {
return
}
result = strings.ReplaceAll(result, " ", ".")
fmt.Println(result)

// Output:
// ..............................................................................
// ..Header.A..............................|Header.B.............................
// ..--------------------------------------|-------------------------------------
// ..Cell.1................................|Cell.2...............................
// ..Cell.3................................|Cell.4...............................
// ..Cell.5................................|Cell.6...............................
}
6 changes: 5 additions & 1 deletion styles/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,11 @@ var (
Margin: uintPtr(defaultMargin),
},
},
Table: ansi.StyleTable{},
Table: ansi.StyleTable{
CenterSeparator: stringPtr("|"),
ColumnSeparator: stringPtr("|"),
RowSeparator: stringPtr("-"),
},
DefinitionDescription: ansi.StylePrimitive{
BlockPrefix: "\n* ",
},
Expand Down

0 comments on commit d394540

Please sign in to comment.