mirror of
https://github.com/restic/restic.git
synced 2026-07-05 14:55:10 +00:00
Use character display width for table padding
Using len(...) for table cell padding produced wrong results for unicode chracters leading to misaligned tables. Implementation changed to take the actual terminal display width into consideration.
This commit is contained in:
@@ -6,6 +6,8 @@ import (
|
||||
"strings"
|
||||
|
||||
"text/template"
|
||||
|
||||
"github.com/restic/restic/internal/ui"
|
||||
)
|
||||
|
||||
// Table contains data for a table to be printed.
|
||||
@@ -89,7 +91,7 @@ func printLine(w io.Writer, print func(io.Writer, string) error, sep string, dat
|
||||
}
|
||||
|
||||
// apply padding
|
||||
pad := widths[fieldNum] - len(v)
|
||||
pad := widths[fieldNum] - ui.TerminalDisplayWidth(v)
|
||||
if pad > 0 {
|
||||
v += strings.Repeat(" ", pad)
|
||||
}
|
||||
@@ -139,16 +141,16 @@ func (t *Table) Write(w io.Writer) error {
|
||||
columnWidths := make([]int, columns)
|
||||
for i, desc := range t.columns {
|
||||
for _, line := range strings.Split(desc, "\n") {
|
||||
if columnWidths[i] < len(line) {
|
||||
columnWidths[i] = len(desc)
|
||||
if columnWidths[i] < ui.TerminalDisplayWidth(line) {
|
||||
columnWidths[i] = ui.TerminalDisplayWidth(desc)
|
||||
}
|
||||
}
|
||||
}
|
||||
for _, line := range lines {
|
||||
for i, content := range line {
|
||||
for _, l := range strings.Split(content, "\n") {
|
||||
if columnWidths[i] < len(l) {
|
||||
columnWidths[i] = len(l)
|
||||
if columnWidths[i] < ui.TerminalDisplayWidth(l) {
|
||||
columnWidths[i] = ui.TerminalDisplayWidth(l)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +161,7 @@ func (t *Table) Write(w io.Writer) error {
|
||||
for _, width := range columnWidths {
|
||||
totalWidth += width
|
||||
}
|
||||
totalWidth += (columns - 1) * len(t.CellSeparator)
|
||||
totalWidth += (columns - 1) * ui.TerminalDisplayWidth(t.CellSeparator)
|
||||
|
||||
// write header
|
||||
if len(t.columns) > 0 {
|
||||
|
||||
@@ -126,7 +126,7 @@ foo 2018-08-19 22:22:22 xxx other /home/user/other
|
||||
Time string
|
||||
Tags, Dirs []string
|
||||
}
|
||||
table.AddRow(data{"foo", "2018-08-19 22:22:22", []string{"work", "go"}, []string{"/home/user/work", "/home/user/go"}})
|
||||
table.AddRow(data{"foo", "2018-08-19 22:22:22", []string{"work", "go’s"}, []string{"/home/user/work", "/home/user/go"}})
|
||||
table.AddRow(data{"foo", "2018-08-19 22:22:22", []string{"other"}, []string{"/home/user/other"}})
|
||||
table.AddRow(data{"foo", "2018-08-19 22:22:22", []string{"other", "bar"}, []string{"/home/user/other"}})
|
||||
return table
|
||||
@@ -135,7 +135,7 @@ foo 2018-08-19 22:22:22 xxx other /home/user/other
|
||||
host name time zz tags dirs
|
||||
------------------------------------------------------------
|
||||
foo 2018-08-19 22:22:22 xxx work /home/user/work
|
||||
go /home/user/go
|
||||
go’s /home/user/go
|
||||
foo 2018-08-19 22:22:22 xxx other /home/user/other
|
||||
foo 2018-08-19 22:22:22 xxx other /home/user/other
|
||||
bar
|
||||
|
||||
Reference in New Issue
Block a user