Skip to content
Myr

README.md

This file

Path

README.md

487 lines · 13.4 KiB · Markdown

Last changed by

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

History of this file

Take it

Show it rendered

1# Gum23<p>4    <a href="https://stuff.charm.sh/gum/nutritional-information.png" target="_blank"><img src="https://stuff.charm.sh/gum/gum.png" alt="Gum Image" width="450" /></a>5    <br><br>6    <a href="https://github.com/charmbracelet/gum/releases"><img src="https://img.shields.io/github/release/charmbracelet/gum.svg" alt="Latest Release"></a>7    <a href="https://pkg.go.dev/github.com/charmbracelet/gum?tab=doc"><img src="https://godoc.org/github.com/golang/gddo?status.svg" alt="Go Docs"></a>8    <a href="https://github.com/charmbracelet/gum/actions"><img src="https://github.com/charmbracelet/gum/workflows/build/badge.svg" alt="Build Status"></a>9</p>1011A tool for glamorous shell scripts. Leverage the power of12[Bubbles](https://github.com/charmbracelet/bubbles) and [Lip13Gloss](https://github.com/charmbracelet/lipgloss) in your scripts and aliases14without writing any Go code!1516<img alt="Shell running the ./demo.sh script" width="600" src="https://vhs.charm.sh/vhs-1qY57RrQlXCuydsEgDp68G.gif">1718The above example is running from a single shell script ([source](./examples/demo.sh)).1920## Tutorial2122Gum provides highly configurable, ready-to-use utilities to help you write23useful shell scripts and dotfile aliases with just a few lines of code.24Let's build a simple script to help you write25[Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/#summary)26for your dotfiles.2728Ask for the commit type with gum choose:2930```bash31gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert"32```3334> [!NOTE]35> This command itself will print to stdout which is not all that useful. To make use of the command later on you can save the stdout to a `$VARIABLE` or `file.txt`.3637Prompt for the scope of these changes:3839```bash40gum input --placeholder "scope"41```4243Prompt for the summary and description of changes:4445```bash46gum input --value "$TYPE$SCOPE: " --placeholder "Summary of this change"47gum write --placeholder "Details of this change"48```4950Confirm before committing:5152```bash53gum confirm "Commit changes?" && git commit -m "$SUMMARY" -m "$DESCRIPTION"54```5556Check out the [complete example](https://github.com/charmbracelet/gum/blob/main/examples/commit.sh) for combining these commands in a single script.5758<img alt="Running the ./examples/commit.sh script to commit to git" width="600" src="https://vhs.charm.sh/vhs-7rRq3LsEuJVwhwr0xf6Er7.gif">5960## Installation6162Use a package manager:6364```bash65# macOS or Linux66brew install gum6768# Arch Linux (btw)69pacman -S gum7071# Fedora or EPEL 1072dnf install gum7374# Nix75nix-env -iA nixpkgs.gum7677# Flox78flox install gum7980# Windows (via WinGet or Scoop)81winget install charmbracelet.gum82scoop install charm-gum83```8485<details>86<summary>Debian/Ubuntu</summary>8788```bash89sudo mkdir -p /etc/apt/keyrings90curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/charm.gpg91echo "deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *" | sudo tee /etc/apt/sources.list.d/charm.list92sudo apt update && sudo apt install gum93```9495</details>9697<details>98<summary>Fedora/RHEL/OpenSuse</summary>99100```bash101echo '[charm]102name=Charm103baseurl=https://repo.charm.sh/yum/104enabled=1105gpgcheck=1106gpgkey=https://repo.charm.sh/yum/gpg.key' | sudo tee /etc/yum.repos.d/charm.repo107sudo rpm --import https://repo.charm.sh/yum/gpg.key108109# yum110sudo yum install gum111112# zypper113sudo zypper refresh114sudo zypper install gum115```116117</details>118119<details>120<summary>FreeBSD</summary>121122```bash123# packages124sudo pkg install gum125126# ports127cd /usr/ports/devel/gum && sudo make install clean128```129130</details>131132Or download it:133134- [Packages][releases] are available in Debian, RPM, and Alpine formats135- [Binaries][releases] are available for Linux, macOS, Windows, FreeBSD, OpenBSD, and NetBSD136137Or just install it with `go`:138139```bash140go install charm.land/gum/v2@latest141```142143[releases]: https://github.com/charmbracelet/gum/releases144145## Commands146147- [`choose`](#choose): Choose an option from a list of choices148- [`confirm`](#confirm): Ask a user to confirm an action149- [`file`](#file): Pick a file from a folder150- [`filter`](#filter): Filter items from a list151- [`format`](#format): Format a string using a template152- [`input`](#input): Prompt for some input153- [`join`](#join): Join text vertically or horizontally154- [`pager`](#pager): Scroll through a file155- [`spin`](#spin): Display spinner while running a command156- [`style`](#style): Apply coloring, borders, spacing to text157- [`table`](#table): Render a table of data158- [`write`](#write): Prompt for long-form text159- [`log`](#log): Log messages to output160161## Customization162163You can customize `gum` options and styles with `--flags` and `$ENVIRONMENT_VARIABLES`.164See `gum <command> --help` for a full view of each command's customization and configuration options.165166Customize with `--flags`:167168```bash169170gum input --cursor.foreground "#FF0" \171          --prompt.foreground "#0FF" \172          --placeholder "What's up?" \173          --prompt "* " \174          --width 80 \175          --value "Not much, hby?"176```177178Customize with `ENVIRONMENT_VARIABLES`:179180```bash181export GUM_INPUT_CURSOR_FOREGROUND="#FF0"182export GUM_INPUT_PROMPT_FOREGROUND="#0FF"183export GUM_INPUT_PLACEHOLDER="What's up?"184export GUM_INPUT_PROMPT="* "185export GUM_INPUT_WIDTH=80186187# --flags can override values set with environment188gum input189```190191<img alt="Gum input displaying most customization options" width="600" src="https://vhs.charm.sh/vhs-5zb9DlQYA70aL9ZpYLTwKv.gif">192193## Input194195Prompt for input with a simple command.196197```bash198gum input > answer.txt199gum input --password > password.txt200```201202<img src="https://vhs.charm.sh/vhs-1nScrStFI3BMlCp5yrLtyg.gif" width="600" alt="Shell running gum input typing Not much, you?" />203204## Write205206Prompt for some multi-line text (`ctrl+d` to complete text entry).207208```bash209gum write > story.txt210```211212<img src="https://vhs.charm.sh/vhs-7abdKKrUEukgx9aJj8O5GX.gif" width="600" alt="Shell running gum write typing a story" />213214## Filter215216Filter a list of values with fuzzy matching:217218```bash219echo Strawberry >> flavors.txt220echo Banana >> flavors.txt221echo Cherry >> flavors.txt222gum filter < flavors.txt > selection.txt223```224225<img src="https://vhs.charm.sh/vhs-61euOQtKPtQVD7nDpHQhzr.gif" width="600" alt="Shell running gum filter on different bubble gum flavors" />226227Select multiple options with the `--limit` flag or `--no-limit` flag. Use `tab` or `ctrl+space` to select, `enter` to confirm.228229```bash230cat flavors.txt | gum filter --limit 2231cat flavors.txt | gum filter --no-limit232```233234## Choose235236Choose an option from a list of choices.237238```bash239echo "Pick a card, any card..."240CARD=$(gum choose --height 15 {{A,K,Q,J},{10..2}}" "{♠,♥,♣,♦})241echo "Was your card the $CARD?"242```243244You can also select multiple items with the `--limit` or `--no-limit` flag, which determines245the maximum of items that can be chosen.246247```bash248cat songs.txt | gum choose --limit 5249cat foods.txt | gum choose --no-limit --header "Grocery Shopping"250```251252<img src="https://vhs.charm.sh/vhs-3zV1LvofA6Cbn5vBu1NHHl.gif" width="600" alt="Shell running gum choose with numbers and gum flavors" />253254## Confirm255256Confirm whether to perform an action. Exits with code `0` (affirmative) or `1`257(negative) depending on selection.258259```bash260gum confirm && rm file.txt || echo "File not removed"261```262263<img src="https://vhs.charm.sh/vhs-3xRFvbeQ4lqGerbHY7y3q2.gif" width="600" alt="Shell running gum confirm" />264265## File266267Prompt the user to select a file from the file tree.268269```bash270$EDITOR $(gum file $HOME)271```272273<img src="https://vhs.charm.sh/vhs-2RMRqmnOPneneIgVJJ3mI1.gif" width="600" alt="Shell running gum file" />274275## Pager276277Scroll through a long document with line numbers and a fully customizable viewport.278279```bash280gum pager < README.md281```282283<img src="https://vhs.charm.sh/vhs-3iMDpgOLmbYr0jrYEGbk7p.gif" width="600" alt="Shell running gum pager" />284285## Spin286287Display a spinner while running a script or command. The spinner will288automatically stop after the given command exits.289290To view or pipe the command's output, use the `--show-output` flag.291292```bash293gum spin --spinner dot --title "Buying Bubble Gum..." -- sleep 5294```295296<img src="https://vhs.charm.sh/vhs-3YFswCmoY4o3Q7MyzWl6sS.gif" width="600" alt="Shell running gum spin while sleeping for 5 seconds" />297298Available spinner types include: `line`, `dot`, `minidot`, `jump`, `pulse`, `points`, `globe`, `moon`, `monkey`, `meter`, `hamburger`.299300## Table301302Select a row from some tabular data.303304```bash305gum table < flavors.csv | cut -d ',' -f 1306```307308<!-- <img src="https://stuff.charm.sh/gum/table.gif" width="600" alt="Shell running gum table" /> -->309310## Style311312Pretty print any string with any layout with one command.313314```bash315gum style \316	--foreground 212 --border-foreground 212 --border double \317	--align center --width 50 --margin "1 2" --padding "2 4" \318	'Bubble Gum (1¢)' 'So sweet and so fresh!'319```320321<img src="https://github.com/charmbracelet/gum/assets/42545625/67468acf-b3e0-4e78-bd89-360739eb44fa" width="600" alt="Bubble Gum, So sweet and so fresh!" />322323## Join324325Combine text vertically or horizontally. Use this command with `gum style` to326build layouts and pretty output.327328Tip: Always wrap the output of `gum style` in quotes to preserve newlines329(`\n`) when using it as an argument in the `join` command.330331```bash332I=$(gum style --padding "1 5" --border double --border-foreground 212 "I")333LOVE=$(gum style --padding "1 4" --border double --border-foreground 57 "LOVE")334BUBBLE=$(gum style --padding "1 8" --border double --border-foreground 255 "Bubble")335GUM=$(gum style --padding "1 5" --border double --border-foreground 240 "Gum")336337I_LOVE=$(gum join "$I" "$LOVE")338BUBBLE_GUM=$(gum join "$BUBBLE" "$GUM")339gum join --align center --vertical "$I_LOVE" "$BUBBLE_GUM"340```341342<img src="https://github.com/charmbracelet/gum/assets/42545625/68f7a25d-b495-48dd-982a-cee0c8ea5786" width="600" alt="I LOVE Bubble Gum written out in four boxes with double borders around them." />343344## Format345346`format` processes and formats bodies of text. `gum format` can parse markdown,347template strings, and named emojis.348349```bash350# Format some markdown351gum format -- "# Gum Formats" "- Markdown" "- Code" "- Template" "- Emoji"352echo "# Gum Formats\n- Markdown\n- Code\n- Template\n- Emoji" | gum format353354# Syntax highlight some code355cat main.go | gum format -t code356357# Render text any way you want with templates358echo '{{ Bold "Tasty" }} {{ Italic "Bubble" }} {{ Color "99" "0" " Gum " }}' \359    | gum format -t template360361# Display your favorite emojis!362echo 'I :heart: Bubble Gum :candy:' | gum format -t emoji363```364365For more information on template helpers, see the [Termenv366docs](https://github.com/muesli/termenv#template-helpers). For a full list of367named emojis see the [GitHub API](https://api.github.com/emojis).368369<img src="https://github.com/charmbracelet/gum/assets/42545625/5cfbb0c8-0022-460d-841b-fec37527ca66" width="300" alt="Running gum format for different types of formats" />370371## Log372373`log` logs messages to the terminal using different levels and styling using374the [`charmbracelet/log`](https://github.com/charmbracelet/log) library.375376```bash377# Log some debug information.378gum log --structured --level debug "Creating file..." name file.txt379# DEBUG Creating file... name=file.txt380381# Log some error.382gum log --structured --level error "Unable to create file." name file.txt383# ERROR Unable to create file. name=temp.txt384385# Include a timestamp.386gum log --time rfc822 --level error "Unable to create file."387```388389See the Go [`time` package](https://pkg.go.dev/time#pkg-constants) for acceptable `--time` formats.390391See [`charmbracelet/log`](https://github.com/charmbracelet/log) for more usage.392393<img src="https://vhs.charm.sh/vhs-6jupuFM0s2fXiUrBE0I1vU.gif" width="600" alt="Running gum log with debug and error levels" />394395## Examples396397How to use `gum` in your daily workflows:398399See the [examples](./examples/) directory for more real world use cases.400401- Write a commit message:402403```bash404git commit -m "$(gum input --width 50 --placeholder "Summary of changes")" \405           -m "$(gum write --width 80 --placeholder "Details of changes")"406```407408- Open files in your `$EDITOR`409410```bash411$EDITOR $(gum filter)412```413414- Connect to a `tmux` session415416```bash417SESSION=$(tmux list-sessions -F \#S | gum filter --placeholder "Pick session...")418tmux switch-client -t "$SESSION" || tmux attach -t "$SESSION"419```420421- Pick a commit hash from `git` history422423```bash424git log --oneline | gum filter | cut -d' ' -f1 # | copy425```426427- Simple [`skate`](https://github.com/charmbracelet/skate) password selector.428429```430skate list -k | gum filter | xargs skate get431```432433- Uninstall packages434435```bash436brew list | gum choose --no-limit | xargs brew uninstall437```438439- Clean up `git` branches440441```bash442git branch | cut -c 3- | gum choose --no-limit | xargs git branch -D443```444445- Checkout GitHub pull requests with [`gh`](https://cli.github.com/)446447```bash448gh pr list | cut -f1,2 | gum choose | cut -f1 | xargs gh pr checkout449```450451- Copy command from shell history452453```bash454gum filter < $HISTFILE --height 20455```456457- `sudo` replacement458459```bash460alias please="gum input --password | sudo -nS"461```462463## Contributing464465See [contributing][contribute].466467[contribute]: https://github.com/charmbracelet/gum/contribute468469## Feedback470471We’d love to hear your thoughts on this project. Feel free to drop us a note!472473- [Twitter](https://twitter.com/charmcli)474- [The Fediverse](https://mastodon.social/@charmcli)475- [Discord](https://charm.sh/chat)476477## License478479[MIT](https://github.com/charmbracelet/gum/raw/main/LICENSE)480481---482483Part of [Charm](https://charm.sh).484485<a href="https://charm.sh/"><img alt="The Charm logo" src="https://stuff.charm.sh/charm-badge.jpg" width="400" /></a>486487Charm热爱开源 • Charm loves open source