Module: LanguageOperator::CLI::Helpers::UxHelper

Overview

Provides unified access to TTY UI components across all CLI commands, formatters, wizards, and error handlers.

This module consolidates TTY initialization that was previously duplicated across multiple files. It provides memoized instances to avoid unnecessary object allocation.

Available helpers:

  • pastel - Terminal colors and styles
  • prompt - Interactive user input
  • spinner - Loading/progress spinners
  • table - Formatted table display
  • box - Framed messages

Examples:

Using in a command

class MyCommand < Thor
  include Helpers::UxHelper

  def execute
    puts pastel.green("Success!")
    answer = prompt.ask("What's your name?")

    spin = spinner("Loading...")
    spin.auto_spin
    # do work
    spin.success("Done!")
  end
end

Using in a formatter

class MyFormatter
  include Helpers::UxHelper

  def format(data)
    tbl = table(['Name', 'Status'], data)
    tbl.render(:unicode)
  end
end

Instance Method Summary collapse

Instance Method Details

#box(message, title: nil, border: :light, padding: 1) ⇒ String

Creates a framed box around a message

Examples:

Simple box

puts box("Important message!")

With title and custom style

puts box("Warning!", title: "Alert", border: :thick)

With custom styling

puts box("Info", style: { border: { fg: :cyan } }, padding: 1)

Parameters:

  • message (String)

    The message to frame

  • title (String, nil) (defaults to: nil)

    Optional title for the box

  • style (Hash, Symbol)

    Box style or preset (:classic, :thick, :light)

  • padding (Integer, Array) (defaults to: 1)

    Padding inside the box

Returns:

  • (String)

    The framed message ready to print



123
124
125
126
127
128
129
130
131
132
133
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 123

def box(message, title: nil, border: :light, padding: 1)
  require 'tty-box'

  options = {
    padding: padding,
    border: border
  }
  options[:title] = { top_left: " #{title} " } if title

  TTY::Box.frame(message, **options)
end

#highlight_ruby_code(code_content) ⇒ String

Highlights Ruby code with syntax highlighting for terminal display

Examples:

puts highlight_ruby_code("puts 'Hello, world!'")

Parameters:

  • code_content (String)

    The Ruby code to highlight

Returns:

  • (String)

    Syntax-highlighted code ready for terminal output



141
142
143
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 141

def highlight_ruby_code(code_content)
  rouge_formatter.format(rouge_lexer.lex(code_content))
end

#logo(title: nil, sparkle: false) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 145

def (title: nil, sparkle: false)
  puts

  if sparkle
    
  else
    puts "#{pastel.bold.green('LANGUAGE OPERATOR')} v#{pastel.bold(LanguageOperator::VERSION)}"
  end

  puts pastel.dim("#{pastel.bold('')} #{title}") if title
  puts
end

#pastelPastel

Returns a memoized Pastel instance for colorizing terminal output

Examples:

puts pastel.green("Success")
puts pastel.red.bold("Error!")

Returns:

  • (Pastel)

    Colorization utility



54
55
56
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 54

def pastel
  @pastel ||= Pastel.new
end

#promptTTY::Prompt

Returns a memoized TTY::Prompt instance for interactive input

Examples:

name = prompt.ask("Name?")
confirmed = prompt.yes?("Continue?")
choice = prompt.select("Pick:", %w[a b c])

Returns:

  • (TTY::Prompt)

    Interactive prompt utility



65
66
67
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 65

def prompt
  @prompt ||= TTY::Prompt.new
end

#spinner(message, format: :dots) ⇒ TTY::Spinner

Creates a new spinner for long-running operations

Examples:

Basic usage

spin = spinner("Loading...")
spin.auto_spin
# do work
spin.success("Done!")

With custom format

spin = spinner("Processing...", format: :dots2)
spin.auto_spin

Parameters:

  • message (String)

    The message to display next to the spinner

  • format (Symbol) (defaults to: :dots)

    Spinner format (:dots, :dots2, :line, :pipe, etc.)

Returns:

  • (TTY::Spinner)

    Spinner instance



82
83
84
85
86
87
88
89
90
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 82

def spinner(message, format: :dots)
  require 'tty-spinner'
  TTY::Spinner.new(
    "[:spinner] #{message}",
    format: format,
    success_mark: pastel.green(''),
    error_mark: pastel.red('')
  )
end

#table(header, rows, style: :unicode) ⇒ TTY::Table

Creates a formatted table for structured data display

Examples:

Basic table

tbl = table(['Name', 'Status'], [['agent1', 'running'], ['agent2', 'stopped']])
puts tbl.render(:unicode)

With padding

tbl = table(['ID', 'Value'], data)
puts tbl.render(:unicode, padding: [0, 1])

Parameters:

  • header (Array<String>)

    Column headers

  • rows (Array<Array>)

    Table rows

  • style (Symbol) (defaults to: :unicode)

    Rendering style (:unicode, :ascii, :basic, etc.)

Returns:

  • (TTY::Table)

    Table instance ready to render



104
105
106
107
108
# File 'lib/language_operator/cli/helpers/ux_helper.rb', line 104

def table(header, rows, style: :unicode)
  require 'tty-table'
  tbl = TTY::Table.new(header, rows)
  tbl.render(style, padding: [0, 1])
end