From 4fc521650b117b09d249748dceec475d66868b1f Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Mon, 13 Apr 2020 18:16:45 +0200 Subject: [PATCH 01/16] Move printBar outside and clean up Signed-off-by: Andres Gongora --- bash-tools | 2 +- .../synth-shell-greeter.org.uma.scbi.config | 8 +- config/synth-shell-greeter.config.default | 35 ++-- setup.sh | 27 +--- synth-shell-greeter/synth-shell-greeter.sh | 153 ++++++------------ 5 files changed, 81 insertions(+), 144 deletions(-) diff --git a/bash-tools b/bash-tools index a965bca..a8d9558 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit a965bca403844ea7f1d8d8179b215a594d426b16 +Subproject commit a8d9558c4c8284769583098a12a156be9c92238c diff --git a/config/examples/synth-shell-greeter.org.uma.scbi.config b/config/examples/synth-shell-greeter.org.uma.scbi.config index a8777e7..b7f155e 100644 --- a/config/examples/synth-shell-greeter.org.uma.scbi.config +++ b/config/examples/synth-shell-greeter.org.uma.scbi.config @@ -54,8 +54,8 @@ print_info=" ##============================================================================== ## STATUS BARS ##============================================================================== -memory_units="GB" -swap_units="GB" -hdd_units="GB" -home_units="TB" +bar_memory_units="GB" +bar_swap_units="GB" +bar_hdd_units="GB" +bar_home_units="TB" diff --git a/config/synth-shell-greeter.config.default b/config/synth-shell-greeter.config.default index 6695f3f..f06413d 100644 --- a/config/synth-shell-greeter.config.default +++ b/config/synth-shell-greeter.config.default @@ -117,31 +117,37 @@ format_logo=" -c blue -e bold" ##============================================================================== ## STATUS BARS ## -## This option controls the behaviour of the visual status bars that are plotted -## for CPU, Memory, Swap and HDD usage. You can set the percentage that +## These option controls the behaviour of the visual status bars that are +## plotted for CPU, Memory, Swap and HDD usage. You can set the percentage that ## determines when the current usage is deemed critical. If said percentage ## is surpassed, the color of the bars will change and extra information ## might be plotted in addition (e.g. if the CPU usage is too high, the most ## demanding processes are printed to terminal). ##============================================================================== -crit_cpu_percent=40 -print_cpu_hogs_num=3 # Num processes to show if CPU usage is high -crit_ram_percent=75 -print_memory_hogs=3 # Num processes to show if memory usage is high -crit_swap_percent=25 -crit_hdd_percent=85 -crit_home_percent=85 +bar_cpu_crit_percent=40 +bar_ram_crit_percent=75 +bar_swap_crit_percent=25 +bar_hdd_crit_percent=85 +bar_home_crit_percent=85 -memory_units="MB" -swap_units="MB" -hdd_units="GB" -home_units="GB" +bar_ram_units="MB" +bar_swap_units="MB" +bar_hdd_units="GB" +bar_home_units="GB" + +cpu_crit_print=true +cpu_crit_print_num=3 +ram_crit_print=true +ram_crit_print_num=3 bar_length=9 # Number of characters that comprise a bar bar_num_digits=5 # Control num digits next to bar info_label_width=16 # Desired length of the info labels - +bar_bracket_char_left='[' +bar_bracket_char_right=']' +bar_fill_char='|' +bar_empty_char=' ' ##============================================================================== @@ -158,3 +164,4 @@ print_extra_new_line_top=true # Extra line before logo and info print_extra_new_line_bot=true # Extra line after logo and info + diff --git a/setup.sh b/setup.sh index 8aea7bd..a075cf1 100755 --- a/setup.sh +++ b/setup.sh @@ -63,28 +63,6 @@ setup() ## HEADER TO BE ADDED AT THE TOP OF THE ASSEMBLED SCRIPT local output_script_header=$(printf '%s'\ - "##!/bin/bash\n"\ - "\n"\ - "## +-----------------------------------+-----------------------------------+\n"\ - "## | |\n"\ - "## | Copyright (c) 2014-2020, Andres Gongora |\n"\ - "## | https://github.com/andresgongora/synth-shell-greeter |\n"\ - "## | Visit the above URL for details of license and authorship. |\n"\ - "## | |\n"\ - "## | This program is free software: you can redistribute it and/or modify |\n"\ - "## | it under the terms of the GNU General Public License as published by |\n"\ - "## | the Free Software Foundation, either version 3 of the License, or |\n"\ - "## | (at your option) any later version. |\n"\ - "## | |\n"\ - "## | This program is distributed in the hope that it will be useful, |\n"\ - "## | but WITHOUT ANY WARRANTY; without even the implied warranty of |\n"\ - "## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |\n"\ - "## | GNU General Public License for more details. |\n"\ - "## | |\n"\ - "## | You should have received a copy of the GNU General Public License |\n"\ - "## | along with this program. If not, see . |\n"\ - "## | |\n"\ - "## +-----------------------------------------------------------------------+\n"\ "##\n"\ "##\n"\ "## =======================\n"\ @@ -93,11 +71,12 @@ setup() "## =======================\n"\ "##\n"\ "## This file was generated by an installation script.\n"\ - "## If you edit this file, it might be overwritten without warning\n"\ - "## and you might lose all your changes.\n"\ + "## It might be overwritten without warning at any time\n"\ + "## and you will lose all your changes.\n"\ "##\n"\ "## Visit for instructions and more information:\n"\ "## https://github.com/andresgongora/synth-shell/\n"\ + "##\n"\ "##\n\n\n") diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 40b6558..f95ef76 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -36,6 +36,7 @@ greeter() include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include '../bash-tools/bash-tools/color.sh' include '../bash-tools/bash-tools/print_utils.sh' +include '../bash-tools/bash-tools/print_bar.sh' include '../config/synth-shell-greeter.config.default' include 'info_os.sh' include 'info_hardware.sh' @@ -49,12 +50,12 @@ include 'info_network.sh' ##------------------------------------------------------------------------------ ## -## printInfo(LABEL, VALUE) +## printInfoLine(LABEL, VALUE) ## Print a formatted message comprised of a label and a value ## 1. LABEL will be printed with info color ## 2. VALUE will be printed with highlight color ## -printInfo() +printInfoLine() { label=$1 value=$2 @@ -65,55 +66,8 @@ printInfo() -##------------------------------------------------------------------------------ -## -## printBar(CURRENT, MAX, SIZE, COLOR, COLOR) -## -## Prints a bar that is filled depending on the relation between -## CURRENT and MAX -## -## 1. CURRENT: amount to display on the bar. -## 2. MAX: amount that means that the bar should be printed -## completely full. -## 3. SIZE: length of the bar as number of characters. -## 4. BRACKET_COLOR: Color for the brackets. May be empty for not colored. -## 5. BAR_COLOR: Color for the bars. May be empty for not colored. -## -printBar() -{ - ## VARIABLES - local current=$1 - local max=$2 - local size=$3 - local bracket_color=$4 - local bar_color=$5 - - - ## COMPUTE VARIABLES - local num_bars=$(bc <<< "$size * $current / $max") - if [ $num_bars -gt $size ]; then - num_bars=$size - fi - ## PRINT BAR - ## - Opening bracket - ## - Full bars - ## - Remaining empty space - ## - Closing bracket - printf "${bracket_color}[${bar_color}" - i=0 - while [ $i -lt $num_bars ]; do - printf "|" - i=$[$i+1] - done - while [ $i -lt $size ]; do - printf " " - i=$[$i+1] - done - printf "${bracket_color}]${fc_none}" -} - ##------------------------------------------------------------------------------ @@ -224,7 +178,7 @@ printMonitor() ## PRINT BAR printf "${fc_info}%-${pad}s" "$label" - printBar $current $max $bar_length $fc_bar_1 $fc_bar_2 + printBar $current $max $bar_length $fc_bar_1 $fc_bar_2 $bar_bracket_char_left $bar_bracket_char_right $bar_fill_char $bar_empty_char ## PRINT NUMERIC VALUE @@ -245,23 +199,23 @@ printMonitor() ## INFO ##============================================================================== -printInfoOS() { printInfo "OS" "$(getNameOS)" ; } -printInfoKernel() { printInfo "Kernel" "$(getNameKernel)" ; } -printInfoShell() { printInfo "Shell" "$(getNameShell)" ; } -printInfoDate() { printInfo "Date" "$(getDate)" ; } -printInfoUptime() { printInfo "Uptime" "$(getUptime)" ; } -printInfoUser() { printInfo "User" "$(getUserHost)" ; } -printInfoNumLoggedIn() { printInfo "Logged in" "$(getNumberLoggedInUsers)" ; } -printInfoNameLoggedIn() { printInfo "Logged in" "$(getNameLoggedInUsers)" ; } +printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } +printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } +printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } +printInfoDate() { printInfoLine "Date" "$(getDate)" ; } +printInfoUptime() { printInfoLine "Uptime" "$(getUptime)" ; } +printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } +printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } +printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } -printInfoCPU() { printInfo "CPU" "$(getNameCPU)" ; } -printInfoGPU() { printInfo "GPU" "$(getNameGPU)" ; } -printInfoCPULoad() { printInfo "Sys load" "$(getCPULoad)" ; } +printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } +printInfoGPU() { printInfoLine "GPU" "$(getNameGPU)" ; } +printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } -printInfoLocalIPv4() { printInfo "Local IPv4" "$(getLocalIPv4)" ; } -printInfoExternalIPv4() { printInfo "External IPv4" "$(getExternalIPv4)" ; } +printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } +printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } -printInfoSpacer() { printInfo "" "" ; } +printInfoSpacer() { printInfoLine "" "" ; } @@ -288,7 +242,7 @@ printInfoSystemctl() local sysctl="${fc_error}$systcl_num_failed services failed!${fc_none}" fi - printInfo "Services" "$sysctl" + printInfoLine "Services" "$sysctl" } @@ -309,7 +263,7 @@ printInfoColorpaletteSmall() "$(formatText "$char" -c cyan -b light-cyan)"\ "$(formatText "$char" -c light-gray -b white)") - printInfo "Color palette" "$palette" + printInfoLine "Color palette" "$palette" } @@ -338,8 +292,8 @@ printInfoColorpaletteFancy() "$(formatText "██" -c light-cyan)$(formatText "▀" -c cyan) "\ "$(formatText "██" -c white)$(formatText "▀" -c light-gray) ") - printInfo "Color palette" "$palette_top" - printInfo "" "$palette_bot" + printInfoLine "Color palette" "$palette_top" + printInfoLine "" "$palette_bot" } @@ -376,9 +330,9 @@ printInfoCPUTemp() ## PRINT MESSAGE - printInfo "CPU temp" "$temp" + printInfoLine "CPU temp" "$temp" else - printInfo "CPU temp" "lm-sensors not installed" + printInfoLine "CPU temp" "lm-sensors not installed" fi @@ -400,7 +354,7 @@ printMonitorCPU() if [ -z "$as_percentage" ]; then local as_percentage=false; fi - printMonitor $current $max $crit_cpu_percent \ + printMonitor $current $max $bar_cpu_crit_percent \ $as_percentage $units $message } @@ -411,7 +365,7 @@ printMonitorCPU() printMonitorRAM() { ## CHOOSE UNITS - case "$memory_units" in + case "$bar_ram_units" in "MB") local units="MB"; local option="--mega" ;; "TB") local units="TB"; local option="--tera" ;; "PB") local units="PB"; local option="--peta" ;; @@ -429,7 +383,7 @@ printMonitorRAM() if [ -z "$as_percentage" ]; then local as_percentage=false; fi - printMonitor $current $max $crit_ram_percent \ + printMonitor $current $max $bar_ram_crit_percent \ $as_percentage $units $message } @@ -440,7 +394,7 @@ printMonitorRAM() printMonitorSwap() { ## CHOOSE UNITS - case "$swap_units" in + case "$bar_swap_units" in "MB") local units="MB"; local option="--mebi" ;; "TB") local units="TB"; local option="--tebi" ;; "PB") local units="PB"; local option="--pebi" ;; @@ -471,7 +425,7 @@ printMonitorSwap() local max=$(echo "$swap_info" |\ awk '{SWAP=($2)} END {printf SWAP}') - printMonitor $current $max $crit_swap_percent \ + printMonitor $current $max $bar_swap_crit_percent \ $as_percentage $units $message fi } @@ -487,7 +441,7 @@ printMonitorHDD() ## CHOOSE UNITS - case "$hdd_units" in + case "$bar_hdd_units" in "MB") local units="MB"; local option="M" ;; "TB") local units="TB"; local option="T" ;; "PB") local units="PB"; local option="P" ;; @@ -501,7 +455,7 @@ printMonitorHDD() local max=$(df "-B1${option}" / | grep "/" | awk '{key=($2)} END {printf key}') - printMonitor $current $max $crit_hdd_percent \ + printMonitor $current $max $bar_hdd_crit_percent \ $as_percentage $units $message } @@ -516,7 +470,7 @@ printMonitorHome() ## CHOOSE UNITS - case "$home_units" in + case "$bar_home_units" in "MB") local units="MB"; local option="M" ;; "TB") local units="TB"; local option="T" ;; "PB") local units="PB"; local option="P" ;; @@ -529,7 +483,7 @@ printMonitorHome() local max=$(df "-B1${option}" ~ | grep "/" | awk '{key=($2)} END {printf key}') - printMonitor $current $max $crit_home_percent \ + printMonitor $current $max $bar_home_crit_percent \ $as_percentage $units $message } @@ -561,7 +515,7 @@ printMonitorCPUTemp() printMonitor $current $max $crit_percent \ false $units "CPU temp" else - printInfo "CPU temp" "lm-sensors not installed" + printInfoLine "CPU temp" "lm-sensors not installed" fi } @@ -618,7 +572,7 @@ printStatusInfo() HDDHOME_MON%) printMonitorHome true;; CPUTEMP_MON) printMonitorCPUTemp;; - *) printInfo "Unknown" "Check your config";; + *) printInfoLine "Unknown" "Check your config";; esac } @@ -718,21 +672,15 @@ printHogsCPU() { export LC_NUMERIC="C" - ## CHECK GLOBAL PARAMETERS - if [ -z $crit_cpu_percent ]; then return ; fi - if [ -z $print_cpu_hogs_num ]; then local print_cpu_hogs_num=3 ; fi - if [ -z $print_cpu_hogs ]; then return ; fi - - ## EXIT IF NOT ENABLED - if [ "$print_cpu_hogs"==true] ; then + if [ "$cpu_crit_print"==true ]; then ## CHECK CPU LOAD local current=$(awk '{avg_1m=($1)} END {printf "%3.2f", avg_1m}' /proc/loadavg) local max=$(nproc --all) local percent=$(bc <<< "$current*100/$max") - if [ $percent -gt $crit_cpu_percent ]; then + if [ $percent -gt $bar_cpu_crit_percent ]; then ## CALL TOP IN BATCH MODE ## Check if "%Cpus(s)" is shown, otherwise, call "top -1" ## Escape all '%' characters @@ -753,13 +701,13 @@ printHogsCPU() local header=$(echo "$top" | grep "%CPU" ) local procs=$(echo "$top" |\ sed '/top - /,/%CPU/d' |\ - head -n "$print_cpu_hogs_num" ) + head -n "$cpu_crit_print_num" ) ## PRINT WITH FORMAT printf "\n${fc_crit}SYSTEM LOAD:${fc_info} ${load}\n" printf "${fc_crit}$header${fc_none}\n" - printf "${fc_info}${procs}${fc_none}\n" + printf "${fc_text}${procs}${fc_none}\n" fi fi } @@ -770,21 +718,15 @@ printHogsCPU() ## printHogsMemory() { - ## CHECK GLOBAL PARAMETERS - if [ -z $crit_ram_percent ]; then return; fi - if [ -z $crit_swap_percent ]; then return; fi - if [ -z $print_memory_hogs ]; then local print_memory_hogs=3 ; fi - - ## EXIT IF NOT ENABLED - if [ "$print_memory_hogs"==true ]; then + if [ "$ram_crit_print"==true ]; then ## CHECK RAM local ram_is_crit=false local mem_info=$('free' -m | head -n 2 | tail -n 1) local current=$(echo "$mem_info" | awk '{mem=($2-$7)} END {printf mem}') local max=$(echo "$mem_info" | awk '{mem=($2)} END {printf mem}') local percent=$(bc <<< "$current*100/$max") - if [ $percent -gt $crit_ram_percent ]; then + if [ $percent -gt $bar_ram_crit_percent ]; then local ram_is_crit=true fi @@ -799,7 +741,7 @@ printHogsMemory() local current=$(echo "$swap_info" | awk '{SWAP=($3)} END {printf SWAP}') local max=$(echo "$swap_info" | awk '{SWAP=($2)} END {printf SWAP}') local percent=$(bc <<< "$current*100/$max") - if [ $percent -gt $crit_swap_percent ]; then + if [ $percent -gt $bar_swap_crit_percent ]; then local swap_is_crit=true fi fi @@ -808,7 +750,8 @@ printHogsMemory() if $ram_is_crit || $swap_is_crit ; then local available=$(echo $mem_info | awk '{print $NF}') local procs=$(ps --cols=80 -eo pmem,size,pid,cmd --sort=-%mem |\ - head -n 4 | tail -n 3 |\ + head -n $(($ram_crit_print_num + 1)) |\ + tail -n $ram_crit_print_num |\ awk '{$2=int($2/1024)"MB";} {printf("%5s%8s%8s\t%s\n", $1, $2, $3, $4)}') @@ -858,6 +801,14 @@ local fc_error=$(getFormatCode $format_error) local fc_logo=$(getFormatCode $format_logo) local fc_none=$(getFormatCode -e reset) +#fc_logo +#fc_ok +#fc_crit +#fc_error +#fc_none +local fc_label="$fc_info" +local fc_text="$fc_highlight" + ## PRINT TOP SPACER From 9f4dd929acfb1521f2a1abe7b534097abb079dbe Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Mon, 13 Apr 2020 18:27:14 +0200 Subject: [PATCH 02/16] Remove color dependency from printBar Signed-off-by: Andres Gongora --- bash-tools | 2 +- synth-shell-greeter/synth-shell-greeter.sh | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/bash-tools b/bash-tools index a8d9558..c25f70f 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit a8d9558c4c8284769583098a12a156be9c92238c +Subproject commit c25f70fcd3289480caf516d9589aa6160cd403be diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index f95ef76..4b95cee 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -178,7 +178,11 @@ printMonitor() ## PRINT BAR printf "${fc_info}%-${pad}s" "$label" - printBar $current $max $bar_length $fc_bar_1 $fc_bar_2 $bar_bracket_char_left $bar_bracket_char_right $bar_fill_char $bar_empty_char + printBar "$current" "$max" "$bar_length" \ + "$fc_bar_1$bar_bracket_char_left" \ + "$fc_bar_2$bar_fill_char" \ + "$fc_bar_2$bar_empty_char" \ + "$fc_bar_1$bar_bracket_char_right$fc_none" ## PRINT NUMERIC VALUE From 4ed6e188aa138513f65e736df09e007931b9ff66 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Mon, 13 Apr 2020 21:45:13 +0200 Subject: [PATCH 03/16] Split resource and info printing functions Signed-off-by: Andres Gongora --- config/synth-shell-greeter.config.default | 2 +- synth-shell-greeter/synth-shell-greeter.sh | 163 ++++++++++++++++----- 2 files changed, 126 insertions(+), 39 deletions(-) diff --git a/config/synth-shell-greeter.config.default b/config/synth-shell-greeter.config.default index f06413d..8c66712 100644 --- a/config/synth-shell-greeter.config.default +++ b/config/synth-shell-greeter.config.default @@ -147,7 +147,7 @@ info_label_width=16 # Desired length of the info labels bar_bracket_char_left='[' bar_bracket_char_right=']' bar_fill_char='|' -bar_empty_char=' ' +bar_background_char=' ' ##============================================================================== diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 4b95cee..8447222 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -36,32 +36,74 @@ greeter() include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include '../bash-tools/bash-tools/color.sh' include '../bash-tools/bash-tools/print_utils.sh' -include '../bash-tools/bash-tools/print_bar.sh' -include '../config/synth-shell-greeter.config.default' -include 'info_os.sh' -include 'info_hardware.sh' -include 'info_network.sh' +####################################################################### +assert_equal() +{ + E_PARAM_ERR=98 + E_ASSERT_FAILED=99 + + + + if [ -z "$1" ]; then + echo "Assert called in $0 with empty argument" + exit $E_PARAM_ERR + fi + + + if [ ! $1 ]; then + echo "Assertion failed in $0: \"$1\" . $2" + exit $E_ASSERT_FAILED + fi +} +####################################################################### + + ##============================================================================== ## INFO AND MONITOR PRINTING HELPERS ##============================================================================== -##------------------------------------------------------------------------------ -## -## printInfoLine(LABEL, VALUE) + + +_getStateColor() +{ + local state=$1 + local E_PARAM_ERR=98 + + case $state in + nominal) echo $fc_ok ;; + critical) echo $fc_crit ;; + error) echo $fc_error ;; + *) echo "$state not valid" ; exit $E_ASSERT_FAILED + esac + +} + + +##============================================================================== +## printInfoLine() ## Print a formatted message comprised of a label and a value -## 1. LABEL will be printed with info color -## 2. VALUE will be printed with highlight color +## +## Arguments: +## 1. LABEL +## 2. VALUE +## +## Optional arguments: +## 3. STATE Determines the color (nominal/critical/error) ## printInfoLine() { - label=$1 - value=$2 - pad=$info_label_width + local label=$1 + local value=$2 + local state=${3:-nominal} - printf "${fc_info}%-${pad}s${fc_highlight}${value}${fc_none}\n" "$label" + local fc_label=${fc_info} + local fc_value=$(_getStateColor $state) + local pad=$info_label_width + + printf "${fc_label}%-${pad}s${fc_value}${value}${fc_none}\n" "$label" } @@ -70,12 +112,12 @@ printInfoLine() -##------------------------------------------------------------------------------ -## -## printFraction(NUMERATOR, DENOMINATOR, PADDING_DIGITS, UNITS) +##============================================================================== +## printFraction() ## ## Prints a color-formatted fraction with padding to reach MAX_DIGITS. ## +## Arguments: ## 1. NUMERATOR: first shown number ## 2. DENOMINATOR: second shown number ## 3. PADDING_DIGITS: determines the minimum length of NUMERATOR and @@ -84,7 +126,9 @@ printInfoLine() ## 4. UNITS: a string that is attached to the end of the fraction, ## meant to include optional units (e.g. MB) for display purposes. ## If "none", no units are displayed. -## 5,6,7. COLORS: of decoration (/), numbers, and units. +## +## Optional arguments: +## 5. STATE Determines the color (nominal/critical/error) ## printFraction() { @@ -92,9 +136,11 @@ printFraction() local b=$2 local padding=$3 local units=$4 - local deco_color=$5 - local num_color=$6 - local units_color=$7 + local state=${5:-nominal} + + local deco_color=$fc_info + local num_color=$(_getStateColor $state) + local units_color=$num_color if [ $units == "none" ]; then local units=""; fi @@ -106,8 +152,41 @@ printFraction() -##------------------------------------------------------------------------------ -## + +include '../bash-tools/bash-tools/print_bar.sh' +printResourceBar() +{ + local label=$1 + local current=$2 + local max=$3 + local bar_length=$4 + local state=${5:-nominal} + + + ## CHOOSE COLORS AND PADDING + local fc_label=${fc_info} + local pad=$info_label_width + local fc_fill_color=$(_getStateColor $state) + local fc_bracket_color=$fc_deco + + + ## COMPOSE CHARACTERS FOR BAR + local bracket_left=$fc_bracket_color$bar_bracket_char_left + local fill=$fc_fill_color$bar_fill_char + local background=$fc_none$bar_background_char + local bracket_right=$fc_bracket_color$bar_bracket_char_right$fc_none + + + ## PRINT LABEL AND BAR + printf "${fc_label}%-${pad}s" "$label" + printBar "$current" "$max" "$bar_length" \ + "$bracket_left" "$fill" "$background" "$bracket_right" +} + + + + +##============================================================================== ## printMonitor() ## ## Prints a resource utilization monitor, comprised of a bar and a fraction. @@ -167,31 +246,27 @@ printMonitor() local fc_txt_1=$fc_info local fc_txt_2=$fc_ok local fc_txt_3=$fc_ok + local state="nominal" if [ $percent -gt 99 ]; then local fc_bar_2=$fc_error local fc_txt_2=$fc_crit + local state="error" elif [ $percent -gt $crit_percent ]; then local fc_bar_2=$fc_crit local fc_txt_2=$fc_crit + local state="critical" fi ## PRINT BAR - printf "${fc_info}%-${pad}s" "$label" - printBar "$current" "$max" "$bar_length" \ - "$fc_bar_1$bar_bracket_char_left" \ - "$fc_bar_2$bar_fill_char" \ - "$fc_bar_2$bar_empty_char" \ - "$fc_bar_1$bar_bracket_char_right$fc_none" - + printResourceBar "$label" "$current" "$max" "$bar_length" "$state" ## PRINT NUMERIC VALUE if $print_as_percentage; then printf "${fc_txt_2}%${bar_num_digits}s${fc_txt_1} %%%%${fc_none}" $percent else printf " " - printFraction $current $max $bar_num_digits $units \ - $fc_txt_1 $fc_txt_2 $fc_txt_3 + printFraction "$current" "$max" "$bar_num_digits" "$units" "$state" fi } @@ -203,6 +278,7 @@ printMonitor() ## INFO ##============================================================================== +include 'info_os.sh' printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } @@ -212,10 +288,12 @@ printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } +include 'info_hardware.sh' printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } printInfoGPU() { printInfoLine "GPU" "$(getNameGPU)" ; } printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } +include 'info_network.sh' printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } @@ -241,9 +319,9 @@ printInfoSystemctl() if [ "$systcl_num_failed" -eq "0" ]; then local sysctl="All services OK" elif [ "$systcl_num_failed" -eq "1" ]; then - local sysctl="${fc_error}1 service failed!${fc_none}" + local sysctl="${fc_error}1 service failed!" else - local sysctl="${fc_error}$systcl_num_failed services failed!${fc_none}" + local sysctl="${fc_error}$systcl_num_failed services failed!" fi printInfoLine "Services" "$sysctl" @@ -781,13 +859,22 @@ printHogsMemory() -## LOAD USER CONFIGURATION -local target_config_file=$1 -local user_config_file="$HOME/.config/synth-shell/synth-shell-greeter.config" +## LOAD CONFIGURATION +## Load default configuration file with all arguments, then try to load any of +## following in order, until first match, to override some or all config params. +## 1. Apply specific configuration file if specified as argument. +## 2. User specific configuration if in user's home folder. +## 3. If root, apply root configuration file if it exists in the system. +## 4. System wide configuration file if it exists. +## 5. Fall back to defaults. +## +include '../config/synth-shell-greeter.config.default' +local target_config_file="$1" # can be empty +local user_config_file="~/.config/synth-shell/synth-shell-greeter.config" local root_config_file="/etc/synth-shell/os/synth-shell-greeter.root.config" local sys_config_file="/etc/synth-shell/synth-shell-greeter.config" if [ -f "$target_config_file" ]; then source "$target_config_file" ; -elif [ -f "$user_config_file" ]; then source "$user_config_file" ; +elif [ -f "$user_config_file" ]; then source "$user_config_file" ; elif [ -f $root_config_file -a "$USER" == "root" ]; then source "$root_config_file" ; elif [ -f "$sys_config_file" ]; then source "$sys_config_file" ; else : # Default config already "included" ; From 5785e30db00938b9448eab5ee1f43e75e632b4bf Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Tue, 14 Apr 2020 00:34:16 +0200 Subject: [PATCH 04/16] Move more functions outside the main script Signed-off-by: Andres Gongora --- bash-tools | 2 +- setup.sh | 2 + synth-shell-greeter/print_info.sh | 285 ++++++++++++++++++ synth-shell-greeter/synth-shell-greeter.sh | 319 +++------------------ 4 files changed, 333 insertions(+), 275 deletions(-) create mode 100755 synth-shell-greeter/print_info.sh diff --git a/bash-tools b/bash-tools index c25f70f..7f8e324 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit c25f70fcd3289480caf516d9589aa6160cd403be +Subproject commit 7f8e324a31c1a74314a45df6e57517101f5239a4 diff --git a/setup.sh b/setup.sh index a075cf1..58de1b6 100755 --- a/setup.sh +++ b/setup.sh @@ -32,7 +32,9 @@ setup() { include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include 'bash-tools/bash-tools/user_io.sh' + include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include 'bash-tools/bash-tools/hook_script.sh' + include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include 'bash-tools/bash-tools/assemble_script.sh' diff --git a/synth-shell-greeter/print_info.sh b/synth-shell-greeter/print_info.sh new file mode 100755 index 0000000..d60ffbf --- /dev/null +++ b/synth-shell-greeter/print_info.sh @@ -0,0 +1,285 @@ +#!/bin/bash + +## +-----------------------------------+-----------------------------------+ +## | | +## | Copyright (c) 2019-2020, Andres Gongora . | +## | | +## | This program is free software: you can redistribute it and/or modify | +## | it under the terms of the GNU General Public License as published by | +## | the Free Software Foundation, either version 3 of the License, or | +## | (at your option) any later version. | +## | | +## | This program is distributed in the hope that it will be useful, | +## | but WITHOUT ANY WARRANTY; without even the implied warranty of | +## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +## | GNU General Public License for more details. | +## | | +## | You should have received a copy of the GNU General Public License | +## | along with this program. If not, see . | +## | | +## +-----------------------------------------------------------------------+ + + +## +## DESCRIPTION: +## +## +## + + + +##============================================================================== +## DEPENDENCIES +##============================================================================== + +include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } +include '../bash-tools/bash-tools/print_bar.sh' +include '../bash-tools/bash-tools/assert.sh' + + +## CHECK EXTERNAL VARIABLES +assert_is_set $fc_ok +assert_is_set $fc_info +assert_is_set $fc_deco +assert_is_set $fc_crit +assert_is_set $fc_error +assert_is_set $fc_none +assert_not_empty $bar_num_digits + + + + + + +##============================================================================== +## HELPERS +##============================================================================== + + + +##============================================================================== +## _getStateColor() +## Select color formating code according to state: +## nominal/critical/error +## +_getStateColor() +{ + local state=$1 + local E_PARAM_ERR=98 + + case $state in + nominal) echo $fc_ok ;; + critical) echo $fc_crit ;; + error) echo $fc_error ;; + *) echo "$state not valid" ; exit $E_PARAM_ERR + esac + +} + + + + + + + +##============================================================================== +## printFraction() +## +## Prints a color-formatted fraction with padding to reach MAX_DIGITS. +## +## Arguments: +## 1. NUMERATOR: first shown number +## 2. DENOMINATOR: second shown number +## 3. PADDING_DIGITS: determines the minimum length of NUMERATOR and +## DENOMINATOR. If they have less digits than this, +## then extra spaces are appended for padding. +## 4. UNITS: a string that is attached to the end of the fraction, +## meant to include optional units (e.g. MB) for display purposes. +## If "none", no units are displayed. +## +## Optional arguments: +## 5. STATE Determines the color (nominal/critical/error) +## +_printFraction() +{ + local a=$1 + local b=$2 + local padding=$3 + local units=$4 + local state=${5:-nominal} + + local deco_color=$fc_info + local num_color=$(_getStateColor $state) + local units_color=$num_color + + if [ $units == "none" ]; then local units=""; fi + + printf "${num_color}%${padding}s" $a + printf "${deco_color}/" + printf "${num_color}%-${padding}s" $b + printf "${units_color} ${units}${fc_none}" +} + + + + + + +##============================================================================== +## _printResourceBar() +## +## +_printResourceBar() +{ + local label=$1 + local current=$2 + local max=$3 + local bar_length=$4 + local state=${5:-nominal} + + + ## CHOOSE COLORS AND PADDING + local fc_label=${fc_info} + local pad=$info_label_width + local fc_fill_color=$(_getStateColor $state) + local fc_bracket_color=$fc_deco + + + ## COMPOSE CHARACTERS FOR BAR + local bracket_left=$fc_bracket_color$bar_bracket_char_left + local fill=$fc_fill_color$bar_fill_char + local background=$fc_none$bar_background_char + local bracket_right=$fc_bracket_color$bar_bracket_char_right$fc_none + + + ## PRINT LABEL AND BAR + printf "${fc_label}%-${pad}s" "$label" + printBar "$current" "$max" "$bar_length" \ + "$bracket_left" "$fill" "$background" "$bracket_right" +} + + + + + +##============================================================================== +## FUNCTIONS +##============================================================================== + + + +##============================================================================== +## printInfoLine() +## Print a formatted message comprised of a label and a value +## +## Arguments: +## 1. LABEL +## 2. VALUE +## +## Optional arguments: +## 3. STATE Determines the color (nominal/critical/error) +## +printInfoLine() +{ + local label=$1 + local value=$2 + local state=${3:-nominal} + + local fc_label=${fc_info} + local fc_value=$(_getStateColor $state) + local pad=$info_label_width + + printf "${fc_label}%-${pad}s${fc_value}${value}${fc_none}\n" "$label" +} + + + + + + +##============================================================================== +## printMonitor() +## +## Prints a resource utilization monitor, comprised of a bar and a fraction. +## +## 1. CURRENT: current resource utilization (e.g. occupied GB in HDD) +## 2. MAX: max resource utilization (e.g. HDD size) +## 3. CRIT_PERCENT: point at which to warn the user (e.g. 80 for 80%) +## 4. PRINT_AS_PERCENTAGE: whether to print a simple percentage after +## the utilization bar (true), or to print a fraction (false). +## 5. UNITS: units of the resource, for display purposes only. This are +## not shown if PRINT_AS_PERCENTAGE=true, but must be set nonetheless. +## 6. LABEL: A description of the resource that will be printed in front +## of the utilization bar. +## +printResourceMonitor() +{ + ## CHECK EXTERNAL CONFIGURATION + if [ -z $bar_num_digits ]; then exit 1; fi + if [ -z $fc_deco ]; then exit 1; fi + if [ -z $fc_ok ]; then exit 1; fi + if [ -z $fc_info ]; then exit 1; fi + if [ -z $fc_crit ]; then exit 1; fi + + + ## VARIABLES + local current=$1 + local max=$2 + local crit_percent=$3 + local print_as_percentage=$4 + local units=$5 + local label=${@:6} + local pad=$info_label_width + + + ## CHECK VARIABLES + ## If max is empty, assign 0 + ## If crit percent is empty, assign 100 + ## If crit_percent > 100, assign 100 + if [ -z $max ]; then local max=0; fi + if [ -z $crit_percent ]; then local local crit_percent=100; fi + if [ "$crit_percent" -gt 100 ]; then local crit_percent=100; fi + + + ## COMPUTE PERCENT + ## If max=0, then avoid division + ## Otherwise compute as usual + if [ "$max" -eq 0 ]; then + local percent=100 + else + local percent=$(bc <<< "$current*100/$max") + fi + + + ## SET COLORS DEPENDING ON LOAD + local fc_bar_1=$fc_deco + local fc_bar_2=$fc_ok + local fc_txt_1=$fc_info + local fc_txt_2=$fc_ok + local fc_txt_3=$fc_ok + local state="nominal" + if [ $percent -gt 99 ]; then + local fc_bar_2=$fc_error + local fc_txt_2=$fc_crit + local state="error" + elif [ $percent -gt $crit_percent ]; then + local fc_bar_2=$fc_crit + local fc_txt_2=$fc_crit + local state="critical" + fi + + + ## PRINT BAR + _printResourceBar "$label" "$current" "$max" "$bar_length" "$state" + + ## PRINT NUMERIC VALUE + if $print_as_percentage; then + printf "${fc_txt_2}%${bar_num_digits}s${fc_txt_1} %%%%${fc_none}" $percent + else + printf " " + _printFraction "$current" "$max" "$bar_num_digits" "$units" "$state" + fi +} + + + diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 8447222..2e5510c 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -39,236 +39,55 @@ include '../bash-tools/bash-tools/print_utils.sh' -####################################################################### -assert_equal() -{ - E_PARAM_ERR=98 - E_ASSERT_FAILED=99 - - - - if [ -z "$1" ]; then - echo "Assert called in $0 with empty argument" - exit $E_PARAM_ERR - fi - - - if [ ! $1 ]; then - echo "Assertion failed in $0: \"$1\" . $2" - exit $E_ASSERT_FAILED - fi -} -####################################################################### - ##============================================================================== -## INFO AND MONITOR PRINTING HELPERS +## CONFIGURATION ##============================================================================== - -_getStateColor() -{ - local state=$1 - local E_PARAM_ERR=98 - - case $state in - nominal) echo $fc_ok ;; - critical) echo $fc_crit ;; - error) echo $fc_error ;; - *) echo "$state not valid" ; exit $E_ASSERT_FAILED - esac - -} - - -##============================================================================== -## printInfoLine() -## Print a formatted message comprised of a label and a value -## -## Arguments: -## 1. LABEL -## 2. VALUE -## -## Optional arguments: -## 3. STATE Determines the color (nominal/critical/error) -## -printInfoLine() -{ - local label=$1 - local value=$2 - local state=${3:-nominal} - - local fc_label=${fc_info} - local fc_value=$(_getStateColor $state) - local pad=$info_label_width - - printf "${fc_label}%-${pad}s${fc_value}${value}${fc_none}\n" "$label" -} - - - - - - - -##============================================================================== -## printFraction() -## -## Prints a color-formatted fraction with padding to reach MAX_DIGITS. -## -## Arguments: -## 1. NUMERATOR: first shown number -## 2. DENOMINATOR: second shown number -## 3. PADDING_DIGITS: determines the minimum length of NUMERATOR and -## DENOMINATOR. If they have less digits than this, -## then extra spaces are appended for padding. -## 4. UNITS: a string that is attached to the end of the fraction, -## meant to include optional units (e.g. MB) for display purposes. -## If "none", no units are displayed. -## -## Optional arguments: -## 5. STATE Determines the color (nominal/critical/error) +## LOAD CONFIGURATION +## Load default configuration file with all arguments, then try to load any of +## following in order, until first match, to override some or all config params. +## 1. Apply specific configuration file if specified as argument. +## 2. User specific configuration if in user's home folder. +## 3. If root, apply root configuration file if it exists in the system. +## 4. System wide configuration file if it exists. +## 5. Fall back to defaults. ## -printFraction() -{ - local a=$1 - local b=$2 - local padding=$3 - local units=$4 - local state=${5:-nominal} - - local deco_color=$fc_info - local num_color=$(_getStateColor $state) - local units_color=$num_color - - if [ $units == "none" ]; then local units=""; fi - - printf "${num_color}%${padding}s" $a - printf "${deco_color}/" - printf "${num_color}%-${padding}s" $b - printf "${units_color} ${units}${fc_none}" -} - - - - -include '../bash-tools/bash-tools/print_bar.sh' -printResourceBar() -{ - local label=$1 - local current=$2 - local max=$3 - local bar_length=$4 - local state=${5:-nominal} - - - ## CHOOSE COLORS AND PADDING - local fc_label=${fc_info} - local pad=$info_label_width - local fc_fill_color=$(_getStateColor $state) - local fc_bracket_color=$fc_deco - - - ## COMPOSE CHARACTERS FOR BAR - local bracket_left=$fc_bracket_color$bar_bracket_char_left - local fill=$fc_fill_color$bar_fill_char - local background=$fc_none$bar_background_char - local bracket_right=$fc_bracket_color$bar_bracket_char_right$fc_none - - - ## PRINT LABEL AND BAR - printf "${fc_label}%-${pad}s" "$label" - printBar "$current" "$max" "$bar_length" \ - "$bracket_left" "$fill" "$background" "$bracket_right" -} - - +include '../config/synth-shell-greeter.config.default' +local target_config_file="$1" +local user_config_file="~/.config/synth-shell/synth-shell-greeter.config" +local root_config_file="/etc/synth-shell/os/synth-shell-greeter.root.config" +local sys_config_file="/etc/synth-shell/synth-shell-greeter.config" -##============================================================================== -## printMonitor() -## -## Prints a resource utilization monitor, comprised of a bar and a fraction. -## -## 1. CURRENT: current resource utilization (e.g. occupied GB in HDD) -## 2. MAX: max resource utilization (e.g. HDD size) -## 3. CRIT_PERCENT: point at which to warn the user (e.g. 80 for 80%) -## 4. PRINT_AS_PERCENTAGE: whether to print a simple percentage after -## the utilization bar (true), or to print a fraction (false). -## 5. UNITS: units of the resource, for display purposes only. This are -## not shown if PRINT_AS_PERCENTAGE=true, but must be set nonetheless. -## 6. LABEL: A description of the resource that will be printed in front -## of the utilization bar. -## -printMonitor() -{ - ## CHECK EXTERNAL CONFIGURATION - if [ -z $bar_num_digits ]; then exit 1; fi - if [ -z $fc_deco ]; then exit 1; fi - if [ -z $fc_ok ]; then exit 1; fi - if [ -z $fc_info ]; then exit 1; fi - if [ -z $fc_crit ]; then exit 1; fi - - - ## VARIABLES - local current=$1 - local max=$2 - local crit_percent=$3 - local print_as_percentage=$4 - local units=$5 - local label=${@:6} - local pad=$info_label_width - - - ## CHECK VARIABLES - ## If max is empty, assign 0 - ## If crit percent is empty, assign 100 - ## If crit_percent > 100, assign 100 - if [ -z $max ]; then local max=0; fi - if [ -z $crit_percent ]; then local local crit_percent=100; fi - if [ "$crit_percent" -gt 100 ]; then local crit_percent=100; fi - - - ## COMPUTE PERCENT - ## If max=0, then avoid division - ## Otherwise compute as usual - if [ "$max" -eq 0 ]; then - local percent=100 - else - local percent=$(bc <<< "$current*100/$max") - fi +if [ -f "$target_config_file" ]; then source "$target_config_file" ; +elif [ -f "$user_config_file" ]; then source "$user_config_file" ; +elif [ -f $root_config_file -a "$USER" == "root" ]; then source "$root_config_file" ; +elif [ -f "$sys_config_file" ]; then source "$sys_config_file" ; +else : # Default config already "included" ; +fi - ## SET COLORS DEPENDING ON LOAD - local fc_bar_1=$fc_deco - local fc_bar_2=$fc_ok - local fc_txt_1=$fc_info - local fc_txt_2=$fc_ok - local fc_txt_3=$fc_ok - local state="nominal" - if [ $percent -gt 99 ]; then - local fc_bar_2=$fc_error - local fc_txt_2=$fc_crit - local state="error" - elif [ $percent -gt $crit_percent ]; then - local fc_bar_2=$fc_crit - local fc_txt_2=$fc_crit - local state="critical" - fi +## COLOR AND TEXT FORMAT CODE +local fc_info=$(getFormatCode $format_info) +local fc_highlight=$(getFormatCode $format_highlight) +local fc_crit=$(getFormatCode $format_crit) +local fc_deco=$(getFormatCode $format_deco) +local fc_ok=$(getFormatCode $format_ok) +local fc_error=$(getFormatCode $format_error) +local fc_logo=$(getFormatCode $format_logo) +local fc_none=$(getFormatCode -e reset) - ## PRINT BAR - printResourceBar "$label" "$current" "$max" "$bar_length" "$state" +#fc_logo +#fc_ok +#fc_crit +#fc_error +#fc_none +local fc_label="$fc_info" +local fc_text="$fc_highlight" - ## PRINT NUMERIC VALUE - if $print_as_percentage; then - printf "${fc_txt_2}%${bar_num_digits}s${fc_txt_1} %%%%${fc_none}" $percent - else - printf " " - printFraction "$current" "$max" "$bar_num_digits" "$units" "$state" - fi -} @@ -278,6 +97,8 @@ printMonitor() ## INFO ##============================================================================== +include 'print_info.sh' + include 'info_os.sh' printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } @@ -436,7 +257,7 @@ printMonitorCPU() if [ -z "$as_percentage" ]; then local as_percentage=false; fi - printMonitor $current $max $bar_cpu_crit_percent \ + printResourceMonitor $current $max $bar_cpu_crit_percent \ $as_percentage $units $message } @@ -465,7 +286,7 @@ printMonitorRAM() if [ -z "$as_percentage" ]; then local as_percentage=false; fi - printMonitor $current $max $bar_ram_crit_percent \ + printResourceMonitor $current $max $bar_ram_crit_percent \ $as_percentage $units $message } @@ -507,7 +328,7 @@ printMonitorSwap() local max=$(echo "$swap_info" |\ awk '{SWAP=($2)} END {printf SWAP}') - printMonitor $current $max $bar_swap_crit_percent \ + printResourceMonitor $current $max $bar_swap_crit_percent \ $as_percentage $units $message fi } @@ -537,7 +358,7 @@ printMonitorHDD() local max=$(df "-B1${option}" / | grep "/" | awk '{key=($2)} END {printf key}') - printMonitor $current $max $bar_hdd_crit_percent \ + printResourceMonitor $current $max $bar_hdd_crit_percent \ $as_percentage $units $message } @@ -565,7 +386,7 @@ printMonitorHome() local max=$(df "-B1${option}" ~ | grep "/" | awk '{key=($2)} END {printf key}') - printMonitor $current $max $bar_home_crit_percent \ + printResourceMonitor $current $max $bar_home_crit_percent \ $as_percentage $units $message } @@ -594,7 +415,7 @@ printMonitorCPUTemp() ## PRINT MONITOR - printMonitor $current $max $crit_percent \ + printResourceMonitor $current $max $crit_percent \ false $units "CPU temp" else printInfoLine "CPU temp" "lm-sensors not installed" @@ -851,57 +672,9 @@ printHogsMemory() ##============================================================================== -## MAIN FUNCTION +## MAIN ##============================================================================== - - - - - -## LOAD CONFIGURATION -## Load default configuration file with all arguments, then try to load any of -## following in order, until first match, to override some or all config params. -## 1. Apply specific configuration file if specified as argument. -## 2. User specific configuration if in user's home folder. -## 3. If root, apply root configuration file if it exists in the system. -## 4. System wide configuration file if it exists. -## 5. Fall back to defaults. -## -include '../config/synth-shell-greeter.config.default' -local target_config_file="$1" # can be empty -local user_config_file="~/.config/synth-shell/synth-shell-greeter.config" -local root_config_file="/etc/synth-shell/os/synth-shell-greeter.root.config" -local sys_config_file="/etc/synth-shell/synth-shell-greeter.config" -if [ -f "$target_config_file" ]; then source "$target_config_file" ; -elif [ -f "$user_config_file" ]; then source "$user_config_file" ; -elif [ -f $root_config_file -a "$USER" == "root" ]; then source "$root_config_file" ; -elif [ -f "$sys_config_file" ]; then source "$sys_config_file" ; -else : # Default config already "included" ; -fi - - - -## COLOR AND TEXT FORMAT CODE -local fc_info=$(getFormatCode $format_info) -local fc_highlight=$(getFormatCode $format_highlight) -local fc_crit=$(getFormatCode $format_crit) -local fc_deco=$(getFormatCode $format_deco) -local fc_ok=$(getFormatCode $format_ok) -local fc_error=$(getFormatCode $format_error) -local fc_logo=$(getFormatCode $format_logo) -local fc_none=$(getFormatCode -e reset) - -#fc_logo -#fc_ok -#fc_crit -#fc_error -#fc_none -local fc_label="$fc_info" -local fc_text="$fc_highlight" - - - ## PRINT TOP SPACER if $clear_before_print; then clear; fi if $print_extra_new_line_top; then echo ""; fi @@ -930,5 +703,3 @@ if $print_extra_new_line_bot; then echo ""; fi unset greeter - -### EOF ### From 529153ef5ea65a341b71fae9e310fa4f432809c2 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Tue, 14 Apr 2020 23:36:35 +0200 Subject: [PATCH 05/16] Clean up functions Signed-off-by: Andres Gongora --- config/synth-shell-greeter.config.default | 1 + synth-shell-greeter/print_info.sh | 233 +++++++-------------- synth-shell-greeter/synth-shell-greeter.sh | 201 +++++++++--------- 3 files changed, 182 insertions(+), 253 deletions(-) diff --git a/config/synth-shell-greeter.config.default b/config/synth-shell-greeter.config.default index 8c66712..79d6c69 100644 --- a/config/synth-shell-greeter.config.default +++ b/config/synth-shell-greeter.config.default @@ -143,6 +143,7 @@ ram_crit_print_num=3 bar_length=9 # Number of characters that comprise a bar bar_num_digits=5 # Control num digits next to bar +bar_padding_after=0 # Extra spaces after bar info_label_width=16 # Desired length of the info labels bar_bracket_char_left='[' bar_bracket_char_right=']' diff --git a/synth-shell-greeter/print_info.sh b/synth-shell-greeter/print_info.sh index d60ffbf..0654128 100755 --- a/synth-shell-greeter/print_info.sh +++ b/synth-shell-greeter/print_info.sh @@ -37,16 +37,6 @@ include '../bash-tools/bash-tools/print_bar.sh' include '../bash-tools/bash-tools/assert.sh' -## CHECK EXTERNAL VARIABLES -assert_is_set $fc_ok -assert_is_set $fc_info -assert_is_set $fc_deco -assert_is_set $fc_crit -assert_is_set $fc_error -assert_is_set $fc_none -assert_not_empty $bar_num_digits - - @@ -64,14 +54,21 @@ assert_not_empty $bar_num_digits ## _getStateColor() { + assert_is_set $fc_ok + assert_is_set $fc_info + assert_is_set $fc_deco + assert_is_set $fc_crit + assert_is_set $fc_error + local state=$1 local E_PARAM_ERR=98 + local fc_none="\e[0m" case $state in nominal) echo $fc_ok ;; critical) echo $fc_crit ;; error) echo $fc_error ;; - *) echo "$state not valid" ; exit $E_PARAM_ERR + *) echo $fc_none ; exit $E_PARAM_ERR esac } @@ -82,86 +79,6 @@ _getStateColor() -##============================================================================== -## printFraction() -## -## Prints a color-formatted fraction with padding to reach MAX_DIGITS. -## -## Arguments: -## 1. NUMERATOR: first shown number -## 2. DENOMINATOR: second shown number -## 3. PADDING_DIGITS: determines the minimum length of NUMERATOR and -## DENOMINATOR. If they have less digits than this, -## then extra spaces are appended for padding. -## 4. UNITS: a string that is attached to the end of the fraction, -## meant to include optional units (e.g. MB) for display purposes. -## If "none", no units are displayed. -## -## Optional arguments: -## 5. STATE Determines the color (nominal/critical/error) -## -_printFraction() -{ - local a=$1 - local b=$2 - local padding=$3 - local units=$4 - local state=${5:-nominal} - - local deco_color=$fc_info - local num_color=$(_getStateColor $state) - local units_color=$num_color - - if [ $units == "none" ]; then local units=""; fi - - printf "${num_color}%${padding}s" $a - printf "${deco_color}/" - printf "${num_color}%-${padding}s" $b - printf "${units_color} ${units}${fc_none}" -} - - - - - - -##============================================================================== -## _printResourceBar() -## -## -_printResourceBar() -{ - local label=$1 - local current=$2 - local max=$3 - local bar_length=$4 - local state=${5:-nominal} - - - ## CHOOSE COLORS AND PADDING - local fc_label=${fc_info} - local pad=$info_label_width - local fc_fill_color=$(_getStateColor $state) - local fc_bracket_color=$fc_deco - - - ## COMPOSE CHARACTERS FOR BAR - local bracket_left=$fc_bracket_color$bar_bracket_char_left - local fill=$fc_fill_color$bar_fill_char - local background=$fc_none$bar_background_char - local bracket_right=$fc_bracket_color$bar_bracket_char_right$fc_none - - - ## PRINT LABEL AND BAR - printf "${fc_label}%-${pad}s" "$label" - printBar "$current" "$max" "$bar_length" \ - "$bracket_left" "$fill" "$background" "$bracket_right" -} - - - - - ##============================================================================== ## FUNCTIONS ##============================================================================== @@ -181,15 +98,24 @@ _printResourceBar() ## printInfoLine() { + assert_is_set $info_label_width + + + ## ARGUMENTS local label=$1 local value=$2 local state=${3:-nominal} + + ## FORMAT local fc_label=${fc_info} local fc_value=$(_getStateColor $state) - local pad=$info_label_width + local fc_none="\e[0m" + local padding_label=$info_label_width + - printf "${fc_label}%-${pad}s${fc_value}${value}${fc_none}\n" "$label" + ## PRINT LABEL AND VALUE + printf "${fc_label}%-${padding_label}s${fc_value}${value}${fc_none}\n" "$label" } @@ -212,74 +138,67 @@ printInfoLine() ## 6. LABEL: A description of the resource that will be printed in front ## of the utilization bar. ## -printResourceMonitor() +printInfoMonitor() { - ## CHECK EXTERNAL CONFIGURATION - if [ -z $bar_num_digits ]; then exit 1; fi - if [ -z $fc_deco ]; then exit 1; fi - if [ -z $fc_ok ]; then exit 1; fi - if [ -z $fc_info ]; then exit 1; fi - if [ -z $fc_crit ]; then exit 1; fi - - - ## VARIABLES - local current=$1 - local max=$2 - local crit_percent=$3 - local print_as_percentage=$4 - local units=$5 - local label=${@:6} - local pad=$info_label_width - - - ## CHECK VARIABLES - ## If max is empty, assign 0 - ## If crit percent is empty, assign 100 - ## If crit_percent > 100, assign 100 - if [ -z $max ]; then local max=0; fi - if [ -z $crit_percent ]; then local local crit_percent=100; fi - if [ "$crit_percent" -gt 100 ]; then local crit_percent=100; fi - - - ## COMPUTE PERCENT - ## If max=0, then avoid division - ## Otherwise compute as usual - if [ "$max" -eq 0 ]; then - local percent=100 - else - local percent=$(bc <<< "$current*100/$max") - fi - - - ## SET COLORS DEPENDING ON LOAD - local fc_bar_1=$fc_deco - local fc_bar_2=$fc_ok - local fc_txt_1=$fc_info - local fc_txt_2=$fc_ok - local fc_txt_3=$fc_ok - local state="nominal" - if [ $percent -gt 99 ]; then - local fc_bar_2=$fc_error - local fc_txt_2=$fc_crit - local state="error" - elif [ $percent -gt $crit_percent ]; then - local fc_bar_2=$fc_crit - local fc_txt_2=$fc_crit - local state="critical" - fi + assert_is_set $info_label_width + assert_is_set $bar_num_digits + assert_is_set $bar_length + assert_is_set $bar_padding_after + + + ## ARGUMENTS + local label=$1 + local value=$2 + local max=$3 + local units=$4 + local format=${5:-fraction} + local state=${6:-nominal} + + + ## FORMAT OPTIONS + local fc_label=${fc_info} + local fc_value=$(_getStateColor $state) + local fc_units=$fc_info + local fc_fill_color=$fc_value + local fc_bracket_color=$fc_deco + local fc_none="\e[0m" + local padding_label=$info_label_width + local padding_value=$bar_num_digits + local padding_bar=$bar_padding_after + + + ## COMPOSE CHARACTERS FOR BAR + local bracket_left=$fc_bracket_color$bar_bracket_char_left + local fill=$fc_fill_color$bar_fill_char + local background=$fc_none$bar_background_char + local bracket_right=$fc_bracket_color$bar_bracket_char_right$fc_none + + + ## PRINT LABEL + printf "${fc_label}%-${padding_label}s" "$label" ## PRINT BAR - _printResourceBar "$label" "$current" "$max" "$bar_length" "$state" - - ## PRINT NUMERIC VALUE - if $print_as_percentage; then - printf "${fc_txt_2}%${bar_num_digits}s${fc_txt_1} %%%%${fc_none}" $percent - else - printf " " - _printFraction "$current" "$max" "$bar_num_digits" "$units" "$state" - fi -} + printBar "$value" "$max" "$bar_length" \ + "$bracket_left" "$fill" "$background" "$bracket_right" + printf "%${bar_padding_after}s" "" + + ## PRINT VALUE + case $format in + "a/b") printf "${fc_value}%${padding_value}s" $value + printf "${fc_deco}/" + printf "${fc_value}%-${padding_value}s" $max + printf "${fc_units} ${units}${fc_none}" + ;; + + '0/0') local percent=$('bc' <<< "$value*100/$max") + printf "${fc_value}%${padding_value}s${fc_units}%%%%${fc_none}" $percent + ;; + + *) echo "Invalid format option $format" + esac +} + diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 2e5510c..98d28d1 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -139,13 +139,16 @@ printInfoSystemctl() if [ "$systcl_num_failed" -eq "0" ]; then local sysctl="All services OK" + local state="nominal" elif [ "$systcl_num_failed" -eq "1" ]; then - local sysctl="${fc_error}1 service failed!" + local sysctl="1 service failed!" + local state="error" else - local sysctl="${fc_error}$systcl_num_failed services failed!" + local sysctl="$systcl_num_failed services failed!" + local state="error" fi - printInfoLine "Services" "$sysctl" + printInfoLine "Services" "$sysctl" "$state" } @@ -222,18 +225,19 @@ printInfoCPUTemp() sed -n 's/^.*crit = +\(.*\)°[[CF]]*[ \t]*.*/\1/p') - ## COMPOSE MESSAGE + ## DETERMINE STATE if (( $(echo "$current < $high" |bc -l) )); then - local temp="$current$units"; + local state="nominal" elif (( $(echo "$current < $max" |bc -l) )); then - local temp="$fc_crit$current$units"; + local state="critical"; else - local temp="$fc_error$current$units"; + local state="error"; fi ## PRINT MESSAGE - printInfoLine "CPU temp" "$temp" + local temp="$current$units" + printInfoLine "CPU temp" "$temp" "$state" else printInfoLine "CPU temp" "lm-sensors not installed" fi @@ -243,22 +247,45 @@ printInfoCPUTemp() +printResourceMonitor() +{ + local label=$1 + local value=$2 + local max=$3 + local units=$4 + local format=$5 + local crit_percent=$6 + local error_percent=${7:-99} + + + ## CHECK STATE + local percent=$('bc' <<< "$value*100/$max") + local state="nominal" + if [ $percent -gt $error_percent ]; then + local state="error" + elif [ $percent -gt $crit_percent ]; then + local state="critical" + fi + + + printInfoMonitor "$label" "$current_value" "$max" "$units" "$format" "$state" +} + + + + ##------------------------------------------------------------------------------ ## printMonitorCPU() { - local message="Sys load avg" - local units="none" - local current=$(awk '{avg_1m=($1)} END {printf "%3.2f", avg_1m}' /proc/loadavg) + local format=$1 + local label="Sys load avg" + local units="" + local current_value=$(awk '{avg_1m=($1)} END {printf "%3.2f", avg_1m}' /proc/loadavg) local max=$(nproc --all) + local crit_percent=$bar_cpu_crit_percent - - local as_percentage=$1 - if [ -z "$as_percentage" ]; then local as_percentage=false; fi - - - printResourceMonitor $current $max $bar_cpu_crit_percent \ - $as_percentage $units $message + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" } @@ -267,7 +294,9 @@ printMonitorCPU() ## printMonitorRAM() { - ## CHOOSE UNITS + local format=$1 + local label="Memory" + case "$bar_ram_units" in "MB") local units="MB"; local option="--mega" ;; "TB") local units="TB"; local option="--tera" ;; @@ -275,19 +304,12 @@ printMonitorRAM() *) local units="GB"; local option="--giga" ;; esac - - local message="Memory" local mem_info=$('free' "$option" | head -n 2 | tail -n 1) - local current=$(echo "$mem_info" | awk '{mem=($2-$7)} END {printf mem}') + local current_value=$(echo "$mem_info" | awk '{mem=($2-$7)} END {printf mem}') local max=$(echo "$mem_info" | awk '{mem=($2)} END {printf mem}') + local crit_percent=$bar_ram_crit_percent - - local as_percentage=$1 - if [ -z "$as_percentage" ]; then local as_percentage=false; fi - - - printResourceMonitor $current $max $bar_ram_crit_percent \ - $as_percentage $units $message + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" } @@ -296,70 +318,70 @@ printMonitorRAM() ## printMonitorSwap() { - ## CHOOSE UNITS + local format=$1 + local label="Swap" + case "$bar_swap_units" in - "MB") local units="MB"; local option="--mebi" ;; - "TB") local units="TB"; local option="--tebi" ;; - "PB") local units="PB"; local option="--pebi" ;; - *) local units="GB"; local option="--gibi" ;; + "MB") local units="MB"; local option="--mega" ;; + "TB") local units="TB"; local option="--tera" ;; + "PB") local units="PB"; local option="--peta" ;; + *) local units="GB"; local option="--giga" ;; esac - - local message="Swap" - local as_percentage=$1 - if [ -z "$as_percentage" ]; then local as_percentage=false; fi - - ## CHECK IF SYSTEM HAS SWAP ## Count number of lines in /proc/swaps, excluding the header (-1) ## This is not fool-proof, but if num_swap_devs>=1, there should be swap local num_swap_devs=$(($(wc -l /proc/swaps | awk '{print $1;}') -1)) - if [ "$num_swap_devs" -lt 1 ]; then ## NO SWAP - - - local pad=${info_label_width} - printf "${fc_info}%-${pad}s${fc_highlight}N/A${fc_none}" "${message}" + if [ "$num_swap_devs" -lt 1 ]; then + printInfoLine "$label" "N/A" - else ## HAS SWAP + else local swap_info=$('free' "$option" | tail -n 1) - local current=$(echo "$swap_info" |\ - awk '{SWAP=($3)} END {printf SWAP}') - local max=$(echo "$swap_info" |\ - awk '{SWAP=($2)} END {printf SWAP}') + local current_value=$(echo "$swap_info" | awk '{SWAP=($3)} END {printf SWAP}') + local max=$(echo "$swap_info" | awk '{SWAP=($2)} END {printf SWAP}') + local crit_percent=$bar_swap_crit_percent - printResourceMonitor $current $max $bar_swap_crit_percent \ - $as_percentage $units $message + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" fi } -##------------------------------------------------------------------------------ -## -printMonitorHDD() +printStorageMonitor() { - local as_percentage=$1 - if [ -z "$as_percentage" ]; then local as_percentage=false; fi - - - ## CHOOSE UNITS - case "$bar_hdd_units" in + local label=$1 + local device=$2 + local units=$3 + local format=$4 + local crit_percent=$5 + local error_percent=${6:-99} + + case "$units" in "MB") local units="MB"; local option="M" ;; "TB") local units="TB"; local option="T" ;; "PB") local units="PB"; local option="P" ;; *) local units="GB"; local option="G" ;; esac + local current_value=$(df "-B1${option}" "${device}" | grep / | awk '{key=($3)} END {printf key}') + local max=$(df "-B1${option}" "${device}" | grep / | awk '{key=($2)} END {printf key}') + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" "$error_percent" + +} - local message="Storage /" - local units="GB" - local current=$(df "-B1${option}" / | grep "/" |awk '{key=($3)} END {printf key}') - local max=$(df "-B1${option}" / | grep "/" | awk '{key=($2)} END {printf key}') +##------------------------------------------------------------------------------ +## +printMonitorHDD() +{ + local format=$1 + local label="Storage /" + local device="/" + local units=$bar_hdd_units + local crit_percent=$bar_hdd_crit_percent - printResourceMonitor $current $max $bar_hdd_crit_percent \ - $as_percentage $units $message + printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" } @@ -368,26 +390,13 @@ printMonitorHDD() ## printMonitorHome() { - local as_percentage=$1 - if [ -z "$as_percentage" ]; then local as_percentage=false; fi - - - ## CHOOSE UNITS - case "$bar_home_units" in - "MB") local units="MB"; local option="M" ;; - "TB") local units="TB"; local option="T" ;; - "PB") local units="PB"; local option="P" ;; - *) local units="GB"; local option="G" ;; - esac - - - local message="Storage /home" - local current=$(df "-B1${option}" ~ | grep "/" |awk '{key=($3)} END {printf key}') - local max=$(df "-B1${option}" ~ | grep "/" | awk '{key=($2)} END {printf key}') - + local format=$1 + local label="Storage /home" + local device=$HOME + local units=$bar_home_units + local crit_percent=$bar_home_crit_percent - printResourceMonitor $current $max $bar_home_crit_percent \ - $as_percentage $units $message + printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" } @@ -463,16 +472,16 @@ printStatusInfo() ## USAGE MONITORS (BARS) ## NAME FUNCTION AS % - SYSLOAD_MON) printMonitorCPU;; - SYSLOAD_MON%) printMonitorCPU true;; - MEMORY_MON) printMonitorRAM;; - MEMORY_MON%) printMonitorRAM true;; - SWAP_MON) printMonitorSwap;; - SWAP_MON%) printMonitorSwap true;; - HDDROOT_MON) printMonitorHDD;; - HDDROOT_MON%) printMonitorHDD true;; - HDDHOME_MON) printMonitorHome;; - HDDHOME_MON%) printMonitorHome true;; + SYSLOAD_MON) printMonitorCPU 'a/b';; + SYSLOAD_MON%) printMonitorCPU '0/0';; + MEMORY_MON) printMonitorRAM 'a/b';; + MEMORY_MON%) printMonitorRAM '0/0';; + SWAP_MON) printMonitorSwap 'a/b';; + SWAP_MON%) printMonitorSwap '0/0';; + HDDROOT_MON) printMonitorHDD 'a/b';; + HDDROOT_MON%) printMonitorHDD '0/0';; + HDDHOME_MON) printMonitorHome 'a/b';; + HDDHOME_MON%) printMonitorHome '0/0';; CPUTEMP_MON) printMonitorCPUTemp;; *) printInfoLine "Unknown" "Check your config";; From ce6d83d4d92b29d79d4473bcd470515027816155 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Tue, 14 Apr 2020 23:57:03 +0200 Subject: [PATCH 06/16] Add extra resilience Signed-off-by: Andres Gongora --- synth-shell-greeter/synth-shell-greeter.sh | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 98d28d1..8f0ba76 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -178,6 +178,9 @@ printInfoColorpaletteSmall() ## printInfoColorpaletteFancy() { + ## Line 1: ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ + ## Line 2: ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ + local palette_top=$(printf '%s'\ "$(formatText "▄" -c dark-gray)$(formatText "▄" -c dark-gray -b black)$(formatText "█" -c black) "\ "$(formatText "▄" -c light-red)$(formatText "▄" -c light-red -b red)$(formatText "█" -c red) "\ @@ -198,8 +201,8 @@ printInfoColorpaletteFancy() "$(formatText "██" -c light-cyan)$(formatText "▀" -c cyan) "\ "$(formatText "██" -c white)$(formatText "▀" -c light-gray) ") - printInfoLine "Color palette" "$palette_top" - printInfoLine "" "$palette_bot" + printInfoLine "" "$palette_top" + printInfoLine "Color palette" "$palette_bot" } @@ -278,6 +281,9 @@ printResourceMonitor() ## printMonitorCPU() { + assert_is_set $bar_cpu_units + #assert_is_set $bar_cpu_crit_percent + local format=$1 local label="Sys load avg" local units="" @@ -294,6 +300,9 @@ printMonitorCPU() ## printMonitorRAM() { + assert_is_set $bar_ram_units + assert_is_set $bar_ram_crit_percent + local format=$1 local label="Memory" @@ -318,6 +327,9 @@ printMonitorRAM() ## printMonitorSwap() { + assert_is_set $bar_swap_units + assert_is_set $bar_swap_crit_percent + local format=$1 local label="Swap" @@ -347,7 +359,8 @@ printMonitorSwap() } - +##------------------------------------------------------------------------------ +## printStorageMonitor() { local label=$1 @@ -375,6 +388,9 @@ printStorageMonitor() ## printMonitorHDD() { + assert_is_set $bar_hdd_units + assert_is_set $bar_hdd_crit_percent + local format=$1 local label="Storage /" local device="/" @@ -390,6 +406,9 @@ printMonitorHDD() ## printMonitorHome() { + assert_is_set $bar_home_units + assert_is_set $bar_home_crit_percent + local format=$1 local label="Storage /home" local device=$HOME From 8d6288f3e1ab8a2c923ad77271070650e4dada17 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Thu, 16 Apr 2020 22:30:45 +0200 Subject: [PATCH 07/16] Fix issue with recursive include Signed-off-by: Andres Gongora --- bash-tools | 2 +- setup.sh | 9 +++++---- synth-shell-greeter/synth-shell-greeter.sh | 16 +++++++++------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/bash-tools b/bash-tools index 7f8e324..ef154c7 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit 7f8e324a31c1a74314a45df6e57517101f5239a4 +Subproject commit ef154c79341f02ed1830895b63a10b8b51e1df90 diff --git a/setup.sh b/setup.sh index 58de1b6..f2aad62 100755 --- a/setup.sh +++ b/setup.sh @@ -30,11 +30,11 @@ ## setup() { - include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } + + + include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} include 'bash-tools/bash-tools/user_io.sh' - include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include 'bash-tools/bash-tools/hook_script.sh' - include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } include 'bash-tools/bash-tools/assemble_script.sh' @@ -44,7 +44,8 @@ setup() local output_config_dir="$HOME/.config/synth-shell" cp "$output_config_dir/synth-shell-greeter.config" "$output_config_dir/synth-shell-greeter.config.backup" printInfo "Installing script as $output_script" - local action=$(promptUser "Add hook your .bashrc file or equivalent?\n\tRequired for autostart on new terminals" "[Y]/[n]?" "yYnN" "y") + #local action=$(promptUser "Add hook your .bashrc file or equivalent?\n\tRequired for autostart on new terminals" "[Y]/[n]?" "yYnN" "y") + local action='y' case "$action" in ""|y|Y ) hookScript $output_script ;; n|N ) ;; diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 8f0ba76..3dc56b5 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -29,13 +29,16 @@ ## - -greeter() -{ ## INCLUDE EXTERNAL DEPENDENCIES -include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } +include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} include '../bash-tools/bash-tools/color.sh' include '../bash-tools/bash-tools/print_utils.sh' +include '../config/synth-shell-greeter.config.default' + + +greeter() +{ + @@ -54,7 +57,7 @@ include '../bash-tools/bash-tools/print_utils.sh' ## 4. System wide configuration file if it exists. ## 5. Fall back to defaults. ## -include '../config/synth-shell-greeter.config.default' + local target_config_file="$1" local user_config_file="~/.config/synth-shell/synth-shell-greeter.config" @@ -281,8 +284,7 @@ printResourceMonitor() ## printMonitorCPU() { - assert_is_set $bar_cpu_units - #assert_is_set $bar_cpu_crit_percent + assert_is_set $bar_cpu_crit_percent local format=$1 local label="Sys load avg" From 1bac4adfa8637138d876bbdba2ab670528d4241d Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Thu, 16 Apr 2020 22:37:55 +0200 Subject: [PATCH 08/16] Change setup from function to script Signed-off-by: Andres Gongora --- setup.sh | 104 +++++++++++++++++++++++-------------------------------- 1 file changed, 43 insertions(+), 61 deletions(-) diff --git a/setup.sh b/setup.sh index f2aad62..ded954b 100755 --- a/setup.sh +++ b/setup.sh @@ -28,44 +28,39 @@ ##============================================================================== ## -setup() -{ - - - include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} - include 'bash-tools/bash-tools/user_io.sh' - include 'bash-tools/bash-tools/hook_script.sh' - include 'bash-tools/bash-tools/assemble_script.sh' - - - ## SWITCH BETWEEN AUTOMATIC AND USER INSTALLATION - if [ "$#" -eq 0 ]; then - local output_script="$HOME/.config/synth-shell/synth-shell-greeter.sh" - local output_config_dir="$HOME/.config/synth-shell" - cp "$output_config_dir/synth-shell-greeter.config" "$output_config_dir/synth-shell-greeter.config.backup" - printInfo "Installing script as $output_script" - #local action=$(promptUser "Add hook your .bashrc file or equivalent?\n\tRequired for autostart on new terminals" "[Y]/[n]?" "yYnN" "y") - local action='y' - case "$action" in - ""|y|Y ) hookScript $output_script ;; - n|N ) ;; - *) printError "Invalid option"; exit 1 - esac +include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} +include 'bash-tools/bash-tools/user_io.sh' +include 'bash-tools/bash-tools/hook_script.sh' +include 'bash-tools/bash-tools/assemble_script.sh' + + +## SWITCH BETWEEN AUTOMATIC AND USER INSTALLATION +if [ "$#" -eq 0 ]; then + OUTPUT_SCRIPT="$HOME/.config/synth-shell/synth-shell-greeter.sh" + OUTPUT_CONFIG_DIR="$HOME/.config/synth-shell" + cp "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config" "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config.backup" + printInfo "Installing script as $OUTPUT_SCRIPT" + USER_CHOICE=$(promptUser "Add hook your .bashrc file or equivalent?\n\tRequired for autostart on new terminals" "[Y]/[n]?" "yYnN" "y") + case "$USER_CHOICE" in + ""|y|Y ) hookScript $OUTPUT_SCRIPT ;; + n|N ) ;; + *) printError "Invalid option"; exit 1 + esac - else - local output_script="$1" - local output_config_dir="$2" - fi +else + OUTPUT_SCRIPT="$1" + OUTPUT_CONFIG_DIR="$2" +fi - ## DEFINE LOCAL VARIABLES - local dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) - local input_script="$dir/synth-shell-greeter/synth-shell-greeter.sh" - local input_config_dir="$dir/config/" +## DEFINE LOCAL VARIABLES +DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) +INPUT_SCRIPT="$DIR/synth-shell-greeter/synth-shell-greeter.sh" +INPUT_CONFIG_DIR="$DIR/config" - ## HEADER TO BE ADDED AT THE TOP OF THE ASSEMBLED SCRIPT - local output_script_header=$(printf '%s'\ +## HEADER TO BE ADDED AT THE TOP OF THE ASSEMBLED SCRIPT +OUTPUT_SCRIPT_HEADER=$(printf '%s'\ "##\n"\ "##\n"\ "## =======================\n"\ @@ -83,35 +78,22 @@ setup() "##\n\n\n") - ## SETUP SCRIPT - assembleScript "$input_script" "$output_script" "$output_script_header" +## SETUP SCRIPT +assembleScript "$INPUT_SCRIPT" "$OUTPUT_SCRIPT" "$OUTPUT_SCRIPT_HEADER" - ## SETUP CONFIGURATION FILES - [ -d "$output_config_dir" ] || mkdir -p "$output_config_dir" - cp -r "$input_config_dir/." "$output_config_dir/" +## SETUP CONFIGURATION FILES +[ -d "$OUTPUT_CONFIG_DIR" ] || mkdir -p "$OUTPUT_CONFIG_DIR" +cp -r "$INPUT_CONFIG_DIR/." "$OUTPUT_CONFIG_DIR/" - ## SETUP DEFAULT SYNTH-SHELL-GREETER CONFIG FILE - local config_file="$output_config_dir/synth-shell-greeter.config" - if [ ! -f "$config_file" ]; then - local distro=$(cat /etc/os-release | grep "ID=" | sed 's/ID=//g' | head -n 1) - case "$distro" in - 'arch' ) cp "$output_config_dir/os/synth-shell-greeter.archlinux.config" "$config_file" ;; - 'manjaro' ) cp "$output_config_dir/os/synth-shell-greeter.manjaro.config" "$config_file" ;; - *) cp "$output_config_dir/synth-shell-greeter.config.default" "$config_file" ;; - esac - fi -} - - - - - - -##============================================================================== -## SCRIPT -##============================================================================== - -setup $@ - +## SETUP DEFAULT SYNTH-SHELL-GREETER CONFIG FILE +CONFIG_FILE="$OUTPUT_CONFIG_DIR/synth-shell-greeter.config" +if [ ! -f "$CONFIG_FILE" ]; then + local DISTRO=$(cat /etc/os-release | grep "ID=" | sed 's/ID=//g' | head -n 1) + case "$DISTRO" in + 'arch' ) cp "$OUTPUT_CONFIG_DIR/os/synth-shell-greeter.archlinux.config" "$CONFIG_FILE" ;; + 'manjaro' ) cp "$OUTPUT_CONFIG_DIR/os/synth-shell-greeter.manjaro.config" "$CONFIG_FILE" ;; + *) cp "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config.default" "$CONFIG_FILE" ;; + esac +fi From 3787b9b8496fb4ae78240f2063499b3d322b16c6 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 00:17:43 +0200 Subject: [PATCH 09/16] Fix issue with recursive include and separate info related functions Signed-off-by: Andres Gongora --- bash-tools | 2 +- setup.sh | 5 +- synth-shell-greeter/info.sh | 398 ++++++++++++++++++ .../{print_info.sh => info_print_info.sh} | 2 +- synth-shell-greeter/synth-shell-greeter.sh | 363 +--------------- 5 files changed, 414 insertions(+), 356 deletions(-) create mode 100755 synth-shell-greeter/info.sh rename synth-shell-greeter/{print_info.sh => info_print_info.sh} (98%) diff --git a/bash-tools b/bash-tools index ef154c7..8f7ca9b 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit ef154c79341f02ed1830895b63a10b8b51e1df90 +Subproject commit 8f7ca9be0f030ca733a1a845735cfa0d440f6cc2 diff --git a/setup.sh b/setup.sh index ded954b..91a7f10 100755 --- a/setup.sh +++ b/setup.sh @@ -28,7 +28,7 @@ ##============================================================================== ## -include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} +include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} include 'bash-tools/bash-tools/user_io.sh' include 'bash-tools/bash-tools/hook_script.sh' include 'bash-tools/bash-tools/assemble_script.sh' @@ -38,7 +38,8 @@ include 'bash-tools/bash-tools/assemble_script.sh' if [ "$#" -eq 0 ]; then OUTPUT_SCRIPT="$HOME/.config/synth-shell/synth-shell-greeter.sh" OUTPUT_CONFIG_DIR="$HOME/.config/synth-shell" - cp "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config" "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config.backup" + cp "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config" \ + "$OUTPUT_CONFIG_DIR/synth-shell-greeter.config.backup" printInfo "Installing script as $OUTPUT_SCRIPT" USER_CHOICE=$(promptUser "Add hook your .bashrc file or equivalent?\n\tRequired for autostart on new terminals" "[Y]/[n]?" "yYnN" "y") case "$USER_CHOICE" in diff --git a/synth-shell-greeter/info.sh b/synth-shell-greeter/info.sh new file mode 100755 index 0000000..288ef96 --- /dev/null +++ b/synth-shell-greeter/info.sh @@ -0,0 +1,398 @@ +#!/bin/bash + +## +-----------------------------------+-----------------------------------+ +## | | +## | Copyright (c) 2019-2020, Andres Gongora . | +## | | +## | This program is free software: you can redistribute it and/or modify | +## | it under the terms of the GNU General Public License as published by | +## | the Free Software Foundation, either version 3 of the License, or | +## | (at your option) any later version. | +## | | +## | This program is distributed in the hope that it will be useful, | +## | but WITHOUT ANY WARRANTY; without even the implied warranty of | +## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | +## | GNU General Public License for more details. | +## | | +## | You should have received a copy of the GNU General Public License | +## | along with this program. If not, see . | +## | | +## +-----------------------------------------------------------------------+ + + +## +## DESCRIPTION: +## +## +## + + + +##============================================================================== +## GENERAL DEPENDENCIES +##============================================================================== + +include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} +include 'info_print_info.sh' + + + + + + +##============================================================================== +## ONE LINERS +##============================================================================== + +include 'info_os.sh' +printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } +printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } +printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } +printInfoDate() { printInfoLine "Date" "$(getDate)" ; } +printInfoUptime() { printInfoLine "Uptime" "$(getUptime)" ; } +printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } +printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } +printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } + +include 'info_hardware.sh' +printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } +printInfoGPU() { printInfoLine "GPU" "$(getNameGPU)" ; } +printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } + +include 'info_network.sh' +printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } +printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } + +printInfoSpacer() { printInfoLine "" "" ; } + + + + + + +##============================================================================== +## +##============================================================================== + +##------------------------------------------------------------------------------ +## +printInfoSystemctl() +{ + local systcl_num_failed=$(systemctl --failed |\ + grep "loaded units listed" |\ + head -c 1) + + if [ "$systcl_num_failed" -eq "0" ]; then + local sysctl="All services OK" + local state="nominal" + elif [ "$systcl_num_failed" -eq "1" ]; then + local sysctl="1 service failed!" + local state="error" + else + local sysctl="$systcl_num_failed services failed!" + local state="error" + fi + + printInfoLine "Services" "$sysctl" "$state" +} + + + +##------------------------------------------------------------------------------ +## +printInfoColorpaletteSmall() +{ + local char="▀▀" + + local palette=$(printf '%s'\ + "$(formatText "$char" -c black -b dark-gray)"\ + "$(formatText "$char" -c red -b light-red)"\ + "$(formatText "$char" -c green -b light-green)"\ + "$(formatText "$char" -c yellow -b light-yellow)"\ + "$(formatText "$char" -c blue -b light-blue)"\ + "$(formatText "$char" -c magenta -b light-magenta)"\ + "$(formatText "$char" -c cyan -b light-cyan)"\ + "$(formatText "$char" -c light-gray -b white)") + + printInfoLine "Color palette" "$palette" +} + + + +##------------------------------------------------------------------------------ +## +printInfoColorpaletteFancy() +{ + ## Line 1: ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ + ## Line 2: ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ + + local palette_top=$(printf '%s'\ + "$(formatText "▄" -c dark-gray)$(formatText "▄" -c dark-gray -b black)$(formatText "█" -c black) "\ + "$(formatText "▄" -c light-red)$(formatText "▄" -c light-red -b red)$(formatText "█" -c red) "\ + "$(formatText "▄" -c light-green)$(formatText "▄" -c light-green -b green)$(formatText "█" -c green) "\ + "$(formatText "▄" -c light-yellow)$(formatText "▄" -c light-yellow -b yellow)$(formatText "█" -c yellow) "\ + "$(formatText "▄" -c light-blue)$(formatText "▄" -c light-blue -b blue)$(formatText "█" -c blue) "\ + "$(formatText "▄" -c light-magenta)$(formatText "▄" -c light-magenta -b magenta)$(formatText "█" -c magenta) "\ + "$(formatText "▄" -c light-cyan)$(formatText "▄" -c light-cyan -b cyan)$(formatText "█" -c cyan) "\ + "$(formatText "▄" -c white)$(formatText "▄" -c white -b light-gray)$(formatText "█" -c light-gray) ") + + local palette_bot=$(printf '%s'\ + "$(formatText "██" -c dark-gray)$(formatText "▀" -c black) "\ + "$(formatText "██" -c light-red)$(formatText "▀" -c red) "\ + "$(formatText "██" -c light-green)$(formatText "▀" -c green) "\ + "$(formatText "██" -c light-yellow)$(formatText "▀" -c yellow) "\ + "$(formatText "██" -c light-blue)$(formatText "▀" -c blue) "\ + "$(formatText "██" -c light-magenta)$(formatText "▀" -c magenta) "\ + "$(formatText "██" -c light-cyan)$(formatText "▀" -c cyan) "\ + "$(formatText "██" -c white)$(formatText "▀" -c light-gray) ") + + printInfoLine "" "$palette_top" + printInfoLine "Color palette" "$palette_bot" +} + + + +##------------------------------------------------------------------------------ +## +printInfoCPUTemp() +{ + if ( which sensors > /dev/null 2>&1 ); then + + ## GET VALUES + local temp_line=$(sensors 2>/dev/null |\ + grep Core |\ + head -n 1 |\ + sed 's/^.*:[ \t]*//g;s/[\(\),]//g') + local units=$(echo $temp_line |\ + sed -n 's/.*\(°[[CF]]*\).*/\1/p') + local current=$(echo $temp_line |\ + sed -n 's/^.*+\(.*\)°[[CF]]*[ \t]*h.*/\1/p') + local high=$(echo $temp_line |\ + sed -n 's/^.*high = +\(.*\)°[[CF]]*[ \t]*c.*/\1/p') + local max=$(echo $temp_line |\ + sed -n 's/^.*crit = +\(.*\)°[[CF]]*[ \t]*.*/\1/p') + + + ## DETERMINE STATE + if (( $(echo "$current < $high" |bc -l) )); then + local state="nominal" + elif (( $(echo "$current < $max" |bc -l) )); then + local state="critical"; + else + local state="error"; + fi + + + ## PRINT MESSAGE + local temp="$current$units" + printInfoLine "CPU temp" "$temp" "$state" + else + printInfoLine "CPU temp" "lm-sensors not installed" + fi + + +} + + + +printResourceMonitor() +{ + local label=$1 + local value=$2 + local max=$3 + local units=$4 + local format=$5 + local crit_percent=$6 + local error_percent=${7:-99} + + + ## CHECK STATE + local percent=$('bc' <<< "$value*100/$max") + local state="nominal" + if [ $percent -gt $error_percent ]; then + local state="error" + elif [ $percent -gt $crit_percent ]; then + local state="critical" + fi + + + printInfoMonitor "$label" "$current_value" "$max" "$units" "$format" "$state" +} + + + + +##------------------------------------------------------------------------------ +## +printMonitorCPU() +{ + assert_is_set $bar_cpu_crit_percent + + local format=$1 + local label="Sys load avg" + local units="" + local current_value=$(awk '{avg_1m=($1)} END {printf "%3.2f", avg_1m}' /proc/loadavg) + local max=$(nproc --all) + local crit_percent=$bar_cpu_crit_percent + + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" +} + + + +##------------------------------------------------------------------------------ +## +printMonitorRAM() +{ + assert_is_set $bar_ram_units + assert_is_set $bar_ram_crit_percent + + local format=$1 + local label="Memory" + + case "$bar_ram_units" in + "MB") local units="MB"; local option="--mega" ;; + "TB") local units="TB"; local option="--tera" ;; + "PB") local units="PB"; local option="--peta" ;; + *) local units="GB"; local option="--giga" ;; + esac + + local mem_info=$('free' "$option" | head -n 2 | tail -n 1) + local current_value=$(echo "$mem_info" | awk '{mem=($2-$7)} END {printf mem}') + local max=$(echo "$mem_info" | awk '{mem=($2)} END {printf mem}') + local crit_percent=$bar_ram_crit_percent + + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" +} + + + +##------------------------------------------------------------------------------ +## +printMonitorSwap() +{ + assert_is_set $bar_swap_units + assert_is_set $bar_swap_crit_percent + + local format=$1 + local label="Swap" + + case "$bar_swap_units" in + "MB") local units="MB"; local option="--mega" ;; + "TB") local units="TB"; local option="--tera" ;; + "PB") local units="PB"; local option="--peta" ;; + *) local units="GB"; local option="--giga" ;; + esac + + ## CHECK IF SYSTEM HAS SWAP + ## Count number of lines in /proc/swaps, excluding the header (-1) + ## This is not fool-proof, but if num_swap_devs>=1, there should be swap + local num_swap_devs=$(($(wc -l /proc/swaps | awk '{print $1;}') -1)) + + if [ "$num_swap_devs" -lt 1 ]; then + printInfoLine "$label" "N/A" + + else + local swap_info=$('free' "$option" | tail -n 1) + local current_value=$(echo "$swap_info" | awk '{SWAP=($3)} END {printf SWAP}') + local max=$(echo "$swap_info" | awk '{SWAP=($2)} END {printf SWAP}') + local crit_percent=$bar_swap_crit_percent + + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" + fi +} + + +##------------------------------------------------------------------------------ +## +printStorageMonitor() +{ + local label=$1 + local device=$2 + local units=$3 + local format=$4 + local crit_percent=$5 + local error_percent=${6:-99} + + case "$units" in + "MB") local units="MB"; local option="M" ;; + "TB") local units="TB"; local option="T" ;; + "PB") local units="PB"; local option="P" ;; + *) local units="GB"; local option="G" ;; + esac + + local current_value=$(df "-B1${option}" "${device}" | grep / | awk '{key=($3)} END {printf key}') + local max=$(df "-B1${option}" "${device}" | grep / | awk '{key=($2)} END {printf key}') + printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" "$error_percent" + +} + + +##------------------------------------------------------------------------------ +## +printMonitorHDD() +{ + assert_is_set $bar_hdd_units + assert_is_set $bar_hdd_crit_percent + + local format=$1 + local label="Storage /" + local device="/" + local units=$bar_hdd_units + local crit_percent=$bar_hdd_crit_percent + + printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" +} + + + +##------------------------------------------------------------------------------ +## +printMonitorHome() +{ + assert_is_set $bar_home_units + assert_is_set $bar_home_crit_percent + + local format=$1 + local label="Storage /home" + local device=$HOME + local units=$bar_home_units + local crit_percent=$bar_home_crit_percent + + printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" +} + + + +##------------------------------------------------------------------------------ +## +printMonitorCPUTemp() +{ + if ( which sensors > /dev/null 2>&1 ); then + + ## GET VALUES + local temp_line=$(sensors |\ + grep Core |\ + head -n 1 |\ + sed 's/^.*:[ \t]*//g;s/[\(\),]//g') + local units=$(echo $temp_line |\ + sed -n 's/.*\(°[[CF]]*\).*/\1/p' ) + local current=$(echo $temp_line |\ + sed -n 's/^.*+\(.*\)°[[CF]]*[ \t]*h.*/\1/p' ) + local high=$(echo $temp_line |\ + sed -n 's/^.*high = +\(.*\)°[[CF]]*[ \t]*c.*/\1/p' ) + local max=$(echo $temp_line |\ + sed -n 's/^.*crit = +\(.*\)°[[CF]]*[ \t]*.*/\1/p' ) + local crit_percent=$(bc <<< "$high*100/$max") + + + ## PRINT MONITOR + printResourceMonitor $current $max $crit_percent \ + false $units "CPU temp" + else + printInfoLine "CPU temp" "lm-sensors not installed" + fi +} + + + diff --git a/synth-shell-greeter/print_info.sh b/synth-shell-greeter/info_print_info.sh similarity index 98% rename from synth-shell-greeter/print_info.sh rename to synth-shell-greeter/info_print_info.sh index 0654128..5adfdfa 100755 --- a/synth-shell-greeter/print_info.sh +++ b/synth-shell-greeter/info_print_info.sh @@ -32,7 +32,7 @@ ## DEPENDENCIES ##============================================================================== -include() { source "$( cd $( dirname "${BASH_SOURCE[0]}" ) >/dev/null 2>&1 && pwd )/$1" ; } +include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} include '../bash-tools/bash-tools/print_bar.sh' include '../bash-tools/bash-tools/assert.sh' diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 3dc56b5..f2d2eea 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -29,20 +29,18 @@ ## + ## INCLUDE EXTERNAL DEPENDENCIES -include(){ local d=$PWD; cd "$(dirname $PWD/$1 )"; . "$(basename $1)"; cd "$d";} +include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} include '../bash-tools/bash-tools/color.sh' include '../bash-tools/bash-tools/print_utils.sh' -include '../config/synth-shell-greeter.config.default' +include 'info.sh' greeter() { - - - ##============================================================================== ## CONFIGURATION ##============================================================================== @@ -57,8 +55,7 @@ greeter() ## 4. System wide configuration file if it exists. ## 5. Fall back to defaults. ## - - +include '../config/synth-shell-greeter.config.default' local target_config_file="$1" local user_config_file="~/.config/synth-shell/synth-shell-greeter.config" local root_config_file="/etc/synth-shell/os/synth-shell-greeter.root.config" @@ -100,28 +97,9 @@ local fc_text="$fc_highlight" ## INFO ##============================================================================== -include 'print_info.sh' -include 'info_os.sh' -printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } -printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } -printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } -printInfoDate() { printInfoLine "Date" "$(getDate)" ; } -printInfoUptime() { printInfoLine "Uptime" "$(getUptime)" ; } -printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } -printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } -printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } -include 'info_hardware.sh' -printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } -printInfoGPU() { printInfoLine "GPU" "$(getNameGPU)" ; } -printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } -include 'info_network.sh' -printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } -printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } - -printInfoSpacer() { printInfoLine "" "" ; } @@ -132,325 +110,6 @@ printInfoSpacer() { printInfoLine "" "" ; } -##------------------------------------------------------------------------------ -## -printInfoSystemctl() -{ - local systcl_num_failed=$(systemctl --failed |\ - grep "loaded units listed" |\ - head -c 1) - - if [ "$systcl_num_failed" -eq "0" ]; then - local sysctl="All services OK" - local state="nominal" - elif [ "$systcl_num_failed" -eq "1" ]; then - local sysctl="1 service failed!" - local state="error" - else - local sysctl="$systcl_num_failed services failed!" - local state="error" - fi - - printInfoLine "Services" "$sysctl" "$state" -} - - - -##------------------------------------------------------------------------------ -## -printInfoColorpaletteSmall() -{ - local char="▀▀" - - local palette=$(printf '%s'\ - "$(formatText "$char" -c black -b dark-gray)"\ - "$(formatText "$char" -c red -b light-red)"\ - "$(formatText "$char" -c green -b light-green)"\ - "$(formatText "$char" -c yellow -b light-yellow)"\ - "$(formatText "$char" -c blue -b light-blue)"\ - "$(formatText "$char" -c magenta -b light-magenta)"\ - "$(formatText "$char" -c cyan -b light-cyan)"\ - "$(formatText "$char" -c light-gray -b white)") - - printInfoLine "Color palette" "$palette" -} - - - -##------------------------------------------------------------------------------ -## -printInfoColorpaletteFancy() -{ - ## Line 1: ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ ▄▄█ - ## Line 2: ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ ██▀ - - local palette_top=$(printf '%s'\ - "$(formatText "▄" -c dark-gray)$(formatText "▄" -c dark-gray -b black)$(formatText "█" -c black) "\ - "$(formatText "▄" -c light-red)$(formatText "▄" -c light-red -b red)$(formatText "█" -c red) "\ - "$(formatText "▄" -c light-green)$(formatText "▄" -c light-green -b green)$(formatText "█" -c green) "\ - "$(formatText "▄" -c light-yellow)$(formatText "▄" -c light-yellow -b yellow)$(formatText "█" -c yellow) "\ - "$(formatText "▄" -c light-blue)$(formatText "▄" -c light-blue -b blue)$(formatText "█" -c blue) "\ - "$(formatText "▄" -c light-magenta)$(formatText "▄" -c light-magenta -b magenta)$(formatText "█" -c magenta) "\ - "$(formatText "▄" -c light-cyan)$(formatText "▄" -c light-cyan -b cyan)$(formatText "█" -c cyan) "\ - "$(formatText "▄" -c white)$(formatText "▄" -c white -b light-gray)$(formatText "█" -c light-gray) ") - - local palette_bot=$(printf '%s'\ - "$(formatText "██" -c dark-gray)$(formatText "▀" -c black) "\ - "$(formatText "██" -c light-red)$(formatText "▀" -c red) "\ - "$(formatText "██" -c light-green)$(formatText "▀" -c green) "\ - "$(formatText "██" -c light-yellow)$(formatText "▀" -c yellow) "\ - "$(formatText "██" -c light-blue)$(formatText "▀" -c blue) "\ - "$(formatText "██" -c light-magenta)$(formatText "▀" -c magenta) "\ - "$(formatText "██" -c light-cyan)$(formatText "▀" -c cyan) "\ - "$(formatText "██" -c white)$(formatText "▀" -c light-gray) ") - - printInfoLine "" "$palette_top" - printInfoLine "Color palette" "$palette_bot" -} - - - -##------------------------------------------------------------------------------ -## -printInfoCPUTemp() -{ - if ( which sensors > /dev/null 2>&1 ); then - - ## GET VALUES - local temp_line=$(sensors 2>/dev/null |\ - grep Core |\ - head -n 1 |\ - sed 's/^.*:[ \t]*//g;s/[\(\),]//g') - local units=$(echo $temp_line |\ - sed -n 's/.*\(°[[CF]]*\).*/\1/p') - local current=$(echo $temp_line |\ - sed -n 's/^.*+\(.*\)°[[CF]]*[ \t]*h.*/\1/p') - local high=$(echo $temp_line |\ - sed -n 's/^.*high = +\(.*\)°[[CF]]*[ \t]*c.*/\1/p') - local max=$(echo $temp_line |\ - sed -n 's/^.*crit = +\(.*\)°[[CF]]*[ \t]*.*/\1/p') - - - ## DETERMINE STATE - if (( $(echo "$current < $high" |bc -l) )); then - local state="nominal" - elif (( $(echo "$current < $max" |bc -l) )); then - local state="critical"; - else - local state="error"; - fi - - - ## PRINT MESSAGE - local temp="$current$units" - printInfoLine "CPU temp" "$temp" "$state" - else - printInfoLine "CPU temp" "lm-sensors not installed" - fi - - -} - - - -printResourceMonitor() -{ - local label=$1 - local value=$2 - local max=$3 - local units=$4 - local format=$5 - local crit_percent=$6 - local error_percent=${7:-99} - - - ## CHECK STATE - local percent=$('bc' <<< "$value*100/$max") - local state="nominal" - if [ $percent -gt $error_percent ]; then - local state="error" - elif [ $percent -gt $crit_percent ]; then - local state="critical" - fi - - - printInfoMonitor "$label" "$current_value" "$max" "$units" "$format" "$state" -} - - - - -##------------------------------------------------------------------------------ -## -printMonitorCPU() -{ - assert_is_set $bar_cpu_crit_percent - - local format=$1 - local label="Sys load avg" - local units="" - local current_value=$(awk '{avg_1m=($1)} END {printf "%3.2f", avg_1m}' /proc/loadavg) - local max=$(nproc --all) - local crit_percent=$bar_cpu_crit_percent - - printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" -} - - - -##------------------------------------------------------------------------------ -## -printMonitorRAM() -{ - assert_is_set $bar_ram_units - assert_is_set $bar_ram_crit_percent - - local format=$1 - local label="Memory" - - case "$bar_ram_units" in - "MB") local units="MB"; local option="--mega" ;; - "TB") local units="TB"; local option="--tera" ;; - "PB") local units="PB"; local option="--peta" ;; - *) local units="GB"; local option="--giga" ;; - esac - - local mem_info=$('free' "$option" | head -n 2 | tail -n 1) - local current_value=$(echo "$mem_info" | awk '{mem=($2-$7)} END {printf mem}') - local max=$(echo "$mem_info" | awk '{mem=($2)} END {printf mem}') - local crit_percent=$bar_ram_crit_percent - - printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" -} - - - -##------------------------------------------------------------------------------ -## -printMonitorSwap() -{ - assert_is_set $bar_swap_units - assert_is_set $bar_swap_crit_percent - - local format=$1 - local label="Swap" - - case "$bar_swap_units" in - "MB") local units="MB"; local option="--mega" ;; - "TB") local units="TB"; local option="--tera" ;; - "PB") local units="PB"; local option="--peta" ;; - *) local units="GB"; local option="--giga" ;; - esac - - ## CHECK IF SYSTEM HAS SWAP - ## Count number of lines in /proc/swaps, excluding the header (-1) - ## This is not fool-proof, but if num_swap_devs>=1, there should be swap - local num_swap_devs=$(($(wc -l /proc/swaps | awk '{print $1;}') -1)) - - if [ "$num_swap_devs" -lt 1 ]; then - printInfoLine "$label" "N/A" - - else - local swap_info=$('free' "$option" | tail -n 1) - local current_value=$(echo "$swap_info" | awk '{SWAP=($3)} END {printf SWAP}') - local max=$(echo "$swap_info" | awk '{SWAP=($2)} END {printf SWAP}') - local crit_percent=$bar_swap_crit_percent - - printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" - fi -} - - -##------------------------------------------------------------------------------ -## -printStorageMonitor() -{ - local label=$1 - local device=$2 - local units=$3 - local format=$4 - local crit_percent=$5 - local error_percent=${6:-99} - - case "$units" in - "MB") local units="MB"; local option="M" ;; - "TB") local units="TB"; local option="T" ;; - "PB") local units="PB"; local option="P" ;; - *) local units="GB"; local option="G" ;; - esac - - local current_value=$(df "-B1${option}" "${device}" | grep / | awk '{key=($3)} END {printf key}') - local max=$(df "-B1${option}" "${device}" | grep / | awk '{key=($2)} END {printf key}') - printResourceMonitor "$label" "$current_value" "$max" "$units" "$format" "$crit_percent" "$error_percent" - -} - - -##------------------------------------------------------------------------------ -## -printMonitorHDD() -{ - assert_is_set $bar_hdd_units - assert_is_set $bar_hdd_crit_percent - - local format=$1 - local label="Storage /" - local device="/" - local units=$bar_hdd_units - local crit_percent=$bar_hdd_crit_percent - - printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" -} - - - -##------------------------------------------------------------------------------ -## -printMonitorHome() -{ - assert_is_set $bar_home_units - assert_is_set $bar_home_crit_percent - - local format=$1 - local label="Storage /home" - local device=$HOME - local units=$bar_home_units - local crit_percent=$bar_home_crit_percent - - printStorageMonitor "$label" "$device" "$units" "$format" "$crit_percent" -} - - - -##------------------------------------------------------------------------------ -## -printMonitorCPUTemp() -{ - if ( which sensors > /dev/null 2>&1 ); then - - ## GET VALUES - local temp_line=$(sensors |\ - grep Core |\ - head -n 1 |\ - sed 's/^.*:[ \t]*//g;s/[\(\),]//g') - local units=$(echo $temp_line |\ - sed -n 's/.*\(°[[CF]]*\).*/\1/p' ) - local current=$(echo $temp_line |\ - sed -n 's/^.*+\(.*\)°[[CF]]*[ \t]*h.*/\1/p' ) - local high=$(echo $temp_line |\ - sed -n 's/^.*high = +\(.*\)°[[CF]]*[ \t]*c.*/\1/p' ) - local max=$(echo $temp_line |\ - sed -n 's/^.*crit = +\(.*\)°[[CF]]*[ \t]*.*/\1/p' ) - local crit_percent=$(bc <<< "$high*100/$max") - - - ## PRINT MONITOR - printResourceMonitor $current $max $crit_percent \ - false $units "CPU temp" - else - printInfoLine "CPU temp" "lm-sensors not installed" - fi -} @@ -488,7 +147,7 @@ printStatusInfo() PALETTE_SMALL) printInfoColorpaletteSmall;; PALETTE) printInfoColorpaletteFancy;; SPACER) printInfoSpacer;; - CPULOAD) printInfoCPULoad;; + CPULOAD) printInfoCPULoad;; CPUTEMP) printInfoCPUTemp;; ## USAGE MONITORS (BARS) @@ -613,7 +272,7 @@ printHogsCPU() local percent=$(bc <<< "$current*100/$max") - if [ $percent -gt $bar_cpu_crit_percent ]; then + if [ "$percent" -gt "$bar_cpu_crit_percent" ]; then ## CALL TOP IN BATCH MODE ## Check if "%Cpus(s)" is shown, otherwise, call "top -1" ## Escape all '%' characters @@ -706,17 +365,17 @@ printHogsMemory() ##============================================================================== ## PRINT TOP SPACER -if $clear_before_print; then clear; fi +#if $clear_before_print; then clear; fi if $print_extra_new_line_top; then echo ""; fi ## PRINT GREETER ELEMENTS printHeader -printLastLogins -printSystemctl -printHogsCPU -printHogsMemory +#printLastLogins +#printSystemctl +#printHogsCPU +#printHogsMemory From bfd35c333036ed7baf2e51ce1db54aaa08bb1a01 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 10:47:50 +0200 Subject: [PATCH 10/16] Fix issue with recursive include, baby one more time Signed-off-by: Andres Gongora --- bash-tools | 2 +- setup.sh | 5 +++-- synth-shell-greeter/synth-shell-greeter.sh | 20 ++------------------ 3 files changed, 6 insertions(+), 21 deletions(-) diff --git a/bash-tools b/bash-tools index 8f7ca9b..32fde20 160000 --- a/bash-tools +++ b/bash-tools @@ -1 +1 @@ -Subproject commit 8f7ca9be0f030ca733a1a845735cfa0d440f6cc2 +Subproject commit 32fde20cdca37d54baeb1ab441f20e830d0147d1 diff --git a/setup.sh b/setup.sh index 91a7f10..afdc61f 100755 --- a/setup.sh +++ b/setup.sh @@ -28,7 +28,8 @@ ##============================================================================== ## -include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} +include(){ { [ -z "$_IR" ]&&_IR="$PWD"&&cd "$(dirname "$PWD/$0")"&&include "$1"&&cd "$_IR"&&unset _IR;}||{ local d=$PWD&&cd "$(dirname "$PWD/$1")"&&. "$(basename "$1")"&&cd "$d";}||{ echo "Include failed $PWD->$1"&&exit 1;};} + include 'bash-tools/bash-tools/user_io.sh' include 'bash-tools/bash-tools/hook_script.sh' include 'bash-tools/bash-tools/assemble_script.sh' @@ -57,7 +58,7 @@ fi ## DEFINE LOCAL VARIABLES DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd ) INPUT_SCRIPT="$DIR/synth-shell-greeter/synth-shell-greeter.sh" -INPUT_CONFIG_DIR="$DIR/config" +INPUT_CONFIG_DIR="$DIR/config/" ## HEADER TO BE ADDED AT THE TOP OF THE ASSEMBLED SCRIPT diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index f2d2eea..1be3a17 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -31,7 +31,8 @@ ## INCLUDE EXTERNAL DEPENDENCIES -include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} +include(){ { [ -z "$_IR" ]&&_IR="$PWD"&&cd "$(dirname "$PWD/$0")"&&include "$1"&&cd "$_IR"&&unset _IR;}||{ local d=$PWD&&cd "$(dirname "$PWD/$1")"&&. "$(basename "$1")"&&cd "$d";}||{ echo "Include failed $PWD->$1"&&exit 1;};} + include '../bash-tools/bash-tools/color.sh' include '../bash-tools/bash-tools/print_utils.sh' include 'info.sh' @@ -100,23 +101,6 @@ local fc_text="$fc_highlight" - - - - -##============================================================================== -## -##============================================================================== - - - - - - - - - - ##============================================================================== ## STATUS INFO COMPOSITION ##============================================================================== From 2a1cc0c5b56f343f2376295cefd8120f11794796 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 10:48:46 +0200 Subject: [PATCH 11/16] Remove stray local Signed-off-by: Andres Gongora --- setup.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.sh b/setup.sh index afdc61f..2705f93 100755 --- a/setup.sh +++ b/setup.sh @@ -92,7 +92,7 @@ cp -r "$INPUT_CONFIG_DIR/." "$OUTPUT_CONFIG_DIR/" ## SETUP DEFAULT SYNTH-SHELL-GREETER CONFIG FILE CONFIG_FILE="$OUTPUT_CONFIG_DIR/synth-shell-greeter.config" if [ ! -f "$CONFIG_FILE" ]; then - local DISTRO=$(cat /etc/os-release | grep "ID=" | sed 's/ID=//g' | head -n 1) + DISTRO=$(cat /etc/os-release | grep "ID=" | sed 's/ID=//g' | head -n 1) case "$DISTRO" in 'arch' ) cp "$OUTPUT_CONFIG_DIR/os/synth-shell-greeter.archlinux.config" "$CONFIG_FILE" ;; 'manjaro' ) cp "$OUTPUT_CONFIG_DIR/os/synth-shell-greeter.manjaro.config" "$CONFIG_FILE" ;; From 8b5b11edf540a7bf8dcf043670be7441ac180275 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 21:36:39 +0200 Subject: [PATCH 12/16] I coudnt split a string with spaces, so I moved printInfoGPU Signed-off-by: Andres Gongora --- synth-shell-greeter/info.sh | 57 +++++++++++++++++-- ...nfo_hardware.sh => info_about_hardware.sh} | 41 +------------ ...{info_network.sh => info_about_network.sh} | 0 .../{info_os.sh => info_about_os.sh} | 0 4 files changed, 54 insertions(+), 44 deletions(-) rename synth-shell-greeter/{info_hardware.sh => info_about_hardware.sh} (68%) rename synth-shell-greeter/{info_network.sh => info_about_network.sh} (100%) rename synth-shell-greeter/{info_os.sh => info_about_os.sh} (100%) diff --git a/synth-shell-greeter/info.sh b/synth-shell-greeter/info.sh index 288ef96..5406873 100755 --- a/synth-shell-greeter/info.sh +++ b/synth-shell-greeter/info.sh @@ -44,7 +44,7 @@ include 'info_print_info.sh' ## ONE LINERS ##============================================================================== -include 'info_os.sh' +include 'info_about_os.sh' printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } @@ -54,12 +54,11 @@ printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } -include 'info_hardware.sh' +include 'info_about_hardware.sh' printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } -printInfoGPU() { printInfoLine "GPU" "$(getNameGPU)" ; } printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } -include 'info_network.sh' +include 'info_about_network.sh' printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } @@ -70,6 +69,56 @@ printInfoSpacer() { printInfoLine "" "" ; } + + +##============================================================================== +## +##============================================================================== + +printInfoGPU() +{ + # DETECT GPU(s)set + local gpu_ids=($(lspci 2>/dev/null | grep ' VGA ' | cut -d" " -f 1)) + + # FOR ALL DETECTED IDs + # Get the GPU name, but trim all buzzwords away + for id in "${gpu_ids[@]}"; do + local gpu=$(lspci -v -s "$id" 2>/dev/null |\ + head -n 1 |\ + sed 's/^.*: //g;s/(.*$//g; + s/Generation Core Processor Family Integrated Graphics Controller /gen IGC/g; + s/Corporation//g; + s/Core Processor//g; + s/Series//g; + s/Chipset//g; + s/Graphics//g; + s/processor//g; + s/Controller//g; + s/Family//g; + s/Inc.//g; + s/,//g; + s/Technology//g; + s/Mobility/M/g; + s/Advanced Micro Devices/AMD/g; + s/\[AMD\/ATI\]/ATI/g; + s/Integrated Graphics Controller/HD Graphics/g; + s/Integrated Controller/IC/g; + s/ */ /g' + ) + # If GPU name still to long, remove anything between [] + if [ "${#gpu}" -gt 30 ]; then + local gpu=$(printf "$gpu" | sed 's/\[.*\]//g' ) + fi + + printInfoLine "GPU" "$gpu" + done +} + + + + + + ##============================================================================== ## ##============================================================================== diff --git a/synth-shell-greeter/info_hardware.sh b/synth-shell-greeter/info_about_hardware.sh similarity index 68% rename from synth-shell-greeter/info_hardware.sh rename to synth-shell-greeter/info_about_hardware.sh index 0d72e63..0f5aa96 100755 --- a/synth-shell-greeter/info_hardware.sh +++ b/synth-shell-greeter/info_about_hardware.sh @@ -60,46 +60,7 @@ getNameCPU() ##============================================================================== ## -getNameGPU() -{ - ## DETECT GPU(s)set - local gpu_id=$(lspci 2>/dev/null | grep ' VGA ' | cut -d" " -f 1) - - ## FOR ALL DETECTED IDs - ## Get the GPU name, but trim all buzzwords away - echo -e "$gpu_id" | while read line ; do - local gpu=$(lspci -v -s "$line" 2>/dev/null |\ - head -n 1 |\ - sed 's/^.*: //g;s/(.*$//g; - s/Corporation//g; - s/Core Processor//g; - s/Series//g; - s/Chipset//g; - s/Graphics//g; - s/processor//g; - s/Controller//g; - s/Family//g; - s/Inc.//g; - s/,//g; - s/Technology//g; - s/Mobility/M/g; - s/Advanced Micro Devices/AMD/g; - s/\[AMD\/ATI\]/ATI/g; - s/Integrated Graphics Controller/HD Graphics/g; - s/Integrated Controller/IC/g; - s/Generation Integrated/Gen IC/g; - s/ */ /g' - ) - - ## If GPU name still to long, remove anything between [] - if [ "${#gpu}" -gt 30 ]; then - local gpu=$(echo "$gpu" | sed 's/\[.*\]//g' ) - fi - - - printf "$gpu" - done -} + diff --git a/synth-shell-greeter/info_network.sh b/synth-shell-greeter/info_about_network.sh similarity index 100% rename from synth-shell-greeter/info_network.sh rename to synth-shell-greeter/info_about_network.sh diff --git a/synth-shell-greeter/info_os.sh b/synth-shell-greeter/info_about_os.sh similarity index 100% rename from synth-shell-greeter/info_os.sh rename to synth-shell-greeter/info_about_os.sh From 96fc1e9dc070bb6d04901d3f5b0491d1c75e7c29 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 21:42:16 +0200 Subject: [PATCH 13/16] Fix Arch Logo Signed-off-by: Andres Gongora --- config/os/synth-shell-greeter.archlinux.config | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/os/synth-shell-greeter.archlinux.config b/config/os/synth-shell-greeter.archlinux.config index d89b6fd..007c520 100644 --- a/config/os/synth-shell-greeter.archlinux.config +++ b/config/os/synth-shell-greeter.archlinux.config @@ -20,11 +20,11 @@ logo=" . \e[0;34m .######; ;###;\`\". \e[0;34m .#######; ;#####. \e[0;34m #########. .########\` -\e[0;34m ######\' \'###### +\e[0;34m ######' '###### \e[0;34m ;#### ####; -\e[0;34m ##\' \'## -\e[0;34m #\' \`# -\e[0;34m ' \` " +\e[0;34m ##' '## +\e[0;34m #' '# +\e[0;34m ' ' " From bd121a7ffaecc21cef906d82ab55c213813e599a Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 21:53:38 +0200 Subject: [PATCH 14/16] Fix Arch Logo Signed-off-by: Andres Gongora --- .../os/synth-shell-greeter.archlinux.config | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/config/os/synth-shell-greeter.archlinux.config b/config/os/synth-shell-greeter.archlinux.config index 007c520..892e6e3 100644 --- a/config/os/synth-shell-greeter.archlinux.config +++ b/config/os/synth-shell-greeter.archlinux.config @@ -6,25 +6,25 @@ ## - https://wiki.archlinux.org/index.php/ASCII_art ## - https://bbs.archlinux.org/viewtopic.php?id=24208&p=6 -logo=" . +logo="\e[0;94m . \e[0;94m # \e[0;94m ### \e[0;94m ##### -\e[0;94m ###### -\e[0;94m ; #####; -\e[0;94m +##.##### -\e[0;94m +########## +\e[0;94m '###### +\e[0;94m .. #####; +\e[0;94m ###.##### +\e[0;94m ########### \e[0;94m ######\e[0;34m#####\e[0;94m##; -\e[0;94m ###\e[0;34m############\e[0;94m+ +\e[0;94m ###\e[0;34m############\e[0;94m \e[0;94m #\e[0;34m###### ####### -\e[0;34m .######; ;###;\`\". -\e[0;34m .#######; ;#####. -\e[0;34m #########. .########\` -\e[0;34m ######' '###### +\e[0;34m ######; ;###;\`\". +\e[0;34m #######; ;#####. +\e[0;34m ;########. .########' +\e[0;34m ;#####' '#####; \e[0;34m ;#### ####; \e[0;34m ##' '## \e[0;34m #' '# -\e[0;34m ' ' " +\e[0;34m ' ' " From 1a55e309ab4ef5969d74e560ccd89306c8351cde Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 21:57:37 +0200 Subject: [PATCH 15/16] Fix Arch Logo Signed-off-by: Andres Gongora --- config/os/synth-shell-greeter.archlinux.config | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/config/os/synth-shell-greeter.archlinux.config b/config/os/synth-shell-greeter.archlinux.config index 892e6e3..18c7a9a 100644 --- a/config/os/synth-shell-greeter.archlinux.config +++ b/config/os/synth-shell-greeter.archlinux.config @@ -11,17 +11,17 @@ logo="\e[0;94m . \e[0;94m ### \e[0;94m ##### \e[0;94m '###### -\e[0;94m .. #####; +\e[0;94m .. #####: \e[0;94m ###.##### \e[0;94m ########### \e[0;94m ######\e[0;34m#####\e[0;94m##; \e[0;94m ###\e[0;34m############\e[0;94m \e[0;94m #\e[0;34m###### ####### -\e[0;34m ######; ;###;\`\". -\e[0;34m #######; ;#####. -\e[0;34m ;########. .########' -\e[0;34m ;#####' '#####; -\e[0;34m ;#### ####; +\e[0;34m ######: :###.\`\". +\e[0;34m #######: :####:. +\e[0;34m :########. .######## +\e[0;34m :#####' '#####: +\e[0;34m :#### ####: \e[0;34m ##' '## \e[0;34m #' '# \e[0;34m ' ' " From 35b5576041ebd19a0053971b049a66022b68d240 Mon Sep 17 00:00:00 2001 From: Andres Gongora Date: Fri, 17 Apr 2020 22:58:29 +0200 Subject: [PATCH 16/16] Update README and quick fix Signed-off-by: Andres Gongora --- README.md | 59 ++++++++++------------ synth-shell-greeter/info.sh | 11 ++-- synth-shell-greeter/synth-shell-greeter.sh | 10 ++-- 3 files changed, 35 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index 69fc0f7..26a5033 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ ![synth-shell](doc/synth-shell-status.jpg) -**synth-shell-status** shows a summary of your system's current health. +**synth-shell-greeter** shows a summary of your system's current health. - Automatically printed in new terminal sessions (local, SSH, ...). - Monitor your servers, RaspberryPis, and workstations. All system info you need at a glance (e.g. external IP address, CPU temperature, etc.). @@ -18,36 +18,33 @@ # Setup -### Automatic setup + +### Arch Linux + +You may install `synth-shell-greeter` from AUR: +https://aur.archlinux.org/packages/synth-shell-greeter-git/ + + + +### Manual setup The included [setup script](setup.sh) will guide you step by step through the -process and let you choose what features to install. During this setup, you can -choose to install synth-shell for your user only (recommended) or system-wide -(superuser privileges required). To proceed, -[open and play this link in a separate tab](https://www.youtube.com/embed/MpN91wHAr1k) -and enter the following into your terminal: +installatioj process. Just clone this repository and run it: ``` -git clone --recursive https://github.com/andresgongora/synth-shell.git -chmod +x synth-shell/setup.sh -synth-shell/setup.sh +git clone --recursive https://github.com/andresgongora/synth-shell-greeter.git +synth-shell-greeter/setup.sh ``` -Note that for `fancy-bash-prompt.sh` you might also need -[power-line fonts](https://github.com/powerline/fonts). You can instal it -as follows (the exact name of the package varies from distro to distro): - -* ArchLinux: `sudo pacman -S powerline-fonts` -* Debian/Ubuntu: `sudo apt install fonts-powerline` +You can then test your script by running it from wherever you installed it. +Usually this is to your user's `.config` folder, so you should run: +``` +~/.config/synth-shell/synth-shell-greeter.sh +``` -Finally, open up a new terminal and test that everything works. Sometimes, -despite power-line fonts being properly installed, the triangle separator -for `fancy-bash-prompt.sh` (if installed) might still not show. In this case, -make sure that your `locale` is set to UTF-8 by editing `/etc/locale.conf` file -(select your language but in UTF-8 format) and running `sudo locale-gen`. -[More info on locale](https://wiki.archlinux.org/index.php/locale). -Alternatively, try a different font in your terminal emulator. Some fonts -do not support special characters. We get the best results with -[hack-ttf](https://sourcefoundry.org/hack/). +If you want it to appear everytime you open a new terminal, run +``` +echo "~/.config/synth-shell/synth-shell-greeter.sh" >> ~/.bashrc +``` @@ -61,11 +58,6 @@ folders depending on how you installed **synth-shell**: -### Uninstallation -Run the setup script again (like during the installation), but choose -`uninstall` when prompted. - -

