-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuitems.go
More file actions
114 lines (96 loc) · 3.33 KB
/
Copy pathuitems.go
File metadata and controls
114 lines (96 loc) · 3.33 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
package ui
import (
"encoding/hex"
"fmt"
"reflect"
"github.com/ethereum/go-ethereum/common"
"github.com/manifoldco/promptui"
"github.com/san-lab/go4337/signer"
"github.com/san-lab/go4337/state"
)
type Item struct {
Label string
Value interface{}
Details string
DisplayValueString string // could be a function, but I am lazy
IsUserOp bool // To show them in a different color now that there are less menus
}
func (i *Item) DisplayValue() string {
if i.DisplayValueString != "" {
return ShortString(i.DisplayValueString, 50)
}
if i.Value == nil {
return ""
}
if addr, ok := (i.Value).(*common.Address); ok {
if addr == nil {
return ""
}
return addr.String()
}
if str, ok := (i.Value).(fmt.Stringer); ok {
return ShortString(str.String(), 50)
}
if reflect.ValueOf(i.Value).IsZero() {
if reflect.TypeOf(i.Value).ConvertibleTo(reflect.TypeOf(0)) {
return "0"
}
return ""
}
var derefv interface{}
//Dereference, if it is a pointer
if reflect.TypeOf(i.Value).Kind() == reflect.Ptr {
derefv = reflect.ValueOf(i.Value).Elem().Interface()
} else {
derefv = i.Value
}
switch derefv.(type) {
case string:
return ShortString(derefv.(string), 40)
case []byte:
return ShortHex(derefv.([]byte), 40)
case [32]byte:
bt32 := derefv.([32]byte)
return ShortHex(bt32[:], 40)
case state.MethodCall:
method := derefv.(state.MethodCall)
return method.MethodName
case signer.Signer:
signer := derefv.(signer.Signer)
return signer.String()
default:
return ShortString(fmt.Sprint(derefv), 50)
}
}
func ShortHex(data []byte, l int) string {
return ShortString(hex.EncodeToString(data), l)
}
func ShortString(data string, l int) string {
if len(data) < l+3 {
return data
}
return fmt.Sprintf("%s...%s", data[:l], data[len(data)-l:])
}
func (i *Item) String() string {
return i.Label
}
const unicorn = "\U0001F984"
const settingsIcon = "\U0001F6E0"
const userOpsIcon = "\U0001F464"
const abisIcon = "\U0001F517"
const (
EXIT = "EXIT"
BACK = "BACK"
)
// Rewrite the above as variable instantiation with OK=&Item{Label: "OK"} pattern
var OK = &Item{Label: "OK", Details: "Confirm and proceed"}
var Back = &Item{Label: BACK, Details: "Go back to previous menu"}
var Exit = &Item{Label: EXIT, Details: "Exit the program"}
var Set = &Item{Label: "Set", Details: "Set the value"}
var ItemTemplate = &promptui.SelectTemplates{
Label: "{{ . | bold | cyan}}",
Inactive: `{{if eq .IsUserOp true}}{{.Label | magenta}}{{else if eq .Label "BACK"}}{{.Label | yellow}}{{else if eq .Label "EXIT"}}{{.Label | red}}{{else if or (eq .Label "Set") (eq .Label "Export User Operation")}}{{.Label | green}}{{else if eq .Label "Utils: hashes and signatures"}}{{.Label | faint}}{{else}}{{ .Label }}{{with .DisplayValue}}: {{.}}{{end}}{{end}}`,
Active: `{{if eq .IsUserOp true}}{{.Label | magenta | bold | underline}}{{else if eq .Label "BACK"}}{{.Label | yellow | bold | underline}}{{else if eq .Label "EXIT"}}{{.Label | red | bold | underline}}{{else if or (eq .Label "Set") (eq .Label "Export User Operation")}}{{.Label | green | bold | underline}}{{else if eq .Label "Utils: hashes and signatures"}}{{.Label | faint | bold | underline}}{{else}}{{ .Label | bold | underline }}{{with .DisplayValue}}: {{. | bold}}{{end}}{{end}}`,
Selected: "{{. | faint}}",
Details: "{{ .Details | faint }}",
}