refactor: cleanup scripts

This commit is contained in:
Abhishek Keshri 2024-03-17 04:00:27 +05:30 committed by Abhishek Keshri
parent 15b06d5196
commit d2e517d76a
9 changed files with 60 additions and 160 deletions

View file

@ -1,12 +1,10 @@
#!/bin/bash #!/bin/bash
INTERVAL="1" # update interval in seconds INTERVAL="1"
# Network interface to monitor
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh source "$current_dir"/utils.sh
# Network interface to monitor
network_name="en0" network_name="en0"
if [[ $(uname -s) == "Darwin" ]]; then if [[ $(uname -s) == "Darwin" ]]; then

View file

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -10,89 +10,45 @@ linux_acpi() {
BAT=$(ls -d /sys/class/power_supply/BAT* | head -1) BAT=$(ls -d /sys/class/power_supply/BAT* | head -1)
if [ ! -x "$(which acpi 2>/dev/null)" ]; then if [ ! -x "$(which acpi 2>/dev/null)" ]; then
case "$arg" in case "$arg" in
status) status) cat "$BAT"/status ;;
cat "$BAT"/status percent) cat "$BAT"/capacity ;;
;;
percent)
cat "$BAT"/capacity
;;
*) ;;
esac esac
else else
case "$arg" in case "$arg" in
status) status) acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' ;;
acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' percent) acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% ' ;;
;;
percent)
acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% '
;;
*) ;;
esac esac
fi fi
} }
battery_percent() { battery_percent() {
# Check OS
case $(uname -s) in case $(uname -s) in
Linux) Linux)
percent=$(linux_acpi percent) percent=$(linux_acpi percent)
[ -n "$percent" ] && echo " $percent" [ -n "$percent" ] && echo " $percent"
;; ;;
Darwin) Darwin) echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%' | sed 's/%//g') ;;
echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%' | sed 's/%//g')
;;
FreeBSD) FreeBSD) echo $(apm | sed '8,11d' | grep life | awk '{print $4}') ;;
echo $(apm | sed '8,11d' | grep life | awk '{print $4}')
;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability
# leaving empty - TODO - windows compatability
;;
esac esac
} }
battery_status() { battery_status() {
# Check OS
case $(uname -s) in case $(uname -s) in
Linux) Linux) status=$(linux_acpi status) ;;
status=$(linux_acpi status) Darwin) status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ") ;;
;; FreeBSD) status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}') ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) ;;
Darwin)
status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ")
;;
FreeBSD)
status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}')
;;
CYGWIN* | MINGW32* | MSYS* | MINGW*)
# leaving empty - TODO - windows compatibility
;;
*) ;;
esac esac
case $status in case $status in
discharging | Discharging) discharging | Discharging) echo '' ;;
echo '' high) echo '' ;;
;; charging) echo '' ;;
high) *) echo '' ;;
echo ''
;;
charging)
echo ''
;;
*)
echo ''
;;
esac esac
} }

View file

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -9,7 +9,7 @@ get_percent() {
case $(uname -s) in case $(uname -s) in
Linux) Linux)
percent=$(LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}') percent=$(LC_NUMERIC=en_US.UTF-8 top -bn2 -d 0.01 | grep "Cpu(s)" | tail -1 | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
normalize_percent_len "$percent" normalize_padding "$percent"
;; ;;
Darwin) Darwin)
@ -17,12 +17,10 @@ get_percent() {
cpucores=$(sysctl -n hw.logicalcpu) cpucores=$(sysctl -n hw.logicalcpu)
cpuusage=$((cpuvalue / cpucores)) cpuusage=$((cpuvalue / cpucores))
percent="$cpuusage%" percent="$cpuusage%"
normalize_percent_len $percent normalize_padding $percent
;; ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility
# TODO - windows compatability
;;
esac esac
} }
@ -33,14 +31,11 @@ get_load() {
echo "$loadavg" echo "$loadavg"
;; ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility
# TODO - windows compatability
;;
esac esac
} }
main() { main() {
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5)
cpu_load=$(get_tmux_option "@tmux2k-cpu-display-load" false) cpu_load=$(get_tmux_option "@tmux2k-cpu-display-load" false)
if [ "$cpu_load" = true ]; then if [ "$cpu_load" = true ]; then
@ -53,5 +48,4 @@ main() {
sleep "$RATE" sleep "$RATE"
} }
# run main driver
main main

View file

