### Install TTY::Color Gem Source: https://context7.com/piotrmurach/tty-color/llms.txt Instructions for installing the TTY::Color gem using Bundler or directly via the `gem install` command. ```ruby # Gemfile gem "tty-color" # Or install directly # $ gem install tty-color ``` -------------------------------- ### Cross-platform Color Output Example Source: https://context7.com/piotrmurach/tty-color/llms.txt Demonstrates how to print colored text to the console in a cross-platform manner. It checks for terminal color support and handles Windows-specific considerations. The function takes text and an ANSI color code as input. ```ruby require "tty-color" def print_colored(text, ansi_code) if TTY::Color.color? if TTY::Color.windows? # Windows may need ANSICON or Windows 10 VT support print "\e[#{ansi_code}m#{text}\e[0m" else print "\e[#{ansi_code}m#{text}\e[0m" end else print text end end print_colored("Cross-platform colored text\n", 33) # Yellow ``` -------------------------------- ### Include TTY::Color Module (Ruby) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Provides an example of how to include the TTY::Color module directly into a Ruby script, allowing direct access to its methods without the TTY::Color:: prefix. This is useful for simpler scripts or when TTY::Color is used extensively. ```ruby #!/usr/bin/env ruby include TTY::Color puts color? ``` -------------------------------- ### Get Terminal Color Mode (Ruby) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Shows how to retrieve the number of colors supported by the terminal using the TTY::Color gem. This information can be used to adapt color usage based on terminal capabilities. ```ruby TTY::Color.mode ``` -------------------------------- ### Get Terminal Color Mode (CLI) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Shows how to use the `tty-color-cli` command-line tool to determine the number of colors supported by the terminal. The `-m` or `--mode` flags output the color mode. ```bash tty-color -m tty-color --mode ``` -------------------------------- ### Check for Disabled Colors with TTY::Color.disabled? Source: https://context7.com/piotrmurach/tty-color/llms.txt Checks if color support is explicitly disabled via the NO_COLOR environment variable, following the no-color.org standard. Returns `true` if disabled, `false` otherwise. Includes a practical example for CLI applications. ```ruby require "tty-color" # Check if user has disabled colors if TTY::Color.disabled? puts "Colors disabled by NO_COLOR environment variable" else puts "Colors are allowed" end # Practical usage pattern for CLI applications def colorize(text, color_code) return text if TTY::Color.disabled? || !TTY::Color.color? "\e[#{color_code}m#{text}\e[0m" end puts colorize("Success!", 32) # Green if colors enabled puts colorize("Error!", 31) # Red if colors enabled # Testing with NO_COLOR set: # $ NO_COLOR=1 ruby script.rb # => TTY::Color.disabled? returns true # => TTY::Color.support? returns false ``` -------------------------------- ### Detect Terminal Color Mode with TTY::Color.mode Source: https://context7.com/piotrmurach/tty-color/llms.txt Detects the number of colors supported by the terminal, returning an integer from 0 to 16777216. It analyzes the TERM environment variable and falls back to tput utility queries. Examples show output for different TERM values. ```ruby require "tty-color" colors = TTY::Color.mode case colors when 0 puts "No color support" when 8 puts "Basic 8 colors (standard ANSI)" when 16 puts "Extended 16 colors" when 256 puts "256 colors (xterm-256color)" when 16_777_216 puts "True color (24-bit RGB)" else puts "#{colors} colors supported" end # Example outputs based on TERM environment variable: # TERM=xterm-256color => 256 # TERM=xterm+direct => 16777216 (true color) # TERM=xterm => 8 (via tput fallback) # TERM=dumb => 0 # TERM=alacritty => 256 # TERM=iTerm.app => 256 # TERM=vscode => 256 ``` -------------------------------- ### Check Terminal Color Support (Ruby) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Demonstrates how to check if the current terminal supports color using the TTY::Color gem. This is useful for conditionally applying color formatting to terminal output. ```ruby TTY::Color.color? TTY::Color.support? ``` -------------------------------- ### Configure TTY-Color Verbose Mode and Output Stream Source: https://context7.com/piotrmurach/tty-color/llms.txt Allows configuration of TTY-Color's behavior, including enabling verbose mode for debugging and selecting the output stream for color detection checks. Verbose mode prints warnings when fallbacks are used, aiding in troubleshooting. The output stream can be changed from the default stderr. ```ruby require "tty-color" # Enable verbose mode for debugging TTY::Color.verbose = true # Now warnings will be printed if curses fails to load: # => "no native curses support" # Change the output stream for tty? checks TTY::Color.output = $stdout # Default is $stderr # Check current configuration puts "Verbose: #{TTY::Color.verbose}" # => true puts "Output: #{TTY::Color.output}" # => #> # Reset to defaults TTY::Color.verbose = false TTY::Color.output = $stderr ``` -------------------------------- ### Check Command Availability with TTY::Color.command? Source: https://context7.com/piotrmurach/tty-color/llms.txt This utility method checks if a given shell command can be executed successfully. It's used internally to verify the availability of terminal utilities like `tput`. It returns `true` if the command runs without error, and `false` otherwise. Useful for ensuring external tools are present before attempting to use them. ```ruby require "tty-color" # Check if tput is available if TTY::Color.command?("tput colors") colors = `tput colors`.to_i puts "tput reports #{colors} colors" else puts "tput not available" end # Check for other terminal utilities TTY::Color.command?("echo test") # => true (echo exists) TTY::Color.command?("nonexistent") # => false ``` -------------------------------- ### Check Terminal Color Support (CLI) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Demonstrates using the `tty-color-cli` command-line tool to check if the terminal supports color. The `-s` or `--support` flags return `true` if color is supported. ```bash tty-color -s tty-color --support ``` -------------------------------- ### Detect Windows Platform with TTY::Color.windows? Source: https://context7.com/piotrmurach/tty-color/llms.txt Detects if the code is currently running on a Windows platform. This helper method is used internally to adjust color detection behavior for Windows-specific environments. ```ruby require "tty-color" if TTY::Color.windows? puts "Running on Windows" # Windows-specific color handling # ANSICON or Windows 10+ native ANSI support else puts "Running on Unix-like system" # Standard ANSI escape sequences end ``` -------------------------------- ### Check Terminal Color Support with TTY::Color.color? Source: https://context7.com/piotrmurach/tty-color/llms.txt Determines if the current terminal supports color output. It checks various sources like the TERM environment variable, tput utility, and others. Returns `true` if supported, `false` otherwise. The module can also be included directly into scripts. ```ruby require "tty-color" # Basic color support check if TTY::Color.color? puts "\e[32mTerminal supports colors!\e[0m" else puts "No color support detected" end # Alternative method names (all equivalent) TTY::Color.support? # => true TTY::Color.supports? # => true TTY::Color.supports_color? # => true TTY::Color.color? # => true # Include the module directly in scripts #!/usr/bin/env ruby include TTY::Color if color? puts "\e[31mRed text\e[0m" else puts "Red text (no color)" end ``` -------------------------------- ### Check if Color Support is Disabled (Ruby) Source: https://github.com/piotrmurach/tty-color/blob/master/README.md Illustrates how to check if terminal color support has been explicitly disabled, typically via the NO_COLOR environment variable. This allows applications to respect user preferences for disabling color. ```ruby TTY::Color.disabled? ``` -------------------------------- ### Check if Output is a TTY with TTY::Color.tty? Source: https://context7.com/piotrmurach/tty-color/llms.txt Determines if the output stream is connected to an interactive terminal (TTY). This is a fundamental check used internally by other methods. Returns `true` if it's a TTY device, `false` if redirected. The output stream can be configured. ```ruby require "tty-color" if TTY::Color.tty? puts "Running in interactive terminal" else puts "Output is redirected (pipe or file)" end # The output stream defaults to $stderr but can be configured TTY::Color.output = $stdout # Practical example: auto-detect when to use colors def smart_puts(message, color_code = nil) if color_code && TTY::Color.tty? && TTY::Color.color? puts "\e[#{color_code}m#{message}\e[0m" else puts message end end smart_puts("Interactive mode!", 36) # Cyan in terminal, plain in pipe ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.