@@ -83,9 +75,10 @@ prints a user-configurable ASCII logo to impress your crush from the library with how awesome you are. Feel free to customize your status report through the many available options -in `~/.config/synth-shell/status.config` (user-only install) or -`/etc/synth-shell/status.config` (system-wide install),or by replacing their -content with the examples files you can find under the same directory. +in `~/.config/synth-shell/synth-shell-greeter.config` (user-only install) or +`/etc/synth-shell/synth-shell-greeter.config` (system-wide install), or by +replacing their content with the examples files you can find under the same +directory. ![status configuration options](doc/status_config_preview.png) diff --git a/synth-shell-greeter/info.sh b/synth-shell-greeter/info.sh index 5406873..34328c0 100755 --- a/synth-shell-greeter/info.sh +++ b/synth-shell-greeter/info.sh @@ -34,9 +34,9 @@ include(){ [ -z "$_IR" ]&&_IR="$PWD"&&cd $( dirname "$PWD/$0" )&&. "$1"&&cd "$_IR"&&unset _IR||. $1;} include 'info_print_info.sh' - - - +include 'info_about_os.sh' +include 'info_about_hardware.sh' +include 'info_about_network.sh' @@ -44,7 +44,7 @@ include 'info_print_info.sh' ## ONE LINERS ##============================================================================== -include 'info_about_os.sh' + printInfoOS() { printInfoLine "OS" "$(getNameOS)" ; } printInfoKernel() { printInfoLine "Kernel" "$(getNameKernel)" ; } printInfoShell() { printInfoLine "Shell" "$(getNameShell)" ; } @@ -54,11 +54,9 @@ printInfoUser() { printInfoLine "User" "$(getUserHost)" ; } printInfoNumLoggedIn() { printInfoLine "Logged in" "$(getNumberLoggedInUsers)" ; } printInfoNameLoggedIn() { printInfoLine "Logged in" "$(getNameLoggedInUsers)" ; } -include 'info_about_hardware.sh' printInfoCPU() { printInfoLine "CPU" "$(getNameCPU)" ; } printInfoCPULoad() { printInfoLine "Sys load" "$(getCPULoad)" ; } -include 'info_about_network.sh' printInfoLocalIPv4() { printInfoLine "Local IPv4" "$(getLocalIPv4)" ; } printInfoExternalIPv4() { printInfoLine "External IPv4" "$(getExternalIPv4)" ; } @@ -70,7 +68,6 @@ printInfoSpacer() { printInfoLine "" "" ; } - ##============================================================================== ## ##============================================================================== diff --git a/synth-shell-greeter/synth-shell-greeter.sh b/synth-shell-greeter/synth-shell-greeter.sh index 1be3a17..befed05 100755 --- a/synth-shell-greeter/synth-shell-greeter.sh +++ b/synth-shell-greeter/synth-shell-greeter.sh @@ -349,17 +349,17 @@ printHogsMemory() ##============================================================================== ## PRINT TOP SPACER -#if $clear_before_print; then clear; fi +if $clear_before_print; then clear; fi if $print_extra_new_line_top; then echo ""; fi ## PRINT GREETER ELEMENTS printHeader -#printLastLogins -#printSystemctl -#printHogsCPU -#printHogsMemory +printLastLogins +printSystemctl +printHogsCPU +printHogsMemory