#!/usr/bin/env bash # Bash 4.0+ only. Faster than using tr or awk + no fork. toLower() { if [[ $# -eq 0 ]]; then while IFS= read -r line; do echo "${line,,}" done else local str="$*" echo "${str,,}" fi } # Return a string corresponding to the current OS. which_os() { case "$OSTYPE" in darwin*) printf "darwin" ;; linux*) if [[ -f /etc/os-release ]]; then # shellcheck disable=SC1091 source /etc/os-release printf "%s" "${ID,,}" else printf "linux" fi ;; esac } # Choose a Charm repo name with Gum. pickCharmRepo() { getCharmRepos | gum filter --height=10 --header "$1" } # Interactively choose a Charm repo. charm_clone() { local repo repo="$(pickCharmRepo "Clone which Charm™ repo?")" if [[ -z "$repo" ]]; then printf "Okay, never mind.\n" return 1 fi git clone "git@github.com:charmbracelet/$repo" "$HOME/src/$repo" } # Interactively use "go get" against a Charm repo. go_get_charm() { local repo repo="$(pickCharmRepo "Which Charm™ repo do you want to ‘go get?’")" if [[ -z "$repo" ]]; then printf "Okay, never mind.\n" return 1 fi go get "github.com/charmbracelet/$repo" } # Returns the number of CPU cores available on the system. num_cores() { nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo "Could not determine core count" } # Return 1 if a given command exists, and 0 if not. command_exists() { if command -v "$1" &>/dev/null; then printf 1 return fi printf 0 } # Return the preferred diff tool for the given OS. gitdiff() { if [[ $(which_os) == darwin ]]; then opendiff else vimdiff fi } #shellcheck disable=1090 decryptAndSource() { source <(gpg --decrypt "$1" 2>/dev/null) } # Print the names of all public and private Charm repos. getCharmRepos() { getRepos() { gh repo list charmbracelet --visibility="$1" --json name } getAllRepos() { printf '%s %s' "$(getRepos "public")" "$(getRepos "private")" | jq -s 'add' } getAllRepos | jq .[].name | tr -d '"' | sort }