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

Conversation

nick-popovic
Copy link

@nick-popovic nick-popovic commented Dec 11, 2024

Ticker Tape Model in Bubble Tea

Overview

The tickertape.Model is used in a Bubble Tea application to create a scrolling ticker tape effect. Here's a summary of its usage:

Initialization

  • The Model struct is initialized with text, position, and ticker width.
  • The Init method starts the ticking process by returning a command that triggers periodic updates.

Updating

  • The Update method handles incoming messages:
    • WindowSizeMsg updates the ticker width.
    • tickMsg updates the position of the ticker tape and schedules the next tick.
  • The UpdateText method allows updating the text of the ticker tape.
  • The UpdateWidth method allows updating the width of the ticker tape.

Rendering

  • The View method generates the current view of the ticker tape based on the position and width.

Integration

  • In the main application model, an instance of tickertape.Model is created and managed.
  • The main model's Update method delegates relevant messages to the ticker tape model's Update method.
  • The main model's View method includes the ticker tape's view.
  • To use this model inside the parent/main model, you need to update you parent/main model's Init method. Example can be found here https://gist.github.com/nick-popovic/2f2dd4f94f5daff52b54edbbcd41164a

Example

demo:
https://github.com/user-attachments/assets/9709d089-f7dd-45be-96d3-7bb96ec0c7b7

This setup allows the ticker tape to scroll text across the screen, updating its position at regular intervals.

@meowgorithm meowgorithm added the enhancement New feature or request label Dec 11, 2024

// 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

@caarlos0
Copy link
Member

this looks fun!

@meowgorithm
Copy link
Member

Agreed, this is a fun one. @nick-popovic do you have any example code we can use to help get a feel for this one? The URL above is returning a 404.

@nick-popovic
Copy link
Author

@meowgorithm thanks for the catch! I edited the link in my main comment above. Here is the link to the following:
demo code: https://gist.github.com/nick-popovic/2f2dd4f94f5daff52b54edbbcd41164a
demo gif: https://github.com/user-attachments/assets/9709d089-f7dd-45be-96d3-7bb96ec0c7b7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants