-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkvtable.go
More file actions
176 lines (153 loc) · 4.46 KB
/
kvtable.go
File metadata and controls
176 lines (153 loc) · 4.46 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
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
165
166
167
168
169
170
171
172
173
174
175
176
package tui
import (
"fmt"
"reflect"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
var (
// kvTableStyle 定义 key-value 表格的基本样式
kvTableStyle = lipgloss.NewStyle().
Align(lipgloss.Left)
// keyStyle 定义键的样式
keyStyle = lipgloss.NewStyle().
Foreground(Blue).
Bold(true).
PaddingRight(2)
// kvBorder defines a complete frame for KV rendering.
kvBorder = table.Border{
Top: lipgloss.RoundedBorder().Top,
Bottom: lipgloss.RoundedBorder().Bottom,
Left: lipgloss.RoundedBorder().Left,
Right: lipgloss.RoundedBorder().Right,
TopLeft: lipgloss.RoundedBorder().TopLeft,
TopRight: lipgloss.RoundedBorder().TopRight,
BottomLeft: lipgloss.RoundedBorder().BottomLeft,
BottomRight: lipgloss.RoundedBorder().BottomRight,
TopJunction: "┬",
BottomJunction: "┴",
LeftJunction: "├",
RightJunction: "┤",
InnerJunction: "┼",
InnerDivider: "│",
}
)
type KVOptions struct {
ShowHeader bool
}
// getValueStyle 根据值的类型返回对应的样式
func getValueStyle(v interface{}) lipgloss.Style {
switch reflect.TypeOf(v).Kind() {
case reflect.String:
return WhiteFg // 字符串使用白色
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
return CyanFg // 数字类型使用青色
case reflect.Bool:
return GreenFg // 布尔值使用绿色
case reflect.Slice, reflect.Array, reflect.Map, reflect.Struct, reflect.Ptr:
return YellowFg // 复合类型统一使用黄色
default:
return WhiteFg // 其他类型使用白色
}
}
// formatValue 格式化值并添加类型信息(如果需要)
func formatValue(v interface{}) string {
if v == nil {
return "nil"
}
t := reflect.TypeOf(v)
switch t.Kind() {
case reflect.Slice, reflect.Array:
val := reflect.ValueOf(v)
if val.Len() == 0 {
return "[]"
}
return fmt.Sprintf("%v", v)
case reflect.Map:
val := reflect.ValueOf(v)
if val.Len() == 0 {
return "{}"
}
return fmt.Sprintf("%v", v)
case reflect.Struct:
return fmt.Sprintf("%+v", v)
case reflect.Ptr:
if reflect.ValueOf(v).IsNil() {
return "nil"
}
return fmt.Sprintf("%v", reflect.ValueOf(v).Elem())
default:
return fmt.Sprintf("%v", v)
}
}
// NewKVTable 创建一个新的 key-value 表格
func NewKVTable(data map[string]interface{}) *TableModel {
return NewKVTableWithOptions(data, KVOptions{})
}
func NewKVTableWithOptions(data map[string]interface{}, opts KVOptions) *TableModel {
// 定义列
columns := []table.Column{
table.NewColumn("key", "Key", 20),
table.NewColumn("value", "Value", 40),
}
// 创建表格模型
t := NewTable(columns, true)
t.table = t.table.
WithBaseStyle(kvTableStyle).
WithHeaderVisibility(opts.ShowHeader).
Border(borderNone)
// 转换数据为表格行
var rows []table.Row
for k, v := range data {
valueStyle := getValueStyle(v)
row := table.NewRow(table.RowData{
"key": keyStyle.Render(k),
"value": valueStyle.Render(formatValue(v)),
})
rows = append(rows, row)
}
// 设置行数据
t.SetRows(rows)
return t
}
func NewOrderedKVTable(data map[string]interface{}, orderedKeys []string) *TableModel {
return NewOrderedKVTableWithOptions(data, orderedKeys, KVOptions{})
}
func NewOrderedKVTableWithOptions(data map[string]interface{}, orderedKeys []string, opts KVOptions) *TableModel {
// 定义列
columns := []table.Column{
table.NewColumn("key", "Key", 20),
table.NewColumn("value", "Value", 40),
}
// 创建表格模型
t := NewTable(columns, true)
t.table = t.table.
WithBaseStyle(kvTableStyle).
WithHeaderVisibility(opts.ShowHeader).
Border(borderNone)
// 转换数据为表格行,按指定的键顺序
var rows []table.Row
for _, k := range orderedKeys {
if v, exists := data[k]; exists {
valueStyle := getValueStyle(v)
row := table.NewRow(table.RowData{
"key": keyStyle.Render(k),
"value": valueStyle.Render(formatValue(v)),
})
rows = append(rows, row)
}
}
// 设置行数据
t.SetRows(rows)
return t
}
// RenderKV 直接渲染 key-value 数据,渲染完立即返回
func RenderKV(data map[string]interface{}, orderedKeys []string) {
RenderKVWithOptions(data, orderedKeys, KVOptions{})
}
func RenderKVWithOptions(data map[string]interface{}, orderedKeys []string, opts KVOptions) {
table := NewOrderedKVTableWithOptions(data, orderedKeys, opts)
fmt.Println(table.View())
}