-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.go
More file actions
45 lines (37 loc) · 932 Bytes
/
string.go
File metadata and controls
45 lines (37 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright ©2022 Foolin. All rights reserved.
package gotab
import (
"github.com/mattn/go-runewidth"
"regexp"
"strings"
)
// Ignore color code
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
func StrWidth(str string) int {
return runewidth.StringWidth(ansi.ReplaceAllLiteralString(str, ""))
}
// StrTruncate return string truncated with w cells
func StrTruncate(s string, w int, tail string) string {
return runewidth.Truncate(s, w, tail)
}
func Padding(s, pad string, width int) string {
sw := StrWidth(s)
gap := width - sw
if gap > 0 {
return s + strings.Repeat(pad, gap)
}
return s
}
func PaddingTab(s, pad string, width int) string {
width = fixTabWidth(width)
sw := StrWidth(s)
tab := "\t"
gap := width - sw
if gap > 0 {
return s + strings.Repeat(pad, gap) + tab
}
return s + tab
}
func fixTabWidth(width int) int {
return width - (width % 4) + 2 //fix tab width
}