From de0e8225a67d6c185805215ab4805897b69e7f0f Mon Sep 17 00:00:00 2001 From: Abhishek Keshri Date: Wed, 29 Mar 2023 14:52:27 +0530 Subject: [PATCH] style: shellcheck fixes --- scripts/battery.sh | 180 +++++++++++++++++++-------------------- scripts/cpu_info.sh | 73 ++++++++-------- scripts/git.sh | 117 +++++++++++-------------- scripts/gpu_usage.sh | 57 ++++++------- scripts/network_ping.sh | 28 +++--- scripts/ram_info.sh | 114 ++++++++++++------------- scripts/sleep_weather.sh | 46 +++++----- scripts/tmux2k.sh | 2 +- scripts/utils.sh | 33 ++++--- scripts/weather.sh | 95 ++++++++++----------- 10 files changed, 356 insertions(+), 389 deletions(-) diff --git a/scripts/battery.sh b/scripts/battery.sh index 5af933f..fcbcf14 100755 --- a/scripts/battery.sh +++ b/scripts/battery.sh @@ -2,128 +2,124 @@ # 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 )" -source $current_dir/utils.sh +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$current_dir"/utils.sh linux_acpi() { - arg=$1 - 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 - ;; + arg=$1 + 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 - ;; + 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 '% ' - ;; - *) - ;; - esac - fi + *) ;; + + 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 '% ' + ;; + *) ;; + + esac + fi } -battery_percent() -{ - # Check OS - case $(uname -s) in +battery_percent() { + # Check OS + case $(uname -s) in Linux) - percent=$(linux_acpi percent) - [ -n "$percent" ] && echo " $percent" - ;; + percent=$(linux_acpi percent) + [ -n "$percent" ] && echo " $percent" + ;; Darwin) - echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') - ;; + echo $(pmset -g batt | grep -Eo '[0-9]?[0-9]?[0-9]%') + ;; 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*) - # leaving empty - TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) + # leaving empty - TODO - windows compatability + ;; - *) - ;; - esac + *) ;; + + esac } -battery_status() -{ - # Check OS - case $(uname -s) in +battery_status() { + # Check OS + case $(uname -s) in Linux) - status=$(linux_acpi status) - ;; + status=$(linux_acpi status) + ;; Darwin) - status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ") - ;; + status=$(pmset -g batt | sed -n 2p | cut -d ';' -f 2 | tr -d " ") + ;; FreeBSD) - status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}') - ;; + status=$(apm | sed '8,11d' | grep Status | awk '{printf $3}') + ;; - CYGWIN*|MINGW32*|MSYS*|MINGW*) - # leaving empty - TODO - windows compatability - ;; + CYGWIN* | MINGW32* | MSYS* | MINGW*) + # leaving empty - TODO - windows compatability + ;; - *) - ;; - esac + *) ;; - case $status in - discharging|Discharging) - echo '' - ;; + esac + + case $status in + discharging | Discharging) + echo '' + ;; high) - echo '' - ;; + echo '' + ;; charging) - echo '' - ;; + echo '' + ;; *) - echo '' - ;; - esac - ### Old if statements didn't work on BSD, they're probably not POSIX compliant, not sure - # if [ $status = 'discharging' ] || [ $status = 'Discharging' ]; then - # echo '' - # # elif [ $status = 'charging' ]; then # This is needed for FreeBSD AC checking support - # # echo 'AC' - # else - # echo 'AC' - # fi + echo '' + ;; + esac + ### Old if statements didn't work on BSD, they're probably not POSIX compliant, not sure + # if [ $status = 'discharging' ] || [ $status = 'Discharging' ]; then + # echo '' + # # elif [ $status = 'charging' ]; then # This is needed for FreeBSD AC checking support + # # echo 'AC' + # else + # echo 'AC' + # fi } -main() -{ - bat_label=$(get_tmux_option "@tmux2k-battery-label" "") - bat_stat=$(battery_status) - bat_perc=$(battery_percent) +main() { + bat_label=$(get_tmux_option "@tmux2k-battery-label" "") + bat_stat=$(battery_status) + bat_perc=$(battery_percent) - if [ -z "$bat_stat" ]; then # Test if status is empty or not - echo "$bat_label$bat_perc" - elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power - echo "$bat_label $bat_stat" - else - echo "$bat_label $bat_stat$bat_perc" - fi + if [ -z "$bat_stat" ]; then # Test if status is empty or not + echo "$bat_label$bat_perc" + elif [ -z "$bat_perc" ]; then # In case it is a desktop with no battery percent, only AC power + echo "$bat_label $bat_stat" + else + echo "$bat_label $bat_stat$bat_perc" + fi } #run main driver program main - diff --git a/scripts/cpu_info.sh b/scripts/cpu_info.sh index a164cf5..2975584 100755 --- a/scripts/cpu_info.sh +++ b/scripts/cpu_info.sh @@ -2,56 +2,55 @@ # 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 )" +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$current_dir"/utils.sh -get_percent() -{ - case $(uname -s) in +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" - ;; + 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" + ;; Darwin) - cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}') - cpucores=$(sysctl -n hw.logicalcpu) - cpuusage=$(( cpuvalue / cpucores )) - percent="$cpuusage%" - normalize_percent_len $percent - ;; + cpuvalue=$(ps -A -o %cpu | awk -F. '{s+=$1} END {print s}') + cpucores=$(sysctl -n hw.logicalcpu) + cpuusage=$((cpuvalue / cpucores)) + percent="$cpuusage%" + normalize_percent_len $percent + ;; - CYGWIN*|MINGW32*|MSYS*|MINGW*) - # TODO - windows compatability - ;; - esac + CYGWIN* | MINGW32* | MSYS* | MINGW*) + # TODO - windows compatability + ;; + esac } get_load() { - case $(uname -s) in - Linux | Darwin) - loadavg=$(uptime | awk -F'[a-z]:' '{ print $2}' | sed 's/,//g') - echo $loadavg - ;; + case $(uname -s) in + Linux | Darwin) + loadavg=$(uptime | awk -F'[a-z]:' '{ print $2}' | sed 's/,//g') + echo "$loadavg" + ;; - CYGWIN* | MINGW32* | MSYS* | MINGW*) - # TODO - windows compatability - ;; - esac + 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) - cpu_load=$(get_tmux_option "@tmux2k-cpu-display-load" false) - if [ "$cpu_load" = true ]; then - echo "$(get_load)" - else - cpu_label=$(get_tmux_option "@tmux2k-cpu-usage-label" "") - cpu_percent=$(get_percent) - echo "$cpu_label $cpu_percent" - fi - sleep "$RATE" + # 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 + echo "$(get_load)" + else + cpu_label=$(get_tmux_option "@tmux2k-cpu-usage-label" "") + cpu_percent=$(get_percent) + echo "$cpu_label $cpu_percent" + fi + sleep "$RATE" } # run main driver diff --git a/scripts/git.sh b/scripts/git.sh index b176144..a1b7605 100755 --- a/scripts/git.sh +++ b/scripts/git.sh @@ -1,39 +1,36 @@ #!/usr/bin/env bash -current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" -source $current_dir/utils.sh +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" "") +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" "") # Get added, modified, updated and deleted files from git status -getChanges() -{ - declare -i added=0; - declare -i modified=0; - declare -i updated=0; - declare -i deleted=0; +getChanges() { + declare -i added=0 + declare -i modified=0 + declare -i updated=0 + declare -i deleted=0 -for i in $(git -C $path status -s) + for i in $(git -C "$path" status -s); do + case $i in + 'A') + added+=1 + ;; + 'M') + modified+=1 + ;; + 'U') + updated+=1 + ;; + 'D') + deleted+=1 + ;; - do - case $i in - 'A') - added+=1 - ;; - 'M') - modified+=1 - ;; - 'U') - updated+=1 - ;; - 'D') - deleted+=1 - ;; - - esac + esac done output="" @@ -42,30 +39,25 @@ for i in $(git -C $path status -s) [ $updated -gt 0 ] && output+=" ${updated} " [ $deleted -gt 0 ] && output+=" ${deleted} " - echo $output + echo "$output" } - # getting the #{pane_current_path} from tmux2k.sh is no longer possible -getPaneDir() -{ - nextone="false" - for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); - do - if [ "$nextone" == "true" ]; then - echo $i - return - fi - if [ "$i" == "1" ]; then - nextone="true" - fi - done +getPaneDir() { + nextone="false" + for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do + if [ "$nextone" == "true" ]; then + echo "$i" + return + fi + if [ "$i" == "1" ]; then + nextone="true" + fi + done } - # check if the current or diff symbol is empty to remove ugly padding -checkEmptySymbol() -{ +checkEmptySymbol() { symbol=$1 if [ "$symbol" == "" ]; then echo "true" @@ -75,10 +67,9 @@ checkEmptySymbol() } # check to see if the current repo is not up to date with HEAD -checkForChanges() -{ +checkForChanges() { if [ "$(checkForGitDir)" == "true" ]; then - if [ "$(git -C $path status -s)" != "" ]; then + if [ "$(git -C "$path" status -s)" != "" ]; then echo "true" else echo "false" @@ -89,9 +80,8 @@ checkForChanges() } # check if a git repo exists in the directory -checkForGitDir() -{ - if [ "$(git -C $path rev-parse --abbrev-ref HEAD)" != "" ]; then +checkForGitDir() { + if [ "$(git -C "$path" rev-parse --abbrev-ref HEAD)" != "" ]; then echo "true" else echo "false" @@ -99,18 +89,16 @@ checkForGitDir() } # return branch name if there is one -getBranch() -{ +getBranch() { 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 - echo $no_repo_message + echo "$no_repo_message" fi } # return the final message for the status bar -getMessage() -{ +getMessage() { if [ $(checkForGitDir) == "true" ]; then branch="$(getBranch)" @@ -119,13 +107,13 @@ getMessage() changes="$(getChanges)" if [ "${hide_status}" == "false" ]; then - if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then echo "${changes} $branch" else echo "$diff_symbol ${changes} $branch" fi else - if [ $(checkEmptySymbol $diff_symbol) == "true" ]; then + if [ $(checkEmptySymbol "$diff_symbol") == "true" ]; then echo "$branch" else echo "$diff_symbol $branch" @@ -133,19 +121,18 @@ getMessage() fi else - if [ $(checkEmptySymbol $current_symbol) == "true" ]; then + if [ $(checkEmptySymbol "$current_symbol") == "true" ]; then echo "$branch" else echo "$current_symbol $branch" fi fi else - echo $no_repo_message + echo "$no_repo_message" fi } -main() -{ +main() { path=$(getPaneDir) getMessage } diff --git a/scripts/gpu_usage.sh b/scripts/gpu_usage.sh index 0412145..d6715cc 100755 --- a/scripts/gpu_usage.sh +++ b/scripts/gpu_usage.sh @@ -2,46 +2,43 @@ # 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 )" +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" source "$current_dir"/utils.sh -get_platform() -{ - case $(uname -s) in +get_platform() { + case $(uname -s) in Linux) - gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') - echo "$gpu" - ;; + gpu=$(lspci -v | grep VGA | head -n 1 | awk '{print $5}') + echo "$gpu" + ;; Darwin) - # TODO - Darwin/Mac compatability - ;; + # TODO - Darwin/Mac compatability + ;; - CYGWIN*|MINGW32*|MSYS*|MINGW*) - # TODO - windows compatability - ;; - esac + CYGWIN* | MINGW32* | MSYS* | MINGW*) + # TODO - windows compatability + ;; + esac } -get_gpu() -{ - gpu=$(get_platform) - if [[ "$gpu" == NVIDIA ]]; then - usage=$(nvidia-smi | grep '%' | awk '{ sum += $13 } END { printf("%d%%\n", sum / NR) }') - else - usage='unknown' - fi - normalize_percent_len $usage +get_gpu() { + gpu=$(get_platform) + if [[ "$gpu" == NVIDIA ]]; then + usage=$(nvidia-smi | grep '%' | awk '{ sum += $13 } END { printf("%d%%\n", sum / NR) }') + else + usage='unknown' + fi + normalize_percent_len $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) - echo "$gpu_label $gpu_usage" - sleep "$RATE" +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) + echo "$gpu_label $gpu_usage" + sleep "$RATE" } # run the main driver diff --git a/scripts/network_ping.sh b/scripts/network_ping.sh index 8d57514..992c970 100644 --- a/scripts/network_ping.sh +++ b/scripts/network_ping.sh @@ -10,25 +10,25 @@ current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 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" - ;; + 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 - ;; - esac + CYGWIN* | MINGW32* | MSYS* | MINGW*) + # TODO - windows compatability + ;; + esac } main() { - echo "$(ping_function)" - RATE=$(get_tmux_option "@tmux2k-ping-rate" 5) - sleep "$RATE" + echo "$(ping_function)" + RATE=$(get_tmux_option "@tmux2k-ping-rate" 5) + sleep "$RATE" } # run main driver diff --git a/scripts/ram_info.sh b/scripts/ram_info.sh index 1662c63..9b9439c 100755 --- a/scripts/ram_info.sh +++ b/scripts/ram_info.sh @@ -2,74 +2,72 @@ # 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 )" -source $current_dir/utils.sh +current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +source "$current_dir"/utils.sh -get_percent() -{ - case $(uname -s) in +get_percent() { + case $(uname -s) in Linux) - total_mem_gb=$(free -g | awk '/^Mem/ {print $2}') - used_mem=$(free -g | awk '/^Mem/ {print $3}') - total_mem=$(free -h | awk '/^Mem/ {print $2}') - if (( "$total_mem_gb" == 0)); then - memory_usage=$(free -m | awk '/^Mem/ {print $3}') - total_mem_mb=$(free -m | awk '/^Mem/ {print $2}') - echo $memory_usage\M\B/$total_mem_mb\M\B - elif (( "$used_mem" == 0 )); then - memory_usage=$(free -m | awk '/^Mem/ {print $3}') - echo $memory_usage\M\B/$total_mem_gb\G\B - else - memory_usage=$(free -g | awk '/^Mem/ {print $3}') - echo $memory_usage\G\B/$total_mem_gb\G\B - fi - ;; + total_mem_gb=$(free -g | awk '/^Mem/ {print $2}') + used_mem=$(free -g | awk '/^Mem/ {print $3}') + total_mem=$(free -h | awk '/^Mem/ {print $2}') + if (("$total_mem_gb" == 0)); then + memory_usage=$(free -m | awk '/^Mem/ {print $3}') + total_mem_mb=$(free -m | awk '/^Mem/ {print $2}') + echo "$memory_usage"\M\B/"$total_mem_mb"\M\B + elif (("$used_mem" == 0)); then + memory_usage=$(free -m | awk '/^Mem/ {print $3}') + echo "$memory_usage"\M\B/"$total_mem_gb"\G\B + else + memory_usage=$(free -g | awk '/^Mem/ {print $3}') + echo "$memory_usage"\G\B/"$total_mem_gb"\G\B + fi + ;; Darwin) - # Get used memory blocks with vm_stat, multiply by page size to get size in bytes, then convert to MiB - used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk -v pagesize=$(pagesize) '{printf "%d\n", ($1+$2) * pagesize / 1048576}') - total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}') - if (( "$used_mem" < 1024 )); then - echo $used_mem\M\B/$total_mem - else - memory=$(($used_mem/1024)) - echo $memory\G\B/$total_mem - fi - ;; + # Get used memory blocks with vm_stat, multiply by page size to get size in bytes, then convert to MiB + used_mem=$(vm_stat | grep ' active\|wired ' | sed 's/[^0-9]//g' | paste -sd ' ' - | awk -v pagesize=$(pagesize) '{printf "%d\n", ($1+$2) * pagesize / 1048576}') + total_mem=$(system_profiler SPHardwareDataType | grep "Memory:" | awk '{print $2 $3}') + if (("$used_mem" < 1024)); then + echo "$used_mem"\M\B/"$total_mem" + else + memory=$(($used_mem / 1024)) + echo $memory\G\B/$total_mem + fi + ;; 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))" - mem_cache="$(($(sysctl -n vm.stats.vm.v_cache_count) * hw_pagesize))" + # 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))" + mem_cache="$(($(sysctl -n vm.stats.vm.v_cache_count) * hw_pagesize))" - free_mem=$(((mem_inactive + mem_unused + mem_cache) / 1024 / 1024)) - total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024)) - used_mem=$((total_mem - free_mem)) - echo $used_mem - if (( "$used_mem" < 1024 )); then - echo $used_mem\M\B/$total_mem - else - memory=$(($used_mem/1024)) - echo $memory\G\B/$total_mem - fi - ;; + free_mem=$(((mem_inactive + mem_unused + mem_cache) / 1024 / 1024)) + total_mem=$(($(sysctl -n hw.physmem) / 1024 / 1024)) + used_mem=$((total_mem - free_mem)) + echo $used_mem + if (("$used_mem" < 1024)); then + echo $used_mem\M\B/$total_mem + else + memory=$(($used_mem / 1024)) + echo $memory\G\B/$total_mem + fi + ;; - CYGWIN*|MINGW32*|MSYS*|MINGW*) - # TODO - windows compatability - ;; - esac + 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) - echo "$ram_label $ram_percent" - sleep "$RATE" +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) + echo "$ram_label $ram_percent" + sleep "$RATE" } #run main driver diff --git a/scripts/sleep_weather.sh b/scripts/sleep_weather.sh index 13446d7..ac1f6ea 100755 --- a/scripts/sleep_weather.sh +++ b/scripts/sleep_weather.sh @@ -11,37 +11,33 @@ fixedlocation=$3 LOCKFILE=/tmp/.tmux2k-tmux-weather.lock DATAFILE=/tmp/.tmux2k-tmux-data -ensure_single_process() -{ - # check for another running instance of this script and terminate it if found - [ -f $LOCKFILE ] && ps -p "$(cat $LOCKFILE)" -o cmd= | grep -F " ${BASH_SOURCE[0]}" && kill "$(cat $LOCKFILE)" - echo $$ > $LOCKFILE +ensure_single_process() { + # check for another running instance of this script and terminate it if found + [ -f $LOCKFILE ] && ps -p "$(cat $LOCKFILE)" -o cmd= | grep -F " ${BASH_SOURCE[0]}" && kill "$(cat $LOCKFILE)" + echo $$ >$LOCKFILE } -main() -{ - ensure_single_process +main() { + ensure_single_process - current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" - if [ ! -f $DATAFILE ]; then - printf "Loading..." > $DATAFILE - fi - - "$current_dir"/weather.sh > $DATAFILE - - while tmux has-session &> /dev/null - do - "$current_dir"/weather.sh "$fahrenheit" "$location" "$fixedlocation" > $DATAFILE - if tmux has-session &> /dev/null - then - sleep 1200 - else - break + if [ ! -f $DATAFILE ]; then + printf "Loading..." >$DATAFILE fi - done - rm $LOCKFILE + "$current_dir"/weather.sh >$DATAFILE + + while tmux has-session &>/dev/null; do + "$current_dir"/weather.sh "$fahrenheit" "$location" "$fixedlocation" >$DATAFILE + if tmux has-session &>/dev/null; then + sleep 1200 + else + break + fi + done + + rm $LOCKFILE } #run main driver function diff --git a/scripts/tmux2k.sh b/scripts/tmux2k.sh index 8289d63..8fbdb11 100755 --- a/scripts/tmux2k.sh +++ b/scripts/tmux2k.sh @@ -65,7 +65,7 @@ main() { # Handle left icon padding padding="" if [ "$show_left_icon_padding" -gt "0" ]; then - padding="$(printf '%*s' $show_left_icon_padding)" + padding="$(printf '%*s' "$show_left_icon_padding")" fi left_icon="$left_icon$padding" diff --git a/scripts/utils.sh b/scripts/utils.sh index a296192..707691d 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -1,25 +1,24 @@ #!/usr/bin/env bash get_tmux_option() { - local option=$1 - local default_value=$2 - local option_value=$(tmux show-option -gqv "$option") - if [ -z "$option_value" ]; then - echo $default_value - else - echo $option_value - fi + local option=$1 + local default_value=$2 + local option_value=$(tmux show-option -gqv "$option") + if [ -z "$option_value" ]; then + echo $default_value + else + echo $option_value + 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 - percent_len=${#1} - let diff_len=$max_len-$percent_len - # if the diff_len is even, left will have 1 more space than right - let left_spaces=($diff_len+1)/2 - let right_spaces=($diff_len)/2 - printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" + # 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} + let diff_len=$max_len-$percent_len + # if the diff_len is even, left will have 1 more space than right + let left_spaces=($diff_len + 1)/2 + let right_spaces=($diff_len)/2 + printf "%${left_spaces}s%s%${right_spaces}s\n" "" $1 "" } - diff --git a/scripts/weather.sh b/scripts/weather.sh index ecf4ca4..1976f85 100755 --- a/scripts/weather.sh +++ b/scripts/weather.sh @@ -6,68 +6,63 @@ fahrenheit=$1 location=$2 fixedlocation=$3 -display_location() -{ - if $location && [[ ! -z "$fixedlocation" ]]; then - echo " $fixedlocation" - elif $location; then - city=$(curl -s https://ipinfo.io/city 2> /dev/null) - region=$(curl -s https://ipinfo.io/region 2> /dev/null) - echo " $city, $region" - else - echo '' - fi +display_location() { + if $location && [[ -n "$fixedlocation" ]]; then + echo " $fixedlocation" + elif $location; then + city=$(curl -s https://ipinfo.io/city 2>/dev/null) + region=$(curl -s https://ipinfo.io/region 2>/dev/null) + echo " $city, $region" + else + echo '' + fi } -fetch_weather_information() -{ - display_weather=$1 - # it gets the weather condition textual name (%C), and the temperature (%t) - curl -sL wttr.in/$fixedlocation\?format="%C+%t$display_weather" +fetch_weather_information() { + display_weather=$1 + # it gets the weather condition textual name (%C), and the temperature (%t) + curl -sL wttr.in/"$fixedlocation"\?format="%C+%t$display_weather" } #get weather display -display_weather() -{ - if $fahrenheit; then - display_weather='&u' # for USA system - else - display_weather='&m' # for metric system - fi - weather_information=$(fetch_weather_information $display_weather) +display_weather() { + if $fahrenheit; then + display_weather='&u' # for USA system + else + display_weather='&m' # for metric system + fi + weather_information=$(fetch_weather_information $display_weather) - weather_condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc - temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc - unicode=$(forecast_unicode "$weather_condition") + weather_condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc + temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc + unicode=$(forecast_unicode "$weather_condition") - echo "$unicode${temperature/+/}" # remove the plus sign to the temperature + echo "$unicode${temperature/+/}" # remove the plus sign to the temperature } -forecast_unicode() -{ - weather_condition=$(echo "$weather_condition" | awk '{print tolower($0)}') +forecast_unicode() { + weather_condition=$(echo "$weather_condition" | awk '{print tolower($0)}') - if [[ $weather_condition =~ 'snow' ]]; then - echo '❄ ' - elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then - echo '☂ ' - elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then - echo '☁ ' - elif [[ $weather_condition = 'NA' ]]; then - echo '' - else - echo '☀ ' - fi + if [[ $weather_condition =~ 'snow' ]]; then + echo '❄ ' + elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then + echo '☂ ' + elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then + echo '☁ ' + elif [[ $weather_condition = 'NA' ]]; then + echo '' + else + echo '☀ ' + fi } -main() -{ - # process should be cancelled when session is killed - if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then - echo "$(display_weather)$(display_location)" - else - echo "Location Unavailable" - fi +main() { + # process should be cancelled when session is killed + if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then + echo "$(display_weather)$(display_location)" + else + echo "Location Unavailable" + fi } #run main driver program