nix/module/common/tmux/Script.sh

143 lines
2.8 KiB
Bash
Raw Normal View History

2024-04-02 06:50:22 +03:00
#!/usr/bin/env bash
function _tmux_battery() {
local IFS=$'\n'
local batteries=($(ls /sys/class/power_supply/ | grep ^BAT[0-9]$))
[[ "${batteries}" = "" ]] && return 1
local battery="/sys/class/power_supply/${batteries[0]}"
local level=$(cat ${battery}/capacity)
local status=$(cat ${battery}/status)
local is_charging=false
[[ "${status}" = "Charging" ]] && is_charging=true
local icon
if ((level>=60))
then
icon="󱊣"
elif ((level>=40))
then
icon="󱊢"
elif ((level>=20))
then
icon="󱊡"
else
icon="󰂎"
fi
2024-04-02 07:42:51 +03:00
echo -n "${icon}"
2024-04-02 06:50:22 +03:00
${is_charging} && echo -n "󱐋"
2024-04-02 15:33:34 +03:00
echo -n "\n${level}"
2024-04-02 07:42:51 +03:00
}
function _tmux_volume() {
local IFS=$'\n'
local level=$(wpctl get-volume @DEFAULT_SINK@ | cut -d\ -f2 | sed -e "s/^0//" -e "s/\.//")
2024-04-02 08:15:31 +03:00
[[ "${level}" = "" ]] && return 1
2024-04-02 07:42:51 +03:00
[[ "${level}" = "00" ]] && level="0"
local icon
if ((level>=80))
then
icon="󰕾"
elif ((level>=40))
then
icon="󰖀"
elif ((level>=10))
then
icon=""
else
icon="󰝟"
fi
2024-04-02 08:43:16 +03:00
local muted=$(wpctl get-volume @DEFAULT_SINK@ | grep '[MUTED]')
if [[ "${muted}" != "" ]]; then
icon="󰸈"
fi
2024-04-02 07:42:51 +03:00
2024-04-02 15:33:34 +03:00
echo "${icon}"
echo -n "${level}"
2024-04-02 08:15:31 +03:00
}
function _tmux_statusbar() {
2024-04-02 15:33:34 +03:00
local IFS=$'\n'
local sep=""
2024-04-02 08:15:31 +03:00
# Get data.
battery=($(_tmux_battery))
volume=($(_tmux_volume))
# Prepare separators.
sep_batvol=" "
if [[ "${battery[1]}" != "" ]] || [[ "${volume[1]}" != "" ]]; then
sep_batvol="  "
fi
if [[ "${battery[0]}" = "" ]]; then
sep_batvol=""
fi
# Print.
echo -n " "
2024-04-02 15:33:34 +03:00
# Assemble.
2024-04-02 15:45:40 +03:00
if $(cat "/tmp/.tmux_uber"); then
local lang=($(_tmux_language))
[[ "${lang[0]}" != "" ]] && {
echo -n "${lang[0]} ${sep} "
};
2024-04-02 15:33:34 +03:00
[[ "${volume[0]}" != "" ]] && echo -n "${volume[0]} ${volume[1]}%${sep_batvol}"
[[ "${battery[0]}" != "" ]] && echo -n "${battery[0]} ${battery[1]}%"
else
[[ "${volume[0]}" != "" ]] && {
echo -n "${volume[0]}"
[[ "${volume[1]}" -gt 100 ]] && echo -n " ${volume[1]}%"
2024-04-02 15:33:34 +03:00
echo -n "${sep_batvol}"
};
[[ "${battery[0]}" != "" ]] && {
echo -n "${battery[0]}"
[[ "${battery[1]}" -lt 40 ]] && echo -n " ${battery[1]}%"
};
fi
2024-04-02 08:15:31 +03:00
echo -n " "
}
function _tmux_language() {
local IFS=$'\n'
local lang=$(swaymsg -t get_inputs | jq 'map(select(has("xkb_active_layout_name")))[0].xkb_active_layout_name')
2024-04-02 15:45:40 +03:00
echo -n ${lang:1:2}
}
2024-04-02 08:15:31 +03:00
function _tmux_client_count() {
local IFS=$'\n'
local session=${1}
local count=$(tmux list-clients -t ${session} | wc -l)
[ ${count} -gt 1 ] && echo -n "(${count}) "
2024-04-02 06:50:22 +03:00
}
2024-04-02 15:33:34 +03:00
function _tmux_toggle_statusbar() {
local file="/tmp/.tmux_uber"
if [[ ! -f ${file} ]]; then
echo "true" > ${file}
elif $(cat ${file}); then
echo "false" > ${file}
else
echo "true" > ${file}
fi
}
2024-04-02 08:15:31 +03:00
IFS=$'\n'
action=${1}
session=${2}
case "${action}" in
2024-04-02 15:33:34 +03:00
"statusbar") _tmux_statusbar ;;
"client_count") _tmux_client_count ${session} ;;
"togglestatusbar") _tmux_toggle_statusbar ;;
2024-04-02 06:50:22 +03:00
esac