gum
迷人的 shell 脚本工具。
用于迷人的 shell
脚本的工具。在您的脚本和别名中利用 Bubbles
和Lip Gloss
的强大功能,而无需编写任何 Go
代码!
有时,对于简单的命令代码,我们不想再用另一个语言来写各种的交互,如果shell能够轻松搞定,那不是很轻松吗?但是传统的shell
并没有太多交互的功能,gum
补上了这个短板。
gum
的安装也是十分的方便,只要通过命令行的安装就能轻松的在本机上部署gum
了,命令如下,可以根据自己不同的平台进行相应的选择。
# macOS or Linux
brew install gum
# Arch Linux (btw)
yay -S gum-bin
# Nix
nix-env -iA nixpkgs.gum
# Debian/Ubuntu
echo 'deb [trusted=yes] https://repo.charm.sh/apt/ /' | sudo tee /etc/apt/sources.list.d/charm.list
sudo apt update && sudo apt install gum
# Fedora
echo '[charm]
name=Charm
baseurl=https://repo.charm.sh/yum/
enabled=1
gpgcheck=0' | sudo tee /etc/yum.repos.d/charm.repo
sudo yum install gum
询问提交类型:
gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert"
提示提交的(可选)范围:
gum input --placeholder "scope"
提示提交消息:
gum input --placeholder "Summary of this change"
提示对更改进行详细(多行)说明:
gum write --placeholder "Details of this change"
提交前提示确认:
gum confirm0如果确认则退出状态,1如果取消则退出状态。
我们在进行命令行的代码提交时,需要提交代码的简要说明,这是使用gum
,来完成这一操作,这个过程基本上包含了上面所讲的大部分的使用类型,可以根据如下的代码,在进行相应的改装,制作一个属于自己的提交脚本。
#!/bin/sh
TYPE=$(gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert")
SCOPE=$(gum input --placeholder "scope")
# Since the scope is optional, wrap it in parentheses if it has a value.
test -n "$SCOPE" && SCOPE="($SCOPE)"
# Pre-populate the input with the type(scope): so that the user may change it
SUMMARY=$(gum input --value "$TYPE$SCOPE: " --placeholder "Summary of this change")
DESCRIPTION=$(gum write --placeholder "Details of this change")
# Commit these changes
gum confirm "Commit changes?" && git commit -m "$SUMMARY" -m "$DESCRIPTION"
这是一个demo
,完成了一个交互过程,可以看看 下图演示的交互。
#!/bin/bash
gum style --border normal --margin "1" --padding "1 2" --border-foreground 212 "Hello, there! Welcome to $(gum style --foreground 212 'Gum')."
NAME=$(gum input --placeholder "What is your name?")
echo -e "Well, it is nice to meet you, $(gum style --foreground 212 "$NAME")."
sleep 2; clear
echo -e "Can you tell me a $(gum style --italic --foreground 99 'secret')?\n"
gum write --placeholder "I'll keep it to myself, I promise!" > /dev/null # we keep the secret to ourselves
clear; echo "What should I do with this information?"; sleep 1
READ="Read"; THINK="Think"; DISCARD="Discard"
ACTIONS=$(gum choose --cursor-prefix "[ ] " --selected-prefix "[✓] " --no-limit "$READ" "$THINK" "$DISCARD")
clear; echo "One moment, please."
grep -q "$READ" <<< "$ACTIONS" && gum spin -s line --title "Reading the secret..." -- sleep 1
grep -q "$THINK" <<< "$ACTIONS" && gum spin -s pulse --title "Thinking about your secret..." -- sleep 1
grep -q "$DISCARD" <<< "$ACTIONS" && gum spin -s monkey --title " Discarding your secret..." -- sleep 2
sleep 1; clear
echo "What's your favorite $(gum style --foreground 212 "Gum") flavor?"
GUM=$(echo -e "Cherry\nGrape\nLime\nOrange" | gum filter)
echo "I'll keep that in mind!"
sleep 1; clear
echo "Do you like $(gum style --foreground "#04B575" "Bubble Gum?")"
sleep 1
CHOICE=$(gum choose --item.foreground 250 "Yes" "No" "It's complicated")
[[ "$CHOICE" == "Yes" ]] && echo "I thought so, $(gum style --bold "Bubble Gum") is the best." || echo "I'm sorry to hear that."
sleep 1
gum spin --title "Chewing some $(gum style --foreground "#04B575" "$GUM") bubble gum..." -- sleep 5
clear
NICE_MEETING_YOU=$(gum style --height 5 --width 25 --padding '1 3' --border double --border-foreground 57 "Well, it was nice meeting you, $(gum style --foreground 212 "$NAME"). Hope to see you soon!")
CHEW_BUBBLE_GUM=$(gum style --width 25 --padding '1 3' --border double --border-foreground 212 "Don't forget to chew some $(gum style --foreground "#04B575" "$GUM") bubble gum.")
gum join --horizontal "$NICE_MEETING_YOU" "$CHEW_BUBBLE_GUM"