Import upstream version 0.0.3, md5 a1dc8c3d345f050adf9cc381d9c855d8
Debian Janitor
4 years ago
2 | 2 | import ( |
3 | 3 | "fmt" |
4 | 4 | |
5 | "github.com/fatih/color" | |
5 | 6 | "github.com/gosuri/uitable" |
6 | 7 | ) |
7 | 8 | |
36 | 37 | table.AddRow("") // blank |
37 | 38 | } |
38 | 39 | fmt.Println(table) |
40 | ||
41 | fmt.Print("\n==> Multicolor Support\n") | |
42 | table = uitable.New() | |
43 | table.MaxColWidth = 80 | |
44 | table.Wrap = true | |
45 | for _, hacker := range hackers { | |
46 | table.AddRow(color.RedString("Name:"), color.WhiteString(hacker.Name)) | |
47 | table.AddRow(color.BlueString("Birthday:"), hacker.Birthday) | |
48 | table.AddRow(color.GreenString("Bio:"), hacker.Bio) | |
49 | table.AddRow("") // blank | |
50 | } | |
51 | fmt.Println(table) | |
39 | 52 | } |
5 | 5 | "strings" |
6 | 6 | "sync" |
7 | 7 | |
8 | "github.com/fatih/color" | |
8 | 9 | "github.com/gosuri/uitable/util/strutil" |
9 | 10 | "github.com/gosuri/uitable/util/wordwrap" |
10 | "github.com/mattn/go-runewidth" | |
11 | 11 | ) |
12 | 12 | |
13 | 13 | // Separator is the default column seperator |
155 | 155 | } |
156 | 156 | lines[x] = strutil.Join(line, r.Separator) |
157 | 157 | } |
158 | return strutil.Join(lines, "\n") | |
158 | return strings.Join(lines, "\n") | |
159 | 159 | } |
160 | 160 | |
161 | 161 | // Cell represents a column in a row |
177 | 177 | func (c *Cell) LineWidth() uint { |
178 | 178 | width := 0 |
179 | 179 | for _, s := range strings.Split(c.String(), "\n") { |
180 | w := runewidth.StringWidth(s) | |
180 | w := strutil.StringWidth(s) | |
181 | 181 | if w > width { |
182 | 182 | width = w |
183 | 183 | } |
190 | 190 | if c.Data == nil { |
191 | 191 | return strutil.PadLeft(" ", int(c.Width), ' ') |
192 | 192 | } |
193 | s := fmt.Sprintf("%v", c.Data) | |
193 | col := color.New(color.FgBlack) | |
194 | col.DisableColor() | |
195 | s := fmt.Sprintf("%v", col.Sprint(c.Data)) | |
194 | 196 | if c.Width > 0 { |
195 | 197 | if c.Wrap && uint(len(s)) > c.Width { |
196 | 198 | return wordwrap.WrapString(s, c.Width) |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "bytes" |
5 | "regexp" | |
6 | ||
5 | 7 | "github.com/mattn/go-runewidth" |
6 | 8 | ) |
7 | 9 | |
10 | const ansi = "[\u001B\u009B][[\\]()#;?]*(?:(?:(?:[a-zA-Z\\d]*(?:;[a-zA-Z\\d]*)*)?\u0007)|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PRZcf-ntqry=><~]))" | |
11 | ||
12 | var re = regexp.MustCompile(ansi) | |
13 | ||
8 | 14 | // PadRight returns a new string of a specified length in which the end of the current string is padded with spaces or with a specified Unicode character. |
9 | 15 | func PadRight(str string, length int, pad byte) string { |
10 | slen := runewidth.StringWidth(str) | |
16 | slen := StringWidth(str) | |
11 | 17 | if slen >= length { |
12 | 18 | return str |
13 | 19 | } |
20 | 26 | |
21 | 27 | // PadLeft returns a new string of a specified length in which the beginning of the current string is padded with spaces or with a specified Unicode character. |
22 | 28 | func PadLeft(str string, length int, pad byte) string { |
23 | slen := runewidth.StringWidth(str) | |
29 | slen := StringWidth(str) | |
24 | 30 | if slen >= length { |
25 | 31 | return str |
26 | 32 | } |
35 | 41 | // Resize resizes the string with the given length. It ellipses with '...' when the string's length exceeds |
36 | 42 | // the desired length or pads spaces to the right of the string when length is smaller than desired |
37 | 43 | func Resize(s string, length uint, rightAlign bool) string { |
38 | slen := runewidth.StringWidth(s) | |
44 | slen := StringWidth(s) | |
39 | 45 | n := int(length) |
40 | 46 | if slen == n { |
41 | 47 | return s |
52 | 58 | w := 0 |
53 | 59 | for _, r := range rs { |
54 | 60 | buf.WriteRune(r) |
55 | rw := runewidth.RuneWidth(r) | |
61 | rw := RuneWidth(r) | |
56 | 62 | if w+rw >= n-3 { |
57 | 63 | break |
58 | 64 | } |
73 | 79 | buf.WriteString(list[len(list)-1]) |
74 | 80 | return buf.String() |
75 | 81 | } |
82 | ||
83 | // Strip strips the string of all colors | |
84 | func Strip(s string) string { | |
85 | return re.ReplaceAllString(s, "") | |
86 | } | |
87 | ||
88 | // StringWidth returns the actual width of the string without colors | |
89 | func StringWidth(s string) int { | |
90 | return runewidth.StringWidth(Strip(s)) | |
91 | } | |
92 | ||
93 | // RuneWidth returns the actual width of the rune | |
94 | func RuneWidth(s rune) int { | |
95 | return runewidth.RuneWidth(s) | |
96 | } |
2 | 2 | |
3 | 3 | import ( |
4 | 4 | "bytes" |
5 | "github.com/mattn/go-runewidth" | |
6 | 5 | "unicode" |
6 | ||
7 | "github.com/gosuri/uitable/util/strutil" | |
7 | 8 | ) |
8 | 9 | |
9 | 10 | // WrapString wraps the given string within lim width in characters. |
29 | 30 | } else { |
30 | 31 | current += uint(spaceWidth) |
31 | 32 | spaceBuf.WriteTo(buf) |
32 | spaceWidth += runewidth.StringWidth(buf.String()) | |
33 | spaceWidth += strutil.StringWidth(buf.String()) | |
33 | 34 | } |
34 | 35 | spaceBuf.Reset() |
35 | 36 | spaceWidth = 0 |
56 | 57 | } |
57 | 58 | |
58 | 59 | spaceBuf.WriteRune(char) |
59 | spaceWidth += runewidth.RuneWidth(char) | |
60 | spaceWidth += strutil.RuneWidth(char) | |
60 | 61 | } else { |
61 | 62 | wordBuf.WriteRune(char) |
62 | wordWidth += runewidth.RuneWidth(char) | |
63 | wordWidth += strutil.RuneWidth(char) | |
63 | 64 | |
64 | 65 | if current+uint(spaceWidth+wordWidth) > lim && uint(wordWidth) < lim { |
65 | 66 | buf.WriteRune('\n') |