@ -3,13 +3,12 @@
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh source "$current_dir"/utils.sh
IFS=' ' read -r -a hide_status <<<$(get_tmux_option "@tmux2k-git-disable-status" "false") hide_status=$(get_tmux_option '@tmux2k-git-disable-status' 'false')
IFS=' ' read -r -a current_symbol <<<$(get_tmux_option "@tmux2k-git-show-current-symbol" "") current_symbol=$(get_tmux_option '@tmux2k-git-show-current-symbol' '')
IFS=' ' read -r -a diff_symbol <<<$(get_tmux_option "@tmux2k-git-show-diff-symbol" "") diff_symbol=$(get_tmux_option '@tmux2k-git-show-diff-symbol' '')
IFS=' ' read -r -a no_repo_message <<<$(get_tmux_option "@tmux2k-git-no-repo-message" "") no_repo_message=$(get_tmux_option '@tmux2k-git-no-repo-message' '')
# Get added, modified, updated and deleted files from git status get_changes() {
getChanges() {
declare -i added=0 declare -i added=0
declare -i modified=0 declare -i modified=0
declare -i updated=0 declare -i updated=0
@ -17,19 +16,10 @@ getChanges() {
for i in $(git -C "$path" status -s); do for i in $(git -C "$path" status -s); do
case $i in case $i in
'A') 'A') added+=1 ;;
added+=1 'M') modified+=1 ;;
;; 'U') updated+=1 ;;
'M') 'D') deleted+=1 ;;
modified+=1
;;
'U')
updated+=1
;;
'D')
deleted+=1
;;
esac esac
done done
@ -42,8 +32,7 @@ getChanges() {
echo "$output" echo "$output"
} }
# getting the #{pane_current_path} from tmux2k.sh is no longer possible get_pane_dir() {
getPaneDir() {
nextone="false" nextone="false"
for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do
if [ "$nextone" == "true" ]; then if [ "$nextone" == "true" ]; then
@ -56,8 +45,7 @@ getPaneDir() {
done done
} }
# check if the current or diff symbol is empty to remove ugly padding check_empty_symbol() {
checkEmptySymbol() {
symbol=$1 symbol=$1
if [ "$symbol" == "" ]; then if [ "$symbol" == "" ]; then
echo "true" echo "true"
@ -66,9 +54,8 @@ checkEmptySymbol() {
fi fi
} }
# check to see if the current repo is not up to date with HEAD check_for_changes() {
checkForChanges() { if [ "$(check_for_git_dir)" == "true" ]; then
if [ "$(checkForGitDir)" == "true" ]; then
if [ "$(git -C "$path" status -s)" != "" ]; then if [ "$(git -C "$path" status -s)" != "" ]; then
echo "true" echo "true"
else else
@ -79,8 +66,7 @@ checkForChanges() {
fi fi
} }
# check if a git repo exists in the directory check_for_git_dir() {
checkForGitDir() {
if [ "$(git -C "$path" rev-parse --abbrev-ref HEAD)" != "" ]; then if [ "$(git -C "$path" rev-parse --abbrev-ref HEAD)" != "" ]; then
echo "true" echo "true"
else else
@ -88,32 +74,30 @@ checkForGitDir() {
fi fi
} }
# return branch name if there is one get_branch() {
getBranch() { if [ $(check_for_git_dir) == "true" ]; then
if [ $(checkForGitDir) == "true" ]; then
printf "%.20s " $(git -C "$path" rev-parse --abbrev-ref HEAD) printf "%.20s " $(git -C "$path" rev-parse --abbrev-ref HEAD)
else else
echo "$no_repo_message" echo "$no_repo_message"
fi fi
} }
# return the final message for the status bar get_message() {
getMessage() { if [ $(check_for_git_dir) == "true" ]; then
if [ $(checkForGitDir) == "true" ]; then branch="$(get_branch)"
branch="$(getBranch)"
if [ $(checkForChanges) == "true" ]; then if [ $(check_for_changes) == "true" ]; then
changes="$(getChanges)" changes="$(get_changes)"
if [ "${hide_status}" == "false" ]; then if [ "${hide_status}" == "false" ]; then
if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then if [ $(check_empty_symbol "$diff_symbol") == "true" ]; then
echo "${changes} $branch" echo "${changes} $branch"
else else
echo "$diff_symbol ${changes} $branch" echo "$diff_symbol ${changes} $branch"
fi fi
else else
if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then if [ $(check_empty_symbol "$diff_symbol") == "true" ]; then
echo "$branch" echo "$branch"
else else
echo "$diff_symbol $branch" echo "$diff_symbol $branch"
@ -121,7 +105,7 @@ getMessage() {
fi fi
else else
if [ $(checkEmptySymbol "$current_symbol") == "true" ]; then if [ $(check_empty_symbol "$current_symbol") == "true" ]; then
echo "$branch" echo "$branch"
else else
echo "$current_symbol $branch" echo "$current_symbol $branch"
@ -133,9 +117,8 @@ getMessage() {
} }
main() { main() {
path=$(getPaneDir) path=$(get_pane_dir)
getMessage get_message
} }
#run main driver program
main main

View file

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -11,14 +11,8 @@ get_platform() {
gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}')
echo "$gpu" echo "$gpu"
;; ;;
Darwin) ;; # TODO - Darwin/Mac compatibility
Darwin) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility
# TODO - Darwin/Mac compatability
;;
CYGWIN* | MINGW32* | MSYS* | MINGW*)
# TODO - windows compatability
;;
esac esac
} }
@ -29,11 +23,10 @@ get_gpu() {
else else
usage='unknown' usage='unknown'
fi fi
normalize_percent_len $usage normalize_padding "$usage"
} }
main() { main() {
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5)
gpu_label=$(get_tmux_option "@tmux2k-gpu-usage-label" "GPU") gpu_label=$(get_tmux_option "@tmux2k-gpu-usage-label" "GPU")
gpu_usage=$(get_gpu) gpu_usage=$(get_gpu)
@ -41,5 +34,4 @@ main() {
sleep "$RATE" sleep "$RATE"
} }
# run the main driver
main main

View file

@ -1,11 +1,10 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
HOSTS="google.com github.com example.com" HOSTS="google.com github.com example.com"
get_ssid() { get_ssid() {
# Check OS
case $(uname -s) in case $(uname -s) in
Linux) Linux)
SSID=$(iwgetid -r) SSID=$(iwgetid -r)
@ -25,14 +24,8 @@ get_ssid() {
fi fi
;; ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability
# leaving empty - TODO - windows compatability
;;
*) ;;
esac esac
} }
main() { main() {
@ -47,5 +40,4 @@ main() {
echo "$network" echo "$network"
} }
#run main driver function
main main

View file

@ -1,10 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
# configuration export LC_ALL=en_US.UTF-8
# @tmux2k-ping-server "example.com"
# @tmux2k-ping-rate 5
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$current_dir"/utils.sh source "$current_dir"/utils.sh
@ -12,15 +8,12 @@ source "$current_dir"/utils.sh
ping_function() { ping_function() {
case $(uname -s) in case $(uname -s) in
Linux | Darwin) Linux | Darwin)
# storing the hostname/IP in the variable PINGSERVER, default is google.com
pingserver=$(get_tmux_option "@tmux2k-ping-server" "google.com") pingserver=$(get_tmux_option "@tmux2k-ping-server" "google.com")
pingtime=$(ping -c 1 "$pingserver" | tail -1 | awk '{print $4}' | cut -d '/' -f 2) pingtime=$(ping -c 1 "$pingserver" | tail -1 | awk '{print $4}' | cut -d '/' -f 2)
echo "$pingtime ms" echo "$pingtime ms"
;; ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability
# TODO - windows compatability
;;
esac esac
} }
@ -31,5 +24,4 @@ main() {
sleep "$RATE" sleep "$RATE"
} }
# run main driver
main main

View file

@ -1,5 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
@ -37,7 +37,6 @@ get_percent() {
;; ;;
FreeBSD) FreeBSD)
# Looked at the code from neofetch
hw_pagesize="$(sysctl -n hw.pagesize)" hw_pagesize="$(sysctl -n hw.pagesize)"
mem_inactive="$(($(sysctl -n vm.stats.vm.v_inactive_count) * hw_pagesize))" mem_inactive="$(($(sysctl -n vm.stats.vm.v_inactive_count) * hw_pagesize))"
mem_unused="$(($(sysctl -n vm.stats.vm.v_free_count) * hw_pagesize))" mem_unused="$(($(sysctl -n vm.stats.vm.v_free_count) * hw_pagesize))"
@ -55,14 +54,11 @@ get_percent() {
fi fi
;; ;;
CYGWIN* | MINGW32* | MSYS* | MINGW*) CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability
# TODO - windows compatability
;;
esac esac
} }
main() { main() {
# storing the refresh rate in the variable RATE, default is 5
RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5)
ram_label=$(get_tmux_option "@tmux2k-ram-usage-label" "") ram_label=$(get_tmux_option "@tmux2k-ram-usage-label" "")
ram_percent=$(get_percent) ram_percent=$(get_percent)
@ -70,5 +66,4 @@ main() {
sleep "$RATE" sleep "$RATE"
} }
#run main driver
main main

View file

@ -11,10 +11,8 @@ get_tmux_option() {
fi fi
} }
# normalize the percentage string to always have a length of 5 normalize_padding() {
normalize_percent_len() { max_len=${2:-5}
# the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%"
max_len=5
percent_len=${#1} percent_len=${#1}
let diff_len=$max_len-$percent_len let diff_len=$max_len-$percent_len
# if the diff_len is even, left will have 1 more space than right # if the diff_len is even, left will have 1 more space than right