一個可用來製作炫彩迷人腳本的工具 Gu
Gum 是一個可以用來製作迷彩腳本的工具。它使用 Golang 編寫。它可以在您的腳本中利用 Bubbles 和 LipGloss 的力量,實現炫彩的交互動畫,而無需編寫任何 Go 代碼。
Gum 提供了高度可配置、即用型的實用程序,可以幫助你用幾行代碼就能編寫有用的 Shell 腳本。下面會演示如何讓腳本更富有表現力以及趣味性。
我們開始吧,來構建一個腳本,讓提交代碼更具有趣味性。首先,我們先安裝 gum 工具。這裏列出常規安裝方法:
# macOS or Linux
brew install gum
# Arch Linux (btw)
pacman -S gum
# Nix
nix-env -iA nixpkgs.gum
# Flox
flox install gum
# Windows (via WinGet or Scoop)
winget install charmbracelet.gum
scoop install charm-gum
如果我們使用 Debian/Ubuntu,可以使用腳本安裝:
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
如果我們使用 CentOS/RHEL,可以這樣安裝:
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
我們還可以使用二進制直接安裝。在安裝完成後,簡要介紹下 gum 支持的幾個命令:
choose:從選項列表中選擇一個選項
confirm:要求用戶確認操作
file:從文件夾中選擇一個文件
filter:從列表中篩選項目
format:使用模板格式化字符串
input:提示輸入
join:垂直或水平連接文本
pager:滾動瀏覽文件
spin:運行命令時顯示微調器
style:對文本應用着色、邊框和間距
table:渲染數據表
write:提示輸入長格式文本
log:記錄要輸出的消息
要製作迷人炫彩的動畫腳本,請牢記上面的命令。下面我們開始正題了。我們先定義提交類型。
gum choose "fix" "feat" "docs" "style" "refactor" "test" "chore" "revert"
提示更改的範圍:
gum input --placeholder "scope"
提示更改的摘要和描述,在這裏短的輸入使用 input,很多內容輸入請使用 write:
gum input --value "$TYPE$SCOPE: " --placeholder "Summary of this change"
gum write --placeholder "Details of this change"
提交前進行確認:
gum confirm "Commit changes?" && git commit -m "$SUMMARY" -m "$DESCRIPTION"
說明一下,上面的腳本執行起來是可以交互的。這段腳本執行的是代碼提交任務。在使用 gum 工具後,更具有交互性,趣味性。如果你的腳本感覺很枯燥,爲了給自己的工作增加一點樂趣,可以使用 gum 工具進行改造,並將你的成果分享給你的好友。
commit.sh 腳本源碼:
#!/bin/sh
# This script is used to write a conventional commit message.
# It prompts the user to choose the type of commit as specified in the
# conventional commit spec. And then prompts for the summary and detailed
# description of the message and uses the values provided. as the summary and
# details of the message.
#
# If you want to add a simpler version of this script to your dotfiles, use:
#
# alias gcm='git commit -m "$(gum input)" -m "$(gum write)"'
# if [ -z "$(git status -s -uno | grep -v '^ ' | awk '{print $2}')" ]; then
# gum confirm "Stage all?" && git add .
# fi
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 if user confirms
gum confirm "Commit changes?" && git commit -m "$SUMMARY" -m "$DESCRIPTION"
更多的內容,請參考 Github:
https://github.com/charmbracelet/gum
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/VU8wSVCaXxcPhbbL_SpY6A