New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add unicode-bidi to support RTL languages #229
Comments
Can you share a sample program with dummy data, and a hand-crafted "expected" output? I've got no clue about Hebrew/Arabic, and would like to know what the input will look like and what the output should look like before I can say if this is something that this library can/should support. I'd prefer text answer that I can run myself, instead of an image like above. Thanks! |
Thanks for getting back so quickly! I've made a repo with example code and expected results. While crafting the expected output, Iv'e encounter issues where I couldn't format it to the desired results without wrapping some strings with In main.go you would find some comments indicating the changes i need to make to adapt RTL to show properly. Still, in with_wrapping_[].txt you would see some spaces not align properly, but It's good enough for me. Do notice this MUST be tested in a terminal that supports bidi, like Konsole, Gnome terminal and more listed here https://unix.stackexchange.com/questions/100811/are-there-terminal-emulators-that-support-bi-directional-text Thanks! |
I rewrote some of the code for simplicity to my benefit as follows: Output:
So if I understand it right, the proper output is the first one but without the brackets. Correct? |
Yes, exactly |
Okay. From my testing, the direction is being auto-determined by some golang internals and not by But here is a workaround using the table styling with not-so-good-looking-output. Until I find a way forward for this issue at least. 😄 Use this before calling t.Style().Box = table.BoxStyle{
BottomLeft: "[",
BottomRight: "]",
BottomSeparator: "][",
EmptySeparator: " ",
Left: "[",
LeftSeparator: "[",
MiddleHorizontal: "-",
MiddleSeparator: "][",
MiddleVertical: "][",
PaddingLeft: " ",
PaddingRight: " ",
PageSeparator: "\n",
Right: "]",
RightSeparator: "]",
TopLeft: "[",
TopRight: "]",
TopSeparator: "][",
UnfinishedRow: " ~",
} Output:
|
Thanks for looking into this! the workaround are totally fine by me :) |
Hey @amitlevy21 I've added support for forcing the text direction for tables with BiDi content: #230 Can you try your code without wrapping, with the following directive: t.Style().Format.Direction = text.LeftToRight Once you verify functionality with the latest content from the |
Thanks @jedib0t ! It's much better, there is a small indentation issue which I'm not sure if its specific to bidi or not. func NewTestExpenses() *Expenses {
return &Expenses{Classified: []*Expense{
{
Date: UTCDate(2021, 03, 18),
Amount: 5.0,
Class: "הי י מחלקה1",
Tags: []Tag{"תג1", "תג2"},
},
{
Date: UTCDate(2021, 04, 19),
Amount: 5.0,
Class: "מחלקה1",
Tags: []Tag{"תג1"},
},
{
Date: UTCDate(2021, 05, 20),
Amount: 5.0,
Class: "מחלקה נסיון אחת שתיים",
Tags: []Tag{},
},
{
Date: UTCDate(2021, 05, 20),
Amount: 5.0,
Class: "מחלקה נסיון אחת שתיים שלוש2",
Tags: []Tag{},
},
}}
} |
This may be an issue with the terminal emulator you are using, or the font/typeface. Here is how it looks on my system: Code used with table@v6.3.9package main
import (
"fmt"
"strings"
"time"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
)
var (
wrapped = false
)
type Tag = string
type Expense struct {
Date time.Time
Amount float64
Class string
Tags []Tag
}
func UTCDate(year int, month time.Month, day int) time.Time {
timeZone, _ := time.LoadLocation("UTC")
return time.Date(year, month, day, 0, 0, 0, 0, timeZone)
}
func displayExpenses(expenses []*Expense) {
t := table.NewWriter()
t.AppendHeader(generateHeader())
for i, e := range expenses {
dateWithoutTime := strings.Split(e.Date.String(), " ")[0]
eClass := processBiDi(e.Class)
eTags := processBiDi(strings.Join(e.Tags, " "))
t.AppendRow(table.Row{i, dateWithoutTime, e.Amount, eClass, eTags})
}
t.AppendFooter(generateFooter())
t.SetCaption("Wrapped: %v", wrapped)
t.Style().Format.Direction = text.LeftToRight
fmt.Printf("%s\n\n", t.Render())
}
func generateFooter() table.Row {
row := table.Row{"", "סהכ", 30}
row[1] = processBiDi(fmt.Sprint(row[1]))
return row
}
func generateHeader() table.Row {
row := table.Row{"#"}
for _, col := range []string{"תאריך", "סכום", "מחלקה", "תגים"} {
row = append(row, processBiDi(col))
}
return row
}
func processBiDi(str string) string {
if wrapped {
return fmt.Sprintf("[%s]", str)
}
return str
}
func main() {
testExpenses := []*Expense{
{
Date: UTCDate(2021, 03, 18),
Amount: 5.0,
Class: "הי י מחלקה1",
Tags: []Tag{"תג1", "תג2"},
},
{
Date: UTCDate(2021, 04, 19),
Amount: 5.0,
Class: "מחלקה1",
Tags: []Tag{"תג1"},
},
{
Date: UTCDate(2021, 05, 20),
Amount: 5.0,
Class: "מחלקה נסיון אחת שתיים",
Tags: []Tag{},
},
{
Date: UTCDate(2021, 05, 20),
Amount: 5.0,
Class: "מחלקה נסיון אחת שתיים שלוש2",
Tags: []Tag{},
},
}
displayExpenses(testExpenses)
} Here it is with tags appended to table as Terminal: terminator; Font/typeface: Ubuntu mono |
I see, looks good to me! thank you for providing this! |
[](https://renovatebot.com) This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/g-rath/osv-detector](https://togithub.com/g-rath/osv-detector) | require | patch | `v0.7.1` -> `v0.7.2` | | [github.com/jedib0t/go-pretty/v6](https://togithub.com/jedib0t/go-pretty) | require | patch | `v6.3.8` -> `v6.3.9` | | [golang.org/x/crypto](https://togithub.com/golang/crypto) | require | digest | `c86fa9a` -> `eccd636` | | [golang.org/x/exp](https://togithub.com/golang/exp) | require | digest | `b168a2c` -> `439092d` | --- ### Release Notes <details> <summary>g-rath/osv-detector</summary> ### [`v0.7.2`](https://togithub.com/G-Rath/osv-detector/releases/tag/v0.7.2) [Compare Source](https://togithub.com/g-rath/osv-detector/compare/v0.7.1...v0.7.2) #### What's Changed - parse & compare versions as big integers to support really large numbers ([#​155](https://togithub.com/g-rath/osv-detector/issues/155)) **Full Changelog**: G-Rath/osv-detector@v0.7.1...v0.7.2 </details> <details> <summary>jedib0t/go-pretty</summary> ### [`v6.3.9`](https://togithub.com/jedib0t/go-pretty/releases/tag/v6.3.9) [Compare Source](https://togithub.com/jedib0t/go-pretty/compare/v6.3.8...v6.3.9) ### Features - **table** - option to force a text direction for tables with BiDi content (in response to [jedib0t/go-pretty#229) - Ex: `table.Style().Format.Direction = text.LeftToRight` </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 6am on monday" in timezone Australia/Sydney, Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://togithub.com/renovatebot/renovate/discussions) if that's undesired. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, click this checkbox. --- This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://app.renovatebot.com/dashboard#github/google/osv.dev). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzMi4xOTguMSIsInVwZGF0ZWRJblZlciI6IjMyLjIwNC41In0=-->
Is your feature request related to a problem? Please describe.
Right to Left (RTL) languages like Hebrew and Arabic don't work well with go-pretty table functionality.
The order of columns doesn't map well, and the headers spacing is also not formatted.
This picture is taken from Konsole, a terminal that supports unicode-bidi

Describe the solution you'd like
I'd like the columns to show nicely as they do for English.
There's built in support for unicode bidi in go: https://pkg.go.dev/golang.org/x/text/unicode/bidi
Describe alternatives you've considered
I could try to translate them from Hebrew to English, but it would result in loss of data in case the translation is not correct, so it won't work for me.
Additional context
Add any other context or screenshots about the feature request here.
Thank you for this great tool and your time.
The text was updated successfully, but these errors were encountered: