Skip to content

Commit fb18fde

Browse files
committed
Update tab config.
1 parent 5aa5a43 commit fb18fde

File tree

4 files changed

+49
-48
lines changed

4 files changed

+49
-48
lines changed

config.go

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ type TabConfig struct {
99
Ellipsis string
1010
MinWiths []int
1111
MaxWiths []int
12+
UseTab bool
1213
}
1314

1415
var DefaultConfig = TabConfig{
@@ -18,4 +19,5 @@ var DefaultConfig = TabConfig{
1819
Ellipsis: "...",
1920
MinWiths: []int{},
2021
MaxWiths: []int{},
22+
UseTab: false,
2123
}

string.go

+5-25
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package gotab
44

55
import (
6-
"fmt"
76
"github.com/mattn/go-runewidth"
87
"regexp"
98
"strings"
@@ -21,35 +20,16 @@ func StrTruncate(s string, w int, tail string) string {
2120
return runewidth.Truncate(s, w, tail)
2221
}
2322

24-
func StrPadding2(s, pad string, width int) string {
25-
//fmt.Println("W", StrWidth(s), "L", len(s), "R", utf8.RuneCountInString(s))
26-
//sw := StrWidth(s)
27-
//rw := utf8.RuneCountInString(s)
28-
//if sw > rw {
29-
// width = width - (sw - rw)
30-
//}
31-
//func Align(s, pad string, width int) string {
32-
format := fmt.Sprint("%-", width, "v")
33-
//println("format: ", format)
34-
return fmt.Sprintf(format, s)
35-
}
36-
37-
func StrPadding1(s, pad string, width int) string {
38-
width += width % 4 //tab
23+
func Padding(s, pad string, width int) string {
3924
sw := StrWidth(s)
4025
gap := width - sw
41-
tab := "\t"
42-
//rw := len(s)
43-
//if sw != rw {
44-
// tab = "\t"
45-
//}
4626
if gap > 0 {
47-
return s + strings.Repeat(pad, gap) + tab
27+
return s + strings.Repeat(pad, gap)
4828
}
49-
return s + tab
29+
return s
5030
}
5131

52-
func Padding(s, pad string, width int) string {
32+
func PaddingTab(s, pad string, width int) string {
5333
width = fixTabWidth(width)
5434
sw := StrWidth(s)
5535
tab := "\t"
@@ -61,5 +41,5 @@ func Padding(s, pad string, width int) string {
6141
}
6242

6343
func fixTabWidth(width int) int {
64-
return width - (width % 4) + 2 //tab fix
44+
return width - (width % 4) + 2 //fix tab width
6545
}

tab.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,13 @@ func (r *TabWriter) Print() {
133133
value = StrTruncate(value, maxWidth, config.Ellipsis)
134134
width = maxWidth
135135
}
136-
padValue := Padding(value, config.Pad, width)
136+
var padValue string
137+
if r.Config.UseTab {
138+
padValue = PaddingTab(value, config.Pad, width)
139+
} else {
140+
padValue = Padding(value, config.Pad, width)
141+
}
142+
137143
if ok && n < (size-1) {
138144
fmt.Fprintf(writer, " %s %s", padValue, config.Split)
139145
} else {

tab_test.go

+35-22
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ const summaryEn = "Gotab is a go library for align text"
1111
const summaryCn = "Gotab是控制台对齐的go库"
1212

1313
func TestPrintTable(t *testing.T) {
14+
println("============================================")
1415
writer := NewWriter()
16+
writer.Config.Pad = "-"
1517
writer.Config.Split = "|"
1618
writer.AddLine("Hello, World", summaryEn)
1719
writer.AddLine("Hello,世A界B", summaryEn)
@@ -25,13 +27,38 @@ func TestPrintTable(t *testing.T) {
2527
{"node4.example.com", "NotReady", "compute", "1.11"},
2628
}
2729
writer.AddLines(data...)
28-
writer.AddDividerWithValue("-----------------")
30+
writer.AddDividerWithValue("----")
31+
writer.AddLine("Ready", "ABC", summaryEn)
32+
writer.AddLine("NotReady", summaryCn, "EDF")
33+
writer.Print()
34+
}
35+
36+
func TestPrintTableUseTab(t *testing.T) {
37+
println("============================================")
38+
writer := NewWriter()
39+
writer.Config.Pad = "-"
40+
writer.Config.Split = "|"
41+
writer.Config.UseTab = true
42+
writer.AddLine("Hello, World", summaryEn)
43+
writer.AddLine("Hello,世A界B", summaryEn)
44+
writer.AddLine("您好,NI世界ABC", summaryCn)
45+
writer.AddDivider()
46+
data := [][]any{
47+
{"node2.example.com", "Ready", "1.12", "1.12"},
48+
{"node3.example.com", "Ready", "compute", "1.13"},
49+
{"node4.example.com", "NotReady", "compute", "1.11"},
50+
{"node3.example.com", "Ready", "compute", "1.13"},
51+
{"node4.example.com", "NotReady", "compute", "1.11"},
52+
}
53+
writer.AddLines(data...)
54+
writer.AddDividerWithValue("----")
2955
writer.AddLine("Ready", "ABC", summaryEn)
3056
writer.AddLine("NotReady", summaryCn, "EDF")
3157
writer.Print()
3258
}
3359

3460
func TestPrintTable2(t *testing.T) {
61+
println("============================================")
3562
writer := NewWriter()
3663
writer.Config.Split = "|"
3764
writer.AddDivider()
@@ -43,13 +70,14 @@ func TestPrintTable2(t *testing.T) {
4370
{"node4.example.com", "NotReady", "compute", "1.11"},
4471
}
4572
writer.AddLines(data...)
46-
writer.AddDividerWithValue("-----------------")
73+
writer.AddDividerWithValue("----")
4774
writer.AddLine("NotReady2", "ABC", summaryEn)
4875
writer.AddLine("NotReady2", summaryCn, "EDF")
4976
writer.Print()
5077
}
5178

5279
func TestPrintTable3(t *testing.T) {
80+
println("============================================")
5381
writer := NewWriter()
5482
writer.Config.Split = "|"
5583
writer.AddLine("A", summaryEn)
@@ -64,18 +92,15 @@ func TestPrintTable3(t *testing.T) {
6492
{"node4.example.com", "NotReady", "compute", "1.11"},
6593
}
6694
writer.AddLines(data...)
67-
writer.AddDividerWithValue("-----------------")
95+
writer.AddDividerWithValue("----")
6896
writer.AddLine("您好,世界", "ABC", summaryEn)
6997
writer.AddLine("Hello, 世界", "ABC", summaryEn)
7098
writer.AddLine("A", summaryEn, "Hello World")
71-
result := writer.String()
72-
73-
println("|---------------------------|")
74-
println(result)
75-
println("|---------------------------|")
99+
writer.Print()
76100
}
77101

78102
func TestPrintTable4(t *testing.T) {
103+
println("============================================")
79104
config := DefaultConfig
80105
config.Split = "|"
81106
config.MinWiths = []int{100}
@@ -85,7 +110,7 @@ func TestPrintTable4(t *testing.T) {
85110
writer.AddLine("A", summaryEn)
86111
writer.AddLine("Hello, 世界", summaryEn)
87112
writer.AddLine("您好, 世界", summaryEn)
88-
writer.AddDividerWithValue("-----------------")
113+
writer.AddDividerWithValue("----")
89114
data := [][]any{
90115
{"node2.example.com", "Ready", "1.12", "1.12"},
91116
{"node3.example.com", "Ready", "compute", "1.13"},
@@ -98,13 +123,11 @@ func TestPrintTable4(t *testing.T) {
98123
writer.AddLine("Hello, 世界", "ABC", summaryEn)
99124
writer.AddLine("A", summaryEn, "Hello World")
100125
result := writer.String()
101-
102-
println("|---------------------------|")
103126
println(result)
104-
println("|---------------------------|")
105127
}
106128

107129
func TestPrintTable5(t *testing.T) {
130+
println("============================================")
108131
data := [][]any{
109132
{"node1.example.com", summaryEn, "A"},
110133
{"----"},
@@ -116,13 +139,3 @@ func TestPrintTable5(t *testing.T) {
116139
}
117140
Print(os.Stdout, data...)
118141
}
119-
120-
func TestIsDivider(t *testing.T) {
121-
var str string = "================="
122-
println("Div: ", isDivider(str))
123-
println("Val: ", str)
124-
125-
var div Divider = "================="
126-
println("Div: ", isDivider(div))
127-
println("Val: ", div)
128-
}

0 commit comments

Comments
 (0)