-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanyHandler.go
More file actions
169 lines (143 loc) · 4.47 KB
/
anyHandler.go
File metadata and controls
169 lines (143 loc) · 4.47 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
package devtui
// ============================================================================
// PRIVATE IMPLEMENTATION - anyHandler Structure
// ============================================================================
type handlerType int
const (
handlerTypeDisplay handlerType = iota
handlerTypeEdit
handlerTypeExecution
handlerTypeInteractive // NEW: Interactive content handler
handlerTypeLoggable // NEW: For Loggable-only handlers
)
// anyHandler - Estructura privada que unifica todos los handlers
type anyHandler struct {
handlerType handlerType
origHandler any // Store original handler for type assertions
handlerColor string // NEW: Handler-specific color for message formatting
// Function pointers - solo los necesarios poblados
nameFunc func() string // Todos
labelFunc func() string // Display/Edit/Execution
valueFunc func() string // Edit/Display
contentFunc func() string // Display únicamente
editableFunc func() bool // Por tipo
editModeFunc func() bool // NEW: Auto edit mode activation
changeFunc func(string) // Edit/Execution (nueva firma)
executeFunc func() // Execution únicamente (nueva firma)
}
// ============================================================================
// anyHandler Methods - Replaces fieldHandler interface
// ============================================================================
func (a *anyHandler) Name() string {
if a.nameFunc != nil {
return a.nameFunc()
}
return ""
}
func (a *anyHandler) Label() string {
if a.labelFunc != nil {
return a.labelFunc()
}
return ""
}
func (a *anyHandler) Value() string {
if a.valueFunc != nil {
return a.valueFunc()
}
return ""
}
func (a *anyHandler) editable() bool {
if a.editableFunc != nil {
return a.editableFunc()
}
return false
}
func (a *anyHandler) Change(newValue string) {
if a.changeFunc != nil {
a.changeFunc(newValue)
}
}
func (a *anyHandler) Execute() {
if a.executeFunc != nil {
a.executeFunc()
}
}
// GetTrackingKey returns the handler name to be used for message tracking
func (a *anyHandler) GetTrackingKey() string {
return a.Name()
}
func (a *anyHandler) WaitingForUser() bool {
if a.editModeFunc != nil {
return a.editModeFunc()
}
return false
}
// ============================================================================
// Factory Methods
// ============================================================================
func NewEditHandler(h HandlerEdit, color string) *anyHandler {
anyH := &anyHandler{
handlerType: handlerTypeEdit,
nameFunc: h.Name,
labelFunc: h.Label,
valueFunc: h.Value,
editableFunc: func() bool { return true },
changeFunc: h.Change,
origHandler: h,
handlerColor: color, // NEW: Store handler color
}
// NEW: Check if handler also implements Value() method (like TestNonEditableHandler)
if valuer, ok := h.(interface{ Value() string }); ok {
anyH.valueFunc = valuer.Value
} else {
anyH.valueFunc = h.Label // Fallback to Label
}
return anyH
}
func NewDisplayHandler(h HandlerDisplay, color string) *anyHandler {
return &anyHandler{
handlerType: handlerTypeDisplay,
nameFunc: h.Name, // Solo Name()
valueFunc: h.Content, // Content como Value para compatibilidad interna
contentFunc: h.Content, // Solo Content()
editableFunc: func() bool { return false },
origHandler: h,
handlerColor: color, // NEW: Store handler color
}
}
func NewExecutionHandler(h HandlerExecution, color string) *anyHandler {
anyH := &anyHandler{
handlerType: handlerTypeExecution,
nameFunc: h.Name,
labelFunc: h.Label,
editableFunc: func() bool { return false },
executeFunc: h.Execute,
changeFunc: func(_ string) {
h.Execute()
},
origHandler: h,
handlerColor: color, // NEW: Store handler color
}
// Check if handler also implements Value() method (like TestNonEditableHandler)
if valuer, ok := h.(interface{ Value() string }); ok {
anyH.valueFunc = valuer.Value
} else {
anyH.valueFunc = h.Label // Fallback to Label
}
return anyH
}
func NewInteractiveHandler(h HandlerInteractive, color string) *anyHandler {
anyH := &anyHandler{
handlerType: handlerTypeInteractive,
nameFunc: h.Name,
labelFunc: h.Label,
valueFunc: h.Value,
// NO contentFunc - interactive handlers use progress() only
editableFunc: func() bool { return true },
changeFunc: h.Change,
editModeFunc: h.WaitingForUser, // NEW: Auto edit mode detection
origHandler: h,
handlerColor: color, // NEW: Store handler color
}
return anyH
}