Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tickertape): implement ticker tape model and functionality #687

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions tickertape/tickertape.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package tickertape

import (
"time"

tea "github.com/charmbracelet/bubbletea"
)

// Model represents the state of the ticker tape.
type Model struct {
Text string // The Text to be displayed in the ticker tape.
Position int // The current Position of the ticker tape.
TickerWidth int // The TickerWidth of the ticker tape display.
}

// tickMsg is a message used to trigger the ticker tape update.
type tickMsg struct{}

// Init initializes the ticker tape model and starts the ticking process.
func (m *Model) Init() tea.Cmd {
return m.tick()
}

// tick returns a command that sends a tickMsg after a specified duration.
func (m *Model) tick() tea.Cmd {
return tea.Tick(time.Millisecond*150, func(time.Time) tea.Msg {

Check failure on line 26 in tickertape/tickertape.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (ubuntu-latest)

Magic number: 150, in <argument> detected (mnd)

Check failure on line 26 in tickertape/tickertape.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (macos-latest)

Magic number: 150, in <argument> detected (mnd)

Check failure on line 26 in tickertape/tickertape.go

View workflow job for this annotation

GitHub Actions / lint / lint-soft (windows-latest)

Magic number: 150, in <argument> detected (mnd)
return tickMsg{}
})
}

// Update handles incoming messages and updates the ticker tape model accordingly.
func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.TickerWidth = msg.Width // Update the TickerWidth of the ticker tape display.
return m, nil
case tickMsg:
m.Position = (m.Position + 1) % len(m.Text) // Update the Position of the ticker tape.
return m, m.tick() // Schedule the next tick.
}
return m, nil
}

// View renders the ticker tape view.
func (m *Model) View() string {
ticker := m.Text[m.Position:] + m.Text[:m.Position]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should probably use ansi.Truncate and ansi.TruncateLeft here 🤔

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I was building an app to manage stock portfolios on the CLI and I thought this was an interesting addition ☺️

I'll have a look at your recommendation in more detail when I'm back from work (still learning how to use this framework 😅)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cool!

to clarify, ansi is from here: https://pkg.go.dev/github.com/charmbracelet/x/ansi

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, accidentally re-requested review XD - disregard


// Get the actual displayable TickerWidth
displayWidth := m.TickerWidth
if displayWidth < len(ticker) {
ticker = ticker[:displayWidth]
}

return ticker
}

// UpdateText updates the Text of the ticker tape.
func (m *Model) UpdateText(newText string) {
m.Text = newText
m.Position = 0 // Reset Position to start.
}

// UpdateWidth updates the TickerWidth of the ticker tape.
func (m *Model) UpdateWidth(newWidth int) {
m.TickerWidth = newWidth
}
Loading