-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivitybar_v2.go
More file actions
213 lines (178 loc) · 4.78 KB
/
activitybar_v2.go
File metadata and controls
213 lines (178 loc) · 4.78 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
//go:build stack
// +build stack
package tui
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/SCKelemen/cli/renderer"
"github.com/SCKelemen/color"
design "github.com/SCKelemen/design-system"
"github.com/SCKelemen/layout"
"github.com/SCKelemen/text"
)
// ActivityBarV2 uses the full SCKelemen stack for rendering
type ActivityBarV2 struct {
width int
height int
message string
active bool
startTime time.Time
elapsed time.Duration
spinner int
focused bool
progress string
cancelable bool
tokens *design.DesignTokens
accentColor *color.Color
}
// NewActivityBarV2 creates an activity bar using the full stack
func NewActivityBarV2(tokens *design.DesignTokens) *ActivityBarV2 {
accent, _ := color.ParseColor(tokens.Accent)
return &ActivityBarV2{
message: "Ready",
cancelable: true,
tokens: tokens,
accentColor: &accent,
}
}
// Init initializes the activity bar
func (a *ActivityBarV2) Init() tea.Cmd {
return nil
}
// Update handles messages
func (a *ActivityBarV2) Update(msg tea.Msg) (Component, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
a.width = msg.Width
a.height = msg.Height
case tickMsg:
if a.active {
a.spinner = (a.spinner + 1) % len(spinnerFrames)
a.elapsed = time.Since(a.startTime)
return a, a.tick()
}
case tea.KeyMsg:
if a.focused && a.active && a.cancelable && msg.String() == "esc" {
a.Stop()
}
}
return a, nil
}
// View renders using the SCKelemen stack
func (a *ActivityBarV2) View() string {
if a.width == 0 {
return ""
}
// Create layout context
ctx := layout.NewLayoutContext(float64(a.width), float64(a.height), 16)
// Create root node
root := &layout.Node{
Style: layout.Style{
Display: layout.DisplayFlex,
FlexDirection: layout.FlexDirectionRow,
AlignItems: layout.AlignItemsCenter,
Width: layout.Px(float64(a.width)),
Height: layout.Ch(1),
Gap: layout.Ch(1),
},
}
// Build content
var content strings.Builder
if !a.active {
// Inactive state
dimmed, _ := color.ParseColor(a.tokens.Color)
dimmed = dimmed.Darken(0.3)
txt := text.NewTerminal()
msg := a.message
if txt.Width(msg) > float64(a.width) {
msg = msg[:a.width-3] + "..."
}
content.WriteString(dimmed.ToANSI() + msg + "\033[0m")
} else {
// Active state with spinner
spinner := spinnerFrames[a.spinner]
// Spinner in accent color
content.WriteString(a.accentColor.ToANSI() + "✳ " + "\033[0m")
content.WriteString(a.message)
// Build status parts
var status []string
if a.cancelable {
dimmed, _ := color.ParseColor(a.tokens.Color)
dimmed = dimmed.Darken(0.3)
status = append(status, dimmed.ToANSI()+"esc to interrupt"+"\033[0m")
}
if a.elapsed > 0 {
status = append(status, a.formatDuration(a.elapsed))
}
if a.progress != "" {
status = append(status, a.accentColor.ToANSI()+a.progress+"\033[0m")
}
if len(status) > 0 {
content.WriteString(" (")
content.WriteString(strings.Join(status, " · "))
content.WriteString(")")
}
}
// Create styled node
white, _ := color.ParseColor("#FAFAFA")
style := &renderer.Style{
Foreground: &white,
}
rootStyled := renderer.NewStyledNode(root, style)
rootStyled.Content = content.String()
// Layout and render
constraints := layout.Tight(float64(a.width), float64(a.height))
layout.Layout(root, constraints, ctx)
screen := renderer.NewScreen(a.width, a.height)
screen.Render(rootStyled)
return screen.String()
}
// Focus is called when this component receives focus
func (a *ActivityBarV2) Focus() {
a.focused = true
}
// Blur is called when this component loses focus
func (a *ActivityBarV2) Blur() {
a.focused = false
}
// Focused returns whether this component is currently focused
func (a *ActivityBarV2) Focused() bool {
return a.focused
}
// Start begins the activity animation
func (a *ActivityBarV2) Start(message string) tea.Cmd {
a.message = message
a.active = true
a.startTime = time.Now()
a.elapsed = 0
a.spinner = 0
return a.tick()
}
// Stop stops the activity animation
func (a *ActivityBarV2) Stop() {
a.active = false
a.message = "Ready"
a.progress = ""
}
// SetProgress updates the progress indicator
func (a *ActivityBarV2) SetProgress(progress string) {
a.progress = progress
}
// tick returns a command that sends a tickMsg after a delay
func (a *ActivityBarV2) tick() tea.Cmd {
return tea.Tick(100*time.Millisecond, func(t time.Time) tea.Msg {
return tickMsg(t)
})
}
// formatDuration formats a duration as "1m 14s" or "14s"
func (a *ActivityBarV2) formatDuration(d time.Duration) string {
seconds := int(d.Seconds())
if seconds < 60 {
return fmt.Sprintf("%ds", seconds)
}
minutes := seconds / 60
seconds = seconds % 60
return fmt.Sprintf("%dm %ds", minutes, seconds)
}