-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanimtext.go
78 lines (65 loc) · 1.34 KB
/
animtext.go
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
package sptui
import (
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
)
type AnimTextModel struct {
text string
offset int
width int
id string
}
func (m AnimTextModel) UpdateAnimText(msg tea.Msg) (AnimTextModel, tea.Cmd) {
switch msg := msg.(type) {
case animTextTickMsg:
if m.id != msg.id {
return m, nil
}
m.offset = (m.offset + 1) % len(m.text)
if m.offset == 0 {
return m, AnimTextTickCmd(m.id, 2000*time.Millisecond)
}
return m, AnimTextTickCmd(m.id, 100*time.Millisecond)
}
return m, nil
}
func (m AnimTextModel) ViewAnimText() string {
left := m.offset
right := (left + m.width) % len(m.text)
if left < right {
return m.text[left:right]
} else {
return m.text[left:] + m.text[:right]
}
}
func NewAnimText(t string, id string, opts ...AnimTextModelOpt) AnimTextModel {
m := AnimTextModel{
text: t + strings.Repeat(" ", 4),
width: listWidth - 4,
offset: 0,
id: id,
}
for _, opt := range opts {
opt(&m)
}
return m
}
func AnimTextTickCmd(id string, t time.Duration) tea.Cmd {
return tea.Tick(t, func(t time.Time) tea.Msg {
return animTextTickMsg{
time: t,
id: id,
}
})
}
type animTextTickMsg struct {
id string
time time.Time
}
type AnimTextModelOpt func(*AnimTextModel)
func WithWidth(w int) AnimTextModelOpt {
return func(m *AnimTextModel) {
m.width = w
}
}