-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.go
More file actions
175 lines (145 loc) · 5.04 KB
/
update.go
File metadata and controls
175 lines (145 loc) · 5.04 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
package devtui
import (
"time"
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
. "github.com/tinywasm/fmt"
tinytime "github.com/tinywasm/time"
)
// listenToMessages crea un comando para escuchar mensajes del canal
func (h *DevTUI) listenToMessages() tea.Cmd {
return func() tea.Msg {
msg := <-h.tabContentsChan
return channelMsg(msg)
}
}
// tickEverySecond crea un comando para actualizar el tiempo
func (h *DevTUI) tickEverySecond() tea.Cmd {
return tea.Every(time.Second, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
// Update maneja las actualizaciones del estado
func (h *DevTUI) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmds []tea.Cmd
cmd tea.Cmd
)
switch msg := msg.(type) {
case shutdownMsg:
h.isShuttingDown.Store(true)
h.sseCancel()
return h, tea.Sequence(tea.ClearScreen, tea.ExitAltScreen, tea.Quit)
case tea.KeyMsg: // Al presionar una tecla
continueProcessing, keyCmd := h.handleKeyboard(msg)
if !continueProcessing {
if keyCmd != nil {
return h, keyCmd
}
return h, nil
}
if keyCmd != nil {
cmds = append(cmds, keyCmd)
}
case channelMsg: // Handle messages from the channel
// Start listening for new messages again after processing the current one
cmds = append(cmds, h.listenToMessages())
// Convert the channel message to a tabContent type
tc := tabContent(msg)
// Only update the viewport if the message belongs to the currently active tab
if tc.tabSection.Index == h.activeTab {
h.updateViewport()
}
case refreshTabMsg: // Handle manual refresh requests from external tools
// Update viewport for the currently active tab
h.updateViewport()
case tea.WindowSizeMsg: // update the viewport size
headerHeight := lipgloss.Height(h.headerView())
footerHeight := lipgloss.Height(h.footerView())
verticalMarginHeight := headerHeight + footerHeight
if !h.ready {
// Since this program is using the full size of the viewport we
// need to wait until we've received the window dimensions before
// we can initialize the viewport. The initial dimensions come in
// quickly, though asynchronously, which is why we wait for them
// here.
h.viewport = viewport.New(msg.Width, msg.Height-verticalMarginHeight)
h.viewport.YPosition = headerHeight
// Disable mouse wheel to enable terminal text selection
h.viewport.MouseWheelEnabled = false
h.viewport.SetContent(h.ContentView())
h.ready = true
} else {
h.viewport.Width = msg.Width
h.viewport.Height = msg.Height - verticalMarginHeight
}
case tickMsg: // update the time every second
h.currentTime = tinytime.FormatTime(tinytime.Now())
cmds = append(cmds, h.tickEverySecond())
case cursorTickMsg: // toggle cursor for blinking effect
h.cursorVisible = !h.cursorVisible
cmds = append(cmds, h.cursorTick())
case tea.FocusMsg:
h.focused = true
case tea.BlurMsg:
h.focused = false
}
// Update viewport with all messages since mouse is disabled
h.viewport, cmd = h.viewport.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}
return h, tea.Batch(cmds...)
}
func (h *DevTUI) updateViewport() {
h.viewport.SetContent(h.ContentView())
h.viewport.GotoBottom()
}
// RefreshUI updates the TUI display for the currently active tab.
// This method is designed to be called from external tools/handlers to notify
// devtui that the UI needs to be refreshed without creating coupling.
//
// Thread-safe and can be called from any goroutine.
// Only updates the view if the TUI is actively running.
//
// Usage from external tools:
//
// tui.RefreshUI() // Triggers a UI refresh for the active tab
func (h *DevTUI) RefreshUI() {
// Only update if the TUI is actively running and ready
if h.tea == nil || !h.ready {
return
}
// Send a custom message to the tea.Program to trigger a view update
// This MUST be non-blocking when called from within Update loop to avoid deadlocks
go h.tea.Send(refreshTabMsg{})
}
// refreshTabMsg is an internal message type for triggering tab refreshes
type refreshTabMsg struct{}
// editingConfigOpen controls the edit mode state.
// forceClose: when true, bypasses WaitingForUser() check and always closes edit mode (used by ESC).
func (h *DevTUI) editingConfigOpen(open bool, currentField *field, msg string, forceClose bool) {
if open {
h.editModeActivated = true
} else {
// Before closing, check if interactive handler still needs input
// UNLESS forceClose is true (ESC was pressed - user explicitly wants to exit)
if !forceClose && currentField != nil && currentField.isInteractiveHandler() && currentField.shouldAutoActivateEditMode() {
// Handler still wants input (e.g., multi-step wizard)
// Keep edit mode active and refresh value
h.editModeActivated = true
currentField.tempEditValue = currentField.handler.Value()
currentField.setCursorAtEnd()
return
}
h.editModeActivated = false
}
if currentField != nil {
currentField.setCursorAtEnd()
}
if msg != "" {
tabSection := h.TabSections[h.activeTab]
tabSection.addNewContent(Msg.Warning, msg)
}
}