How to use the command "gum" (with examples)
“Gum” is a command-line tool that allows you to create glamorous shell scripts. It provides various interactive prompts and formatting options to enhance the user experience when working with shell scripts. This article will guide you through several use cases of the “gum” command.
Use case 1: Interactively picking a specific option
Code:
gum choose "option_1" "option_2" "option_3"
Motivation: You may want to give the user the ability to choose from a set of predefined options and print the selected option to the standard output. This use case is useful when designing interactive scripts that require user input.
Explanation:
gum choose
: Initiates the interactive selection prompt."option_1" "option_2" "option_3"
: The available options for the user to choose from.
Example output:
Please choose an option:
1. option_1
2. option_2
3. option_3
Selected option: option_2
Use case 2: Opening an interactive prompt for user input with a placeholder
Code:
gum input --placeholder "value"
Motivation: You may need to prompt the user to enter a specific value into your script and include a placeholder text to guide them. This use case is helpful for collecting user input and enhancing the usability of your script.
Explanation:
gum input
: Opens the interactive prompt for user input.--placeholder "value"
: Specifies the placeholder text that will be shown in the input prompt.
Example output:
Please enter a value: [value]
Use case 3: Opening an interactive confirmation prompt
Code:
gum confirm "Continue?" --default=false --affirmative "Yes" --negative "No" && echo "Yes selected" || echo "No selected"
Motivation: Sometimes, you need to get user confirmation before proceeding with a specific action in your script. This interactive confirmation prompt allows the user to either confirm or cancel an operation, while providing an informative message and customizing the available options.
Explanation:
gum confirm
: Initiates the interactive confirmation prompt."Continue?"
: The message displayed in the confirmation prompt.--default=false
: Sets the default option for the prompt to “No” (i.e., the user must actively choose “Yes”).--affirmative "Yes"
: Specifies the label for the affirmative option.--negative "No"
: Specifies the label for the negative option.&& echo "Yes selected" || echo "No selected"
: Prints a corresponding message based on the selected option.
Example output:
Continue? [Yes/No]: No selected
Use case 4: Showing a spinner while a command is taking place
Code:
gum spin --spinner dot|line|minidot|jump|pulse|points|globe|moon|monkey|meter|hamburger --title "loading..." -- command
Motivation: When running time-consuming commands, it’s helpful to provide visual feedback to the user to indicate that the script is still working. The spinner provides an animated visual cue while running a command, making the script more interactive and user-friendly.
Explanation:
gum spin
: Starts the spinner.--spinner
: Specifies the type of spinner animation to use (options: dot, line, minidot, jump, pulse, points, globe, moon, monkey, meter, hamburger).--title "loading..."
: Sets the descriptive title shown alongside the spinner.-- command
: Specifies the command to execute while the spinner is active.
Example output:
loading... [spinner animation]
Use case 5: Formatting text to include emojis
Code:
gum format -t emoji ":smile: :heart: hello"
Motivation: Emojis can add a touch of personality and visual appeal to your shell scripts. The “gum format” command allows you to format text and replace specific emoji codes with their corresponding graphical representations.
Explanation:
gum format
: Formats the provided text.-t emoji
: Specifies the type of formatting to apply.:smile: :heart: hello
: The text to format, including emoji codes.
Example output:
😄 ❤️ hello
Use case 6: Interactively prompting for multi-line text and writing to a file
Code:
gum write > data.txt
Motivation: Sometimes, you need to collect multi-line input from the user and save it to a file. The “gum write” command provides an interactive prompt where the user can enter a multi-line text, and the resulting input is then written to the specified file.
Explanation:
gum write
: Opens the interactive prompt for multi-line text input.> data.txt
: Redirects the output of the command (user input) to the “data.txt” file.
Example (user input):
This is a sample text.
It spans multiple lines.
After executing the command, the contents of “data.txt” will be:
This is a sample text.
It spans multiple lines.
Conclusion:
The “gum” command enhances the interactivity and visual appeal of shell scripts by providing several interactive prompts, formatting options, and functionalities like spinner animation. By leveraging these features, you can create more user-friendly and glamorous shell scripts.