-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolors.go
More file actions
226 lines (205 loc) · 7.87 KB
/
colors.go
File metadata and controls
226 lines (205 loc) · 7.87 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package tui
import (
"fmt"
"os"
"reflect"
"strings"
"sync"
"github.com/charmbracelet/lipgloss"
"github.com/muesli/termenv"
)
var (
output = termenv.NewOutput(os.Stdout)
//profile = termenv.ColorProfile()
Normal = lipgloss.NewStyle().String()
Bold = lipgloss.NewStyle().Bold(true).String()
Underline = lipgloss.NewStyle().Underline(true).String()
Blue = lipgloss.Color("#3398DA")
Yellow = lipgloss.Color("#F1C40F")
Purple = lipgloss.Color("#8D44AD")
Green = lipgloss.Color("#2FCB71")
Red = lipgloss.Color("#E74C3C")
Gray = lipgloss.Color("#BDC3C7")
DarkGray = lipgloss.Color("#808080")
Cyan = lipgloss.Color("#1ABC9C")
Orange = lipgloss.Color("#E67E22")
Black = lipgloss.Color("#000000")
Pink = lipgloss.Color("#EE82EE")
SlateBlue = lipgloss.Color("#6A5ACD")
White = lipgloss.Color("#FFFFFF")
BlueFg = lipgloss.NewStyle().Foreground(Blue)
YellowFg = lipgloss.NewStyle().Foreground(Yellow)
PurpleFg = lipgloss.NewStyle().Foreground(Purple)
GreenFg = lipgloss.NewStyle().Foreground(Green)
RedFg = lipgloss.NewStyle().Foreground(Red)
CyanFg = lipgloss.NewStyle().Foreground(Cyan)
OrangeFg = lipgloss.NewStyle().Foreground(Orange)
WhiteFg = lipgloss.NewStyle().Foreground(White)
BlackFg = lipgloss.NewStyle().Foreground(Black)
GrayFg = lipgloss.NewStyle().Foreground(Gray)
SlateBlueFg = lipgloss.NewStyle().Foreground(SlateBlue)
DarkGrayFg = lipgloss.NewStyle().Foreground(DarkGray)
PinkFg = lipgloss.NewStyle().Foreground(Pink)
BlueBg = lipgloss.NewStyle().Background(Blue)
YellowBg = lipgloss.NewStyle().Background(Yellow)
PurpleBg = lipgloss.NewStyle().Background(Purple)
GreenBg = lipgloss.NewStyle().Background(Green)
RedBg = lipgloss.NewStyle().Background(Red)
CyanBg = lipgloss.NewStyle().Background(Cyan)
OrangeBg = lipgloss.NewStyle().Background(Orange)
WhiteBg = lipgloss.NewStyle().Background(White)
BlackBg = lipgloss.NewStyle().Background(Black)
GrayBg = lipgloss.NewStyle().Background(Gray)
SlateBlueBg = lipgloss.NewStyle().Background(SlateBlue)
DarkGrayBg = lipgloss.NewStyle().Background(DarkGray)
PinkBg = lipgloss.NewStyle().Background(Pink)
)
var (
darkBgOnce sync.Once
darkBgValue bool
)
func HasDarkBackground() bool {
darkBgOnce.Do(func() {
darkBgValue = termenv.HasDarkBackground()
})
return darkBgValue
}
var (
Reset = output.Reset
Clear = output.ClearLine
UpN = output.CursorPrevLine
Down = output.CursorNextLine
ClearLines = output.ClearLines
ClearAll = output.ClearScreen
)
//var ClientPrompt = AdaptTermColor()
// adaptTermColor - Adapt term color
// TODO: Adapt term color by term(fork grumble ColorTableFg)
func AdaptTermColor(prompt string) string {
var color string
if HasDarkBackground() {
color = fmt.Sprintf("\033[37m%s> \033[0m", prompt)
} else {
color = fmt.Sprintf("\033[30m%s> \033[0m", prompt)
}
return color
}
func AdaptSessionColor(prePrompt, sId string) string {
var sessionPrompt string
runes := []rune(sId)
if HasDarkBackground() {
sessionPrompt = fmt.Sprintf("\033[37m%s [%s]> \033[0m", prePrompt, string(runes))
} else {
sessionPrompt = fmt.Sprintf("\033[30m%s [%s]> \033[0m", prePrompt, string(runes))
}
return sessionPrompt
}
func NewSessionColor(prePrompt, sId string) string {
runes := []rune(sId)
return fmt.Sprintf("%s [%s]", DefaultGroupStyle.Render(prePrompt), DefaultNameStyle.Render(string(runes)))
}
func RendStructDefault(stru interface{}, blacklist ...string) string {
return RenderStruct(stru, 5, 1, blacklist...)
}
func RenderStruct(stru interface{}, keyWidth int, indentLevel int, blacklist ...string) string {
var builder strings.Builder
v := reflect.ValueOf(stru)
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
if !v.IsValid() {
return ""
}
t := v.Type()
blacklistMap := make(map[string]struct{})
for _, name := range blacklist {
blacklistMap[name] = struct{}{}
}
if v.Kind() == reflect.Struct {
// 遍历结构体的字段
for i := 0; i < v.NumField(); i++ {
field := v.Field(i)
fieldType := t.Field(i)
// 检查字段是否可访问
if !field.CanInterface() {
continue
}
key := fieldType.Name
// 检查字段是否在黑名单中
if _, ok := blacklistMap[key]; ok {
continue
}
// 格式化字段名和字段值
coloredKey := BlueFg.Render(fmt.Sprintf("%-*s", keyWidth, key+":"))
valueStr := fmt.Sprintf("%v", field.Interface())
coloredValue := GreenFg.Render(valueStr)
switch field.Kind() {
case reflect.Struct:
// 处理结构体字段,递归调用 RenderStruct
builder.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", indentLevel), coloredKey))
builder.WriteString(RenderStruct(field.Addr().Interface(), keyWidth, indentLevel+1))
case reflect.Ptr:
if field.IsNil() {
field.Set(reflect.New(field.Type().Elem()))
builder.WriteString(fmt.Sprintf("%s%s <nil>\n", strings.Repeat(" ", indentLevel), coloredKey))
continue
}
builder.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", indentLevel), coloredKey))
builder.WriteString(RenderStruct(field.Interface(), keyWidth, indentLevel+1))
case reflect.Slice:
// 处理切片字段
builder.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", indentLevel), coloredKey))
for j := 0; j < field.Len(); j++ {
element := field.Index(j)
// 如果元素是结构体或指针,递归调用 RenderStruct
if element.Kind() == reflect.Struct || element.Kind() == reflect.Ptr {
builder.WriteString(fmt.Sprintf("%s- \n", strings.Repeat(" ", indentLevel+1)))
builder.WriteString(RenderStruct(element.Interface(), keyWidth, indentLevel+2, blacklist...))
} else {
// 否则直接输出切片中的元素值
builder.WriteString(fmt.Sprintf("%s- %s\n", strings.Repeat(" ", indentLevel+1), GreenFg.Render(fmt.Sprintf("%v", element.Interface()))))
}
}
case reflect.Map:
// 处理映射类型字段
builder.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", indentLevel), coloredKey))
for _, mapKey := range field.MapKeys() {
mapValue := field.MapIndex(mapKey)
coloredMapKey := BlueFg.Render(fmt.Sprintf("%v", mapKey.Interface()))
if mapValue.Kind() == reflect.Struct || mapValue.Kind() == reflect.Ptr {
builder.WriteString(fmt.Sprintf("%s%s \n", strings.Repeat(" ", indentLevel+1), coloredMapKey))
builder.WriteString(RenderStruct(mapValue.Interface(), keyWidth, indentLevel+2, blacklist...))
} else {
builder.WriteString(fmt.Sprintf("%s%s %s\n", strings.Repeat(" ", indentLevel+1), coloredMapKey, GreenFg.Render(fmt.Sprintf("%v", mapValue.Interface()))))
}
}
case reflect.Interface:
// 处理接口类型字段
if !field.IsNil() {
builder.WriteString(fmt.Sprintf("%s%s\n", strings.Repeat(" ", indentLevel), coloredKey))
builder.WriteString(RenderStruct(field.Interface(), keyWidth, indentLevel+1, blacklist...))
continue
} else {
builder.WriteString(fmt.Sprintf("%s%s <nil>\n", strings.Repeat(" ", indentLevel), coloredKey))
}
default:
// 处理其他类型的字段
builder.WriteString(fmt.Sprintf("%s%s %s\n", strings.Repeat(" ", indentLevel), coloredKey, coloredValue))
}
}
} else if v.Kind() == reflect.Slice {
// 如果是切片类型,遍历切片中的元素
for i := 0; i < v.Len(); i++ {
element := v.Index(i)
// 递归处理切片中的每个元素
if element.Kind() == reflect.Struct || element.Kind() == reflect.Ptr {
builder.WriteString(fmt.Sprintf("%s- \n", strings.Repeat(" ", indentLevel)))
builder.WriteString(RenderStruct(element.Interface(), keyWidth, indentLevel+1, blacklist...))
} else {
// 否则直接输出元素值
builder.WriteString(fmt.Sprintf("%s- %s\n", strings.Repeat(" ", indentLevel), GreenFg.Render(fmt.Sprintf("%v", element.Interface()))))
}
}
}
return builder.String()
}