diff --git a/src/batfetch b/src/batfetch index 4a043eb..2e0d26c 100755 --- a/src/batfetch +++ b/src/batfetch @@ -1,49 +1,49 @@ #!/usr/bin/env bash JSON_OUTPUT=false -log(){ - - # Do nothig if info field is empty. - [ "$2" ] || return - - # Store the value of '$1' as we reset the argument list below. - name=$1 - - # Use 'set --' as a means of stripping all leading and trailing - # white-space from the info string. This also normalizes all - # white-space inside of the string. - # - # Disable the shellcheck warning for word-splitting - # as it's safe and intended ('set -f' disables globbing). - { - set -f - set +f -- $2 - info=$* - } - - if [ "$JSON_OUTPUT" = true ]; then - echo "$info" - return - fi - # Move the courcer toward right to print name and info right - # of the ascii with an aditional gap using BF_GAP = variable. - printf '[%sC' "$(( $ascii_width + ${BF_GAP:-4}))" - # printf '[%sC' "$(( $ascii_width + ${BF_GAP:-4}))" - - # Print the info name with color - # printf '[3%s;1m%s' "${BF_COL1:-2}" "$name" - printf '[3%s;1m%s' "${BF_COL1}" "$name" - - # Move courcer backword at starting of info name and then move - # it forward ( max-info-name + extra gap ) to alogn all information. - printf '[%sD[%sC' "${#name}" "${PF_INFOALIGN:-$(($info_length + 1))}" - - # Print the information striping all the extra spaces. - printf '[3%sm%s\n' "${BF_COL2}" "$info" - # printf '[3%sm%s\n' "${BF_COL2:-7}" "$info" - - # Incriment whenever log is called to keep track of lenfo info_height - # so that correctely align the the prompet. - info_height=$((${info_height:-0} + 1)) +log() { + + # Do nothig if info field is empty. + [ "$2" ] || return + + # Store the value of '$1' as we reset the argument list below. + name=$1 + + # Use 'set --' as a means of stripping all leading and trailing + # white-space from the info string. This also normalizes all + # white-space inside of the string. + # + # Disable the shellcheck warning for word-splitting + # as it's safe and intended ('set -f' disables globbing). + { + set -f + set +f -- $2 + info=$* + } + + if [ "$JSON_OUTPUT" = true ]; then + echo "$info" + return + fi + # Move the courcer toward right to print name and info right + # of the ascii with an aditional gap using BF_GAP = variable. + printf '[%sC' "$(($ascii_width + ${BF_GAP:-4}))" + # printf '[%sC' "$(( $ascii_width + ${BF_GAP:-4}))" + + # Print the info name with color + # printf '[3%s;1m%s' "${BF_COL1:-2}" "$name" + printf '[3%s;1m%s' "${BF_COL1}" "$name" + + # Move courcer backword at starting of info name and then move + # it forward ( max-info-name + extra gap ) to alogn all information. + printf '[%sD[%sC' "${#name}" "${PF_INFOALIGN:-$(($info_length + 1))}" + + # Print the information striping all the extra spaces. + printf '[3%sm%s\n' "${BF_COL2}" "$info" + # printf '[3%sm%s\n' "${BF_COL2:-7}" "$info" + + # Incriment whenever log is called to keep track of lenfo info_height + # so that correctely align the the prompet. + info_height=$((${info_height:-0} + 1)) } # batfetch default config. @@ -86,85 +86,84 @@ read -rd '' config <<'EOF' # BF_COL3=6 EOF -load_config(){ - - if [[ -f "${XDG_CONFIG_HOME}/batfetch/config.conf" ]]; then - config_path="${XDG_CONFIG_HOME}/batfetch/config.conf" - source "${XDG_CONFIG_HOME}/batfetch/config.conf" +load_config() { + + if [[ -f "${XDG_CONFIG_HOME}/batfetch/config.conf" ]]; then + config_path="${XDG_CONFIG_HOME}/batfetch/config.conf" + source "${XDG_CONFIG_HOME}/batfetch/config.conf" - elif [[ -f "${XDG_CONFIG_HOME}/batfetch/config" ]]; then - config_path="${XDG_CONFIG_HOME}/batfetch/config" - source "${XDG_CONFIG_HOME}/batfetch/config" + elif [[ -f "${XDG_CONFIG_HOME}/batfetch/config" ]]; then + config_path="${XDG_CONFIG_HOME}/batfetch/config" + source "${XDG_CONFIG_HOME}/batfetch/config" - else - config_file="${XDG_CONFIG_HOME}/batfetch/config.conf" + else + config_file="${XDG_CONFIG_HOME}/batfetch/config.conf" - # The config file doesn't exist, create it. - mkdir -p "${XDG_CONFIG_HOME}/batfetch/" - printf '%s\n' "$config" > "$config_file" - fi + # The config file doesn't exist, create it. + mkdir -p "${XDG_CONFIG_HOME}/batfetch/" + printf '%s\n' "$config" >"$config_file" + fi } - # proctected CAT -pcat(){ +pcat() { value=$(cat $1 2>/dev/null || echo " ") echo $value } # This function gets the title of the current user and host and prints it out using the log function. get_title() { - # Get the current user. If the USER environment variable is not set, use the result of the id -un command. - user=${USER:-$(id -un)} - # Get the hostname. If the HOSTNAME environment variable is not set, use the output of the hostname command. - # If the hostname command fails, use the hostname variable if it is set. - hostname=${HOSTNAME:-${hostname:-$(hostname)}} - - # Use the log function to print the user and hostname in the format "[user]@[hostname]". - # The BF_COL3 variable is used to set the color of the text (in this case, the color code is 1). - # If BF_COL3 is not set, use the default color (code 7). - if [ $JSON_OUTPUT == true ]; then - log " " "$user@$hostname" - else - log "[3${BF_COL3:-1}m${user}${c8}@[3${BF_COL3:-1}m${hostname}" " " - fi -} -get_model(){ + # Get the current user. If the USER environment variable is not set, use the result of the id -un command. + user=${USER:-$(id -un)} + # Get the hostname. If the HOSTNAME environment variable is not set, use the output of the hostname command. + # If the hostname command fails, use the hostname variable if it is set. + hostname=${HOSTNAME:-${hostname:-$(hostname)}} + + # Use the log function to print the user and hostname in the format "[user]@[hostname]". + # The BF_COL3 variable is used to set the color of the text (in this case, the color code is 1). + # If BF_COL3 is not set, use the default color (code 7). + if [ $JSON_OUTPUT == true ]; then + log " " "$user@$hostname" + else + log "[3${BF_COL3:-1}m${user}${c8}@[3${BF_COL3:-1}m${hostname}" " " + fi +} +get_model() { vandor=$(pcat /sys/class/power_supply/BAT*/manufacturer) model=$(pcat /sys/class/power_supply/BAT*/model_name) log "model" "$vandor $model" } -get_charge(){ +get_charge() { charge="$(pcat /sys/class/power_supply/BAT*/capacity)%" Energy_now="$(pcat /sys/class/power_supply/BAT*/energy_now)" Draw="$(pcat /sys/class/power_supply/BAT*/power_now)" if [ $Draw = 0 ]; then - ESTIMATION="AC pluged " + ESTIMATION="AC pluged " else - time=$(echo "scale=4; $Energy_now / $Draw" | bc) - hours=${time%.*} - decimal_part=$(echo "$time" | cut -d '.' -f 2) - minutes=$((decimal_part * 60 / 10000)) - ESTIMATION="${hours}h ${minutes}m" + time=$(echo "scale=4; $Energy_now / $Draw" | bc) + hours=${time%.*} + decimal_part=$(echo "$time" | cut -d '.' -f 2) + minutes=$((decimal_part * 60 / 10000)) + ESTIMATION="${hours}h ${minutes}m" fi log "charge" "$charge $ESTIMATION" } -get_power(){ - powerDraw="$(($(pcat /sys/class/power_supply/BAT*/power_now)/1000000))W" +get_power() { + powerDraw="$(($(pcat /sys/class/power_supply/BAT*/power_now) / 1000000))W" governer="$(pcat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)" log "power" "$powerDraw $governer" } -get_state(){ +get_state() { # online=$(${$(cat /sys/class/power_supply/AC*/online)}:- ${$(cat /sys/class/power_supply/AC*/state)}) online=$(pcat /sys/class/power_supply/A*/online) -# online= [ -f /sys/class/power_supply/AC*/online ] && cat /sys/class/power_supply/AC*/online \ -# || [ -f /sys/class/power_supply/A*/state ] && cat next.txt + # online= [ -f /sys/class/power_supply/AC*/online ] && cat /sys/class/power_supply/AC*/online \ + # || [ -f /sys/class/power_supply/A*/state ] && cat next.txt if [[ $online -eq 1 ]]; then - powerInput="connected" + powerInput="connected" else powerInput="not connected" fi @@ -172,42 +171,50 @@ get_state(){ log "state" "$powerInput & $status" } -get_health(){ - health=$(($(pcat /sys/class/power_supply/BAT*/energy_full) * 100 / $(pcat /sys/class/power_supply/BAT*/energy_full_design))) - cycle=$(cat /sys/class/power_supply/BAT*/cycle_count) - log "health" "$health% | cycles $cycle" +get_health() { + health=$(($(pcat /sys/class/power_supply/BAT*/energy_full) * 100 / $(pcat /sys/class/power_supply/BAT*/energy_full_design))) + cycle=$(cat /sys/class/power_supply/BAT*/cycle_count) + log "health" "$health% | cycles $cycle" } -get_ascii(){ +get_ascii() { + # _charge=100 # Test value for debugging; replace with actual command for production _charge=$(pcat /sys/class/power_supply/BAT*/capacity) # This is the battery percentage - if [[ $_charge == 100 ]]; then - var1="█ FULL █" - else - var1="█ $_charge% █" - fi - online=${online:-$(pcat /sys/class/power_supply/A*/online)} - if [[ $online -eq 1 ]]; then - var2=${var2:-"󱐌"} - else - var2=${var2:-" "} - fi -cat << EOF - 🬭🬭🬭█████🬭🬭🬭 - █ █ - █ █ - $var1 - █ █ - █ $var2 █ - █ █ - 🮂🮂🮂🮂🮂🮂🮂🮂🮂🮂🮂 + ascii_width=11 + ascii_height=7 + + # Calculating the number of filled lines based on battery percentage + filled_lines=$((ascii_height * _charge / 100)) + + # Creating the ASCii art + cat < "$config_path" - source "$config_path" - fi + if [[ -f "$config_path" ]]; then + source "$config_path" + else + # The config file doesn't exist, create it. + mkdir -p "$(dirname "$config_path")" + printf '%s\n' "$config" >"$config_path" + source "$config_path" + fi } -load_colors(){ +load_colors() { COLOR=${COLOR:-true} if [[ $COLOR = true ]]; then - BF_COL1=${BF_COL1:-2} - BF_COL2=${BF_COL2:-7} - BF_COL3=${BF_COL2:-6} + BF_COL1=${BF_COL1:-2} + BF_COL2=${BF_COL2:-7} + BF_COL3=${BF_COL2:-6} else - BF_COL1=8 - BF_COL2=8 - BF_COL3=8 + BF_COL1=8 + BF_COL2=8 + BF_COL3=8 fi } -get_json(){ +get_json() { if ! hash jq 2>/dev/null; then echo "jq package not installed, kindly install jq" else # shellcheck disable=SC2016 - JSON_FORMAT=$(jq \ - --null-input \ - --arg title "$(get_title)" \ - --arg model "$(get_model)" \ - --arg charge "$(get_charge)" \ - --arg power "$(get_power)" \ - --arg state "$(get_state)" \ - --arg health "$(get_health)" \ - '{ "title": $title, "model": $model, "charge": $charge, "power": $power, "state": $state, "health": $health }' \ + JSON_FORMAT=$( + jq \ + --null-input \ + --arg title "$(get_title)" \ + --arg model "$(get_model)" \ + --arg charge "$(get_charge)" \ + --arg power "$(get_power)" \ + --arg state "$(get_state)" \ + --arg health "$(get_health)" \ + '{ "title": $title, "model": $model, "charge": $charge, "power": $power, "state": $state, "health": $health }' ) printf '%s' "$JSON_FORMAT" fi } -main(){ - +main() { + load_config load_colors @@ -329,21 +337,21 @@ main(){ # ;; # esac # shift - # done - # Disable globbing and set the positional parameters + # done + # Disable globbing and set the positional parameters while [ "$#" -gt 0 ]; do case "$1" in - -v|--verbose) - # Hide 'stderr' if the argument is '-v' - # exec 2>/dev/null - LOG_VERBOSE=true - ;; - -j|--json) - # Execute get_json function if the argument is '-j' or '--json' - JSON_OUTPUT=true - get_json - exit # Exit after executing get_json - ;; + -v | --verbose) + # Hide 'stderr' if the argument is '-v' + # exec 2>/dev/null + LOG_VERBOSE=true + ;; + -j | --json) + # Execute get_json function if the argument is '-j' or '--json' + JSON_OUTPUT=true + get_json + exit # Exit after executing get_json + ;; esac shift done @@ -352,16 +360,16 @@ main(){ $LOG_VERBOSE || exec 2>/dev/null set -f - set +f ascii title model charge power state health + set +f ascii title model charge power state health # Iterate over the info functions to determine the lengths of the # "info names" for output alignment. The option names and subtitles # match 1:1 so this is thankfully simple for info; do - command -v "get_$info" >/dev/null || continue - # This was a ternary operation but they aren't supported in - # Minix's shell. - [ "${#info}" -gt "${info_length:-0}" ] && info_length=${#info} + command -v "get_$info" >/dev/null || continue + # This was a ternary operation but they aren't supported in + # Minix's shell. + [ "${#info}" -gt "${info_length:-0}" ] && info_length=${#info} done # for info; do # case "$info" in @@ -376,22 +384,21 @@ main(){ # esac # done - # Add an additional space of length to act as a gap. - info_length=$((info_length + 1 )) + info_length=$((info_length + 1)) # Iterate over the above list and run any existing "get_" functions. for info; do "get_$info"; done - # This was a ternary operation but they aren't supported in Minix's shell. - [ "${info_height:-0}" -lt "${ascii_height:-0}" ] && - cursor_pos=$((ascii_height - info_height - 1 )) - - # Move courcer downword after printing all the information - # to position prompt correctly - while [ "${i:=0}" -le "${cursor_pos:-0}" ]; do - printf '\n' - i=$((i + 1)) - done - } + # This was a ternary operation but they aren't supported in Minix's shell. + [ "${info_height:-0}" -lt "${ascii_height:-0}" ] && + cursor_pos=$((ascii_height - info_height - 1)) + + # Move courcer downword after printing all the information + # to position prompt correctly + while [ "${i:=0}" -le "${cursor_pos:-0}" ]; do + printf '\n' + i=$((i + 1)) + done +} main "$@"