Add major improvements

Add dynamic foreground color support
Add dynamic background color support
Add dynamic icon support
This commit is contained in:
Camille TJHOA 2017-05-07 23:44:24 +02:00
parent 9cfbd9d95c
commit ed2f1590b6
15 changed files with 207 additions and 52 deletions

115
README.md
View file

@ -2,28 +2,7 @@
Enables displaying cpu percentage and status icon in Tmux status-right.
CPU:<br/>
`CPU: ❏ 8.7%`
This is done by introducing 2 new format strings that can be added to
`status-right` option:
- `#{cpu_icon}` - will display a cpu status icon
- `#{cpu_percentage}` - will show cpu percentage
### Usage
Add `#{cpu_icon}` or `#{cpu_percentage}` format strings to existing
`status-right` Tmux option. Example:
# in .tmux.conf
set -g status-right "CPU: #{cpu_icon} #{cpu_percentage} | %a %h-%d %H:%M "
### Optional requirement (Linux, BSD, OSX)
`iostat` or `sar` are the best way to get an accurate cpu percentage
A fallback is included using `ps -aux` but could be inaccurate.
## Installation
### Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) (recommended)
Add plugin to the list of TPM plugins in `.tmux.conf`:
@ -51,35 +30,93 @@ Reload TMUX environment:
If format strings are added to `status-right`, they should now be visible.
### Changing icons
### Optional requirement (Linux, BSD, OSX)
`iostat` or `sar` are the best way to get an accurate cpu percentage
A fallback is included using `ps -aux` but could be inaccurate.
## Usage
Add `#{cpu_icon}`, `#{cpu_percentage}` `#{cpu_fg_color}`, or
`#{cpu_bg_color}` format strings to existing `status-right` tmux option.
Example:
# in .tmux.conf
set -g status-right '#{cpu_bg_color} CPU: #{cpu_icon} #{cpu_percentage} | %a %h-%d %H:%M '
## Examples
CPU higher than 80%:<br/>
![low_fg](/screenshots/low_fg.png)
![low_bg](/screenshots/low_bg.png)
![low_icon](/screenshots/low_icon.png)
CPU between 30% and 80%:<br/>
![medium_fg](/screenshots/medium_fg.png)
![medium_bg](/screenshots/medium_bg.png)
![medium_icon](/screenshots/medium_icon.png)
CPU lower than 30%:<br/>
![high_fg](/screenshots/high_fg.png)
![high_bg](/screenshots/high_bg.png)
![high_icon](/screenshots/high_icon.png)
This is done by introducing 4 new format strings that can be added to
`status-right` option:
- `#{cpu_icon}` - will display a cpu status icon
- `#{cpu_percentage}` - will show cpu percentage
- `#{cpu_bg_color}` - will set the background color of the status bar based on the cpu percentage
- `#{cpu_fg_color}` - will set the foreground color of the status bar based on the cpu percentage
## Changing default
By default, these icons are displayed:
- cpu: "❏"
- low: "="
- medium: "≡"
- high: "≣"
You can change these defaults by adding the following to `.tmux.conf` (the
following lines are not in the code block so that emojis can be seen):
And these colors are used:
- set-option -g @cpu_icon ":sunglasses:"
- low: `#[fg=green]` `#[bg=green]`
- medium: `#[fg=yellow]` `#[bg=yellow]`
- high: `#[fg=red]` `#[bg=red]`
Don't forget to reload TMUX environment (`$ tmux source-file ~/.tmux.conf`)
Note that these colors depend on your terminal / X11 config.
You can change these defaults by adding the following to `.tmux.conf`:
```
set -g @cpu_low_icon "ᚋ"
set -g @cpu_medium_icon "ᚌ"
set -g @cpu_high_icon "ᚍ"
set -g @cpu_low_fg_color "#[fg=#00ff00]"
set -g @cpu_medium_fg_color "#[fg=#ffff00]"
set -g @cpu_high_fg_color "#[fg=#ff0000]"
set -g @cpu_low_bg_color "#[bg=#00ff00]"
set -g @cpu_medium_bg_color "#[bg=#ffff00]"
set -g @cpu_high_bg_color "#[bg=#ff0000]"
```
Don't forget to reload tmux environment (`$ tmux source-file ~/.tmux.conf`)
after you do this.
### Limitations
### Tmux Plugins
- CPU change most likely won't be instant.<br/>
It will take some time (15 - 60 seconds) for the value to change.
This depends on the `status-interval` TMUX option.
This plugin is part of the [tmux-plugins](https://github.com/tmux-plugins) organisation. Checkout plugins as [battery](https://github.com/tmux-plugins/tmux-battery), [logging](https://github.com/tmux-plugins/tmux-logging), [online status](https://github.com/tmux-plugins/tmux-online-status), and many more over at the [tmux-plugins](https://github.com/tmux-plugins) organisation page.
### Other plugins
You might want to follow [@brunosutic](https://twitter.com/brunosutic) on
twitter if you want to hear about new tmux plugins or feature updates.
You might also find these useful:
### Maintainer
- [battery](https://github.com/tmux-plugins/tmux-battery) - Plug and play battery percentage and icon indicator for Tmux.
- [online status](https://github.com/tmux-plugins/tmux-online-status) - online status
indicator in Tmux `status-right`. Useful when on flaky connection to see if
you're online.
- [Camille Tjhoa](https://github.com/ctjhoa)
### License
[MIT](LICENSE)
[MIT](LICENSE.md)

View file

@ -4,10 +4,18 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/scripts/helpers.sh"
cpu_percentage="#($CURRENT_DIR/scripts/cpu_percentage.sh)"
cpu_icon="#($CURRENT_DIR/scripts/cpu_icon.sh)"
cpu_percentage_interpolation="\#{cpu_percentage}"
cpu_icon_interpolation="\#{cpu_icon}"
cpu_interpolation=(
"\#{cpu_percentage}"
"\#{cpu_icon}"
"\#{cpu_bg_color}"
"\#{cpu_fg_color}"
)
cpu_commands=(
"#($CURRENT_DIR/scripts/cpu_percentage.sh)"
"#($CURRENT_DIR/scripts/cpu_icon.sh)"
"#($CURRENT_DIR/scripts/cpu_bg_color.sh)"
"#($CURRENT_DIR/scripts/cpu_fg_color.sh)"
)
set_tmux_option() {
local option=$1
@ -16,10 +24,11 @@ set_tmux_option() {
}
do_interpolation() {
local string=$1
local percentage_interpolated=${string/$cpu_percentage_interpolation/$cpu_percentage}
local all_interpolated=${percentage_interpolated/$cpu_icon_interpolation/$cpu_icon}
echo $all_interpolated
local all_interpolated="$1"
for ((i=0; i<${#cpu_commands[@]}; i++)); do
all_interpolated=${all_interpolated/${cpu_interpolation[$i]}/${cpu_commands[$i]}}
done
echo "$all_interpolated"
}
update_tmux_option() {

BIN
screenshots/high_bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 B

BIN
screenshots/high_fg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 919 B

BIN
screenshots/high_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1 KiB

BIN
screenshots/low_bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 908 B

BIN
screenshots/low_fg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 855 B

BIN
screenshots/low_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1,016 B

BIN
screenshots/medium_bg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

BIN
screenshots/medium_fg.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

BIN
screenshots/medium_icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

37
scripts/cpu_bg_color.sh Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
cpu_low_bg_color=""
cpu_medium_bg_color=""
cpu_high_bg_color=""
cpu_low_default_bg_color="#[bg=green]"
cpu_medium_default_bg_color="#[bg=yellow]"
cpu_high_default_bg_color="#[bg=red]"
get_bg_color_settings() {
cpu_low_bg_color=$(get_tmux_option "@cpu_low_bg_color" "$cpu_low_default_bg_color")
cpu_medium_bg_color=$(get_tmux_option "@cpu_medium_bg_color" "$cpu_medium_default_bg_color")
cpu_high_bg_color=$(get_tmux_option "@cpu_high_bg_color" "$cpu_high_default_bg_color")
}
print_bg_color() {
local cpu_percentage=$($CURRENT_DIR/cpu_percentage.sh | sed -e 's/%//')
local cpu_load_status=$(cpu_load_status $cpu_percentage)
if [ $cpu_load_status == "low" ]; then
echo "$cpu_low_bg_color"
elif [ $cpu_load_status == "medium" ]; then
echo "$cpu_medium_bg_color"
elif [ $cpu_load_status == "high" ]; then
echo "$cpu_high_bg_color"
fi
}
main() {
get_bg_color_settings
print_bg_color
}
main

37
scripts/cpu_fg_color.sh Executable file
View file

@ -0,0 +1,37 @@
#!/usr/bin/env bash
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
cpu_low_fg_color=""
cpu_medium_fg_color=""
cpu_high_fg_color=""
cpu_low_default_fg_color="#[fg=green]"
cpu_medium_default_fg_color="#[fg=yellow]"
cpu_high_default_fg_color="#[fg=red]"
get_fg_color_settings() {
cpu_low_fg_color=$(get_tmux_option "@cpu_low_fg_color" "$cpu_low_default_fg_color")
cpu_medium_fg_color=$(get_tmux_option "@cpu_medium_fg_color" "$cpu_medium_default_fg_color")
cpu_high_fg_color=$(get_tmux_option "@cpu_high_fg_color" "$cpu_high_default_fg_color")
}
print_fg_color() {
local cpu_percentage=$($CURRENT_DIR/cpu_percentage.sh | sed -e 's/%//')
local cpu_load_status=$(cpu_load_status $cpu_percentage)
if [ $cpu_load_status == "low" ]; then
echo "$cpu_low_fg_color"
elif [ $cpu_load_status == "medium" ]; then
echo "$cpu_medium_fg_color"
elif [ $cpu_load_status == "high" ]; then
echo "$cpu_high_fg_color"
fi
}
main() {
get_fg_color_settings
print_fg_color
}
main

View file

@ -5,17 +5,36 @@ CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$CURRENT_DIR/helpers.sh"
# script global variables
cpu_icon=""
cpu_low_icon=""
cpu_medium_icon=""
cpu_high_icon=""
cpu_default="❏ "
cpu_low_default_icon="="
cpu_medium_default_icon="≡"
cpu_high_default_icon="≣"
# icons are set as script global variables
get_icon_settings() {
cpu_icon=$(get_tmux_option "@cpu_icon" "$cpu_default")
cpu_low_icon=$(get_tmux_option "@cpu_low_icon" "$cpu_low_default_icon")
cpu_medium_icon=$(get_tmux_option "@cpu_medium_icon" "$cpu_medium_default_icon")
cpu_high_icon=$(get_tmux_option "@cpu_high_icon" "$cpu_high_default_icon")
}
print_icon() {
local cpu_percentage=$($CURRENT_DIR/cpu_percentage.sh | sed -e 's/%//')
local cpu_load_status=$(cpu_load_status $cpu_percentage)
if [ $cpu_load_status == "low" ]; then
echo "$cpu_low_icon"
elif [ $cpu_load_status == "medium" ]; then
echo "$cpu_medium_icon"
elif [ $cpu_load_status == "high" ]; then
echo "$cpu_high_icon"
fi
}
main() {
get_icon_settings
printf "$cpu_icon"
get_icon_settings
local cpu_icon=$(print_icon "$1")
echo "$cpu_icon"
}
main

View file

@ -34,6 +34,22 @@ is_linux_iostat() {
iostat -c &> /dev/null
}
# is second float bigger?
fcomp() {
awk -v n1=$1 -v n2=$2 'BEGIN {if (n1<n2) exit 0; exit 1}'
}
cpu_load_status() {
local percentage=$1
if fcomp 80 $percentage; then
echo "high"
elif fcomp 30 $percentage && fcomp $percentage 80; then
echo "medium"
else
echo "low"
fi
}
cpus_number() {
if is_linux; then
nproc