From b3ca6fcdaa80ba68dd66b57840e292574dca6248 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Sun, 12 May 2013 23:14:06 +0200 Subject: [PATCH 1/5] Fix _lp_battery return value when LP_ENABLE_BATT != 1 --- liquidprompt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/liquidprompt b/liquidprompt index 9cc75a7..46b887b 100755 --- a/liquidprompt +++ b/liquidprompt @@ -1014,7 +1014,7 @@ _lp_bzr_branch_color() # returns 4 if no battery support _lp_battery() { - [[ "$LP_ENABLE_BATT" != 1 ]] && return + [[ "$LP_ENABLE_BATT" != 1 ]] && return 4 local acpi acpi="$(acpi --battery 2>/dev/null)" # Extract the battery load value in percent From b50970f634d16d4c59154acf126c69602f343d86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 13 May 2013 20:23:55 +0200 Subject: [PATCH 2/5] Optimize _lp_color_map using arithmetic comparisons --- liquidprompt | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/liquidprompt b/liquidprompt index 46b887b..1e14ad4 100755 --- a/liquidprompt +++ b/liquidprompt @@ -1127,28 +1127,28 @@ _lp_battery_color() } _lp_color_map() { - if [[ $1 -ge 0 ]] && [[ $1 -lt 20 ]] ; then + local -i load + load=$1 + if (( load < 20 )); then echo -ne "${LP_COLORMAP_0}" - elif [[ $1 -ge 20 ]] && [[ $1 -lt 40 ]] ; then + elif (( load < 40 )); then echo -ne "${LP_COLORMAP_1}" - elif [[ $1 -ge 40 ]] && [[ $1 -lt 60 ]] ; then + elif (( load < 60 )); then echo -ne "${LP_COLORMAP_2}" - elif [[ $1 -ge 60 ]] && [[ $1 -lt 80 ]] ; then + elif (( load < 80 )); then echo -ne "${LP_COLORMAP_3}" - elif [[ $1 -ge 80 ]] && [[ $1 -lt 100 ]] ; then + elif (( load < 100 )); then echo -ne "${LP_COLORMAP_4}" - elif [[ $1 -ge 100 ]] && [[ $1 -lt 120 ]] ; then + elif (( load < 120 )); then echo -ne "${LP_COLORMAP_5}" - elif [[ $1 -ge 120 ]] && [[ $1 -lt 140 ]] ; then + elif (( load < 140 )); then echo -ne "${LP_COLORMAP_6}" - elif [[ $1 -ge 140 ]] && [[ $1 -lt 160 ]] ; then + elif (( load < 160 )); then echo -ne "${LP_COLORMAP_7}" - elif [[ $1 -ge 160 ]] && [[ $1 -lt 180 ]] ; then + elif (( load < 180 )) ; then echo -ne "${LP_COLORMAP_8}" - elif [[ $1 -ge 180 ]] ; then + else # (( load >= 180 )) echo -ne "${LP_COLORMAP_9}" - else - echo -ne "${LP_COLORMAP_0}" fi } From 43511f6f15375236d575a043f0806fd1a27e6c0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 13 May 2013 20:40:04 +0200 Subject: [PATCH 3/5] _lp_color_map: use dichotomy to more evenly distribute cost --- liquidprompt | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/liquidprompt b/liquidprompt index 1e14ad4..e64c9c4 100755 --- a/liquidprompt +++ b/liquidprompt @@ -1129,22 +1129,28 @@ _lp_battery_color() _lp_color_map() { local -i load load=$1 - if (( load < 20 )); then - echo -ne "${LP_COLORMAP_0}" - elif (( load < 40 )); then - echo -ne "${LP_COLORMAP_1}" - elif (( load < 60 )); then - echo -ne "${LP_COLORMAP_2}" - elif (( load < 80 )); then - echo -ne "${LP_COLORMAP_3}" - elif (( load < 100 )); then - echo -ne "${LP_COLORMAP_4}" - elif (( load < 120 )); then - echo -ne "${LP_COLORMAP_5}" - elif (( load < 140 )); then - echo -ne "${LP_COLORMAP_6}" + if (( load < 100 )); then + if (( load < 60 )); then + if (( load < 20 )); then + echo -ne "${LP_COLORMAP_0}" + elif (( load < 40 )); then + echo -ne "${LP_COLORMAP_1}" + else # 40..59 + echo -ne "${LP_COLORMAP_2}" + fi + elif (( load < 80 )); then + echo -ne "${LP_COLORMAP_3}" + else # 80..99 + echo -ne "${LP_COLORMAP_4}" + fi elif (( load < 160 )); then - echo -ne "${LP_COLORMAP_7}" + if (( load < 120 )); then + echo -ne "${LP_COLORMAP_5}" + elif (( load < 140 )); then + echo -ne "${LP_COLORMAP_6}" + else + echo -ne "${LP_COLORMAP_7}" + fi elif (( load < 180 )) ; then echo -ne "${LP_COLORMAP_8}" else # (( load >= 180 )) From ea1c897aefa32dc0790aa717ba286631e693bb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Mon, 13 May 2013 20:46:44 +0200 Subject: [PATCH 4/5] _lp_color_map: rename 'load' to 'value' --- liquidprompt | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/liquidprompt b/liquidprompt index e64c9c4..d560f69 100755 --- a/liquidprompt +++ b/liquidprompt @@ -1127,33 +1127,33 @@ _lp_battery_color() } _lp_color_map() { - local -i load - load=$1 - if (( load < 100 )); then - if (( load < 60 )); then - if (( load < 20 )); then + local -i value + value=$1 + if (( value < 100 )); then + if (( value < 60 )); then + if (( value < 20 )); then echo -ne "${LP_COLORMAP_0}" - elif (( load < 40 )); then + elif (( value < 40 )); then echo -ne "${LP_COLORMAP_1}" else # 40..59 echo -ne "${LP_COLORMAP_2}" fi - elif (( load < 80 )); then + elif (( value < 80 )); then echo -ne "${LP_COLORMAP_3}" else # 80..99 echo -ne "${LP_COLORMAP_4}" fi - elif (( load < 160 )); then - if (( load < 120 )); then + elif (( value < 160 )); then + if (( value < 120 )); then echo -ne "${LP_COLORMAP_5}" - elif (( load < 140 )); then + elif (( value < 140 )); then echo -ne "${LP_COLORMAP_6}" else echo -ne "${LP_COLORMAP_7}" fi - elif (( load < 180 )) ; then + elif (( value < 180 )) ; then echo -ne "${LP_COLORMAP_8}" - else # (( load >= 180 )) + else # (( value >= 180 )) echo -ne "${LP_COLORMAP_9}" fi } From e7ae332fc805829a542676484244f53012e7b801 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Mengu=C3=A9?= Date: Tue, 14 May 2013 01:15:04 +0200 Subject: [PATCH 5/5] Optimize _lp_load_color --- liquidprompt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/liquidprompt b/liquidprompt index d560f69..c105154 100755 --- a/liquidprompt +++ b/liquidprompt @@ -1174,22 +1174,23 @@ _lp_load_color() [[ "$LP_ENABLE_LOAD" != 1 ]] && return - local load - load="$(_lp_cpu_load | sed 's/\.//g;s/^0*//g' )" - let "load=${load:-0}/$_lp_CPUNUM" + local tmp=$(_lp_cpu_load) + tmp=${tmp/./} # Remove '.' + tmp=${tmp#0} # Remove leading '0' + tmp=${tmp#0} # Remove leading '0', again (ex: 0.09) + local -i load=${tmp:-0}/$_lp_CPUNUM - if [[ $load -ge $LP_LOAD_THRESHOLD ]] - then - local ret="$(_lp_color_map $load) ${LP_MARK_LOAD}" + if (( load > $LP_LOAD_THRESHOLD )); then + local ret="$(_lp_color_map $load)${LP_MARK_LOAD}" - if [[ "$LP_PERCENTS_ALWAYS" -eq "1" ]]; then + if [[ "$LP_PERCENTS_ALWAYS" == 1 ]]; then if $_LP_SHELL_bash; then ret="${ret}$load%" else # zsh ret="${ret}$load%%" fi fi - echo -ne "${ret}${NO_COL}" + echo -nE "${ret}${NO_COL}" fi }