-
Notifications
You must be signed in to change notification settings - Fork 281
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
nick-popovic
wants to merge
1
commit into
charmbracelet:master
Choose a base branch
from
nick-popovic:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
|
||
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] | ||
|
||
// 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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
andansi.TruncateLeft
here 🤔There was a problem hiding this comment.
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 😅)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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