Gum

repository·main·Indexed 12 days ago

https://github.com/charmbracelet/gum

A tool for creating glamorous shell scripts by leveraging Bubbles and Lip Gloss without writing Go code. It provides interactive CLI components such as fuzzy filters, input prompts, file pickers, spinners, and styled text formatting for Markdown, code, and templates.

Tokens
5.1K
Snippets
36
Records
37
Agent score
97%

What's inside Gum

  1. Overview of gum format

    main

    The gum format command allows you to transform plain text into human-readable, styled output. It supports four primary parseable formats via the --type flag:

    1. Markdown: Renders input as styled Markdown text (powered by Glamour).
    2. Code: Renders code snippets with syntax highlighting (powered by Chroma via Glamour).
    3. Template: Renders styled input using string templates (powered by Termenv).
    4. Emoji: Parses and renders emojis from :name: syntax (powered by Goldmark Emoji via Glamour).

    Input can be provided either as direct command-line arguments or via stdin.

    gum format --type <format> [input]
  2. Customize Gum with flags and environment variables

    main

    Gum options and styles can be customized using command-line --flags or $ENVIRONMENT_VARIABLES. Flags take precedence over environment variables.

    Using flags:

    gum input --cursor.foreground "#FF0" --prompt.foreground "#0FF" --placeholder "What's up?" --prompt "* " --width 80 --value "Not much, hby?"

    Using environment variables:

    export GUM_INPUT_CURSOR_FOREGROUND="#FF0"
    export GUM_INPUT_PROMPT_FOREGROUND="#0FF"
    export GUM_INPUT_PLACEHOLDER="What's up?"
    export GUM_INPUT_PROMPT="* "
    export GUM_INPUT_WIDTH=80
    
    # --flags can override values set with environment
    gum input
  3. Install Gum on Debian/Ubuntu

    main

    To install Gum on Debian or Ubuntu-based systems, add the Charm repository and install via apt:

    sudo mkdir -p /etc/apt/keyrings
    curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg
    echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | sudo tee /etc/apt/sources.list.d/charm.list
    sudo apt update && sudo apt install gum
  4. Install Gum on FreeBSD

    main

    On FreeBSD, you can install via packages or build from ports:

    # packages
    sudo pkg install gum
    
    # ports
    cd /usr/ports/devel/gum && sudo make install clean
  5. Install Gum on Fedora/RHEL/OpenSuse

    main

    To install Gum on Fedora, RHEL, or OpenSuse, configure the Charm repository and use yum or zypper:

    echo '[charm]
    name=Charm
    baseurl=https://repo.charm.sh/yum/
    enabled=1
    gpgcheck=1
    gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo
    sudo rpm --import https://repo.charm.sh/yum/gpg.key
    
    # yum
    sudo yum install gum
    
    # zypper
    sudo zypper refresh
    sudo zypper install gum
  6. Install Gum

    main

    You can install Gum using various package managers depending on your operating system or by using Go.

    # macOS or Linux
    brew install gum
    
    # Arch Linux
    pacman -S gum
    
    # Fedora or EPEL 10
    dnf install gum
    
    # Nix
    nix-env -iA nixpkgs.gum
    
    # Flox
    flox install gum
    
    # Windows (via WinGet or Scoop)
    winget install charmbracelet.gum
    scoop install charm-gum
    
    # Using Go
    go install github.com/charmbracelet/gum@latest
  7. Use gum in daily shell workflows

    main

    Gum can be integrated into shell scripts and aliases to provide interactive UI elements for common CLI tasks. Common patterns include using gum input for text/passwords, gum filter for searching lists, and gum choose for selecting items from a list.

    # Write a commit message with interactive input and text areas
    git commit -m "$(gum input --width 50 --placeholder "Summary of changes")" \
               -m "$(gum write --width 80 --placeholder "Details of changes")"
    
    # Open files in your $EDITOR using a filter
    $EDITOR $(gum filter)
    
    # Connect to a tmux session by filtering active sessions
    SESSION=$(tmux list-sessions -F \#S | gum filter --placeholder "Pick session...")
    tmux switch-client -t "$SESSION" || tmux attach -t "$SESSION"
    
    # Pick a commit hash from git history
    git log --oneline | gum filter | cut -d' ' -f1
    
    # Uninstall packages via brew using multi-select
    brew list | gum choose --no-limit | xargs brew uninstall
    
    # Clean up git branches using multi-select
    git branch | cut -c 3- | gum choose --no-limit | xargs git branch -D
    
    # Checkout GitHub pull requests using gh cli
    gh pr list | cut -f1,2 | gum choose | cut -f1 | xargs gh pr checkout
    
    # Search through shell history
    gum filter < $HISTFILE --height 20
    
    # Create a sudo replacement alias that prompts for a password
    alias please="gum input --password | sudo -nS"
  8. Run the gum CLI

    main

    Gum is a command-line tool designed to make shell scripts more 'glamorous' by providing interactive components. It is invoked via the gum binary. The CLI uses a subcommand structure (managed by kong) to provide various interactive utilities like spinners, inputs, and selections.

    # Example of how you would typically use the gum command in a shell script
    # (Note: specific subcommands are defined in other parts of the repository)
    
    gum input
    gum confirm
    gum spin
  9. Render Markdown tables with gum format

    main

    Tables are rendered using the Markdown engine (Glamour). Ensure your input follows standard Markdown table syntax.

    echo '| Bubble Gum Flavor | Price |
    | ----------------- | ----- |
    | Strawberry        | $0.99 |
    | Cherry            | $0.50 |' | gum format --type markdown
  10. Use gum write for multi-line text

    main

    The write command prompts for multi-line text. Users can complete the entry by pressing ctrl+d.

    gum write > story.txt
  11. Display a spinner with gum spin

    main

    The spin command displays a spinner while a command or script is running. The spinner stops automatically when the command exits. Use --show-output to view or pipe the command's output.

    Available spinner types: line, dot, minidot, jump, pulse, points, globe, moon, monkey, meter, hamburger.

    gum spin --spinner dot --title "Buying Bubble Gum..." -- sleep 5
  12. Select a row from a table

    main

    The table command renders tabular data and allows the user to select a row.

    gum table < flavors.csv | cut -d ',' -f 1