-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvttable.v
More file actions
139 lines (129 loc) · 3.74 KB
/
vttable.v
File metadata and controls
139 lines (129 loc) · 3.74 KB
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
// table.v
module vttable
import strings
pub struct Table {
pub mut:
data [][]string
position string
style string
}
pub fn(t Table) colmax() []int {
cols := t.data[0].len
mut maxs := []int{}
// 获取列字符串的最大长度值
for j:=0;j<cols;j++ {
mut b := []string{}
for i in t.data {
b << i[j]
}
mut max := 0
for y in b {
if y.len > max {
max = y.len
}
}
maxs << max
}
return maxs
}
pub fn version() {
println("vttable: v0.01")
}
pub fn(t Table) tables() {
mut lines := []string{}
// 渲染表格
for ri,row in t.data {
// println(t.data)
mut srowcontent := []string{}
// 清空lines
lines = []
if t.position == "center" {
for i, c in row {
mut cfilling := ""
mut wrow := 0
mut nc := ""
mut filling := ""
maxcol := t.colmax()[i]
if maxcol % 2 == 1 {
wrow = maxcol + 1
} else {
wrow = maxcol + 2
}
mut line := strings.repeat(`-`,wrow)
if c.len % 2 == 1 {
nc = c + " "
filling = strings.repeat(` `,wrow-1-c.len)
} else {
nc = c
filling = strings.repeat(` `,wrow-c.len)
}
cfilling = strings.repeat(` `,(filling.len +2 )/2)
line = strings.repeat(`-`,line.len +2)
if srowcontent.len >0 {
srowcontent << "$cfilling$nc$cfilling|"
lines << "$line+"
}else {
srowcontent << "|$cfilling$nc$cfilling|"
lines << "+$line+"
}
}
if ri > 1 && t.style == "simplicity"{
println(arraytostr(srowcontent))
} else {
for p in [lines,srowcontent] {
println(arraytostr(p))
}
}
} else if t.position == "right" {
for i, c in row {
wrow := t.colmax()[i]+1
line := strings.repeat(`-`,wrow)
filling := strings.repeat(` `,wrow-c.len)
if srowcontent.len >0 {
srowcontent << "$filling$c|"
lines << "$line+"
}else {
srowcontent << "|$filling$c|"
lines << "+$line+"
}
}
if ri > 1 && t.style == "simplicity"{
println(arraytostr(srowcontent))
} else {
for p in [lines,srowcontent] {
println(arraytostr(p))
}
}
} else {
for i, c in row {
wrow := t.colmax()[i]+1
line := strings.repeat(`-`,wrow)
filling := strings.repeat(` `,wrow-c.len)
if srowcontent.len >0 {
srowcontent << "$c$filling|"
lines << "$line+"
}else {
srowcontent << "|$c$filling|"
lines << "+$line+"
}
}
if ri > 1 && t.style == "simplicity"{
println(arraytostr(srowcontent))
} else {
for p in [lines,srowcontent] {
println(arraytostr(p))
}
}
}
}
// 最后打印一行
println(arraytostr(lines))
}
// array 转 string
fn arraytostr(a []string) string {
mut s := ""
for i in a {
s += i
}
return s
}