From b53ca78ad2591076e2bc1f353bdc22bac6ccebce Mon Sep 17 00:00:00 2001 From: Botond Hende Date: Wed, 19 Jun 2024 22:47:12 +0200 Subject: initial commit --- animatedtext.go | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 animatedtext.go (limited to 'animatedtext.go') 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 -- cgit v1.2.3-70-g09d2