forked from kgretzky/evilginx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
164 lines (136 loc) · 3.56 KB
/
table.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
This source file is a modified version of what was taken from the amazing bettercap (https://github.com/bettercap/bettercap) project.
Credits go to Simone Margaritelli (@evilsocket) for providing awesome piece of code!
*/
package core
import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
"github.com/fatih/color"
)
func viewLen(s string) int {
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
for _, m := range ansi.FindAllString(s, -1) {
s = strings.Replace(s, m, "", -1)
}
return utf8.RuneCountInString(s)
}
func truncString(s string, maxLen int) string {
var ansi = regexp.MustCompile("\033\\[(?:[0-9]{1,3}(?:;[0-9]{1,3})*)?[m|K]")
sm := s
for _, m := range ansi.FindAllString(sm, -1) {
sm = strings.Replace(sm, m, "", -1)
}
nsm := sm
if utf8.RuneCountInString(sm) > maxLen {
if maxLen > 3 {
nsm = nsm[:maxLen-3] + "..."
} else {
nsm = nsm[:maxLen]
}
s = strings.Replace(s, sm, nsm, -1)
}
return s
}
func maxLen(strings []string) int {
maxLen := 0
for _, s := range strings {
len := viewLen(s)
if len > maxLen {
maxLen = len
}
}
return maxLen
}
type Alignment int
const (
AlignLeft = Alignment(0)
AlignCenter = Alignment(1)
AlignRight = Alignment(2)
)
const minColLen = 16
func getPads(s string, maxLen int, align Alignment) (lPad int, rPad int) {
len := viewLen(s)
diff := maxLen - len
if align == AlignLeft {
lPad = 0
rPad = diff - lPad + 1
} else if align == AlignCenter {
lPad = diff / 2
rPad = diff - lPad + 1
} else if align == AlignRight {
lPad = diff + 1
rPad = 0
}
return
}
func padded(s string, maxLen int, align Alignment) string {
lPad, rPad := getPads(s, maxLen, align)
return fmt.Sprintf("%s%s%s", strings.Repeat(" ", lPad), s, strings.Repeat(" ", rPad))
}
func AsTable(columns []string, rows [][]string) string {
colMaxLens := make([]int, 0)
dg := color.New(color.FgHiBlack)
for i, col := range columns {
clen := viewLen(col) + 4
if clen < minColLen {
clen = minColLen
}
colMaxLens = append(colMaxLens, clen)
columns[i] = fmt.Sprintf(" %s ", col)
}
for i, row := range rows {
for j, cell := range row {
rows[i][j] = fmt.Sprintf(" %s ", truncString(cell, colMaxLens[j])) //cell)
}
}
colPaddings := make([]int, 0)
lineSep := ""
for colIndex, colHeader := range columns {
column := []string{colHeader}
for _, row := range rows {
column = append(column, row[colIndex])
}
mLen := maxLen(column)
colPaddings = append(colPaddings, mLen)
lineSep += fmt.Sprintf("+%s", strings.Repeat("-", mLen+1))
}
lineSep += "+"
table := ""
// header
table += dg.Sprintf("%s\n", lineSep)
for colIndex, colHeader := range columns {
table += dg.Sprintf("|") + fmt.Sprintf("%s", padded(colHeader, colPaddings[colIndex], AlignCenter))
}
table += dg.Sprintf("|\n")
table += dg.Sprintf("%s\n", lineSep)
// rows
for _, row := range rows {
for colIndex, cell := range row {
table += dg.Sprintf("|") + fmt.Sprintf("%s", padded(cell, colPaddings[colIndex], AlignLeft))
}
table += dg.Sprintf("|\n")
}
// footer
table += dg.Sprintf(lineSep) + "\n"
return table
}
func AsRows(keys []string, vals []string) string {
clr := color.New(color.FgHiBlack)
mLen := maxLen(keys)
var table string
for i, _ := range keys {
table += clr.Sprintf(" %s : ", padded(keys[i], mLen, AlignLeft)) + fmt.Sprintf("%s\n", vals[i])
}
return table
}
func AsDescription(keys []string, vals []string) string {
clr := color.New(color.FgHiBlack)
var table string
for i, _ := range keys {
table += clr.Sprintf(" %s", keys[i]) + fmt.Sprintf("\n %s\n", vals[i])
}
return table
}