Skip to content

Instantly share code, notes, and snippets.

@Reinis
Created November 21, 2019 14:19
Show Gist options
  • Save Reinis/c53aa4d3ab179078893104148a6ba7b3 to your computer and use it in GitHub Desktop.
Save Reinis/c53aa4d3ab179078893104148a6ba7b3 to your computer and use it in GitHub Desktop.
Bash script to toggle Powerline segment visibility for tmux
#!/usr/bin/bash
#
# Dependencies:
# jq — JSON processing
# morutils — updating file in place using sponge
#
# TODO: Check for existence of the config file
config_file="${HOME}/.config/powerline/themes/tmux/default.json"
# Commands
commands=( "help" "network_load" "uptime" "system_load" "battery" "date" "time" "hostname" )
help_string=$(cat <<EOF
Usage: ${0} segment [segment ...]
Toggle visibility of segments in tmux powerline theme.
Commands:
help — print help message
Available segments:
network_load
uptime
system_load
battery
date
time
hostname
EOF
)
declare -A segments
segments["network_load"]=1
segments["uptime"]=2
segments["system_load"]=3
segments["battery"]=4
segments["date"]=5
segments["time"]=6
segments["hostname"]=7
si=(
""
"function"
"function"
"function"
"function"
"name"
"name"
"function"
)
sv=(
""
"powerline.segments.common.net.network_load"
"powerline.segments.common.sys.uptime"
"powerline.segments.common.sys.system_load"
"powerline.segments.common.bat.battery"
"date"
"time"
"powerline.segments.common.net.hostname"
)
# Check input
is_cmd() {
for ei in "${commands[@]}"; do
if [[ "${ei}" == "${1}" ]]; then
return 1
fi
done
return 0
}
for cmd in "${@}"; do
if is_cmd "${cmd}"; then
echo "Error: Unknown command — ${cmd}"
exit 1
fi
done
# Print help
for arg in "${@}"; do
if [[ "${arg}" == "help" ]]; then
echo "${help_string}"
exit 0
fi
done
# Toggle segment(s)
for i in ${@}; do
s=segments[${i}]
jq '(.segments.right[] | select(.'"${si[$s]}"' == "'"${sv[$s]}"'") | .display) |= (. | not)' "${config_file}" | sponge "${config_file}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment