From d2e517d76a394fc562bee9bc111301b630b35488 Mon Sep 17 00:00:00 2001 From: Abhishek Keshri Date: Sun, 17 Mar 2024 04:00:27 +0530 Subject: [PATCH] refactor: cleanup scripts --- scripts/bandwidth.sh | 4 +-- scripts/battery.sh | 76 ++++++++++---------------------------------- scripts/cpu.sh | 16 +++------- scripts/git.sh | 69 +++++++++++++++------------------------- scripts/gpu.sh | 16 +++------- scripts/network.sh | 12 ++----- scripts/ping.sh | 12 ++----- scripts/ram.sh | 9 ++---- scripts/utils.sh | 6 ++-- 9 files changed, 60 insertions(+), 160 deletions(-) diff --git a/scripts/bandwidth.sh b/scripts/bandwidth.sh index 90462dd..702e25f 100755 --- a/scripts/bandwidth.sh +++ b/scripts/bandwidth.sh @@ -1,12 +1,10 @@ #!/bin/bash -INTERVAL="1" # update interval in seconds +INTERVAL="1" -# Network interface to monitor current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$current_dir"/utils.sh -# Network interface to monitor network_name="en0" if [[ $(uname -s) == "Darwin" ]]; then diff --git a/scripts/battery.sh b/scripts/battery.sh index 5f12174..4ffcc3a 100755 --- a/scripts/battery.sh +++ b/scripts/battery.sh @@ -1,5 +1,5 @@ #!/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 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -10,89 +10,45 @@ linux_acpi() { BAT=$(ls -d /sys/class/power_supply/BAT* | head -1) if [ ! -x "$(which acpi 2>/dev/null)" ]; then case "$arg" in - status) - cat "$BAT"/status - ;; - - percent) - cat "$BAT"/capacity - ;; - - *) ;; - + status) cat "$BAT"/status ;; + percent) cat "$BAT"/capacity ;; esac else case "$arg" in - status) - acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' - ;; - percent) - acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% ' - ;; - *) ;; - + status) acpi | cut -d: -f2- | cut -d, -f1 | tr -d ' ' ;; + percent) acpi | cut -d: -f2- | cut -d, -f2 | tr -d '% ' ;; esac fi } battery_percent() { - # Check OS case $(uname -s) in Linux) percent=$(linux_acpi percent) [ -n "$percent" ] && echo " $percent" ;; - Darwin) - echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%' | sed 's/%//g') - ;; + Darwin) echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%' | sed 's/%//g') ;; - FreeBSD) - echo $(apm | sed '8,11d' | grep life | awk '{print $4}') - ;; + FreeBSD) echo $(apm | sed '8,11d' | grep life | awk '{print $4}') ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # leaving empty - TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability esac } battery_status() { - # Check OS case $(uname -s) in - Linux) - 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*) - # leaving empty - TODO - windows compatibility - ;; - - *) ;; - + Linux) 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*) ;; esac case $status in - discharging | Discharging) - echo '' - ;; - high) - echo '' - ;; - charging) - echo '' - ;; - *) - echo '' - ;; + discharging | Discharging) echo '' ;; + high) echo '' ;; + charging) echo '' ;; + *) echo '' ;; esac } diff --git a/scripts/cpu.sh b/scripts/cpu.sh index 2975584..72332e2 100755 --- a/scripts/cpu.sh +++ b/scripts/cpu.sh @@ -1,5 +1,5 @@ #!/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 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -9,7 +9,7 @@ get_percent() { case $(uname -s) in 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"%"}') - normalize_percent_len "$percent" + normalize_padding "$percent" ;; Darwin) @@ -17,12 +17,10 @@ get_percent() { cpucores=$(sysctl -n hw.logicalcpu) cpuusage=$((cpuvalue / cpucores)) percent="$cpuusage%" - normalize_percent_len $percent + normalize_padding $percent ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility esac } @@ -33,14 +31,11 @@ get_load() { echo "$loadavg" ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility esac } main() { - # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) cpu_load=$(get_tmux_option "@tmux2k-cpu-display-load" false) if [ "$cpu_load" = true ]; then @@ -53,5 +48,4 @@ main() { sleep "$RATE" } -# run main driver main diff --git a/scripts/git.sh b/scripts/git.sh index a1b7605..1c309c6 100755 --- a/scripts/git.sh +++ b/scripts/git.sh @@ -3,13 +3,12 @@ current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$current_dir"/utils.sh -IFS=' ' read -r -a hide_status <<<$(get_tmux_option "@tmux2k-git-disable-status" "false") -IFS=' ' read -r -a current_symbol <<<$(get_tmux_option "@tmux2k-git-show-current-symbol" "") -IFS=' ' read -r -a 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" "") +hide_status=$(get_tmux_option '@tmux2k-git-disable-status' 'false') +current_symbol=$(get_tmux_option '@tmux2k-git-show-current-symbol' '') +diff_symbol=$(get_tmux_option '@tmux2k-git-show-diff-symbol' '') +no_repo_message=$(get_tmux_option '@tmux2k-git-no-repo-message' '') -# Get added, modified, updated and deleted files from git status -getChanges() { +get_changes() { declare -i added=0 declare -i modified=0 declare -i updated=0 @@ -17,19 +16,10 @@ getChanges() { for i in $(git -C "$path" status -s); do case $i in - 'A') - added+=1 - ;; - 'M') - modified+=1 - ;; - 'U') - updated+=1 - ;; - 'D') - deleted+=1 - ;; - + 'A') added+=1 ;; + 'M') modified+=1 ;; + 'U') updated+=1 ;; + 'D') deleted+=1 ;; esac done @@ -42,8 +32,7 @@ getChanges() { echo "$output" } -# getting the #{pane_current_path} from tmux2k.sh is no longer possible -getPaneDir() { +get_pane_dir() { nextone="false" for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do if [ "$nextone" == "true" ]; then @@ -56,8 +45,7 @@ getPaneDir() { done } -# check if the current or diff symbol is empty to remove ugly padding -checkEmptySymbol() { +check_empty_symbol() { symbol=$1 if [ "$symbol" == "" ]; then echo "true" @@ -66,9 +54,8 @@ checkEmptySymbol() { fi } -# check to see if the current repo is not up to date with HEAD -checkForChanges() { - if [ "$(checkForGitDir)" == "true" ]; then +check_for_changes() { + if [ "$(check_for_git_dir)" == "true" ]; then if [ "$(git -C "$path" status -s)" != "" ]; then echo "true" else @@ -79,8 +66,7 @@ checkForChanges() { fi } -# check if a git repo exists in the directory -checkForGitDir() { +check_for_git_dir() { if [ "$(git -C "$path" rev-parse --abbrev-ref HEAD)" != "" ]; then echo "true" else @@ -88,32 +74,30 @@ checkForGitDir() { fi } -# return branch name if there is one -getBranch() { - if [ $(checkForGitDir) == "true" ]; then +get_branch() { + if [ $(check_for_git_dir) == "true" ]; then printf "%.20s " $(git -C "$path" rev-parse --abbrev-ref HEAD) else echo "$no_repo_message" fi } -# return the final message for the status bar -getMessage() { - if [ $(checkForGitDir) == "true" ]; then - branch="$(getBranch)" +get_message() { + if [ $(check_for_git_dir) == "true" ]; then + branch="$(get_branch)" - if [ $(checkForChanges) == "true" ]; then + if [ $(check_for_changes) == "true" ]; then - changes="$(getChanges)" + changes="$(get_changes)" if [ "${hide_status}" == "false" ]; then - if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then + if [ $(check_empty_symbol "$diff_symbol") == "true" ]; then echo "${changes} $branch" else echo "$diff_symbol ${changes} $branch" fi else - if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then + if [ $(check_empty_symbol "$diff_symbol") == "true" ]; then echo "$branch" else echo "$diff_symbol $branch" @@ -121,7 +105,7 @@ getMessage() { fi else - if [ $(checkEmptySymbol "$current_symbol") == "true" ]; then + if [ $(check_empty_symbol "$current_symbol") == "true" ]; then echo "$branch" else echo "$current_symbol $branch" @@ -133,9 +117,8 @@ getMessage() { } main() { - path=$(getPaneDir) - getMessage + path=$(get_pane_dir) + get_message } -#run main driver program main diff --git a/scripts/gpu.sh b/scripts/gpu.sh index b72de8d..80605e1 100755 --- a/scripts/gpu.sh +++ b/scripts/gpu.sh @@ -1,5 +1,5 @@ #!/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 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}') echo "$gpu" ;; - - Darwin) - # TODO - Darwin/Mac compatability - ;; - - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; + Darwin) ;; # TODO - Darwin/Mac compatibility + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatibility esac } @@ -29,11 +23,10 @@ get_gpu() { else usage='unknown' fi - normalize_percent_len $usage + normalize_padding "$usage" } main() { - # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) gpu_label=$(get_tmux_option "@tmux2k-gpu-usage-label" "GPU") gpu_usage=$(get_gpu) @@ -41,5 +34,4 @@ main() { sleep "$RATE" } -# run the main driver main diff --git a/scripts/network.sh b/scripts/network.sh index 0a8dd50..6bb1610 100755 --- a/scripts/network.sh +++ b/scripts/network.sh @@ -1,11 +1,10 @@ #!/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 HOSTS="google.com github.com example.com" get_ssid() { - # Check OS case $(uname -s) in Linux) SSID=$(iwgetid -r) @@ -25,14 +24,8 @@ get_ssid() { fi ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # leaving empty - TODO - windows compatability - ;; - - *) ;; - + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability esac - } main() { @@ -47,5 +40,4 @@ main() { echo "$network" } -#run main driver function main diff --git a/scripts/ping.sh b/scripts/ping.sh index 992c970..d9a71f4 100644 --- a/scripts/ping.sh +++ b/scripts/ping.sh @@ -1,10 +1,6 @@ #!/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 -# @tmux2k-ping-server "example.com" -# @tmux2k-ping-rate 5 +export LC_ALL=en_US.UTF-8 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$current_dir"/utils.sh @@ -12,15 +8,12 @@ source "$current_dir"/utils.sh ping_function() { case $(uname -s) in Linux | Darwin) - # storing the hostname/IP in the variable PINGSERVER, default is 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) echo "$pingtime ms" ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability esac } @@ -31,5 +24,4 @@ main() { sleep "$RATE" } -# run main driver main diff --git a/scripts/ram.sh b/scripts/ram.sh index 9b9439c..2b71362 100755 --- a/scripts/ram.sh +++ b/scripts/ram.sh @@ -1,5 +1,5 @@ #!/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 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" @@ -37,7 +37,6 @@ get_percent() { ;; FreeBSD) - # Looked at the code from neofetch hw_pagesize="$(sysctl -n 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))" @@ -55,14 +54,11 @@ get_percent() { fi ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) ;; # TODO - windows compatability esac } main() { - # storing the refresh rate in the variable RATE, default is 5 RATE=$(get_tmux_option "@tmux2k-refresh-rate" 5) ram_label=$(get_tmux_option "@tmux2k-ram-usage-label" "") ram_percent=$(get_percent) @@ -70,5 +66,4 @@ main() { sleep "$RATE" } -#run main driver main diff --git a/scripts/utils.sh b/scripts/utils.sh index 707691d..29efcc0 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -11,10 +11,8 @@ get_tmux_option() { fi } -# normalize the percentage string to always have a length of 5 -normalize_percent_len() { - # the max length that the percent can reach, which happens for a two digit number with a decimal house: "99.9%" - max_len=5 +normalize_padding() { + max_len=${2:-5} percent_len=${#1} let diff_len=$max_len-$percent_len # if the diff_len is even, left will have 1 more space than right