grc (Generic Colouriser)

repository·master·Indexed 24 days ago

https://github.com/garabik/grc

A tool to make command outputs and log files more readable by inserting ANSI colour control codes based on regular expression matching. It includes grcat, a core filter that applies colourisation via configuration files, and grc, a smart frontend that automatically selects configurations for common commands like netstat, ping, tail, and ps.

Tokens
1.5K
Snippets
3
Records
9
Agent score
31%

What's inside grc

  1. How grcat and grc work together

    master

    The project provides two main programs:

    1. grcat: The core filter. It takes standard input, applies colourisation based on a configuration file, and writes to standard output.
    2. grc: A frontend for grcat. It executes a command and automatically pipes its stdout into grcat using a configuration determined by /etc/grc.conf or ~/.grc/grc.conf.

    To use grcat manually with a specific config:

    grcat my_config.conf < input_file
    grcat conf.log < /var/log/syslog
  2. Quickstart with grc

    master

    The grc command acts as a frontend for grcat. It automatically selects appropriate configuration files for common commands and pipes their output through the colouriser. You can use it directly with commands like netstat, ping, tail, or ps.

    Example usage:

    grc netstat
    grc ping hostname
    grc tail /var/log/syslog
    grc ps aux
  3. Configure grcat with custom configuration files

    master

    grcat accepts a configuration file as a parameter. It searches for the file in the following order:

    1. ~/.grc/
    2. /usr/local/share/grc/
    3. /usr/share/grc/

    If the provided name is not found in these directories, grcat treats it as an absolute path.

    Each configuration file consists of entries separated by lines where the first character is non-alphanumeric (except #). Empty lines and lines starting with # are ignored.

  4. Configure grc command aliases

    master

    To automatically use grc for supported commands, you can source the provided shell scripts. The location depends on your installation (e.g., /etc/profile.d/grc.sh or /usr/local/etc/grc.sh for Homebrew).

    Bash

    Append to ~/.bashrc:

    GRC_ALIASES=true
    [[ -s "/etc/profile.d/grc.sh" ]] && source /etc/profile.d/grc.sh

    ZSH

    Append to ~/.zshrc:

    [[ -s "/etc/grc.zsh" ]] && source /etc/grc.zsh

    Fish

    Add to ~/.config/fish/config.fish or ~/.config/fish/conf.d/:

    source /usr/local/etc/grc.fish
  5. Install nmh-in-color for grc

    master

    To colorize the NMH (New Mail Handler) system using grc, follow these installation steps:

    1. Prerequisites: Ensure you have nmh and grc installed. grc requires a Python interpreter.
    2. Configuration Files: Copy the conf.* files from the nmh-in-color package to your grc configuration directory (typically /usr/share/grc/ or /usr/local/share/grc/).
    3. Scripts: Copy the viewmail and vimail scripts to a directory in your $PATH (e.g., ~/bin/).
      • viewmail: Used for viewing colorized e-mail.
      • vimail: Used to call vim in mail-syntax mode for composing messages. Note that vimail uses vim syntax highlighting rather than grc.
    4. Shell & Profile Integration:
      • Append the contents of bashrc-entries to your ~/.bashrc.
      • Append the contents of mh_profile-entries to your ~/.mh_profile.
      • Append the contents of grc.conf-entries to /etc/grc.conf.
    5. Terminal Settings: For optimal results, the author recommends using rxvt. You can use the settings provided in rxvt-resources and load them using xrdb -load .Xdefaults.
  6. Generate dynamic aliases for installed programs

    master

    If you want to create one-off aliases for all programs currently in your $PATH that have a corresponding grc configuration, you can run this loop in your shell. It checks for the existence of the command and echoes an alias definition:

    for cmd in g++ gas head make ld ping6 tail traceroute6 $( ls /usr/share/grc/ ); do
      cmd="${cmd##*conf.}"
      type "${cmd}" >/dev/null 2>&1 && echo alias "${cmd}"="$( which grc ) --colour=auto ${cmd}"
    done
  7. Configure nmh-in-color files

    master

    The nmh-in-color package provides several configuration files to integrate grc with NMH commands. These files should be placed in the grc configuration directory (e.g., /usr/share/grc/ or /usr/local/share/grc/):

    • conf.flists: Configuration for the flists command.
    • conf.inc: Configuration for the inc command.
    • conf.mail: Configuration for the show command.
    • conf.scan: Configuration for the scan command.

    Note: The color scheme in these files is optimized for light text on a dark background.

  8. Reference: Supported colours and attributes

    master

    The following colour names are supported:

    • none, default, bold, underline, blink, reverse, concealed
    • black, green, yellow, blue, magenta, cyan, white
    • on_black, on_green, on_yellow, on_blue, on_magenta, on_cyan, on_white
    • beep

    Background colours are specified with the on_ prefix (e.g., on_red).

    Additional attributes (supported on some terminals):

    • dark, italic, rapidblink, strikethrough

    Multiple attributes can be combined in a single line separated by spaces:

    colours=bold blink green
  9. Reference: grcat configuration keywords

    master

    Each entry in a grcat configuration file uses keyword=value pairs. The following keywords are supported:

    • regexp: The regular expression to match (mandatory).
    • colours: A comma-separated list of colours (one per regexp group). Supports special names previous (uses colour of the previous line's first character) and unchanged (leaves colour as is). You can also use arbitrary strings in straight quotes (e.g., "\033[38;5;22m") to insert raw ANSI codes.
    • command: A command to execute when the regexp matches. Output is mixed with stdout.
    • concat: The name of a file to which the current line will be appended when the regexp matches.
    • skip: Set to yes to discard the matched line from output, or no to keep it (default).
    • replace: Replaces the matched text using Python's re.sub() syntax (e.g., \1, \2). Note: Place replace rules at the beginning of the config file to avoid position shifts.
    • count: Controls how many times the regexp is applied:
      • once: Colour only the first occurrence.
      • more: Colour all matches in a line.
      • stop: Colour the match and move to the next line (ignoring other regexps).
      • previous: Inherit count from the previous line.
      • block: Marks the start of a multiline block.
      • unblock: Marks the end of a multiline block.