### Initialize and Start Timer Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Create a Timer component for a countdown and start it. The `start` method returns a command. ```ruby timer = Bubbles::Timer.new(60) command = timer.start ``` -------------------------------- ### Key Binding Examples for Help Component Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/key.md Demonstrates how to structure key bindings for use with a help component, showing examples for both short and full help displays. ```ruby class AppWithHelp def short_help [ @keybindings[:up], @keybindings[:down], @keybindings[:quit] ] end def full_help [ [@keybindings[:up], @keybindings[:down]], [@keybindings[:left], @keybindings[:right]], [@keybindings[:save], @keybindings[:quit]] ] end end help = Bubbles::Help.new puts help.view(app_with_help) ``` -------------------------------- ### Initialize and Start Timer Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/timer.md Starts the timer and returns the initial tick command. The timer begins in a running state. ```ruby command = timer.init ``` -------------------------------- ### Full Spinner Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/spinner.md Demonstrates how to integrate the `Bubbles::Spinner` component into a Bubble Tea application. This example shows initialization, handling messages, and rendering the spinner with custom styling. ```ruby require "bubbletea" require "bubbles" class LoadingApp include Bubbletea::Model def initialize @spinner = Bubbles::Spinner.new(spinner: Bubbles::Spinners::DOT) @spinner.style = Lipgloss::Style.new.foreground("205") end def init [self, @spinner.tick] end def update(message) case message when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "q" when Bubbles::Spinner::TickMessage @spinner, command = @spinner.update(message) return [self, command] end [self, nil] end def view "#{@spinner.view} Loading...\n\nPress q to quit" end end Bubbletea.run(LoadingApp.new) ``` -------------------------------- ### Full Editor Application Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-area.md A complete example demonstrating how to integrate the Bubbles TextArea into a Bubbletea application. It shows initialization, focus management, message handling, and rendering. ```ruby require "bubbletea" require "bubbles" class EditorApp include Bubbletea::Model def initialize @editor = Bubbles::TextArea.new(width: 80, height: 20) @editor.placeholder = "Start typing..." @editor.show_line_numbers = true @editor.prompt_style = Lipgloss::Style.new.foreground("240") end def init [self, @editor.focus] end def update(message) case message when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "ctrl+c" end @editor, command = @editor.update(message) [self, command] end def view info = "Row: #{@editor.row + 1}, Col: #{@editor.col + 1}, " info += "Lines: #{@editor.line_count}\n\n" info + @editor.view + "\n\nctrl+c to quit" end end Bubbletea.run(EditorApp.new) ``` -------------------------------- ### Full Editor Application Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/key.md This example demonstrates a complete text editor application using Bubbles::Key for handling various keyboard inputs like navigation, saving, and quitting. It initializes key bindings and processes messages to update the application state. ```ruby require "bubbletea" require "bubbles" class EditorApp include Bubbletea::Model def initialize @keybindings = { up: Bubbles::Key.binding( keys: ["up", "k"], help: ["↑/k", "move up"] ), down: Bubbles::Key.binding( keys: ["down", "j"], help: ["↓/j", "move down"] ), left: Bubbles::Key.binding( keys: ["left", "h"], help: ["←/h", "move left"] ), right: Bubbles::Key.binding( keys: ["right", "l"], help: ["→/l", "move right"] ), save: Bubbles::Key.binding( keys: ["ctrl+s"], help: ["ctrl+s", "save"] ), quit: Bubbles::Key.binding( keys: ["q", "esc", "ctrl+c"], help: ["q", "quit"] ) } @cursor = { row: 0, col: 0 } end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage if Bubbles::Key.matches?(message, @keybindings[:up]) @cursor[:row] = [@cursor[:row] - 1, 0].max elsif Bubbles::Key.matches?(message, @keybindings[:down]) @cursor[:row] += 1 elsif Bubbles::Key.matches?(message, @keybindings[:left]) @cursor[:col] = [@cursor[:col] - 1, 0].max elsif Bubbles::Key.matches?(message, @keybindings[:right]) @cursor[:col] += 1 elsif Bubbles::Key.matches?(message, @keybindings[:save]) # Save action elsif Bubbles::Key.matches?(message, @keybindings[:quit]) return [self, Bubbletea.quit] end end [self, nil] end def view cursor_str = "Cursor: #{@cursor[:row]}, #{@cursor[:col]}\n\n" bindings_str = "" @keybindings.each_value do |binding| if binding.help? key_text, desc = binding.help bindings_str += "#{key_text} - #{desc}\n" end end cursor_str + bindings_str end end Bubbles::Key.binding(keys: "single", help: ["key", "description"]) Bubbles::Key.binding(keys: ["multi", "keys"], help: ["m/k", "multiple keys"]) ``` -------------------------------- ### Progress Component Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/progress.md Demonstrates how to use the Bubbles Progress component to create a visual progress bar. This example shows initialization, setting gradients, updating progress on user input (spacebar), and quitting the application (q key). ```ruby require "bubbles" class DownloadApp include Bubbletea::Model def initialize @progress = Bubbles::Progress.new(width: 40) @progress.gradient("#00ff00", "#ffff00", scaled: true) end def init [self, @progress.set_percent(0.0)] end def update(message) case message when Bubbletea::KeyMessage if message.to_s == "space" @progress, command = @progress.update(message) if @progress.percent < 1.0 new_command = @progress.set_percent(@progress.percent + 0.1) return [self, Bubbletea.sequence(command, new_command)] end end return [self, Bubbletea.quit] if message.to_s == "q" end @progress, command = @progress.update(message) [self, command] end def view "Progress: #{@progress.view}\n\nSpace to advance, q to quit" end end Bubbletea.run(DownloadApp.new) ``` -------------------------------- ### Full Table Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/table.md A complete example demonstrating how to initialize and use the Bubbles Table component within a Bubbletea application. It includes setting up columns, rows, styles, and handling user input for selection and quitting. ```ruby require "bubbletea" require "bubbles" class DataApp include Bubbletea::Model def initialize @table = Bubbles::Table.new( columns: [ { title: "Name", width: 15 }, { title: "Age", width: 5 }, { title: "City", width: 20 } ], rows: [ ["Alice Johnson", "28", "New York"], ["Bob Smith", "34", "London"], ["Carol Davis", "31", "Tokyo"], ["David Wilson", "29", "Berlin"], ["Eve Miller", "26", "Paris"] ], height: 10 ) @table.header_style = Lipgloss::Style.new.bold(true).foreground("5") @table.selected_style = Lipgloss::Style.new.background("5") end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage if message.to_s == "enter" row = @table.selected_row_data return [self, Bubbletea.quit] end return [self, Bubbletea.quit] if message.to_s == "q" end @table, command = @table.update(message) [self, command] end def view info = "Row #{@table.selected_row + 1}/#{@table.rows.length}\n\n" info + @table.view + "\n\nq to quit" end end Bubbletea.run(DataApp.new) ``` -------------------------------- ### Start Stopwatch Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/stopwatch.md Initializes and starts the stopwatch, returning the first tick command. The stopwatch begins counting from zero. ```ruby command = stopwatch.init ``` -------------------------------- ### Install Gem Dependencies Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Installs the necessary Ruby gem dependencies for the project using Bundler. ```bash bundle install ``` -------------------------------- ### Complete Spinner Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md A full example demonstrating how to use the Bubbles Spinner component within a Bubble Tea application. This includes initialization, update logic, and view rendering. ```ruby require "bubbletea" require "lipgloss" require "bubbles" class MyApp include Bubbletea::Model def initialize @spinner = Bubbles::Spinner.new @spinner.spinner = Bubbles::Spinners::DOT end def init [self, @spinner.tick] end def update(message) case message when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "q" end @spinner, command = @spinner.update(message) [self, command] end def view "#{@spinner.view} Loading...\n\nPress q to quit" end end Bubbletea.run(MyApp.new) ``` -------------------------------- ### Initialize and Control Stopwatch Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a Stopwatch and use its methods to start, stop, or toggle its running state. `start` and `toggle` return commands. ```ruby stopwatch = Bubbles::Stopwatch.new command = stopwatch.start stopwatch.stop command = stopwatch.toggle ``` -------------------------------- ### Keymap Interface for Full Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Example implementation of the `full_help` method for a keymap object, returning an array of arrays of `Key::Binding` objects for columnar display. ```ruby def full_help # Return array of arrays for columnar display [ [binding1, binding2], [binding3, binding4], [binding5] ] end ``` -------------------------------- ### Keymap Interface for Short Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Example implementation of the `short_help` method for a keymap object, returning an array of `Key::Binding` objects for compact display. ```ruby def short_help # Return array of Key::Binding for compact display [binding1, binding2, binding3] end ``` -------------------------------- ### Full Application Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Demonstrates a complete application integrating the Bubbles Help component. It includes defining key bindings, initializing the help component with custom styles, and handling user input to toggle help display. ```ruby require "bubbletea" require "bubbles" class AppWithHelp include Bubbletea::Model KEYS = { up: Bubbles::Key.binding( keys: ["up", "k"], help: ["↑/k", "up"] ), down: Bubbles::Key.binding( keys: ["down", "j"], help: ["↓/j", "down"] ), left: Bubbles::Key.binding( keys: ["left", "h"], help: ["←/h", "left"] ), right: Bubbles::Key.binding( keys: ["right", "l"], help: ["→/l", "right"] ), save: Bubbles::Key.binding( keys: ["ctrl+s"], help: ["ctrl+s", "save"] ), quit: Bubbles::Key.binding( keys: ["q"], help: ["q", "quit"] ) } def initialize @help = Bubbles::Help.new @help.width = 80 @help.key_style = Lipgloss::Style.new.bold(true).foreground("5") @help.desc_style = Lipgloss::Style.new.foreground("8") @help.separator_style = Lipgloss::Style.new.foreground("8") @show_full_help = false end def short_help [ KEYS[:up], KEYS[:down], KEYS[:quit] ] end def full_help [ [KEYS[:up], KEYS[:down]], [KEYS[:left], KEYS[:right]], [KEYS[:save], KEYS[:quit]] ] end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage case message.to_s when "?" @show_full_help = !@show_full_help when "q" return [self, Bubbletea.quit] end end [self, nil] end def view output = "Application\n\n" @help.show_all = @show_full_help help_text = @help.view(self) output += help_text output += "\n\n? to toggle help, q to quit" output end end Bubbletea.run(AppWithHelp.new) ``` -------------------------------- ### Start Timer Countdown Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/timer.md Explicitly starts the timer countdown. Returns a Bubbletea command. ```ruby command = timer.start ``` -------------------------------- ### Full FilePicker Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/file-picker.md Demonstrates how to integrate and use the FilePicker component within a Bubbletea application, including initialization and event handling. ```ruby require "bubbletea" require "bubbles" class FileSelectApp include Bubbletea::Model def initialize @picker = Bubbles::FilePicker.new(directory: File.expand_path("~")) @picker.allowed_types = ["rb", "md", "txt"] @picker.show_permissions = true @picker.dir_allowed = false @picker.file_allowed = true end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "q" end @picker, command = @picker.update(message) if @picker.did_select_file? # File was selected return [self, Bubbletea.quit] end [self, command] end def view if @picker.selected? info = "Selected: #{@picker.path}\n\n" else info = "No selection\n\n" end info + @picker.view + "\n\nq to quit" end end Bubbletea.run(FileSelectApp.new) ``` -------------------------------- ### Customizing Help View Styles Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Instantiate Bubbles::Help and customize styles for keys, descriptions, and separators using Lipgloss::Style. This example demonstrates setting up a styled help view. ```ruby help = Bubbles::Help.new help.width = 100 help.key_style = Lipgloss::Style.new.bold(true).foreground("212") help.desc_style = Lipgloss::Style.new.foreground("248") help.separator_style = Lipgloss::Style.new.foreground("240") help.short_separator = " | " puts help.short_help_view([binding1, binding2, binding3]) ``` -------------------------------- ### Initialize Spinner and Start Animation Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/spinner.md Initializes the spinner and returns a command to schedule the first animation frame update. ```ruby spinner = Bubbles::Spinner.new spinner, command = spinner.init ``` -------------------------------- ### Initialize FilePicker Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/file-picker.md Creates a new FilePicker instance, specifying the starting directory. The default is the current directory. ```ruby Bubbles::FilePicker.new(directory: ".") ``` -------------------------------- ### Start or Resume Stopwatch Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/stopwatch.md Starts the stopwatch if paused, or resumes it if it was stopped. Returns a Bubbletea::Command. ```ruby command = stopwatch.start ``` -------------------------------- ### Bubbles::Timer#start Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/timer.md Explicitly starts the timer countdown. This method returns a command that should be processed by the application's update loop. ```APIDOC ## Bubbles::Timer#start ### Description Starts the timer countdown. ### Method `start()` ### Returns - `Bubbletea::Command` - The command to start the timer. ``` -------------------------------- ### Move Cursor to Start Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-input.md Moves the cursor to the beginning of the input text. ```ruby input.cursor_start ``` -------------------------------- ### Full Paginator Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/paginator.md Demonstrates a complete application using Bubbles::Paginator with Bubbletea for a paginated list. Includes initialization, updating total pages, and handling navigation input. ```ruby require "bubbletea" require "bubbles" class PaginatedListApp include Bubbletea::Model def initialize @items = (1..100).map { |i| "Item #{i}" } @paginator = Bubbles::Paginator.new(type: Bubbles::Paginator::DOTS, per_page: 10) @paginator.update_total_pages(@items.length) end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage case message.to_s when "left", "j" @paginator.prev_page when "right", "k" @paginator.next_page when "q" return [self, Bubbletea.quit] end end [self, nil] end def view start_idx, end_idx = @paginator.slice_bounds(@items.length) page_items = @items[start_idx...end_idx] output = "" page_items.each { |item| output += "#{item}\n" } output += "\nPagination: #{@paginator.view}\n\nj/k to navigate, q to quit" output end end Bubbletea.run(PaginatedListApp.new) ``` -------------------------------- ### Bubbles::Stopwatch#init Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/stopwatch.md Initializes and starts the stopwatch, returning the first tick command. ```APIDOC ## Bubbles::Stopwatch#init() -> Bubbletea::Command ### Description Initializes and starts the stopwatch counting from zero. Returns the first tick command to begin the update cycle. ### Method `init` ### Returns - **Bubbletea::Command** - The initial command to start the stopwatch tick. ``` -------------------------------- ### Install Bubbles Gem Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Add the Bubbles gem to your project's Gemfile or install it directly using the gem command. ```ruby gem "bubbles" ``` ```bash gem install bubbles ``` -------------------------------- ### Bubbles::CrypticSpinner#init Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Initializes and starts the spinner animation. Returns the spinner instance and a command to begin the animation loop. ```APIDOC ## Method `init()` ### Description Initializes and starts the spinner animation. Returns the spinner instance and a command to begin the animation loop. ### Method `init() -> [CrypticSpinner, Bubbletea::Command]` ### Parameters None ### Returns A tuple containing the initialized `CrypticSpinner` instance and a `Bubbletea::Command` to start the animation. ``` -------------------------------- ### Graceful Degradation Example: FilePicker Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/errors.md Demonstrates how FilePicker handles permission errors gracefully by rendering an empty file list instead of crashing. This allows for fallback mechanisms. ```ruby # FilePicker permission error doesn't crash picker.current_directory = "/root" # May not be readable # Renders empty file list instead of raising ``` -------------------------------- ### Viewport Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/viewport.md Demonstrates how to use the Bubbles Viewport to display and scroll through a text file. It includes initialization, content loading, message handling for quitting, and rendering the viewport with scroll percentage. ```ruby require "bubbletea" require "bubbles" class ReaderApp include Bubbletea::Model def initialize @viewport = Bubbles::Viewport.new(width: 80, height: 24) @viewport.content = File.read("document.txt") end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "q" end @viewport, command = @viewport.update(message) [self, command] end def view scroll_pct = ((@viewport.scroll_percent * 100).to_i) info = "Page: #{scroll_pct}%\n\n" info + @viewport.view + "\n\nq to quit" end end Bubbletea.run(ReaderApp.new) ``` -------------------------------- ### Bubbles::FilePicker Constructor Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/file-picker.md Initializes a new FilePicker instance. You can specify the starting directory. ```APIDOC ## Constructor ```ruby Bubbles::FilePicker.new(directory: ".") ``` ### Parameters #### Path Parameters - **directory** (String) - Optional - Starting directory path. Defaults to ".". ``` -------------------------------- ### Full Stopwatch Application Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/stopwatch.md A complete Bubbletea application demonstrating the usage of the Bubbles::Stopwatch component. It handles user input for controlling the stopwatch and displays its status. ```ruby require "bubbletea" require "bubbles" class StopwatchApp include Bubbletea::Model def initialize @stopwatch = Bubbles::Stopwatch.new end def init [self, @stopwatch.init] end def update(message) case message when Bubbles::Stopwatch::TickMessage, Bubbles::Stopwatch::StartStopMessage, Bubbles::Stopwatch::ResetMessage @stopwatch, command = @stopwatch.update(message) return [self, command] when Bubbletea::KeyMessage case message.to_s when " " return [self, @stopwatch.toggle] when "r" return [self, @stopwatch.reset] when "q" return [self, Bubbletea.quit] end end [self, nil] end def view running = @stopwatch.running? ? "running" : "paused" "Stopwatch: #{@stopwatch.view} (#{running})\n\n" "Space to pause/resume, r to reset, q to quit" end end Bubbletea.run(StopwatchApp.new) ``` -------------------------------- ### Render Single-Line TextInput Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Get the view representation of the single-line text input. ```ruby input.view ``` -------------------------------- ### CrypticSpinner Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Demonstrates how to integrate and use CrypticSpinner within a Bubbletea application for loading indicators. ```ruby require "bubbletea" require "bubbles" class LoadingApp include Bubbletea::Model def initialize # Simple single-row spinner @spinner = Bubbles::CrypticSpinner.new( size: 15, label: "Loading", cycle_colors: true ) # Matrix-style multi-row spinner @matrix = Bubbles::CrypticSpinner.new( size: 40, rows: 5, label: "Decrypting", color_a: "#00ff00", color_b: "#003300", cycle_colors: true ) end def init [self, @spinner.tick] end def update(message) case message when Bubbles::CrypticSpinner::TickMessage @spinner, command = @spinner.update(message) return [self, command] when Bubbletea::KeyMessage return [self, Bubbletea.quit] if message.to_s == "q" end [self, nil] end def view <<~VIEW Cryptic Spinner Demo Basic: #{@spinner.view} Matrix: #{@matrix.view} Press q to quit VIEW end end Bubbletea.run(LoadingApp.new) ``` -------------------------------- ### Get Viewport Content Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/viewport.md Retrieves the entire content currently set for the viewport as a string. ```ruby text = viewport.content ``` -------------------------------- ### Configure List Component via Constructor and Properties Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/README.md Provides examples of configuring a `Bubbles::List` component, first using constructor options and then by setting properties like title and title style. ```ruby # Constructor list = Bubbles::List.new(items, width: 40, height: 10) # Properties list.title = "My List" list.title_style = Lipgloss::Style.new.bold(true) ``` -------------------------------- ### Get Slice Bounds for Data with Paginator Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Calculate the start and end indices for slicing data based on the current pagination state. ```ruby start_index, end_index = paginator.slice_bounds(items.length) visible_items = items[start_index...end_index] ``` -------------------------------- ### Get Slice Bounds for Current Page Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/paginator.md Retrieves the start and end indices for the items belonging to the current page. Useful for slicing arrays or collections. ```ruby start_idx, end_idx = paginator.slice_bounds(150) items = all_items[start_idx...end_idx] ``` -------------------------------- ### Start Filtering Mode Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Initiate the filtering process, allowing the user to input text to filter the list items. ```ruby command = list.start_filtering ``` -------------------------------- ### Bubbles::Spinner#init Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/spinner.md Initializes the spinner and starts its animation loop. This method returns the spinner instance along with a Bubble Tea command that schedules the first frame update. ```APIDOC ## Bubbles::Spinner#init Initialize the spinner and start animation. ### Returns - `[Spinner, Bubbletea::Command]` - The spinner instance and a tick command that schedules the first frame update. ### Example ```ruby spinner = Bubbles::Spinner.new spinner, command = spinner.init ``` ``` -------------------------------- ### Bubbles::Timer#init Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/timer.md Initializes and starts the timer. This method should be called to begin the countdown process and will return the first tick command if the timer is set to run. ```APIDOC ## Bubbles::Timer#init ### Description Initializes and starts the timer, returning the first tick command. ### Method `init()` ### Returns - `Bubbletea::Command?` - The first tick command, or nil. ``` -------------------------------- ### Bubbles::Stopwatch#start Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/stopwatch.md Starts or resumes the stopwatch's time counting. ```APIDOC ## Bubbles::Stopwatch#start() -> Bubbletea::Command ### Description Starts the stopwatch if it is paused, or resumes it if it was stopped. Returns a command related to starting the stopwatch. ### Method `start` ### Returns - **Bubbletea::Command** - A command indicating the stopwatch has started or resumed. ``` -------------------------------- ### Pomodoro App Usage Example Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/timer.md Demonstrates how to integrate the Bubbles::Timer into a Bubbletea application for a Pomodoro timer. Handles timer messages, user input for toggling and quitting, and displays the remaining time. ```ruby require "bubbletea" require "bubbles" class PomodoroApp include Bubbletea::Model def initialize @timer = Bubbles::Timer.new(25 * 60) # 25 minute Pomodoro end def init [self, @timer.init] end def update(message) case message when Bubbles::Timer::TimeoutMessage return [self, Bubbletea.quit] when Bubbles::Timer::TickMessage, Bubbles::Timer::StartStopMessage @timer, command = @timer.update(message) return [self, command] when Bubbletea::KeyMessage case message.to_s when " " return [self, @timer.toggle] when "q" return [self, Bubbletea.quit] end end [self, nil] end def view "Pomodoro Timer: #{@timer.view}\n\n" "Space to pause/resume, q to quit" end end Bubbletea.run(PomodoroApp.new) ``` -------------------------------- ### Send Initial Blink Message Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cursor.md A static method to manually send the initial message to start the cursor's blink cycle. ```ruby command = Bubbles::Cursor.blink ``` -------------------------------- ### Render Help from Keymap Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Renders help text using the `view` method, which intelligently calls either `full_help` or `short_help` based on the `show_all` property and method availability. ```ruby help_text = help.view(app) puts help_text ``` -------------------------------- ### Add Key Binding Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/README.md Demonstrates how to generate help text for key bindings using the Bubbles Help and Key components. Shows how to define bindings and display short help information. ```ruby help = Bubbles::Help.new help.width = 80 bindings = [ Bubbles::Key.binding(keys: ["q"], help: ["q", "quit"]), Bubbles::Key.binding(keys: ["?"], help: ["?", "help"]) ] puts help.short_help_view(bindings) ``` -------------------------------- ### Create and Configure FilePicker Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a file and directory browser, set its height, and configure visibility of hidden files and allowed types. ```ruby picker = Bubbles::FilePicker.new(directory: ".") picker.height = 15 picker.show_hidden = false picker.allowed_types = ["rb", "txt"] ``` -------------------------------- ### Instantiate Bubbles Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Creates a new instance of the Bubbles::Help component. Configuration of properties should be done after instantiation. ```ruby Bubbles::Help.new ``` -------------------------------- ### Bubbles::Help#view Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Renders help text from a keymap object. It calls `full_help` if `show_all` is true and the method exists, otherwise it calls `short_help`. ```APIDOC #### `view(keymap: untyped) -> String` Render help from keymap object. ```ruby help_text = help.view(app) puts help_text ``` | Parameter | Type | Description | |---|---|---| | keymap | untyped | Object with `short_help` and/or `full_help` methods | ``` -------------------------------- ### Generate Help Text with Bindings Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Create a help text generator and define key bindings with their associated help text. ```ruby help = Bubbles::Help.new bindings = [ Bubbles::Key.binding(keys: ["up", "k"], help: ["↑/k", "up"]), Bubbles::Key.binding(keys: ["down", "j"], help: ["↓/j", "down"]), Bubbles::Key.binding(keys: ["q"], help: ["q", "quit"]) ] help.short_help_view(bindings) ``` -------------------------------- ### Common TextInput Validation Patterns Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/errors.md Provides examples for common validation rules including minimum length, maximum length, pattern matching (email), and custom numeric validation. ```ruby # Minimum length validate: ->(text) { text.length < 1 ? StandardError.new("Required") : nil } ``` ```ruby # Maximum length validate: ->(text) { text.length > 50 ? StandardError.new("Too long") : nil } ``` ```ruby # Pattern matching validate: ->(text) { text.match?(/^\w+@\w+\.\w+$/) ? nil : StandardError.new("Invalid email") } ``` ```ruby # Custom validation validate: ->(text) do begin Integer(text) nil rescue ArgumentError StandardError.new("Must be a number") end end ``` -------------------------------- ### Configure Gradient Coloring Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/progress.md Sets up gradient coloring for the progress bar fill. You can specify start and end colors, and whether the gradient should be scaled to the bar's width. ```ruby progress.gradient("#ff0000", "#0000ff", scaled: true) ``` -------------------------------- ### Render Cursor Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Get the view representation of the cursor. ```ruby cursor.view ``` -------------------------------- ### Full Cursor Demo Application Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cursor.md A complete Bubbletea application demonstrating cursor initialization, mode switching, and rendering. ```ruby require "bubbletea" require "bubbles" class CursorDemoApp include Bubbletea::Model def initialize @cursor = Bubbles::Cursor.new @cursor.char = "_" @cursor.style = Lipgloss::Style.new.bold(true).foreground("5") end def init [self, @cursor.focus] end def update(message) case message when Bubbletea::KeyMessage case message.to_s when "b" return [self, @cursor.set_mode(Bubbles::Cursor::MODE_BLINK)] when "s" return [self, @cursor.set_mode(Bubbles::Cursor::MODE_STATIC)] when "h" return [self, @cursor.set_mode(Bubbles::Cursor::MODE_HIDE)] when "q" return [self, Bubbletea.quit] end end @cursor, command = @cursor.update(message) [self, command] end def view modes = "b=blink, s=static, h=hide\n" mode = case @cursor.mode when Bubbles::Cursor::MODE_BLINK then "blink" when Bubbles::Cursor::MODE_STATIC then "static" when Bubbles::Cursor::MODE_HIDE then "hidden" end "Cursor mode: #{mode}\n\n" "#{@cursor.view}\n\n" "#{modes}q to quit" end end Bubbletea.run(CursorDemoApp.new) ``` -------------------------------- ### Render Paginator Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Get the view representation of the pagination controls. ```ruby paginator.view ``` -------------------------------- ### Bubbles::TextInput#value Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-input.md Gets the current input text. ```APIDOC ## value() -> String ### Description Gets the current input text. ### Returns - `String`: The current input text. ``` -------------------------------- ### Render Viewport Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Get the view representation of the scrollable content pane. ```ruby viewport.view ``` -------------------------------- ### Render Full-Form Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Renders multi-line help text in a grid layout from organized binding groups. Each group forms a column, separated by `full_separator`. ```ruby groups = [ [up_binding, down_binding], [left_binding, right_binding], [save_binding, quit_binding] ] help_text = help.full_help_view(groups) puts help_text ``` -------------------------------- ### selected_index Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Gets the index of the currently selected item within the visible list. ```APIDOC ## Method selected_index Bubbles::List#selected_index ### Description Returns the index of the currently selected item relative to the list of visible items. ### Method `selected_index() -> Integer` ### Parameters None ### Request Example ```ruby index = list.selected_index ``` ### Response #### Success Response (200) Returns an `Integer` representing the index of the selected item. #### Response Example ```ruby 2 ``` ``` -------------------------------- ### Create and Configure Single-Line TextInput Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a single-line text input, set its placeholder and prompt, and focus it for user interaction. ```ruby input = Bubbles::TextInput.new input.placeholder = "Enter your name..." input.prompt = "> " input.focus ``` -------------------------------- ### slice_bounds Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/paginator.md Determines the start and end indices for slicing the items that belong to the current page. ```APIDOC ## slice_bounds(total_items: Integer) -> [Integer, Integer] ### Description Get start and end indices for current page items. ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **total_items** (Integer) - Required - Total number of items. ``` -------------------------------- ### Create a Simple Form with Text Input Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/README.md Demonstrates how to create and manage a single-line text input component for capturing user input. Focuses on initialization, prompt/placeholder setting, and basic update/view cycles. ```ruby require "bubbles" input = Bubbles::TextInput.new input.prompt = "Name: " input.placeholder = "Enter your name" input.focus # In update method input, command = input.update(message) # In view method puts input.view ``` -------------------------------- ### Bubbles::Help#full_help_view Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Renders full-form help from organized binding groups in a grid layout. Each group becomes a column, separated by `full_separator`. ```APIDOC #### `full_help_view(binding_groups: Array[Array[Key::Binding]]?) -> String` Render full-form help from organized binding groups. ```ruby groups = [ [up_binding, down_binding], [left_binding, right_binding], [save_binding, quit_binding] ] help_text = help.full_help_view(groups) puts help_text ``` | Parameter | Type | Description | |---|---|---| | binding_groups | Array[Array[Key::Binding]]? | Groups arranged by column | Displays bindings in a grid layout with columns separated by `full_separator`. Each group becomes a column. **Returns:** Multi-line formatted help text or empty string if no bindings. ``` -------------------------------- ### Initialize Viewport with Dimensions Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/configuration.md Create a Viewport component, defining its display width and height. Mouse wheel scrolling and horizontal step can also be configured. ```ruby Bubbles::Viewport.new(width: 80, height: 24) ``` -------------------------------- ### CrypticSpinner Color Example: Red to Yellow Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Sets up a CrypticSpinner with a red to yellow gradient. ```ruby # Red to yellow spinner = Bubbles::CrypticSpinner.new( color_a: "#ff0000", # Red color_b: "#ffff00" # Yellow ) ``` -------------------------------- ### CrypticSpinner Color Example: Blue to Cyan Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Configures a CrypticSpinner with a blue to cyan gradient. ```ruby # Blue to cyan spinner = Bubbles::CrypticSpinner.new( color_a: "#0000ff", # Blue color_b: "#00ffff" # Cyan ) ``` -------------------------------- ### Get Filter State from List Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the current filter state of the interactive list. ```ruby list.filter_state ``` -------------------------------- ### Display a List with Selection Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/README.md Shows how to initialize and render an interactive list component, allowing users to select items. Includes basic update/view logic and retrieving the selected item. ```ruby items = ["Option 1", "Option 2", "Option 3"] list = Bubbles::List.new(items, width: 40, height: 10) # In update list, command = list.update(message) # In view puts list.view # Get selection selected = list.selected_item ``` -------------------------------- ### Get Selected Item from List Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the currently selected item from the interactive list. ```ruby list.selected_item ``` -------------------------------- ### Bubbles::Help Constructor Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Initializes a new Bubbles::Help object. Properties can be configured after instantiation. ```APIDOC ## Class: Bubbles::Help ### Constructor ```ruby Bubbles::Help.new ``` No parameters. Configure properties after instantiation. ``` -------------------------------- ### Get Selected Item Object Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Retrieve the actual object that is currently selected in the list. ```ruby item = list.selected_item ``` -------------------------------- ### Get Target Percent Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/progress.md Retrieves the current target percentage value of the progress bar. ```ruby target = progress.percent ``` -------------------------------- ### Go to Top of Content Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/viewport.md Jumps the viewport to the very beginning of the content. Returns the lines that become visible. ```ruby visible = viewport.goto_top ``` -------------------------------- ### Get Current Input Value Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-input.md Retrieves the current text content of the input field. ```ruby text = input.value ``` -------------------------------- ### Initialize and Run a Selectable List Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md This snippet demonstrates how to initialize a List component with items, set its title and styles, and run it within a Bubbletea application. It handles user input for selection and quitting. ```ruby require "bubbletea" require "bubbles" class SelectApp include Bubbletea::Model def initialize items = [ { title: "Ruby", description: "Language" }, { title: "Rails", description: "Framework" }, { title: "Bubbles", description: "UI Kit" } ] @list = Bubbles::List.new(items, width: 40, height: 10) @list.title = "Select a project" @list.title_style = Lipgloss::Style.new.bold(true).foreground("5") @list.selected_item_style = Lipgloss::Style.new.foreground("5") end def init [self, nil] end def update(message) case message when Bubbletea::KeyMessage if message.to_s == "enter" item = @list.selected_item return [self, Bubbletea.quit] end return [self, Bubbletea.quit] if message.to_s == "q" end @list, command = @list.update(message) [self, command] end def view @list.view + "\nEnter to select, q to quit" end end Bubbletea.run(SelectApp.new) ``` -------------------------------- ### Get Line Count Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-area.md Returns the total number of lines currently present in the TextArea. ```ruby total = textarea.line_count ``` -------------------------------- ### CrypticSpinner Color Example: Green Matrix Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Sets up a multi-row CrypticSpinner with a green gradient that animates. ```ruby # Green matrix spinner = Bubbles::CrypticSpinner.new( size: 40, rows: 5, color_a: "#00ff00", # Bright green color_b: "#003300", # Dark green cycle_colors: true ) ``` -------------------------------- ### Get Selection from Data Table Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the currently selected row and its data from the data table. ```ruby table.selected_row table.selected_row_data ``` -------------------------------- ### Create and Configure Data Table Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a data table with specified columns and rows, and set its height. ```ruby columns = [ { title: "Name", width: 20 }, { title: "Age", width: 5 }, { title: "City", width: 15 } ] rows = [ ["Alice", "30", "New York"], ["Bob", "25", "London"] ] table = Bubbles::Table.new(columns: columns, rows: rows, height: 10) ``` -------------------------------- ### Get Scroll Information from Viewport Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the scroll percentage and check if the viewport is at the top or bottom. ```ruby viewport.scroll_percent vport.at_top? vport.at_bottom? ``` -------------------------------- ### Get Value from Multi-line TextArea Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the current value entered into the multi-line text area. ```ruby textarea.value ``` -------------------------------- ### Get Value from Single-Line TextInput Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Retrieve the current value entered into the single-line text input. ```ruby input.value ``` -------------------------------- ### Initialize TextArea Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-area.md Creates a new TextArea instance with specified dimensions. The width and height parameters control the visible area of the text editor. ```ruby Bubbles::TextArea.new(width: 40, height: 6) ``` -------------------------------- ### Bubbles::Help#short_help_view Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Renders short-form help from an array of bindings, displaying them horizontally separated by `short_separator`. Filters to enabled bindings with help text. ```APIDOC #### `short_help_view(bindings: Array[Key::Binding]?) -> String` Render short-form help from bindings. ```ruby help_text = help.short_help_view([binding1, binding2, binding3]) puts help_text ``` | Parameter | Type | Description | |---|---|---| | bindings | Array[Key::Binding]? | Array of bindings with help text | **Returns:** Single-line formatted help text or empty string if no bindings. ``` -------------------------------- ### Get Visible Items Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Retrieve the array of items that are currently visible after any active filtering has been applied. ```ruby items = list.visible_items ``` -------------------------------- ### Get Current Line Text Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/text-area.md Retrieves the text content of the line where the cursor is currently located. ```ruby line = textarea.current_line ``` -------------------------------- ### view Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Renders the complete user interface for the list, including title, filter, items, and pagination. ```APIDOC ## Method view Bubbles::List#view ### Description Generates the string representation of the list component for rendering in the terminal. This includes the title, filter input (if active), the list items, pagination controls, and any status messages. ### Method `view() -> String` ### Parameters None ### Request Example ```ruby puts list.view ``` ### Response #### Success Response (200) Returns a `String` representing the rendered UI of the list. #### Response Example ```ruby "\n\n My List\n\n> filter\n\n \e[32m> Item 1\e[0m\n Item 2\n\n (1/2)\n" ``` ``` -------------------------------- ### CrypticSpinner Color Example: Purple to Pink Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/cryptic-spinner.md Configures a CrypticSpinner with default purple to pink gradient colors. ```ruby # Purple to pink (default) spinner = Bubbles::CrypticSpinner.new( color_a: "#6B50FF", # Charple color_b: "#FF60FF" # Dolly ) ``` -------------------------------- ### Create and Configure Multi-line TextArea Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a multi-line text area with specified dimensions, set its placeholder, enable line numbers, and focus it. ```ruby textarea = Bubbles::TextArea.new(width: 60, height: 10) extarea.placeholder = "Type your message..." extarea.show_line_numbers = true extarea.focus ``` -------------------------------- ### Create and Configure Scrollable Viewport Source: https://github.com/marcoroth/bubbles-ruby/blob/main/README.md Instantiate a scrollable content pane with specified dimensions and set its content. ```ruby viewport = Bubbles::Viewport.new(width: 80, height: 20) vport.content = long_text ``` -------------------------------- ### Get Total Line Count Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/viewport.md Returns the total number of lines present in the viewport's content. ```ruby total = viewport.total_line_count ``` -------------------------------- ### Render Short-Form Help Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/help.md Generates a single-line formatted help text from an array of key bindings. It filters for enabled bindings with help text and uses the `short_separator` for display. ```ruby help_text = help.short_help_view([binding1, binding2, binding3]) puts help_text ``` -------------------------------- ### Get Selected Index Source: https://github.com/marcoroth/bubbles-ruby/blob/main/_autodocs/api-reference/list.md Obtain the index of the currently selected item within the visible list of items. ```ruby idx = list.selected_index ```