TTY Toolkit

repository·master·Indexed 25 days ago

https://github.com/piotrmurach/tty

A modular toolbox for developing command-line interfaces in Ruby. It includes the teletype executable for project scaffolding and command generation, as well as a collection of specialized components for gathering input, querying terminal properties, and displaying information.

Tokens
7.1K
Snippets
10
Records
46
Agent score
81%

What's inside TTY

  1. Explore the TTY component ecosystem

    master
    The TTY project is a collection of modular components designed for building terminal-based applications. You can mix and match these components to suit your specific needs. Applications generated via the teletype executable typically reference many of these libraries.
  2. Implement command logic in `App::Command`

    master

    Commands generated by teletype consist of a class that inherits from App::Command. The core logic must be implemented within the execute method.

    Accessing Helpers

    App::Command provides helper methods that lazy-load tty components. You can access these within your execute method:

    • prompt(**options): Returns a TTY::Prompt instance for user input.
    • command(**options): Returns a TTY::Command instance for running external shell commands.

    Note: All tty components are added to your app.gemspec by default, but you can modify this to include only what you need.

  3. Bootstrap a new project with `teletype new`

    master

    Use the teletype new [app-name] command to bootstrap a complete command-line application project structure. This setup is based on the Bundler gem command but enhanced with directories for command development.

    Available Flags

    • --author, -a: Injects a name into the generated documentation.
    • --ext: Creates a binary executable in exe/GEM_NAME and includes it in the .gemspec. Disabled by default.
    • --license, -l: Sets the open source license. Supported values: agplv3, apache, bsd2, bsd3, gplv2, gplv3, lgplv3, mit, mplv2, custom. Defaults to mit.
    • --test, -t: Configures the testing framework. Supported values: rspec (default) and minitest.
  4. Jump-start a new CLI application with `teletype new`

    master

    Use the teletype executable to scaffold a new command-line application. This provides the initial project structure following Unix-style patterns.

    To create a new application named app, run:

    $ teletype new app
  5. Install the TTY toolkit

    master

    You can install the entire TTY toolkit or individual components via RubyGems.

    To install all components, add gem 'tty' to your Gemfile and run bundle. Alternatively, you can install the toolkit directly via the command line.

    To install only specific components, use the gem 'tty-*' pattern in your Gemfile.

  6. Add commands to your application with `teletype add`

    master

    Once you have scaffolded an application, you can expand its functionality by adding new commands. Navigate into your application's directory and use the add command.

    To add a command named config to your application, run:

    $ cd app
    $ teletype add config
  7. Manage gemspec files with TTY::Gemspec

    master

    The TTY::Gemspec class provides utilities for reading, writing, and formatting Ruby .gemspec files. It is designed to handle the extraction of the gemspec variable name and its indentation levels to ensure that newly added dependencies match the existing file's style.

    Key Capabilities:

    • Reading: Loads gemspec content and automatically detects the variable name (e.g., spec or gem) and its indentation.
    • Writing: Persists the current content back to a file.
    • Formatting: Generates formatted dependency strings that respect the existing indentation and variable naming conventions.
  8. Define global flags with `class_option`

    master

    To define a flag that is available to every command and subcommand in your application, use class_option within your CLI class. These options are also merged into the options hash available to individual command instances.

    module App
      class CLI < Thor
        class_option :debug, type: :boolean, default: false, desc: 'Run in debug mode'
    
        # ... commands ...
      end
    end
  9. Define command arguments and descriptions

    master

    Teletype uses Thor for command-line parsing. You define how commands appear in help menus using desc and long_desc in your CLI class (usually lib/app/cli.rb).

    Argument Syntax

    • desc 'command NAME': Shows a required argument.
    • desc 'command [NAME]': Shows an optional argument.
    • desc 'command NAME...': Shows a variadic argument.

    Long Descriptions

    Use long_desc to provide detailed usage instructions, which are displayed when a user runs app [command] --help.

    module App
      class CLI < Thor
        desc 'config [FILE]', 'Set and get configuration options'
        long_desc <<-DESC
          You can query/set/replace/unset options with this command.
          The name is an optional key separated by a dot.
        DESC
        def config(file = nil)
          # ...
        end
      end
    end
  10. Reference TTY component libraries

    master

    The following table lists the available TTY components and their primary purposes. Use these libraries to handle specific terminal tasks like styling, input, or layout.

    |  Component   | Description | API docs |
    | ------------ | ----------- | -------- |
    | [pastel](https://github.com/piotrmurach/pastel) | Terminal strings styling with intuitive and clean API. | [docs](http://www.rubydoc.info/gems/pastel) |
    | [tty-box](https://github.com/piotrmurach/tty-box) | Draw various frames and boxes in your terminal. | [docs](http://www.rubydoc.info/gems/tty-box) |
    | [tty-color](https://github.com/piotrmurach/tty-color) | Terminal color capabilities detection. | [docs](http://www.rubydoc.info/gems/tty-color) |
    | [tty-command](https://github.com/piotrmurach/tty-command) | Execute shell commands with pretty logging and capture stdout, stderr and exit status. | [docs](http://www.rubydoc.info/gems/tty-command) |
    | [tty-config](https://github.com/piotrmurach/tty-config) | Define, read and write any Ruby app configurations with a penchant for terminal clients. | [docs](http://www.rubydoc.info/gems/tty-config) |
    | [tty-cursor](https://github.com/piotrmurach/tty-cursor) | Move terminal cursor around. | [docs](http://www.rubydoc.info/gems/tty-cursor) |
    | [tty-editor](https://github.com/piotrmurach/tty-editor) | Open a file or text in the user preferred editor. | [docs](http://www.rubydoc.info/gems/tty-editor) |
    | [tty-file](https://github.com/piotrmurach/tty-file) | File manipulation utility methods. | [docs](http://www.rubydoc.info/gems/tty-file) |
    | [tty-font](https://github.com/piotrmurach/tty-font) | Write text in large stylized characters using a variety of terminal fonts. | [docs](http://www.rubydoc.info/gems/tty-font) |
    | [tty-link](https://github.com/piotrmurach/tty-link) | Hyperlinks in your terminal. | [docs](http://www.rubydoc.info/gems/tty-link) |
    | [tty-logger](https://github.com/piotrmurach/tty-logger) | A readable and structured logging for the terminal. | [docs](http://www.rubydoc.info/gems/tty-logger) |
    | [tty-markdown](https://github.com/piotrmurach/tty-markdown) | Convert a markdown document or text into a terminal friendly output. | [docs](http://www.rubydoc.info/gems/tty-markdown) |
    | [tty-option](https://github.com/piotrmurach/tty-option) | Parser for command line arguments, keywords and options. | [docs](http://www.rubydoc.info/gems/tty-option) |
    | [tty-pager](https://github.com/piotrmurach/tty-pager) | Terminal output paging in a cross-platform way. | [docs](http://www.rubydoc.info/gems/tty-pager) |
    | [tty-pie](https://github.com/piotrmurach/tty-pie_chart) | Draw pie charts in your terminal window. | [docs](http://www.rubydoc.info/gems/tty-pie_chart) |
    | [tty-platform](https://github.com/piotrmurach/tty-platform) | Detecting different operating systems. | [docs](http://www.rubydoc.info/gems/tty-platform) |
    | [tty-progressbar](https://github.com/piotrmurach/tty-progressbar) | A flexible progress bars drawing in terminal emulators. | [docs](http://www.rubydoc.info/gems/tty-progressbar) |
    | [tty-prompt](https://github.com/piotrmurach/tty-prompt) | A beautiful and powerful interactive command line prompt. | [docs](http://www.rubydoc.info/gems/tty-prompt) |
    | [tty-reader](https://github.com/piotrmurach/tty-reader) | A set of methods for processing keyboard input in character, line and multiline modes. | [docs](http://www.rubydoc.info/gems/tty-reader) |
    | [tty-screen](https://github.com/piotrmurach/tty-screen) | Terminal screen properties detection. | [docs](http://www.rubydoc.info/gems/tty-screen) |
    | [tty-spinner](https://github.com/piotrmurach/tty-spinner) | A terminal spinner for tasks with non-deterministic time.| [docs](http://www.rubydoc.info/gems/tty-spinner) |
    | [tty-table](https://github.com/piotrmurach/tty-table) | A flexible and intuitive table output generator. | [docs](http://www.rubydoc.info/gems/tty-table) |
    | [tty-tree](https://github.com/piotrmurach/tty-tree) | Print directory or structured data in a tree like format. | [docs](http://www.rubydoc.info/gems/tty-tree) |
    | [tty-which](https://github.com/piotrmurach/tty-which) | Platform independent implementation of Unix which command. | [docs](http://www.rubydoc.info/gems/tty-which) |
  11. Configure command options and flags

    master

    Use method_option (or method_options for multiple) to define flags and options for a specific command. Options are accessed via the options hash inside the command's execute method.

    Supported Metadata

    • :aliases: Array of short flags (e.g., ['-e']).
    • :banner: Description of the value (e.g., `
  12. Add new commands with `teletype add`

    master

    Once a project is initialized, use teletype add [command-name] to generate new command files and templates. Command names should use camelCase or snake_case (e.g., add_config_command).

    Command Flags

    • --args: Defines positional arguments. Use space-delimited names.
      • Required: name
      • Optional: "name = nil" (enclosed in quotes)
      • Variadic: *names
    • --desc: Provides a custom description for the command.
    • --force: Overwrites an existing command implementation.

    Subcommands

    To create a subcommand, provide both the parent command and the subcommand name: teletype add [parent] [subcommand].