summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBotond Hende <nettingman@gmail.com>2024-06-19 22:47:12 +0200
committerBotond Hende <nettingman@gmail.com>2024-06-19 22:47:12 +0200
commitb53ca78ad2591076e2bc1f353bdc22bac6ccebce (patch)
tree53bbac5f100711e759e985cbacb4758c7640454c
initial commitHEADv0.1.0master
-rw-r--r--animatedtext.go94
-rw-r--r--go.mod22
-rw-r--r--go.sum34
3 files changed, 150 insertions, 0 deletions
diff --git a/animatedtext.go b/animatedtext.go
new file mode 100644
index 0000000..155e371
--- /dev/null
+++ b/animatedtext.go
@@ -0,0 +1,94 @@
+package animatedtext
+
+import (
+ "sync"
+ "time"
+
+ tea "github.com/charmbracelet/bubbletea"
+)
+
+var (
+ lastID int
+ mutex sync.Mutex
+)
+
+func nextID() int {
+ mutex.Lock()
+ defer mutex.Unlock()
+ lastID++
+ return lastID
+}
+
+type AnimatedText struct {
+ FullText string
+ PrintPauseTime time.Duration
+}
+
+type Model struct {
+ AnimatedText AnimatedText
+
+ id int
+ currentCharIdx int
+ finished bool
+}
+
+func New(text string, pauseTime time.Duration) Model {
+ m := Model{
+ AnimatedText: AnimatedText{FullText: text, PrintPauseTime: pauseTime},
+ id: nextID(),
+ currentCharIdx: 0,
+ }
+
+ return m
+}
+
+type TickMsg struct {
+ Time time.Time
+ ID int
+}
+
+func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
+ if m.finished {
+ return m, nil
+ }
+
+ switch msg := msg.(type) {
+ case TickMsg:
+ if msg.ID > 0 && msg.ID != m.id {
+ return m, nil
+ }
+
+ if m.currentCharIdx + 1 >= len(m.AnimatedText.FullText) {
+ m.finished = true
+ return m, nil
+ }
+
+ m.currentCharIdx++
+
+ return m, m.tick(m.id)
+
+ default:
+ return m, nil
+ }
+}
+
+func (m Model) View() string {
+ if (m.finished) {
+ return m.AnimatedText.FullText
+ }
+
+ return m.AnimatedText.FullText[0:m.currentCharIdx]
+}
+
+func (m Model) tick(id int) tea.Cmd {
+ return tea.Tick(m.AnimatedText.PrintPauseTime, func(t time.Time) tea.Msg {
+ return TickMsg{
+ Time: t,
+ ID: id,
+ }
+ })
+}
+
+func Tick() tea.Msg {
+ return TickMsg{Time: time.Now()}
+} \ No newline at end of file
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..0b5baa8
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,22 @@
+module git.wazul.moe/bubbles/animatedtext
+
+go 1.22.4
+
+require github.com/charmbracelet/bubbletea v0.26.4
+
+require (
+ github.com/charmbracelet/x/ansi v0.1.2 // indirect
+ github.com/charmbracelet/x/input v0.1.0 // indirect
+ github.com/charmbracelet/x/term v0.1.1 // indirect
+ github.com/charmbracelet/x/windows v0.1.0 // indirect
+ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
+ github.com/mattn/go-localereader v0.0.1 // indirect
+ github.com/mattn/go-runewidth v0.0.15 // indirect
+ github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
+ github.com/muesli/cancelreader v0.2.2 // indirect
+ github.com/rivo/uniseg v0.4.7 // indirect
+ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
+ golang.org/x/sync v0.7.0 // indirect
+ golang.org/x/sys v0.20.0 // indirect
+ golang.org/x/text v0.3.8 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..2e2fe9b
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,34 @@
+github.com/charmbracelet/bubbletea v0.26.4 h1:2gDkkzLZaTjMl/dQBpNVtnvcCxsh/FCkimep7FC9c40=
+github.com/charmbracelet/bubbletea v0.26.4/go.mod h1:P+r+RRA5qtI1DOHNFn0otoNwB4rn+zNAzSj/EXz6xU0=
+github.com/charmbracelet/x/ansi v0.1.2 h1:6+LR39uG8DE6zAmbu023YlqjJHkYXDF1z36ZwzO4xZY=
+github.com/charmbracelet/x/ansi v0.1.2/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
+github.com/charmbracelet/x/input v0.1.0 h1:TEsGSfZYQyOtp+STIjyBq6tpRaorH0qpwZUj8DavAhQ=
+github.com/charmbracelet/x/input v0.1.0/go.mod h1:ZZwaBxPF7IG8gWWzPUVqHEtWhc1+HXJPNuerJGRGZ28=
+github.com/charmbracelet/x/term v0.1.1 h1:3cosVAiPOig+EV4X9U+3LDgtwwAoEzJjNdwbXDjF6yI=
+github.com/charmbracelet/x/term v0.1.1/go.mod h1:wB1fHt5ECsu3mXYusyzcngVWWlu1KKUmmLhfgr/Flxw=
+github.com/charmbracelet/x/windows v0.1.0 h1:gTaxdvzDM5oMa/I2ZNF7wN78X/atWemG9Wph7Ika2k4=
+github.com/charmbracelet/x/windows v0.1.0/go.mod h1:GLEO/l+lizvFDBPLIOk+49gdX49L9YWMB5t+DZd0jkQ=
+github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
+github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
+github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
+github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
+github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
+github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
+github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
+github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
+github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
+github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
+github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
+github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
+github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
+github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
+golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E=
+golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
+golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
+golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
+golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
+golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
+golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=