Skip to content

Commit

Permalink
table: option to force text direction for tables with BiDi content (#230
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jedib0t committed Sep 25, 2022
1 parent fb89930 commit d28aad5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 4 deletions.
1 change: 1 addition & 0 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ func (t *Table) renderLineMergeOutputs(out *strings.Builder, outLine *strings.Bu
}

func (t *Table) renderMarginLeft(out *strings.Builder, hint renderHint) {
out.WriteString(t.style.Format.Direction.Modifier())
if t.style.Options.DrawBorder {
border := t.getBorderLeft(hint)
colors := t.getBorderColors(hint)
Expand Down
43 changes: 43 additions & 0 deletions table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,49 @@ func TestTable_Render_AutoMerge_Wrapped(t *testing.T) {
└───┴──────────────────────────────┴───────────┴───────────────────┘`)
}

func TestTable_Render_BiDiText(t *testing.T) {
table := Table{}
table.AppendHeader(Row{"תאריך", "סכום", "מחלקה", "תגים"})
table.AppendRow(Row{"2020-01-01", 5.0, "מחלקה1", []string{"תג1", "תג2"}})
table.AppendRow(Row{"2021-02-01", 5.0, "מחלקה1", []string{"תג1"}})
table.AppendRow(Row{"2022-03-01", 5.0, "מחלקה2", []string{"תג1"}})
table.AppendFooter(Row{"סהכ", 30})
table.SetAutoIndex(true)

//table.Style().Format.Direction = text.Default
compareOutput(t, table.Render(), `+---+------------+------+--------+-----------+
| | תאריך | סכום | מחלקה | תגים |
+---+------------+------+--------+-----------+
| 1 | 2020-01-01 | 5 | מחלקה1 | [תג1 תג2] |
| 2 | 2021-02-01 | 5 | מחלקה1 | [תג1] |
| 3 | 2022-03-01 | 5 | מחלקה2 | [תג1] |
+---+------------+------+--------+-----------+
| | סהכ | 30 | | |
+---+------------+------+--------+-----------+`)

table.Style().Format.Direction = text.LeftToRight
compareOutput(t, table.Render(), `‪+---+------------+------+--------+-----------+
‪| | ‪תאריך | ‪סכום | ‪מחלקה | ‪תגים |
‪+---+------------+------+--------+-----------+
‪| 1 | ‪2020-01-01 | ‪5 | ‪מחלקה1 | ‪[תג1 תג2] |
‪| 2 | ‪2021-02-01 | ‪5 | ‪מחלקה1 | ‪[תג1] |
‪| 3 | ‪2022-03-01 | ‪5 | ‪מחלקה2 | ‪[תג1] |
‪+---+------------+------+--------+-----------+
‪| | ‪סהכ | ‪30 | | |
‪+---+------------+------+--------+-----------+`)

table.Style().Format.Direction = text.RightToLeft
compareOutput(t, table.Render(), `‫+---+------------+------+--------+-----------+
‫| | ‫תאריך | ‫סכום | ‫מחלקה | ‫תגים |
‫+---+------------+------+--------+-----------+
‫| 1 | ‫2020-01-01 | ‫5 | ‫מחלקה1 | ‫[תג1 תג2] |
‫| 2 | ‫2021-02-01 | ‫5 | ‫מחלקה1 | ‫[תג1] |
‫| 3 | ‫2022-03-01 | ‫5 | ‫מחלקה2 | ‫[תג1] |
‫+---+------------+------+--------+-----------+
‫| | ‫סהכ | ‫30 | | |
‫+---+------------+------+--------+-----------+`)
}

func TestTable_Render_BorderAndSeparators(t *testing.T) {
table := Table{}
table.AppendHeader(testHeader)
Expand Down
3 changes: 3 additions & 0 deletions table/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestTable_sortRows_WithName(t *testing.T) {
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
table.SetStyle(StyleDefault)
table.initForRenderRows()

// sort by nothing
Expand Down Expand Up @@ -76,6 +77,7 @@ func TestTable_sortRows_WithoutName(t *testing.T) {
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
table.SetStyle(StyleDefault)
table.initForRenderRows()

// sort by nothing
Expand Down Expand Up @@ -137,6 +139,7 @@ func TestTable_sortRows_InvalidMode(t *testing.T) {
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
table.SetStyle(StyleDefault)
table.initForRenderRows()

// sort by "First Name"
Expand Down
7 changes: 4 additions & 3 deletions table/style.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,10 @@ var (

// FormatOptions defines the text-formatting to perform on parts of the Table.
type FormatOptions struct {
Footer text.Format // footer row(s) text format
Header text.Format // header row(s) text format
Row text.Format // (data) row(s) text format
Direction text.Direction // (forced) BiDi direction for each Column
Footer text.Format // footer row(s) text format
Header text.Format // header row(s) text format
Row text.Format // (data) row(s) text format
}

var (
Expand Down
2 changes: 1 addition & 1 deletion table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (t *Table) analyzeAndStringifyColumn(colIdx int, col interface{}, hint rend
if strings.Contains(colStr, "\r") {
colStr = strings.Replace(colStr, "\r", "", -1)
}
return colStr
return fmt.Sprintf("%s%s", t.style.Format.Direction.Modifier(), colStr)
}

func (t *Table) getAlign(colIdx int, hint renderHint) text.Align {
Expand Down
24 changes: 24 additions & 0 deletions text/direction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package text

// Direction defines the overall flow of text. Similar to bidi.Direction, but
// simplified and specific to this package.
type Direction int

// Available Directions.
const (
Default Direction = iota
LeftToRight
RightToLeft
)

// Modifier returns a character to force the given direction for the text that
// follows the modifier.
func (d Direction) Modifier() string {
switch d {
case LeftToRight:
return "\u202a"
case RightToLeft:
return "\u202b"
}
return ""
}

0 comments on commit d28aad5

Please sign in to comment.