-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation.go
More file actions
208 lines (170 loc) · 4.94 KB
/
Copy pathanimation.go
File metadata and controls
208 lines (170 loc) · 4.94 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
package main
import (
"math/rand"
"time"
tea "github.com/charmbracelet/bubbletea"
)
// ── Tick ────────────────────────────────────────────────────
type animTickMsg time.Time
func animTickCmd() tea.Cmd {
return tea.Tick(100*time.Millisecond, func(t time.Time) tea.Msg {
return animTickMsg(t)
})
}
// ── Snapshot diffing ────────────────────────────────────────
type snapshot struct {
files map[string]string // path → status
}
func newSnapshot(files []fileEntry) snapshot {
m := make(map[string]string, len(files))
for _, f := range files {
m[f.path] = f.status
}
return snapshot{files: m}
}
func (s snapshot) diff(current []fileEntry) []changeEvent {
now := time.Now()
var events []changeEvent
currentMap := make(map[string]string, len(current))
for _, f := range current {
currentMap[f.path] = f.status
}
// Detect added or changed
for path, status := range currentMap {
oldStatus, existed := s.files[path]
if !existed {
events = append(events, changeEvent{path: path, status: status, at: now})
} else if oldStatus != status {
events = append(events, changeEvent{path: path, status: status, at: now})
}
}
// Detect removed
for path := range s.files {
if _, exists := currentMap[path]; !exists {
events = append(events, changeEvent{path: path, status: "D", at: now})
}
}
return events
}
// ── Change events ───────────────────────────────────────────
type changeEvent struct {
path string
status string
at time.Time
}
// ── Events ring buffer ──────────────────────────────────────
type eventsRing struct {
items []changeEvent
capacity int
}
func newEventsRing(capacity int) eventsRing {
return eventsRing{
items: make([]changeEvent, 0, capacity),
capacity: capacity,
}
}
func (r *eventsRing) push(events []changeEvent) {
r.items = append(r.items, events...)
if len(r.items) > r.capacity {
r.items = r.items[len(r.items)-r.capacity:]
}
}
func (r *eventsRing) recentPaths(within time.Duration) map[string]bool {
cutoff := time.Now().Add(-within)
result := make(map[string]bool)
for _, e := range r.items {
if e.at.After(cutoff) {
result[e.path] = true
}
}
return result
}
func (r *eventsRing) count() int {
return len(r.items)
}
// ── Owl state ───────────────────────────────────────────────
type owlExpression int
const (
owlIdle owlExpression = iota // ( o.o )
owlBlink // ( -.- )
owlWide // ( O.O )
owlRight // ( o.o)>
owlLeft // <(o.o )
)
type owlState struct {
expr owlExpression
exprTTL int // ticks remaining in current expression
// Countdowns to next trigger (in ticks)
blinkIn int // 20-50 ticks (2-5s)
wideIn int // 100-200 ticks (10-20s)
glanceIn int // 200-400 ticks (20-40s)
}
func newOwlState() owlState {
return owlState{
expr: owlIdle,
blinkIn: 20 + rand.Intn(31), // 20-50
wideIn: 100 + rand.Intn(101), // 100-200
glanceIn: 200 + rand.Intn(201), // 200-400
}
}
func (o *owlState) tick() {
// Decrement expression TTL
if o.exprTTL > 0 {
o.exprTTL--
if o.exprTTL == 0 {
o.expr = owlIdle
}
}
// Decrement trigger countdowns
o.blinkIn--
o.wideIn--
o.glanceIn--
// Fire triggers (priority: wide > glance > blink)
if o.wideIn <= 0 {
o.expr = owlWide
o.exprTTL = 3 + rand.Intn(6) // 3-8 ticks
o.wideIn = 100 + rand.Intn(101)
} else if o.glanceIn <= 0 {
if rand.Intn(2) == 0 {
o.expr = owlRight
} else {
o.expr = owlLeft
}
o.exprTTL = 3 + rand.Intn(6) // 3-8 ticks
o.glanceIn = 200 + rand.Intn(201)
} else if o.blinkIn <= 0 {
o.expr = owlBlink
o.exprTTL = 1 + rand.Intn(2) // 1-2 ticks
o.blinkIn = 20 + rand.Intn(31)
}
}
// owlTop returns the top line of the owl sprite (always 7 chars).
func owlTop() string {
return ` /\_/\ `
}
// owlBottom returns the bottom line based on current expression (always 7 chars).
func (o *owlState) owlBottom() string {
switch o.expr {
case owlBlink:
return "( -.- )"
case owlWide:
return "( O.O )"
case owlRight:
return "( o.o)>"
case owlLeft:
return "<(o.o )"
default:
return "( o.o )"
}
}
// ── Spinner state ───────────────────────────────────────────
var brailleFrames = []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"}
type spinnerState struct {
frame int
}
func (s *spinnerState) tick() {
s.frame = (s.frame + 1) % len(brailleFrames)
}
func (s *spinnerState) view() string {
return brailleFrames[s.frame]
}