Skip to content

marcoroth/gum-ruby

Repository files navigation

Gum for Ruby

Gum Image

Ruby wrapper for Charm's Gum. A tool for glamorous scripts.

This gem bundles the gum binary and provides an idiomatic Ruby API for all gum commands.

Installation

Add to your Gemfile:

bundle add gum

Or install directly:

gem install gum

Usage

require "gum"

Input

Prompt for single-line input:

name = Gum.input(placeholder: "Enter your name")

Password input (masked):

password = Gum.input(password: true)

With default value and custom prompt:

email = Gum.input(value: "user@", prompt: "> ", placeholder: "email")

With character limit:

code = Gum.input(placeholder: "Enter code", char_limit: 6)

Write

Prompt for multi-line text input (Ctrl+D to submit):

description = Gum.write(placeholder: "Enter description...")

With dimensions:

notes = Gum.write(width: 80, height: 10, header: "Notes")

With line numbers:

content = Gum.write(show_line_numbers: true)

Choose

Single selection (array):

color = Gum.choose(["red", "green", "blue"])

Single selection (splat):

color = Gum.choose("red", "green", "blue")

Multiple selection with limit:

colors = Gum.choose(["red", "green", "blue"], limit: 2)

Unlimited selection:

colors = Gum.choose(["red", "green", "blue"], no_limit: true)

With header and custom height:

choice = Gum.choose(options, header: "Pick one:", height: 10)

Pre-selected items:

choice = Gum.choose(["a", "b", "c"], selected: ["b"])

Filter

Single selection (array):

file = Gum.filter(Dir.glob("**/*.rb"))

Single selection (splat):

file = Gum.filter("file1.rb", "file2.rb", "file3.rb")

Multiple selection:

files = Gum.filter(Dir.glob("*"), limit: 5)

Unlimited selection:

files = Gum.filter(items, no_limit: true)

With placeholder and height:

selection = Gum.filter(items, placeholder: "Search...", height: 20)

Disable fuzzy matching for exact search:

result = Gum.filter(items, fuzzy: false)

Confirm

Ask for yes/no confirmation:

if Gum.confirm("Delete file?")
  File.delete(path)
end

With default value:

proceed = Gum.confirm("Continue?", default: true)

Custom button labels:

Gum.confirm("Save changes?", affirmative: "Save", negative: "Discard")

File

Start from current directory:

path = Gum.file

Start from specific directory:

path = Gum.file("~/Documents")

Show hidden files:

path = Gum.file(all: true)

Only show directories:

dir = Gum.file(directory_only: true)

Pager

Scroll through content:

Gum.pager(File.read("README.md"))

With line numbers:

Gum.pager(content, show_line_numbers: true)

Soft wrap long lines:

Gum.pager(content, soft_wrap: true)

Spin

With shell command:

Gum.spin("Installing...", command: "npm install")

With Ruby block:

result = Gum.spin("Processing...") do
  expensive_computation
end

Custom spinner type:

Gum.spin("Loading...", spinner: :dot, command: "sleep 5")

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

Style

Basic styling:

styled = Gum.style("Hello", foreground: "212", bold: true)

With border:

box = Gum.style("Content", border: :double, padding: "1 2")

Multiple lines with alignment:

styled = Gum.style("Line 1", "Line 2", align: :center, width: 50)

Full styling example:

styled = Gum.style(
  "Bubble Gum",
  foreground: "212",
  border: :double,
  border_foreground: "212",
  align: :center,
  width: 50,
  margin: "1 2",
  padding: "2 4"
)

Available border types: :none, :hidden, :rounded, :double, :thick, :normal

Join

Join text blocks horizontally or vertically:

box1 = Gum.style("A", border: :rounded, padding: "1 3")
box2 = Gum.style("B", border: :rounded, padding: "1 3")

Horizontal join (default):

combined = Gum.join(box1, box2)

Vertical join:

stacked = Gum.join(box1, box2, vertical: true)

With alignment:

aligned = Gum.join(box1, box2, vertical: true, align: :center)

Format

Markdown:

Gum.format("# Hello\n- Item 1\n- Item 2", type: :markdown)

Template (see Termenv docs for helpers):

Gum.format('{{ Bold "Hello" }} {{ Color "99" "0" " World " }}', type: :template)

Emoji:

Gum.format("I :heart: Ruby :gem:", type: :emoji)
# => "I ❤️ Ruby 💎"

Shorthand methods:

Gum::Format.markdown("# Hello")
Gum::Format.emoji("I :heart: Ruby")

Table

From array of arrays:

data = [["Alice", "30"], ["Bob", "25"]]
selection = Gum.table(data, columns: %w[Name Age])

Just print (no selection):

Gum.table(data, columns: %w[Name Age], print: true)

From CSV string:

Gum.table(File.read("data.csv"))

With custom border:

Gum.table(data, columns: %w[Name Age], border: :rounded)

Log

Basic logging:

Gum.log("Application started", level: :info)

Structured logging with key-value pairs:

Gum.log("User created", level: :info, user_id: 123, email: "[email protected]")

With timestamp:

Gum.log("Error occurred", level: :error, time: :rfc822)

Shorthand methods:

Gum::Log.debug("Debug message")
Gum::Log.info("Info message")
Gum::Log.warn("Warning message")
Gum::Log.error("Error message", code: 500)

Available log levels: :debug, :info, :warn, :error, :fatal

Styling Options

Most commands support styling options as hashes. Common style properties include:

{
  foreground: "212",        # ANSI color code, hex (#ff0000), or name
  background: "0",
  bold: true,
  italic: true,
  underline: true,
  strikethrough: true,
  faint: true,
}

Example with input styling:

Gum.input(
  placeholder: "Enter name",
  cursor: { foreground: "#FF0" },
  prompt_style: { foreground: "#0FF" }
)

Environment Variables

  • GUM_INSTALL_DIR - Override the path to the gum binary

Raw Command Execution

For commands not covered by the Ruby API, you can execute gum directly:

Gum.execute("choose", "a", "b", "c", "--limit", "2")

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

Building Platform Gems

rake gem:ruby           # Build pure Ruby gem (no binary)
rake gem:arm64-darwin   # Build macOS ARM64 gem
rake gem:x86_64-darwin  # Build macOS Intel gem
rake gem:arm64-linux    # Build Linux ARM64 gem
rake gem:x86_64-linux   # Build Linux x86_64 gem
rake package            # Build all platform gems
rake download           # Download all binaries

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/marcoroth/gum-ruby.

License

The gem is available as open source under the terms of the MIT License.

Acknowledgements

Charm Ruby is not affiliated with or endorsed by Charmbracelet, Inc.


Part of Charm Ruby.

Charm Ruby

LipglossBubble TeaBubblesGlamourHuh?HarmonicaBubblezoneGumntcharts

The terminal doesn't have to be boring.

About

Ruby wrapper for Charm's Gum. A tool for glamorous scripts.

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project