├── etc ├── conf └── README.md ├── examples ├── README.md ├── i3-config └── i3blocks-config ├── .travis.yml ├── .editorconfig ├── LICENSE ├── .gitignore ├── doc └── USAGE.md ├── Makefile ├── README.md └── modules ├── date ├── cputemp ├── backlight ├── online └── memory /etc/conf: -------------------------------------------------------------------------------- 1 | # i3blocks-modules color configuration 2 | 3 | # Set your default colors 4 | color_def="#666666" 5 | color_good="#88b090" 6 | color_warn="#ccdc90" 7 | color_crit="#e89393" 8 | color_info="#FCE94F" 9 | -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | # Examples 2 | 3 | This directory holds i3 and i3blocks example configuration. 4 | 5 | 1. Copy `i3blocks-config` to `~/.config/i3blocks/config`. 6 | 2. Add the content from `i3-config` to your `~/.config/i3/config`. 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | 3 | ### 4 | ### Enable sudo (required for docker service) 5 | ### 6 | sudo: required 7 | 8 | 9 | ### 10 | ### Language 11 | ### 12 | language: minimal 13 | 14 | 15 | ### 16 | ### Lint 17 | ### 18 | before_script: 19 | - make lint 20 | -------------------------------------------------------------------------------- /etc/README.md: -------------------------------------------------------------------------------- 1 | # Configuration 2 | 3 | ## Installation 4 | 5 | Install via: 6 | ```bash 7 | make install 8 | ``` 9 | 10 | Or copy `conf` to `~/.config/i3blocks-modules/conf` in order to set global module options. 11 | 12 | 13 | ## Configuration 14 | 15 | You can define global default colors for all modules to fit your theme: 16 | 17 | ``` 18 | # Set your default colors 19 | color_def="#666666" 20 | color_good="#88b090" 21 | color_warn="#ccdc90" 22 | color_crit="#e89393" 23 | color_info="#FCE94F" 24 | ``` 25 | -------------------------------------------------------------------------------- /examples/i3-config: -------------------------------------------------------------------------------- 1 | # i3 bar section 2 | bar { 3 | status_command i3blocks 4 | position top 5 | colors { 6 | # 7 | background #222222 8 | separator #444444 9 | statusline #666666 10 | 11 | # 12 | focused_workspace #da2263 #d70a53 #ffffff 13 | active_workspace #565656 #444444 #ffffff 14 | inactive_workspace #373737 #222222 #888888 15 | urgent_workspace #ec7ea4 #eb709b #ffffff 16 | } 17 | 18 | # tray_output none|primary| 19 | tray_output primary 20 | 21 | # separator between bar sections 22 | #separator_symbol " | " 23 | } 24 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: https://EditorConfig.org 2 | 3 | # ------------------------------------------------------------------------------------------------- 4 | # Default configuration 5 | # ------------------------------------------------------------------------------------------------- 6 | # top-most EditorConfig file 7 | root = true 8 | 9 | # Default for all files 10 | [*] 11 | charset = utf-8 12 | end_of_line = lf 13 | insert_final_newline = true 14 | trim_trailing_whitespace = true 15 | 16 | # ------------------------------------------------------------------------------------------------- 17 | # Specific config 18 | # ------------------------------------------------------------------------------------------------- 19 | [*.yml] 20 | indent_style = space 21 | indent_size = 2 22 | 23 | [*.sh}] 24 | indent_style = tab 25 | indent_size = 4 26 | 27 | [Makefile] 28 | indent_style = tab 29 | indent_size = 4 30 | 31 | [*.md] 32 | indent_style = space 33 | trim_trailing_whitespace = false 34 | indent_size = 2 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 cytopia 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Note: 2 | # To effectively apply the changes you will have 3 | # to re-index the git index (if there are already 4 | # commited files) 5 | # 6 | # $ git rm -r --cached . 7 | # $ git add . 8 | # $ git commit -m ".gitignore index rebuild" 9 | # 10 | 11 | 12 | ###################################### 13 | # CUSTOM 14 | ###################################### 15 | 16 | 17 | 18 | ###################################### 19 | # GENERIC 20 | ###################################### 21 | 22 | ###### std ###### 23 | .lock 24 | *.log 25 | 26 | ###### patches/diffs ###### 27 | *.patch 28 | *.diff 29 | *.orig 30 | *.rej 31 | 32 | 33 | ###################################### 34 | # Operating Systems 35 | ###################################### 36 | 37 | ###### OSX ###### 38 | ._* 39 | .DS* 40 | .Spotlight-V100 41 | .Trashes 42 | 43 | ###### Windows ###### 44 | Thumbs.db 45 | ehthumbs.db 46 | Desktop.ini 47 | $RECYCLE.BIN/ 48 | *.lnk 49 | *.shortcut 50 | 51 | ###################################### 52 | # Editors 53 | ###################################### 54 | 55 | ###### Sublime ###### 56 | *.sublime-workspace 57 | *.sublime-project 58 | 59 | ###### Eclipse ###### 60 | .classpath 61 | .buildpath 62 | .project 63 | .settings/ 64 | 65 | ###### Netbeans ###### 66 | /nbproject/ 67 | 68 | ###### Intellij IDE ###### 69 | .idea/ 70 | .idea_modules/ 71 | 72 | ###### vim ###### 73 | *.swp 74 | *.swo 75 | *~ 76 | 77 | ###### TextMate ###### 78 | .tm_properties 79 | *.tmproj 80 | 81 | ###### BBEdit ###### 82 | *.bbprojectd 83 | *.bbproject 84 | -------------------------------------------------------------------------------- /examples/i3blocks-config: -------------------------------------------------------------------------------- 1 | # i3blocks config file 2 | # 3 | # Please see man i3blocks for a complete reference! 4 | # The man page is also hosted at http://vivien.github.io/i3blocks 5 | # 6 | # List of valid properties: 7 | # 8 | # align 9 | # color 10 | # background 11 | # border 12 | # command 13 | # full_text 14 | # instance 15 | # interval 16 | # label 17 | # min_width 18 | # name 19 | # separator 20 | # separator_block_width 21 | # short_text 22 | # signal 23 | # urgent 24 | 25 | 26 | ### 27 | ### Global properties 28 | ### 29 | ### The top properties below are applied to every block, but can be overridden. 30 | ### Each block command defaults to the script name to avoid boilerplate. 31 | command=$SCRIPT_DIR/$BLOCK_NAME 32 | separator_block_width=35 33 | markup=pango 34 | 35 | 36 | ### 37 | ### Modules defines 38 | ### 39 | [cpu] 40 | command=~/.local/share/i3blocks-modules/cpu -tw '{percent}' '>' '60' -tc '{percent}' '>' '80' 41 | interval=1 42 | 43 | [cputemp] 44 | command=~/.local/share/i3blocks-modules/cputemp -tw '{temp}' '>' 75 -tc '{temp}' '>' 90 45 | instance=Core 0 46 | interval=1 47 | 48 | [memory] 49 | command=~/.local/share/i3blocks-modules/memory -f ' {used} {uunit}B' -tw '{pfree}' '<' 30 -tc '{pfree}' '<' 15 50 | interval=10 51 | 52 | [disk] 53 | command=~/.local/share/i3blocks-modules/disk -f ' {used} {uunit}B' -tc '{pfree}' '<' 20 -tc '{pfree}' '<' 10 54 | instance=/ 55 | interval=30 56 | 57 | [bitcoin] 58 | command=~/.local/share/i3blocks-modules/bitcoin -f 'B {eur}€' -tc '{eur}' '<' 2500 59 | instance=coindesk 60 | interval=60 61 | 62 | [ethereum] 63 | command=~/.local/share/i3blocks-modules/ethereum -f 'E {eur}€' -tc '{eur}' '<' 220 64 | instance=ethereumprice 65 | interval=60 66 | 67 | [online] 68 | command=~/.local/share/i3blocks-modules/online 69 | interval=2 70 | 71 | [wifi] 72 | command=~/.local/share/i3blocks-modules/wifi -fe '{status}' '=' 'up' ' {ssid} ({quality}%)' -fe '{status}' '!=' 'up' ' down' 73 | instance=wlp58s0 74 | interval=2 75 | 76 | [iface] 77 | command=~/.local/share/i3blocks-modules/iface -f ' {status}' -fe '{status}' = 'absent' '' 78 | instance=enxd481d733d87c 79 | interval=2 80 | 81 | [battery] 82 | command=~/.local/share/i3blocks-modules/battery -fe '{percent}' '<' 90 ' {percent}% ({time})' -fe '{percent}' '<' 75 ' {percent}% ({time})' -fe '{percent}' '<' 60 ' {percent}% ({time})' -fe '{percent}' '<' 35 ' {percent}% ({time})' -fe '{percent}' '<' 5 ' {percent}% ({time})' -fe '{status}' '=' '^charging' '⚡ {percent}% ({time})' -fe '{status}' '=' 'full' ' {percent}%' -tg '{status}' '=' 'full' -tg '{percent}' '=' 100 -ti '{status}' '=' '^charging' -tw '{percent}' '<' 30 -tc '{percent}' '<' 5 83 | instance=Battery 0 84 | interval=1 85 | 86 | [backlight] 87 | command=~/.local/share/i3blocks-modules/backlight 88 | instance=intel_backlight 89 | interval=2 90 | 91 | [stock] 92 | command=~/.local/share/i3blocks-modules/stock -f 'TLSA {usd}$' -tc '{usd}' '<' 820 93 | instance=TLSA 94 | interval=180 95 | 96 | [volume] 97 | command=~/.local/share/i3blocks-modules/volume -f "{icon} {volume}%" -id "" -im "🔇" -fe '{port}' '=' 'headphone' '{icon}  {volume}%' -fe '{dev_bus}' '=' 'bluetooth' '{icon}  {volume}%' -tw '{muted}' '=' 'yes' 98 | interval=1 99 | 100 | [date] 101 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%a, %d.%m.%Y" -tg '{time}' '=' '^(Sat|Sun).*' 102 | interval=60 103 | 104 | [time] 105 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%H:%M" -tw '{time}' '=' '^23.*' -tc '{time}' '=' '^0(0|1|2|3).*' 106 | interval=5 107 | 108 | [weather] 109 | command=~/.local/share/i3blocks-modules/modules/weather -tc '{celsius}' '<' 0 110 | instance=Berlin 111 | interval=60 112 | -------------------------------------------------------------------------------- /doc/USAGE.md: -------------------------------------------------------------------------------- 1 | # Usage example 2 | 3 | 4 | ```shell 5 | $ disk -h 6 | Usage: disk [-f ] [-fe

] [-d ] [-tg|-ti|-tw|-tc

] [-np] [-cd|-cg|-cw|-cc|-ci ] 7 | disk -h 8 | disk -v 9 | 10 | Optional variables: 11 | -------------------------------------------------------------------------------- 12 | -d Specify the disk being used. This can also be set via i3blocks 'instance=' value. If neither is set, it will default to '/'. 13 | 14 | Optional threshold arguments: 15 | -------------------------------------------------------------------------------- 16 | You can optionally enable threshold checking against any placeholder value. 17 | This enables the colorizing of the final output depending on any met 18 | conditions specified. 19 | Default is not to use any threshold 20 | You can use unlimited number of threshold for each type. 21 | 22 | -tg

Enable threshold for 'good status' 23 | -ti

Enable threshold for 'info status' 24 | -tw

Enable threshold for 'warn status' 25 | -tc

Enable threshold for 'crit status' 26 | 27 | Explanation: 28 |

is the placeholder value you want to check against. 29 | valid placeholders: {total} {used} {free} {pused} {pfree} {uunit} {funit} {tunit} 30 | Note 1: placeholder values will be converted to integers 31 | Any decimal places will simply be cut off. 32 | Note 2: In equal mode ( '=') is a string regex comparison and 33 | no placeholder will be converted. 34 | Note 3: In unequal mode ( '!=') is a string comparison and 35 | no placeholder will be converted. 36 | Note 3: In equal mode ( '=') regex is allowed :-) 37 | must either be '<', '>', '=' or '!=' depending on what direction 38 | you want to check the threshold placeholder against. 39 | The integer number you want to check against the placeholder. 40 | The string you want to check against the placeholder. 41 | You can only use a string when in equal mode '='. 42 | You can also use regex here. 43 | 44 | Examples: 45 | 1. Check if value of {total} < 50, then format using the good color 46 | -tg '{total}' '<' 50 47 | 48 | 2. Check if value of {total} > 90, then format using the warn color 49 | -tw '{total}' '>' 90 50 | 51 | 3. Check if value of {total} equals the string 'foo', then format using the info color 52 | -ti '{total}' '=' 'foo' 53 | 54 | 4. Check if value of {total} equals the regex '^[0-9]+$', then format using the info color 55 | -ti '{total}' '=' '^[0-9]+$' 56 | 57 | Optional markup (pango): 58 | -------------------------------------------------------------------------------- 59 | -np Disable pango markup 60 | 61 | Optional color arguments: 62 | -------------------------------------------------------------------------------- 63 | If not specified, script default colors are used 64 | If config file with color codes is present in: 65 | '/home/cytopia/.config/i3blocks/contrib.conf', these colors will be used. 66 | 67 | -cd Default color (hexadecimal color code) 68 | Default value is: #666666 69 | -cg Good color (hexadecimal color code) 70 | Default value is: #88b090 71 | -cw Warning color (hexadecimal color code) 72 | Default value is: #ccdc90 73 | -cc Critical color (hexadecimal color code) 74 | Default value is: #e89393 75 | -ci Info color (hexadecimal color code) 76 | Default value is: #fce94f 77 | 78 | Optional Format placeholders: 79 | -------------------------------------------------------------------------------- 80 | Available color placeholders: 81 | (Use with pango disabled for custom markup) building 82 | {color} Current active color depending on thresholds 83 | {color_def} Default color 84 | {color_good} Good color 85 | {color_warn} Warning color 86 | {color_crit} Critical color 87 | {color_info} Info color 88 | Format example: 89 | -np -f "Colored text" 90 | 91 | Available specific placeholders: 92 | {total} Total space 93 | {used} Used space 94 | {free} Free space 95 | {pused} Percentage of used space 96 | {pfree} Percentage of free space 97 | {uunit} Used sace unit 98 | {funit} Free space unit 99 | {tunit} Total space unit 100 | Format example: 101 | -f " {free} {funit} ({pfree}%)" 102 | -f " {used} {uunit} / {total} {tunit}" 103 | Default: 104 | -f " {free} {funit}iB free" 105 | 106 | Optional extended Format output: 107 | -------------------------------------------------------------------------------- 108 | You can conditionally set your output text depending on the value of any placeholder. 109 | For example, If you have a placeholder {status} that either is 'up' or 'down', you 110 | can specify different outputs for 'up' and for 'down'. 111 | Usage 112 | -fe

113 | 114 | Format example: 115 | -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}' 116 | Explanation: 117 |

is the placeholder value you want to check against. 118 | valid placeholders: {total} {used} {free} {pused} {pfree} {uunit} {funit} {tunit} 119 | Note 1: placeholder values will be converted to integers 120 | Any decimal places will simply be cut off. 121 | Note 2: In equal mode ( '=') is a string regex comparison and 122 | no placeholder will be converted. 123 | Note 3: In unequal mode ( '!=') is a string comparison and 124 | no placeholder will be converted. 125 | Note 3: In equal mode ( '=') regex is allowed :-) 126 | must either be '<', '>', '=' or '!=' depending on what direction 127 | you want to check the threshold placeholder against. 128 | The integer number you want to check against the placeholder. 129 | The string you want to check against the placeholder. 130 | You can only use a string when in equal mode '='. 131 | You can also use regex here. 132 | Is the format string that should be displayed under the above condition. 133 | Of course you can also use placeholders here ;-). 134 | ``` 135 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ifneq (,) 2 | .error This Makefile requires GNU Make. 3 | endif 4 | 5 | # ------------------------------------------------------------------------------------------------- 6 | # Default configuration 7 | # ------------------------------------------------------------------------------------------------- 8 | .PHONY: help lint install uninstall 9 | 10 | # Configuration 11 | SHELL = /bin/sh 12 | MKDIR_P = mkdir -p 13 | 14 | FL_VERSION = 0.3 15 | FL_IGNORES = .git/,.github/ 16 | 17 | # ------------------------------------------------------------------------------------------------- 18 | # Default Target 19 | # ------------------------------------------------------------------------------------------------- 20 | help: 21 | @echo "Type 'make install' or 'make uninstall'" 22 | @echo 23 | @echo "make install" 24 | @echo " Will install config to ~/.config/i3blocks-modules/conf" 25 | @echo " Will install modules to ~/.local/share/i3blocks-modules/" 26 | @echo "" 27 | @echo "make uninstall" 28 | @echo " Will remove config from ~/.config/i3blocks-modules/conf" 29 | @echo " Will remove modules from ~/.local/share/i3blocks-modules/" 30 | 31 | 32 | # ------------------------------------------------------------------------------------------------- 33 | # Targets 34 | # ------------------------------------------------------------------------------------------------- 35 | lint: 36 | @$(MAKE) --no-print-directory _lint-file 37 | @$(MAKE) --no-print-directory _lint-bash 38 | 39 | _lint-file: _pull-docker-filelint 40 | @# Lint all files 41 | @echo "################################################################################" 42 | @echo "# File lint" 43 | @echo "################################################################################" 44 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-cr --text --ignore '$(FL_IGNORES)' --path . 45 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-crlf --text --ignore '$(FL_IGNORES)' --path . 46 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-single-newline --text --ignore '$(FL_IGNORES)' --path . 47 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-trailing-space --text --ignore '$(FL_IGNORES)' --path . 48 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8 --text --ignore '$(FL_IGNORES)' --path . 49 | @docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/data cytopia/file-lint:$(FL_VERSION) file-utf8-bom --text --ignore '$(FL_IGNORES)' --path . 50 | @echo 51 | 52 | _lint-bash: _pull-docker-shellcheck 53 | @# Lint all files 54 | @echo "################################################################################" 55 | @echo "# File bash" 56 | @echo "################################################################################" 57 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/backlight 58 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/battery 59 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/bitcoin 60 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/bitcoincash 61 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/cpu 62 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/cputemp 63 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/date 64 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/disk 65 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/ethereum 66 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/gateway 67 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/iface 68 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/memory 69 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/online 70 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/stock 71 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/volume 72 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/weather 73 | docker run --rm $$(tty -s && echo "-it" || echo) -v $(PWD):/mnt koalaman/shellcheck:stable --exclude=SC2034 --shell=bash modules/wifi 74 | 75 | 76 | install: 77 | @# root check 78 | @[ `id -u` != 0 ] || { echo "Must be run as normal user"; exit 1; } 79 | 80 | @# Create dir 81 | ${MKDIR_P} "${HOME}/.config/i3blocks-modules/" 82 | ${MKDIR_P} "${HOME}/.local/share/i3blocks-modules/" 83 | 84 | @# Install config 85 | install -m 0644 etc/conf "${HOME}/.config/i3blocks-modules/conf" 86 | 87 | @# Install modules 88 | install -m 0755 modules/backlight "${HOME}/.local/share/i3blocks-modules/backlight" 89 | install -m 0755 modules/battery "${HOME}/.local/share/i3blocks-modules/battery" 90 | install -m 0755 modules/bitcoin "${HOME}/.local/share/i3blocks-modules/bitcoin" 91 | install -m 0755 modules/bitcoincash "${HOME}/.local/share/i3blocks-modules/bitcoincash" 92 | install -m 0755 modules/cpu "${HOME}/.local/share/i3blocks-modules/cpu" 93 | install -m 0755 modules/cputemp "${HOME}/.local/share/i3blocks-modules/cputemp" 94 | install -m 0755 modules/date "${HOME}/.local/share/i3blocks-modules/date" 95 | install -m 0755 modules/disk "${HOME}/.local/share/i3blocks-modules/disk" 96 | install -m 0755 modules/ethereum "${HOME}/.local/share/i3blocks-modules/ethereum" 97 | install -m 0755 modules/gateway "${HOME}/.local/share/i3blocks-modules/gateway" 98 | install -m 0755 modules/iface "${HOME}/.local/share/i3blocks-modules/iface" 99 | install -m 0755 modules/memory "${HOME}/.local/share/i3blocks-modules/memory" 100 | install -m 0755 modules/online "${HOME}/.local/share/i3blocks-modules/online" 101 | install -m 0755 modules/stock "${HOME}/.local/share/i3blocks-modules/stock" 102 | install -m 0755 modules/volume "${HOME}/.local/share/i3blocks-modules/volume" 103 | install -m 0755 modules/weather "${HOME}/.local/share/i3blocks-modules/weather" 104 | install -m 0755 modules/wifi "${HOME}/.local/share/i3blocks-modules/wifi" 105 | 106 | 107 | uninstall: 108 | @# root check 109 | @[ `id -u` != 0 ] || { echo "Must be run as normal user"; exit 1; } 110 | 111 | @# Remove config 112 | rm -f "${HOME}/.config/i3blocks-modules/conf" 113 | 114 | @# Remove modules 115 | rm -f "${HOME}/.local/share/i3blocks-modules/backlight" 116 | rm -f "${HOME}/.local/share/i3blocks-modules/battery" 117 | rm -f "${HOME}/.local/share/i3blocks-modules/bitcoin" 118 | rm -f "${HOME}/.local/share/i3blocks-modules/bitcoincash" 119 | rm -f "${HOME}/.local/share/i3blocks-modules/cpu" 120 | rm -f "${HOME}/.local/share/i3blocks-modules/cputemp" 121 | rm -f "${HOME}/.local/share/i3blocks-modules/date" 122 | rm -f "${HOME}/.local/share/i3blocks-modules/disk" 123 | rm -f "${HOME}/.local/share/i3blocks-modules/ethereum" 124 | rm -f "${HOME}/.local/share/i3blocks-modules/gateway" 125 | rm -f "${HOME}/.local/share/i3blocks-modules/iface" 126 | rm -f "${HOME}/.local/share/i3blocks-modules/memory" 127 | rm -f "${HOME}/.local/share/i3blocks-modules/online" 128 | rm -f "${HOME}/.local/share/i3blocks-modules/stock" 129 | rm -f "${HOME}/.local/share/i3blocks-modules/volume" 130 | rm -f "${HOME}/.local/share/i3blocks-modules/weather" 131 | rm -f "${HOME}/.local/share/i3blocks-modules/wifi" 132 | 133 | @# Remove dirs 134 | rm -rf "${HOME}/.config/i3blocks-modules" 135 | rm -rf "${HOME}/.local/share/i3blocks-modules" 136 | 137 | 138 | # ------------------------------------------------------------------------------------------------- 139 | # Helper Target 140 | # ------------------------------------------------------------------------------------------------- 141 | 142 | _pull-docker-filelint: 143 | docker pull cytopia/file-lint:$(FL_VERSION) 144 | 145 | _pull-docker-shellcheck: 146 | docker pull koalaman/shellcheck:stable 147 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # i3blocks modules 2 | 3 | --- 4 | 5 | **This repository is part of the [i3-utils](https://github.com/cytopia/i3-utils).** 6 | 7 | --- 8 | 9 | **[Install](#tada-install)** | 10 | **[Uninstall](#no_entry_sign-uninstall)** | 11 | **[Modules](#star-available-modules)** | 12 | **[Documentation](#information_source-documentation)** | 13 | **[Examples](#bulb-examples)** | 14 | **[Configuration](#wrench-configuration-file)** | 15 | **[Usage](#computer-general-module-usage)** | 16 | **[License](#page_facing_up-license)** 17 | 18 | [![Build Status](https://travis-ci.com/cytopia/i3blocks-modules.svg?branch=master)](https://travis-ci.com/cytopia/i3blocks-modules) 19 | ![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/cytopia/i3blocks-modules) 20 | [![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/licenses/MIT) 21 | 22 | High-quality, highly configurable [i3blocks](https://github.com/vivien/i3blocks/) modules. 23 | 24 | 1. Use of **placeholders** to configure your output (`-f`) 25 | 2. **Conditional output format** based on placeholder comparison (`-fe`) 26 | 3. **Threshold** settings by checking against placeholders (`-tg|-ti|-tw|-tc`) 27 | 4. **Arithmetic** and **regex** comparison against all placeholder for threshold settings (`<`, `>`, `=`, `!=`) 28 | 5. Use of global or specific **colors** for all types of stati 29 | 6. Custom pango **markup** via color placeholders (`-np`) 30 | 31 | 32 | 33 | 34 | ## :tada: Install 35 | 36 | ```bash 37 | make install 38 | ``` 39 | 40 | 41 | 42 | ## :no_entry_sign: Uninstall 43 | ```bash 44 | make uninstall 45 | ``` 46 | 47 | 48 | 49 | ## :star: Available Modules 50 | 51 | | Module | Placeholders | Description | 52 | |--------|--------------|-------------| 53 | | **[backlight](modules/backlight)** | {percent} | Show percentage of current screen brightness | 54 | | **[battery](modules/battery)** | {time} {percent} {status} {capacity} {ucapacity} | Show battery information | 55 | | **[bitcoin](modules/bitcoin)** | {usd} {eur} | Show current bitcoin price (either from coindesk or btc-e) | 56 | | **[cpu](modules/cpu)** | {speed} {uspeed} {percent} {usage5} {usage10} {usage15} | Show current cpu statistics | 57 | | **[cputemp](modules/cputemp)** | {temp} | Show current cpu temperature | 58 | | **[date](modules/date)** | {time} | Show defined date/time string | 59 | | **[disk](modules/disk)** | {total} {used} {free} {pused} {pfree} {uunit} {funit} {tunit} | Show space consumption of a disk | 60 | | **[ethereum](modules/ethereum)** | {usd} {eur} | Show current Ethereum price (from ethereumprice) | 61 | | **[gateway](modules/gateway)** | {iface} {ip} {ip_gw} {metric} {num_def_routes} | Show information about default gateways and routes | 62 | | **[iface](modules/iface)** | {ip} {ip_nm} {ip6} {ip6_nm} {mac} {mtu} {iface} {status} {status_or_ip} {status_or_ip6} | Show status and various values of network interface | 63 | | **[memory](modules/memory)** | {total} {used} {free} {pused} {pfree} {uunit} {funit} {tunit} | Show RAM memory consumption | 64 | | **[online](modules/online)** | {status} {status_or_ip} {ip} {country} {city} | Show online status with IP including your location | 65 | | **[stock](modules/stock)** | {usd} | Show current stock prices (by share) | 66 | | **[volume](modules/volume)** | {volume} {muted} {port} {dev_api} {dev_bus} {dev_form_factor} {dev_profile} {dev_icon_name} {dev_description} {alsa_name} {alsa_card} {alsa_driver} {alsa_mixer} {icon} | Show info about current volume (auto-changes when headphone or usb/bluetooth is connected) | 67 | | **[weather](modules/weather)** | {celsius} {fahrenheit} {wind_speed_kmh} {wind_speed_mph} {icon_weather} {icon_wind_speed} {icon_wind_direction} | Show current temperature (by city) | 68 | | **[wifi](modules/wifi)** | {ip} {ip_nm} {ip6} {ip6_nm} {mac} {mtu} {iface} {status} {status_or_ip} {status_or_ip6} {ssid} {freq} {freq_unit} {tx_power} {tx_power_unit} {quality} {signal} {signal_unit} {noise} {bit_rate_} {bit_rate_unit} | Show info about your wireless connection | 69 | 70 | 71 | 72 | ## :information_source: Documentation 73 | 74 | ### Install paths 75 | 76 | When using the provided Makefile to install i3blocks-modules, it will do so into the following locations: 77 | 78 | | What | Install location | 79 | |------------------------------|------------------------------------| 80 | | i3blocks-modules config file | `~/.config/i3blocks-modules/conf` | 81 | | i3blocks-modules modules | `~/.local/share/i3blocks-modules/` | 82 | 83 | 84 | ### Format string 85 | 86 | When defining a module in the configuration file, you can either go with default settings or specify a custom **format string** (`-f '...'`) which can contain text, unicode icons or dynamic placeholder variables. This allows for full customization of each module. 87 | 88 | Additionally to a **fixed format string**, you can also define as many **conditional format strings** as required for your ouput. *Conditional format strings* are based on the values of placeholders returned by each module. So when the `date` module for example returns any date between Saturday and Sunday, you could simply make it display `Weekend`. Or when the `iface` module returns that nothing is connected, you could simply hide all output completely. Another useful example would be the `battery` module which could display different icons depending on wether it is currently charging or discharging. 89 | 90 | 91 | ### Placeholders 92 | 93 | Placeholders are **dynamic variables** (the names in curly braces) that can be placed anywhere in your fixed or conditional **format string** for each of your defined modules to make the look&feel of each module according to your needs. 94 | Each module comes with their own set of placeholders giving you various ways for customization. (See Available modules for placeholders). 95 | 96 | Additionally to specific placeholder, each module also has color placeholders in case you want to create your own colorized markup output (see `-np`): 97 | * {color} 98 | * {color_def} 99 | * {color_good} 100 | * {color_warn} 101 | * {color_crit} 102 | * {color_info} 103 | 104 | To sum it up briefly, placeholders have three use-cases: 105 | 106 | 1. Placeholders can be used in the format argument to format your output.
(e.g.: `-f 'Signal: {percent}%'`) 107 | 2. Placeholders can be checked and according to their value the output will differ
(e.g.: `-fe '{status}' '=' 'up' '{iface} {ip} ({ssid} {signal}%)'`) 108 | 3. Placeholders can be used to determine the status and thus affect the final output color.
(e.g.: `-tc '{percent}' '<' 50` or `-tc '{date}' '=' '^(Sat|Sun)*'`) 109 | 110 | 111 | ### Threshold and stati 112 | 113 | Depending on the status of a module (self-evaluated or custom threshold comparison), the output text will be shown in different colors. There are different ways to determine the final status: 114 | 115 | 1. **Self evaluation**: Some modules are able to evaluate their status independently. E.g.: `iface` will have a *good status* if it is up and has an IP assigned or it will have a *critical status* if it is down or absent. 116 | 2. **Argument thresholds**: You can specify custom command line arguments with which you can compare the value of *placeholders* (less than, greater then or a regex comparison). This can be done for *good*, *info*, *warning* and *critical* states. 117 | 3. **Status precedence**: Even though the module is in *good state* it can still show up in a *critical state*. This is evaluated by the precedence. Precedence is evaluated in the following order (later ones always override previous ones): 118 | - good 119 | - info 120 | - warning 121 | - critical 122 | 123 | 124 | ### Status colors 125 | 126 | Every module can set its own set of colors for available stati 127 | 128 | * Default 129 | * Good status 130 | * Info status 131 | * Warning status 132 | * Critical status 133 | 134 | Stati can be set via the following module arguments: 135 | 136 | ``` 137 | -cd Default color (hexadecimal color code) 138 | Default value is: #666666 139 | -cg Good color (hexadecimal color code) 140 | Default value is: #88b090 141 | -cw Warning color (hexadecimal color code) 142 | Default value is: #ccdc90 143 | -cc Critical color (hexadecimal color code) 144 | Default value is: #e89393 145 | -ci Info color (hexadecimal color code) 146 | Default value is: #fce94f 147 | ``` 148 | 149 | Instead of setting the color on each module separately, you can also use the `i3blocks-modules` configuration file to set it globally in `~/.config/i3blocks-modules/config`. 150 | 151 | 152 | 153 | ## :bulb: Examples 154 | 155 | The following examples show command line calls of the modules (each module is simply a bash script that you can execute on the command line) as well as their corresponding configuration settings. In order to get the desired output on the command line, you will need to set the `BLOCK_INSTANCE` variable to a meaningful value. This variable corresponds to the `instance` setting of each module in the configuration file. 156 | 157 | 158 | ### Set *format string* 159 | 160 | Use placeholders to specify how the module should output the provided information: 161 | 162 | `command line`: 163 | ```bash 164 | # Note: set BLOCK_INSTANCE to your wifi interface 165 | $ BLOCK_INSTANCE=wlp3s0 ~/.local/share/i3blocks-modules/wifi -f 'WIFI: {ip} {ssid} ({signal}%)' 166 | ``` 167 | 168 | `~/.config/i3blocks/config`: 169 | ```ini 170 | # Note: Set instance to your wifi interface 171 | # Note: interval specifies how often the module should be updated (in seconds) 172 | # So in this example, it will be updated every 2 seconds. 173 | [wifi] 174 | command=~/.local/share/i3blocks-modules/wifi -f 'WIFI: {ip} {ssid} ({signal}%)' 175 | instance=wlp3s0 176 | interval=2 177 | ``` 178 | 179 | 180 | ### Set *conditional format string* 181 | 182 | Imagine you want the output to be dynamic in case the module reports a different state and/or the placeholders will not have any values. In the above case, if your wifi interface is not connected or not present, the module would output something like this: `WIFI: (%)`. 183 | 184 | So in order to still give a meaningful output for this case, you will need to set a conditional format. 185 | 186 | The following example will output your wifi information if it is available, but if the `{status}` does not equal `up`, the it changes the format to `WIFI: down` 187 | 188 | `command line`: 189 | ```bash 190 | # Note: set BLOCK_INSTANCE to your wifi interface 191 | $ BLOCK_INSTANCE=wlp3s0 ~/.local/share/i3blocks-modules/wifi \ 192 | -f 'WIFI: {ip} {ssid} ({signal}%)' \ 193 | -fe '{status}' '!=' 'up' 'WIFI: down' 194 | ``` 195 | 196 | `~/.config/i3blocks/config`: 197 | ```ini 198 | # Note: Set instance to your wifi interface 199 | # Note: interval specifies how often the module should be updated (in seconds) 200 | # So in this example, it will be updated every 2 seconds. 201 | [wifi] 202 | command=~/.local/share/i3blocks-modules/wifi -f 'WIFI: {ip} {ssid} ({signal}%)' -fe '{status}' '!=' 'up' 'WIFI: down' 203 | instance=wlp3s0 204 | interval=2 205 | ``` 206 | 207 | In the above example you will have different outputs based on whether the Wifi interface is up or not up. 208 | 209 | 210 | ### Set *thresholds* 211 | 212 | Thresholds allow you to colorize your output depending on the module state (`default`, `good`, `warning`, `critical` or `info`). 213 | You will need to tell each module (based on your specified conditions) when it reaches a certain state. 214 | The normal behaviour of each module is to run in `default` state. 215 | 216 | 217 | **1\. Colorize date with 'good status' during weekends:** 218 | 219 | `command line`: 220 | ```bash 221 | $ ~/.local/share/i3blocks-modules/date -f " {time}" -t "%a, %d.%m.%Y" -tg '{time}' '=' '^(Sat|Sun).*' 222 | ``` 223 | 224 | `~/.config/i3blocks/config`: 225 | ```ini 226 | [date] 227 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%a, %d.%m.%Y" -tg '{time}' '=' '^(Sat|Sun).*' 228 | interval=60 229 | ``` 230 | 231 | **2\. Colorize time with warning state after 23 o'clock and between 0 and 3 with critical state:** 232 | 233 | `command line`: 234 | ```bash 235 | $ ~/.local/share/i3blocks-modules/date -f " {time}" -t "%H:%M" -tw '{time}' '=' '^23.*' -tc '{time}' '=' '^0(0|1|2|3).*' 236 | ``` 237 | 238 | `~/.config/i3blocks/config`: 239 | ```ini 240 | [time] 241 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%H:%M" -tw '{time}' '=' '^23.*' -tc '{time}' '=' '^0(0|1|2|3).*' 242 | interval=1 243 | ``` 244 | 245 | 246 | 247 | ## :wrench: Configuration file 248 | 249 | ### i3blocks configuration 250 | 251 | Configuration is done as you would normally do it with [i3blocks](https://github.com/vivien/i3blocks). The only customization would be to setting the `command` accordingly. 252 | The configuration file is available in `~/.config/i3blocks/config`. 253 | 254 | 255 | ### i3blocks-modules configuration 256 | 257 | The here provided `i3blocks-modules` also come with their own configuration file, which sets status colors globally. 258 | 259 | `~/config/i3blocks-modules/config`: 260 | ```ini 261 | # i3blocks-modules configuration 262 | 263 | # Set your default colors 264 | color_def="#666666" 265 | color_good="#88b090" 266 | color_warn="#ccdc90" 267 | color_crit="#e89393" 268 | color_info="#FCE94F" 269 | ``` 270 | If the above file is present, those colors are used as the defaults. 271 | 272 | 273 | ### Example configuration 274 | 275 | `~/.config/i3blocks/config` 276 | ```ini 277 | # output:  51°C 278 | # shows 'good color' at temperatures below 71 degrees 279 | # shows 'warn color' at temperatures above 70 degrees 280 | # shows 'crit color' at temperatures above 90 degrees 281 | [cputemp] 282 | command=~/.local/share/i3blocks-modules/cputemp -tg '{temp}' '<' 71 -tw '{temp}' '>' 70 -tc '{temp}' '>' 90 283 | instance=Core 0 284 | interval=2 285 | 286 | # output discharging:  31% (01:50) 287 | # output charging: ⚡ 31% (00:23) 288 | # output full:  100% (08:32) 289 | # output full with ac: ⚡ 100% 290 | # shows 'good color' when fully charged 291 | # shows 'warn color' when remaining percentage is below 30% 292 | # shows 'crit color' when remaining percentage is below 10% 293 | # shows different icons depending on remaining percent 294 | # shows different icon for charging and discharging 295 | [battery] 296 | command=~/.local/share/i3blocks-modules/battery -fe '{percent}' '<' 90 ' {percent}% ({time})' -fe '{percent}' '<' 75 ' {percent}% ({time})' -fe '{percent}' '<' 60 ' {percent}% ({time})' -fe '{percent}' '<' 35 ' {percent}% ({time})' -fe '{percent}' '<' 5 ' {percent}% ({time})' -fe '{status}' '=' '^charging' '⚡ {percent}% ({time})' -fe '{status}' '=' 'full' ' {percent}%' -tg '{status}' '=' 'full' -tg '{percent}' '=' 100 -ti '{status}' '=' '^charging' -tw '{percent}' '<' 30 -tc '{percent}' '<' 5 297 | instance=Battery 0 298 | interval=1 299 | 300 | # output:  375 GiB / 435 GiB (10%) 301 | # show 'warn color' when disk space is less than 20% 302 | # show 'crit color' when disk space is less than 10% 303 | [disk] 304 | command=~/.local/share/i3blocks-modules/disk -f " {free} {funit}iB / {total} {tunit}iB ({pused}%)" -tc '{pfree}' '<' 20 -tc '{pfree}' '<' 10 305 | instance=/ 306 | interval=30 307 | 308 | # output: ☼ 100% 309 | [backlight] 310 | command=~/.local/share/i3blocks-modules/backlight 311 | instance=intel_backlight 312 | interval=2 313 | 314 | # output:  Sun, 05.03.2017 315 | [date] 316 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%a, %d.%m.%Y" 317 | interval=60 318 | 319 | # output:  14:36 320 | [time] 321 | command=~/.local/share/i3blocks-modules/date -f " {time}" -t "%H:%M" 322 | interval=5 323 | ``` 324 | 325 | 326 | 327 | ## :computer: General module usage 328 | 329 | Each module provides an extensive help output which gives you information about available placeholder, thresholds, colors and custom module arguments. Help screens can be shown via `-h`. 330 | ```bash 331 | # Generic 332 | /path/to/module -h 333 | 334 | # Specific for the 'wifi' module 335 | ~/.local/share/i3blocks-modules/wifi -h 336 | ``` 337 | 338 | All modules are designed to work in the same way. The only difference are their custom specific options `Optional variables`. 339 | 340 | Have a look at the [doc/](doc/) folder for usage. 341 | 342 | 343 | 344 | ## :page_facing_up: License 345 | 346 | **[MIT License](LICENSE)** 347 | 348 | Copyright (c) 2017 [cytopia](https://github.com/cytopia) 349 | -------------------------------------------------------------------------------- /modules/date: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" 4 | 5 | ################################################################################ 6 | # 7 | # CUSTOM PLUGIN SECTION 8 | # 9 | ################################################################################ 10 | 11 | ############################################################ 12 | # Variables 13 | ############################################################ 14 | 15 | ### 16 | ### Custom Defines 17 | ### 18 | appname="date" 19 | time="${BLOCK_INSTANCE:-%H:%M}" 20 | format=" {time}" 21 | 22 | 23 | ### 24 | ### Thresholds 25 | ### 26 | 27 | # Enable 28 | has_threshold=1 29 | 30 | # Depending on the conditions in your custom_action() 31 | # you can force the output to be critical or warning 32 | # Set these vars to 1 (in custom_action) 33 | force_crit=0 34 | force_warn=0 35 | force_good=0 36 | 37 | 38 | ### 39 | ### Additional arguments 40 | ### 41 | arg_params=( 42 | "-t" 43 | ) 44 | arg_vars=( 45 | "time" 46 | ) 47 | arg_desc_val=( 48 | "

] " 195 | fi 196 | 197 | echo "Usage: ${appname} [-f ] [-fe

] ${custom_args}[-np] [-cd|-cg|-cw|-cc|-ci ]" 198 | echo " ${appname} -h" 199 | echo " ${appname} -v" 200 | echo 201 | 202 | if [ "${#custom_args}" -gt "0" ]; then 203 | echo "Optional variables:" 204 | echo "--------------------------------------------------------------------------------" 205 | 206 | for (( i=0; i<${#arg_params[@]}; i++ )); do 207 | printf " %-13s%s\\n" "${arg_params[$i]} ${arg_desc_val[$i]}" "${arg_desc_long[$i]}" 208 | done 209 | echo 210 | fi 211 | 212 | if [ "${has_threshold}" = "1" ]; then 213 | echo "Optional threshold arguments:" 214 | echo "--------------------------------------------------------------------------------" 215 | echo "You can optionally enable threshold checking against any placeholder value." 216 | echo "This enables the colorizing of the final output depending on any met" 217 | echo "conditions specified." 218 | echo "Default is not to use any threshold" 219 | echo "You can use unlimited number of threshold for each type." 220 | echo 221 | 222 | echo " -tg

Enable threshold for 'good status'" 223 | echo " -ti

Enable threshold for 'info status'" 224 | echo " -tw

Enable threshold for 'warn status'" 225 | echo " -tc

Enable threshold for 'crit status'" 226 | echo 227 | echo " Explanation:" 228 | echo "

is the placeholder value you want to check against." 229 | printf " valid placeholders: " 230 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 231 | printf "%s" "${format_nodes[$i]} " 232 | done 233 | printf "\\n" 234 | echo " Note 1: placeholder values will be converted to integers" 235 | echo " Any decimal places will simply be cut off." 236 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 237 | echo " no placeholder will be converted." 238 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 239 | echo " no placeholder will be converted." 240 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 241 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 242 | echo " you want to check the threshold placeholder against." 243 | echo " The integer number you want to check against the placeholder." 244 | echo " The string you want to check against the placeholder." 245 | echo " You can only use a string when in equal mode '='." 246 | echo " You can also use regex here." 247 | echo 248 | echo " Examples:" 249 | echo " 1. Check if value of ${format_nodes[0]} < 50, then format using the good color" 250 | echo " -tg '${format_nodes[0]}' '<' 50" 251 | echo 252 | echo " 2. Check if value of ${format_nodes[0]} > 90, then format using the warn color" 253 | echo " -tw '${format_nodes[0]}' '>' 90" 254 | echo 255 | echo " 3. Check if value of ${format_nodes[0]} equals the string 'foo', then format using the info color" 256 | echo " -ti '${format_nodes[0]}' '=' 'foo'" 257 | echo 258 | echo " 4. Check if value of ${format_nodes[0]} equals the regex '^[0-9]+\$', then format using the info color" 259 | echo " -ti '${format_nodes[0]}' '=' '^[0-9]+\$'" 260 | echo 261 | fi 262 | 263 | echo "Optional markup (pango):" 264 | echo "--------------------------------------------------------------------------------" 265 | echo " -np Disable pango markup" 266 | echo 267 | 268 | echo "Optional color arguments:" 269 | echo "--------------------------------------------------------------------------------" 270 | echo "If not specified, script default colors are used" 271 | echo "If config file with color codes is present in:" 272 | echo "'${HOME}/.config/i3blocks-modules/conf', these colors will be used." 273 | echo 274 | echo " -cd Default color (hexadecimal color code)" 275 | echo " Default value is: ${color_def}" 276 | echo " -cg Good color (hexadecimal color code)" 277 | echo " Default value is: ${color_good}" 278 | echo " -cw Warning color (hexadecimal color code)" 279 | echo " Default value is: ${color_warn}" 280 | echo " -cc Critical color (hexadecimal color code)" 281 | echo " Default value is: ${color_crit}" 282 | echo " -ci Info color (hexadecimal color code)" 283 | echo " Default value is: ${color_info}" 284 | echo 285 | 286 | echo "Optional Format placeholders:" 287 | echo "--------------------------------------------------------------------------------" 288 | echo " Available color placeholders:" 289 | echo " (Use with pango disabled for custom markup) building" 290 | echo " {color} Current active color depending on thresholds" 291 | echo " {color_def} Default color" 292 | echo " {color_good} Good color" 293 | echo " {color_warn} Warning color" 294 | echo " {color_crit} Critical color" 295 | echo " {color_info} Info color" 296 | echo " Format example:" 297 | echo " -np -f \"Colored text\"" 298 | echo 299 | 300 | echo " Available specific placeholders:" 301 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 302 | printf " %-15s%s\\n" "${format_nodes[$i]}" "${format_descs[$i]}" 303 | done 304 | 305 | echo " Format example:" 306 | for (( i=0; i<${#format_examples[@]}; i++ )); do 307 | printf " %s\\n" "${format_examples[$i]}" 308 | done 309 | echo " Default:" 310 | echo " -f \"${format}\"" 311 | echo 312 | 313 | echo "Optional extended Format output:" 314 | echo "--------------------------------------------------------------------------------" 315 | echo "You can conditionally set your output text depending on the value of any placeholder." 316 | echo "For example, If you have a placeholder {status} that either is 'up' or 'down', you" 317 | echo "can specify different outputs for 'up' and for 'down'." 318 | echo "Usage" 319 | echo " -fe

" 320 | echo 321 | echo " Format example:" 322 | echo " -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}'" 323 | echo " Explanation:" 324 | echo "

is the placeholder value you want to check against." 325 | printf " valid placeholders: " 326 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 327 | printf "%s" "${format_nodes[$i]} " 328 | done 329 | printf "\\n" 330 | echo " Note 1: placeholder values will be converted to integers" 331 | echo " Any decimal places will simply be cut off." 332 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 333 | echo " no placeholder will be converted." 334 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 335 | echo " no placeholder will be converted." 336 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 337 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 338 | echo " you want to check the threshold placeholder against." 339 | echo " The integer number you want to check against the placeholder." 340 | echo " The string you want to check against the placeholder." 341 | echo " You can only use a string when in equal mode '='." 342 | echo " You can also use regex here." 343 | echo " Is the format string that should be displayed under the above condition." 344 | echo " Of course you can also use placeholders here ;-)." 345 | } 346 | 347 | print_version() { 348 | echo "${appname} v1.8 by cytopia" 349 | echo "https://github.com/cytopia/i3blocks-modules" 350 | } 351 | 352 | ### 353 | ### Decide about final output color color 354 | ### 355 | get_status_color() { 356 | local _color_def="${1}" 357 | local _color_good="${2}" 358 | local _color_warn="${3}" 359 | local _color_crit="${4}" 360 | local _color_info="${5}" 361 | 362 | # final color 363 | local _color="${_color_def}" 364 | 365 | local pval 366 | 367 | # has custom critical color? 368 | if [ "${force_crit}" = "1" ]; then 369 | _color="${_color_crit}" 370 | echo "${_color}" 371 | return 372 | fi 373 | # has custom warning color? 374 | if [ "${force_warn}" = "1" ]; then 375 | _color="${_color_warn}" 376 | echo "${_color}" 377 | return 378 | fi 379 | 380 | 381 | # has custom good color? 382 | if [ "${force_good}" = "1" ]; then 383 | _color="${_color_good}" 384 | fi 385 | 386 | # has good color? 387 | for (( i=0; i < ${#tg_placeholder[@]}; i++ )); do 388 | 389 | if [ "${tg_sign[$i]}" = "=" ] || [ "${tg_sign[$i]}" = "!=" ]; then 390 | pval="${!tg_placeholder[$i]}" 391 | else 392 | pval="$( echo "${!tg_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 393 | fi 394 | 395 | if [ "${tg_sign[$i]}" = "<" ]; then 396 | if [ "${pval}" -lt "${tg_value[$i]}" ]; then 397 | _color="${_color_good}" 398 | fi 399 | elif [ "${tg_sign[$i]}" = "=" ]; then 400 | if [[ "${pval}" =~ ${tg_value[$i]} ]]; then 401 | _color="${_color_good}" 402 | fi 403 | elif [ "${tg_sign[$i]}" = "!=" ]; then 404 | if [[ "${pval}" != "${tg_value[$i]}" ]]; then 405 | _color="${_color_good}" 406 | fi 407 | elif [ "${tg_sign[$i]}" = ">" ]; then 408 | if [ "${pval}" -gt "${tg_value[$i]}" ]; then 409 | _color="${_color_good}" 410 | fi 411 | fi 412 | done 413 | # has info color? 414 | for (( i=0; i < ${#ti_placeholder[@]}; i++ )); do 415 | 416 | if [ "${ti_sign[$i]}" = "=" ] || [ "${ti_sign[$i]}" = "!=" ]; then 417 | pval="${!ti_placeholder[$i]}" 418 | else 419 | pval="$( echo "${!ti_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 420 | fi 421 | 422 | if [ "${ti_sign[$i]}" = "<" ]; then 423 | if [ "${pval}" -lt "${ti_value[$i]}" ]; then 424 | _color="${_color_info}" 425 | fi 426 | elif [ "${ti_sign[$i]}" = "=" ]; then 427 | if [[ "${pval}" =~ ${ti_value[$i]} ]]; then 428 | _color="${_color_info}" 429 | fi 430 | elif [ "${ti_sign[$i]}" = "!=" ]; then 431 | if [[ "${pval}" != "${ti_value[$i]}" ]]; then 432 | _color="${_color_info}" 433 | fi 434 | elif [ "${ti_sign[$i]}" = ">" ]; then 435 | if [ "${pval}" -gt "${ti_value[$i]}" ]; then 436 | _color="${_color_info}" 437 | fi 438 | fi 439 | done 440 | # has warning color? 441 | for (( i=0; i < ${#tw_placeholder[@]}; i++ )); do 442 | 443 | if [ "${tw_sign[$i]}" = "=" ] || [ "${tw_sign[$i]}" = "!=" ]; then 444 | pval="${!tw_placeholder[$i]}" 445 | else 446 | pval="$( echo "${!tw_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 447 | fi 448 | 449 | if [ "${tw_sign[$i]}" = "<" ]; then 450 | if [ "${pval}" -lt "${tw_value[$i]}" ]; then 451 | _color="${color_warn}" 452 | fi 453 | elif [ "${tw_sign[$i]}" = "=" ]; then 454 | if [[ "${pval}" =~ ${tw_value[$i]} ]]; then 455 | _color="${_color_warn}" 456 | fi 457 | elif [ "${tw_sign[$i]}" = "!=" ]; then 458 | if [[ "${pval}" != "${tw_value[$i]}" ]]; then 459 | _color="${_color_warn}" 460 | fi 461 | elif [ "${tw_sign[$i]}" = ">" ]; then 462 | if [ "${pval}" -gt "${tw_value[$i]}" ]; then 463 | _color="${_color_warn}" 464 | fi 465 | fi 466 | done 467 | 468 | 469 | # has critical color? 470 | for (( i=0; i < ${#tc_placeholder[@]}; i++ )); do 471 | 472 | if [ "${tc_sign[$i]}" = "=" ] || [ "${tc_sign[$i]}" = "!=" ]; then 473 | pval="${!tc_placeholder[$i]}" 474 | else 475 | pval="$( echo "${!tc_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 476 | fi 477 | 478 | if [ "${tc_sign[$i]}" = "<" ]; then 479 | if [ "${pval}" -lt "${tc_value[$i]}" ]; then 480 | _color="${_color_crit}" 481 | fi 482 | elif [ "${tc_sign[$i]}" = "=" ]; then 483 | if [[ "${pval}" =~ ${tc_value[$i]} ]]; then 484 | _color="${_color_crit}" 485 | fi 486 | elif [ "${tc_sign[$i]}" = "!=" ]; then 487 | if [[ "${pval}" != "${tc_value[$i]}" ]]; then 488 | _color="${_color_crit}" 489 | fi 490 | elif [ "${tc_sign[$i]}" = ">" ]; then 491 | if [ "${pval}" -gt "${tc_value[$i]}" ]; then 492 | _color="${_color_crit}" 493 | fi 494 | fi 495 | done 496 | 497 | 498 | echo "${_color}" 499 | } 500 | 501 | 502 | ### 503 | ### Replace custom stuff in format string 504 | ### 505 | replace_placeholders() { 506 | local _format="${1}" 507 | local _search 508 | local _replace 509 | 510 | # Select format based on extended placeholders 511 | for (( i=0; i < ${#fe_placeholder[@]}; i++ )); do 512 | 513 | if [ "${fe_sign[$i]}" = "=" ] || [ "${fe_sign[$i]}" = "!=" ]; then 514 | pval="${!fe_placeholder[$i]}" 515 | else 516 | pval="$( echo "${!fe_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 517 | fi 518 | 519 | if [ "${fe_sign[$i]}" = "<" ]; then 520 | if [ "${pval}" -lt "${fe_value[$i]}" ]; then 521 | _format="${fe_format[$i]}" 522 | fi 523 | elif [ "${fe_sign[$i]}" = "=" ]; then 524 | if [[ "${pval}" =~ ${fe_value[$i]} ]]; then 525 | _format="${fe_format[$i]}" 526 | fi 527 | elif [ "${fe_sign[$i]}" = "!=" ]; then 528 | if [[ "${pval}" != "${fe_value[$i]}" ]]; then 529 | _format="${fe_format[$i]}" 530 | fi 531 | elif [ "${fe_sign[$i]}" = ">" ]; then 532 | if [ "${pval}" -gt "${fe_value[$i]}" ]; then 533 | _format="${fe_format[$i]}" 534 | fi 535 | fi 536 | done 537 | 538 | 539 | 540 | # Replace placeholders in $format 541 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 542 | _search="${format_nodes[$i]}" 543 | _replace="${!format_vars[$i]}" 544 | _format="${_format/${_search}/${_replace}}" 545 | done 546 | echo "${_format}" 547 | } 548 | 549 | 550 | ### 551 | ### Replace colors in format string 552 | ### 553 | replace_colors() { 554 | local _format="${1}" 555 | local _color="${2}" 556 | local _color_def="${3}" 557 | local _color_good="${4}" 558 | local _color_warn="${5}" 559 | local _color_crit="${6}" 560 | local _color_info="${7}" 561 | 562 | _format="${_format/'{color}'/${_color}}" 563 | _format="${_format/'{color_def}'/${_color_def}}" 564 | _format="${_format/'{color_good}'/${_color_good}}" 565 | _format="${_format/'{color_warn}'/${_color_warn}}" 566 | _format="${_format/'{color_crit}'/${_color_crit}}" 567 | _format="${_format/'{color_info}'/${_color_info}}" 568 | 569 | echo "${_format}" 570 | } 571 | 572 | 573 | 574 | ################################################################################ 575 | # 576 | # MAIN ENTRY POINT 577 | # 578 | ################################################################################ 579 | 580 | # Enable/Disable threshold argument 581 | if [ "${has_threshold}" = "1" ]; then 582 | th_chk="" 583 | else 584 | th_chk="__THRESHOLD_DISABLED__" 585 | fi 586 | 587 | 588 | while [ $# -gt 0 ]; do 589 | case "$1" in 590 | ### 591 | ### Extended formats 592 | ### 593 | -fe) 594 | # 1/4 Check placeholder 595 | shift 596 | if [ "${1}" = "" ]; then 597 | echo "Error, -fe

- no placeholder specified." 598 | echo "Type ${appname} -h for help" 599 | exit 1 600 | fi 601 | f=0 602 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 603 | if [ "${format_nodes[$i]}" = "${1}" ]; then 604 | f=1 605 | break 606 | fi 607 | done 608 | if [ "${f}" = "0" ]; then 609 | echo "Error, -fe '${1}' no such placeholder." 610 | echo "Type ${appname} -h for help" 611 | exit 1 612 | fi 613 | fe_placeholder+=("${format_vars[$i]}") 614 | 615 | # 2/4 Check sign 616 | shift 617 | if [ "${1}" = "" ]; then 618 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - sign argyment is empty." 619 | echo "Type ${appname} -h for help" 620 | exit 1 621 | fi 622 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 623 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 624 | echo "Type ${appname} -h for help" 625 | exit 1 626 | fi 627 | fe_sign+=("${1}") 628 | 629 | # 3/4 Check value 630 | shift 631 | if [ "${1}" = "" ]; then 632 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is empty." 633 | echo "Type ${appname} -h for help" 634 | exit 1 635 | fi 636 | if [ "${fe_sign[${#fe_sign[@]}-1]}" = ">" ] || [ "${fe_sign[${#fe_sign[@]}-1]}" = "<" ]; then 637 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 638 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is not a number." 639 | echo "Type ${appname} -h for help" 640 | exit 1 641 | fi 642 | fi 643 | fe_value+=("${1}") 644 | 645 | # 4/4 Check placeholder string 646 | shift 647 | fe_format+=("${1}") 648 | ;; 649 | ### 650 | ### Threshold good 651 | ### 652 | "-tg${th_chk}") 653 | # 1/3 Check placeholder 654 | shift 655 | if [ "${1}" = "" ]; then 656 | echo "Error, -tg

- no placeholder specified." 657 | echo "Type ${appname} -h for help" 658 | exit 1 659 | fi 660 | f=0 661 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 662 | if [ "${format_nodes[$i]}" = "${1}" ]; then 663 | f=1 664 | break 665 | fi 666 | done 667 | if [ "${f}" = "0" ]; then 668 | echo "Error, -tg '${1}' no such placeholder." 669 | echo "Type ${appname} -h for help" 670 | exit 1 671 | fi 672 | tg_placeholder+=("${format_vars[$i]}") 673 | 674 | # 2/3 Check sign 675 | shift 676 | if [ "${1}" = "" ]; then 677 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 678 | echo "Type ${appname} -h for help" 679 | exit 1 680 | fi 681 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 682 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 683 | echo "Type ${appname} -h for help" 684 | exit 1 685 | fi 686 | tg_sign+=("${1}") 687 | 688 | # 3/3 Check value 689 | shift 690 | if [ "${1}" = "" ]; then 691 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argyment is empty." 692 | echo "Type ${appname} -h for help" 693 | exit 1 694 | fi 695 | if [ "${tg_sign[${#tg_sign[@]}-1]}" = ">" ] || [ "${tg_sign[${#tg_sign[@]}-1]}" = "<" ]; then 696 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 697 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argument is not a number." 698 | echo "Type ${appname} -h for help" 699 | exit 1 700 | fi 701 | fi 702 | tg_value+=("${1}") 703 | ;; 704 | ### 705 | ### Threshold info 706 | ### 707 | "-ti${th_chk}") 708 | # 1/3 Check placeholder 709 | shift 710 | if [ "${1}" = "" ]; then 711 | echo "Error, -ti

- no placeholder specified." 712 | echo "Type ${appname} -h for help" 713 | exit 1 714 | fi 715 | f=0 716 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 717 | if [ "${format_nodes[$i]}" = "${1}" ]; then 718 | f=1 719 | break 720 | fi 721 | done 722 | if [ "${f}" = "0" ]; then 723 | echo "Error, -ti '${1}' no such placeholder." 724 | echo "Type ${appname} -h for help" 725 | exit 1 726 | fi 727 | ti_placeholder+=("${format_vars[$i]}") 728 | 729 | # 2/3 Check sign 730 | shift 731 | if [ "${1}" = "" ]; then 732 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 733 | echo "Type ${appname} -h for help" 734 | exit 1 735 | fi 736 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 737 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 738 | echo "Type ${appname} -h for help" 739 | exit 1 740 | fi 741 | ti_sign+=("${1}") 742 | 743 | # 3/3 Check value 744 | shift 745 | if [ "${1}" = "" ]; then 746 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argyment is empty." 747 | echo "Type ${appname} -h for help" 748 | exit 1 749 | fi 750 | if [ "${ti_sign[${#ti_sign[@]}-1]}" = ">" ] || [ "${ti_sign[${#ti_sign[@]}-1]}" = "<" ]; then 751 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 752 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argument is not a number." 753 | echo "Type ${appname} -h for help" 754 | exit 1 755 | fi 756 | fi 757 | ti_value+=("${1}") 758 | ;; 759 | ### 760 | ### Threshold warning 761 | ### 762 | "-tw${th_chk}") 763 | # 1/3 Check placeholder 764 | shift 765 | if [ "${1}" = "" ]; then 766 | echo "Error, -tw

- no placeholder specified." 767 | echo "Type ${appname} -h for help" 768 | exit 1 769 | fi 770 | f=0 771 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 772 | if [ "${format_nodes[$i]}" = "${1}" ]; then 773 | f=1 774 | break 775 | fi 776 | done 777 | if [ "${f}" = "0" ]; then 778 | echo "Error, -tw '${1}' no such placeholder." 779 | echo "Type ${appname} -h for help" 780 | exit 1 781 | fi 782 | tw_placeholder+=("${format_vars[$i]}") 783 | 784 | # 2/3 Check sign 785 | shift 786 | if [ "${1}" = "" ]; then 787 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 788 | echo "Type ${appname} -h for help" 789 | exit 1 790 | fi 791 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 792 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 793 | echo "Type ${appname} -h for help" 794 | exit 1 795 | fi 796 | tw_sign+=("${1}") 797 | 798 | # 3/3 Check value 799 | shift 800 | if [ "${1}" = "" ]; then 801 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argyment is empty." 802 | echo "Type ${appname} -h for help" 803 | exit 1 804 | fi 805 | if [ "${tw_sign[${#tw_sign[@]}-1]}" = ">" ] || [ "${tw_sign[${#tw_sign[@]}-1]}" = "<" ]; then 806 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 807 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argument is not a number." 808 | echo "Type ${appname} -h for help" 809 | exit 1 810 | fi 811 | fi 812 | tw_value+=("${1}") 813 | ;; 814 | ### 815 | ### Threshold critical 816 | ### 817 | "-tc${th_chk}") 818 | # 1/3 Check placeholder 819 | shift 820 | if [ "${1}" = "" ]; then 821 | echo "Error, -tc

- no placeholder specified." 822 | echo "Type ${appname} -h for help" 823 | exit 1 824 | fi 825 | f=0 826 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 827 | if [ "${format_nodes[$i]}" = "${1}" ]; then 828 | f=1 829 | break 830 | fi 831 | done 832 | if [ "${f}" = "0" ]; then 833 | echo "Error, -tc '${1}' no such placeholder." 834 | echo "Type ${appname} -h for help" 835 | exit 1 836 | fi 837 | tc_placeholder+=("${format_vars[$i]}") 838 | 839 | # 2/3 Check sign 840 | shift 841 | if [ "${1}" = "" ]; then 842 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 843 | echo "Type ${appname} -h for help" 844 | exit 1 845 | fi 846 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 847 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 848 | echo "Type ${appname} -h for help" 849 | exit 1 850 | fi 851 | tc_sign+=("${1}") 852 | 853 | # 3/3 Check value 854 | shift 855 | if [ "${1}" = "" ]; then 856 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argyment is empty." 857 | echo "Type ${appname} -h for help" 858 | exit 1 859 | fi 860 | if [ "${tc_sign[${#tc_sign[@]}-1]}" = ">" ] || [ "${tc_sign[${#tc_sign[@]}-1]}" = "<" ]; then 861 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 862 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argument is not a number." 863 | echo "Type ${appname} -h for help" 864 | exit 1 865 | fi 866 | fi 867 | tc_value+=("${1}") 868 | ;; 869 | ### 870 | ### Format overwrite 871 | ### 872 | -f) 873 | shift 874 | if [ "${1}" = "" ]; then 875 | echo "Error, -f requires a string" 876 | echo "Type ${appname} -h for help" 877 | exit 1 878 | fi 879 | format="${1}" 880 | ;; 881 | ### 882 | ### Disable pango markup output 883 | ### 884 | -np) 885 | pango=0 886 | ;; 887 | ### 888 | ### Color overwrites 889 | ### 890 | -cd) 891 | # default color 892 | shift 893 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 894 | echo "Error, invalid color string: ${1}" 895 | echo "Type ${appname} -h for help" 896 | exit 1 897 | fi 898 | color_def="${1}" 899 | ;; 900 | -cg) 901 | # good color 902 | shift 903 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 904 | echo "Error, invalid color string: ${1}" 905 | echo "Type ${appname} -h for help" 906 | exit 1 907 | fi 908 | color_good="${1}" 909 | ;; 910 | -cw) 911 | # warning color 912 | shift 913 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 914 | echo "Error, invalid color string: ${1}" 915 | echo "Type ${appname} -h for help" 916 | exit 1 917 | fi 918 | color_warn="${1}" 919 | ;; 920 | -cc) 921 | # critical color 922 | shift 923 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 924 | echo "Error, invalid color string: ${1}" 925 | echo "Type ${appname} -h for help" 926 | exit 1 927 | fi 928 | color_crit="${1}" 929 | ;; 930 | -ci) 931 | # info color 932 | shift 933 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 934 | echo "Error, invalid color string: ${1}" 935 | echo "Type ${appname} -h for help" 936 | exit 1 937 | fi 938 | color_info="${1}" 939 | ;; 940 | ### 941 | ### System options 942 | ### 943 | -h) 944 | print_usage 945 | exit 0 946 | ;; 947 | -v) 948 | print_version 949 | exit 0 950 | ;; 951 | ### 952 | ### Unknown/Custom option 953 | ### 954 | *) 955 | 956 | ### 957 | ### Evaluate user-specified arguments 958 | ### 959 | found=0 960 | if [ "${#arg_params}" -gt "0" ]; then 961 | for (( i=0; i<${#arg_params[@]}; i++ )); do 962 | if [ "${arg_params[$i]}" = "${1}" ]; then 963 | shift 964 | var_name="${arg_vars[$i]}" 965 | eval "${var_name}=\"${1}\"" 966 | found=1 967 | break; 968 | fi 969 | done 970 | fi 971 | 972 | ### 973 | ### Unknown option 974 | ### 975 | if [ "${found}" = "0" ]; then 976 | echo "Invalid argument: '${1}'" 977 | echo "Type ${appname} -h for help" 978 | exit 1 979 | fi 980 | ;; 981 | esac 982 | shift 983 | done 984 | 985 | 986 | 987 | 988 | ### 989 | ### Call custom function 990 | ### 991 | custom_action 992 | 993 | ### 994 | ### Get final output color (based on custom specs) 995 | ### 996 | color="$( get_status_color "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 997 | 998 | ### 999 | ### Format (colors) 1000 | ### 1001 | format="$( replace_colors "${format}" "${color}" "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1002 | 1003 | ### 1004 | ### Format (custom) 1005 | ### 1006 | format="$( replace_placeholders "${format}" )" 1007 | 1008 | 1009 | ### 1010 | ### Output pango or plain style? 1011 | ### 1012 | if [ "${pango}" = "1" ]; then 1013 | if [ "${format}" != "" ]; then 1014 | echo "${format}" 1015 | fi 1016 | else 1017 | echo "${format}" # Long output 1018 | echo "${format}" # short output 1019 | echo "\\${color}" # color code '\#RRGGBB' 1020 | fi 1021 | 1022 | exit 0 1023 | -------------------------------------------------------------------------------- /modules/cputemp: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" 4 | 5 | ################################################################################ 6 | # 7 | # CUSTOM PLUGIN SECTION 8 | # 9 | ################################################################################ 10 | 11 | ############################################################ 12 | # Variables 13 | ############################################################ 14 | 15 | ### 16 | ### Custom Defines 17 | ### 18 | appname="cputemp" 19 | core="${BLOCK_INSTANCE:-Core 0}" 20 | format=" {temp}°C" 21 | 22 | 23 | ### 24 | ### Thresholds 25 | ### 26 | 27 | # Enable 28 | has_threshold=1 29 | 30 | # Depending on the conditions in your custom_action() 31 | # you can force the output to be critical or warning 32 | # Set these vars to 1 (in custom_action) 33 | force_crit=0 34 | force_warn=0 35 | force_good=0 36 | 37 | 38 | ### 39 | ### Additional arguments 40 | ### 41 | arg_params=( 42 | "-c" 43 | ) 44 | arg_vars=( 45 | "core" 46 | ) 47 | arg_desc_val=( 48 | "" 49 | ) 50 | arg_desc_long=( 51 | "Specify the core name being used by 'sensors'. This can also be set via i3blocks 'instance=' value. If neither is set, it will default to 'Core 0'." 52 | ) 53 | 54 | 55 | ### 56 | ### Format placeholders 57 | ### 58 | 59 | # bash variable names 60 | format_vars=( 61 | temp 62 | ) 63 | 64 | # Format placeholders 65 | format_nodes=( 66 | "{temp}" 67 | ) 68 | 69 | # Format description (for help display) 70 | format_descs=( 71 | "Temperature" 72 | ) 73 | 74 | # Format examples (for help display) 75 | format_examples=( 76 | "-f \" {temp}°C\"" 77 | ) 78 | 79 | 80 | ############################################################ 81 | # custom_actio function 82 | ############################################################ 83 | 84 | ### 85 | ### Evaluate disk space 86 | ### 87 | custom_action() { 88 | if ! command -v sensors > /dev/null 2>&1; then 89 | echo "Error, binary 'sensos' not installed, but required for this module to work." 90 | echo "Type '${appname} -h' for help" 91 | exit 1 92 | fi 93 | temp="$( sensors | grep "${core}" | grep -oE '[.0-9]*.C' | head -1 | grep -oE '[0-9]*'| head -1 )" 94 | } 95 | 96 | 97 | 98 | 99 | ### 100 | ### 101 | ### 102 | ### D O N O T E D I T A F T E R H E R E 103 | ### 104 | ### 105 | ### 106 | 107 | 108 | 109 | ################################################################################ 110 | # 111 | # BUILT-IN VARIABLES 112 | # 113 | ################################################################################ 114 | 115 | ### 116 | ### General default values 117 | ### 118 | color_def="#666666" # gray 119 | color_good="#88b090" # green 120 | color_warn="#ccdc90" # yellow 121 | color_crit="#e89393" # red 122 | color_info="#fce94f" # bright yellow 123 | 124 | ### 125 | ### Extended format arrays 126 | ### 127 | fe_placeholder=() 128 | fe_sign=() 129 | fe_value=() 130 | fe_format=() 131 | 132 | 133 | ### 134 | ### Threshold arrays 135 | ### 136 | tg_placeholder=() 137 | tg_sign=() 138 | tg_value=() 139 | 140 | ti_placeholder=() 141 | ti_sign=() 142 | ti_value=() 143 | 144 | tw_placeholder=() 145 | tw_sign=() 146 | tw_value=() 147 | 148 | tc_placeholder=() 149 | tc_sign=() 150 | tc_value=() 151 | 152 | 153 | ### 154 | ### Use of pango markup? 155 | ### 156 | pango=1 157 | 158 | 159 | ### 160 | ### source configuration file if it exists 161 | ### 162 | if [ -f "${HOME}/.config/i3blocks-modules/conf" ]; then 163 | # shellcheck disable=SC1090 164 | . "${HOME}/.config/i3blocks-modules/conf" 165 | fi 166 | 167 | 168 | ### 169 | ### i3blocks vars info 170 | ### https://vivien.github.io/i3blocks/ 171 | ### 172 | # name= ${BLOCK_NAME} 173 | # instace= ${BLOCK_INSTANCE} 174 | # button= ${BLOCK_BUTTON} 175 | # x-coor ${BLOCK_X} 176 | # y-coor ${BLOCK_Y} 177 | 178 | 179 | ################################################################################ 180 | # 181 | # BUILT-IN FUNCTIONS 182 | # 183 | ################################################################################ 184 | 185 | ### 186 | ### System functions 187 | ### 188 | print_usage() { 189 | custom_args="" 190 | 191 | # program specific arguments 192 | for (( i=0; i<${#arg_params[@]}; i++ )); do 193 | custom_args="${custom_args}[${arg_params[$i]} ${arg_desc_val[$i]}] " 194 | done 195 | 196 | # Show/Hide threshold 197 | if [ "${has_threshold}" = "1" ]; then 198 | custom_args="${custom_args}[-tg|-ti|-tw|-tc

] " 199 | fi 200 | 201 | echo "Usage: ${appname} [-f ] [-fe

] ${custom_args}[-np] [-cd|-cg|-cw|-cc|-ci ]" 202 | echo " ${appname} -h" 203 | echo " ${appname} -v" 204 | echo 205 | 206 | if [ "${#custom_args}" -gt "0" ]; then 207 | echo "Optional variables:" 208 | echo "--------------------------------------------------------------------------------" 209 | 210 | for (( i=0; i<${#arg_params[@]}; i++ )); do 211 | printf " %-13s%s\\n" "${arg_params[$i]} ${arg_desc_val[$i]}" "${arg_desc_long[$i]}" 212 | done 213 | echo 214 | fi 215 | 216 | if [ "${has_threshold}" = "1" ]; then 217 | echo "Optional threshold arguments:" 218 | echo "--------------------------------------------------------------------------------" 219 | echo "You can optionally enable threshold checking against any placeholder value." 220 | echo "This enables the colorizing of the final output depending on any met" 221 | echo "conditions specified." 222 | echo "Default is not to use any threshold" 223 | echo "You can use unlimited number of threshold for each type." 224 | echo 225 | 226 | echo " -tg

Enable threshold for 'good status'" 227 | echo " -ti

Enable threshold for 'info status'" 228 | echo " -tw

Enable threshold for 'warn status'" 229 | echo " -tc

Enable threshold for 'crit status'" 230 | echo 231 | echo " Explanation:" 232 | echo "

is the placeholder value you want to check against." 233 | printf " valid placeholders: " 234 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 235 | printf "%s" "${format_nodes[$i]} " 236 | done 237 | printf "\\n" 238 | echo " Note 1: placeholder values will be converted to integers" 239 | echo " Any decimal places will simply be cut off." 240 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 241 | echo " no placeholder will be converted." 242 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 243 | echo " no placeholder will be converted." 244 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 245 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 246 | echo " you want to check the threshold placeholder against." 247 | echo " The integer number you want to check against the placeholder." 248 | echo " The string you want to check against the placeholder." 249 | echo " You can only use a string when in equal mode '='." 250 | echo " You can also use regex here." 251 | echo 252 | echo " Examples:" 253 | echo " 1. Check if value of ${format_nodes[0]} < 50, then format using the good color" 254 | echo " -tg '${format_nodes[0]}' '<' 50" 255 | echo 256 | echo " 2. Check if value of ${format_nodes[0]} > 90, then format using the warn color" 257 | echo " -tw '${format_nodes[0]}' '>' 90" 258 | echo 259 | echo " 3. Check if value of ${format_nodes[0]} equals the string 'foo', then format using the info color" 260 | echo " -ti '${format_nodes[0]}' '=' 'foo'" 261 | echo 262 | echo " 4. Check if value of ${format_nodes[0]} equals the regex '^[0-9]+\$', then format using the info color" 263 | echo " -ti '${format_nodes[0]}' '=' '^[0-9]+\$'" 264 | echo 265 | fi 266 | 267 | echo "Optional markup (pango):" 268 | echo "--------------------------------------------------------------------------------" 269 | echo " -np Disable pango markup" 270 | echo 271 | 272 | echo "Optional color arguments:" 273 | echo "--------------------------------------------------------------------------------" 274 | echo "If not specified, script default colors are used" 275 | echo "If config file with color codes is present in:" 276 | echo "'${HOME}/.config/i3blocks-modules/conf', these colors will be used." 277 | echo 278 | echo " -cd Default color (hexadecimal color code)" 279 | echo " Default value is: ${color_def}" 280 | echo " -cg Good color (hexadecimal color code)" 281 | echo " Default value is: ${color_good}" 282 | echo " -cw Warning color (hexadecimal color code)" 283 | echo " Default value is: ${color_warn}" 284 | echo " -cc Critical color (hexadecimal color code)" 285 | echo " Default value is: ${color_crit}" 286 | echo " -ci Info color (hexadecimal color code)" 287 | echo " Default value is: ${color_info}" 288 | echo 289 | 290 | echo "Optional Format placeholders:" 291 | echo "--------------------------------------------------------------------------------" 292 | echo " Available color placeholders:" 293 | echo " (Use with pango disabled for custom markup) building" 294 | echo " {color} Current active color depending on thresholds" 295 | echo " {color_def} Default color" 296 | echo " {color_good} Good color" 297 | echo " {color_warn} Warning color" 298 | echo " {color_crit} Critical color" 299 | echo " {color_info} Info color" 300 | echo " Format example:" 301 | echo " -np -f \"Colored text\"" 302 | echo 303 | 304 | echo " Available specific placeholders:" 305 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 306 | printf " %-15s%s\\n" "${format_nodes[$i]}" "${format_descs[$i]}" 307 | done 308 | 309 | echo " Format example:" 310 | for (( i=0; i<${#format_examples[@]}; i++ )); do 311 | printf " %s\\n" "${format_examples[$i]}" 312 | done 313 | echo " Default:" 314 | echo " -f \"${format}\"" 315 | echo 316 | 317 | echo "Optional extended Format output:" 318 | echo "--------------------------------------------------------------------------------" 319 | echo "You can conditionally set your output text depending on the value of any placeholder." 320 | echo "For example, If you have a placeholder {status} that either is 'up' or 'down', you" 321 | echo "can specify different outputs for 'up' and for 'down'." 322 | echo "Usage" 323 | echo " -fe

" 324 | echo 325 | echo " Format example:" 326 | echo " -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}'" 327 | echo " Explanation:" 328 | echo "

is the placeholder value you want to check against." 329 | printf " valid placeholders: " 330 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 331 | printf "%s" "${format_nodes[$i]} " 332 | done 333 | printf "\\n" 334 | echo " Note 1: placeholder values will be converted to integers" 335 | echo " Any decimal places will simply be cut off." 336 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 337 | echo " no placeholder will be converted." 338 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 339 | echo " no placeholder will be converted." 340 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 341 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 342 | echo " you want to check the threshold placeholder against." 343 | echo " The integer number you want to check against the placeholder." 344 | echo " The string you want to check against the placeholder." 345 | echo " You can only use a string when in equal mode '='." 346 | echo " You can also use regex here." 347 | echo " Is the format string that should be displayed under the above condition." 348 | echo " Of course you can also use placeholders here ;-)." 349 | } 350 | 351 | print_version() { 352 | echo "${appname} v1.8 by cytopia" 353 | echo "https://github.com/cytopia/i3blocks-modules" 354 | } 355 | 356 | ### 357 | ### Decide about final output color color 358 | ### 359 | get_status_color() { 360 | local _color_def="${1}" 361 | local _color_good="${2}" 362 | local _color_warn="${3}" 363 | local _color_crit="${4}" 364 | local _color_info="${5}" 365 | 366 | # final color 367 | local _color="${_color_def}" 368 | 369 | local pval 370 | 371 | # has custom critical color? 372 | if [ "${force_crit}" = "1" ]; then 373 | _color="${_color_crit}" 374 | echo "${_color}" 375 | return 376 | fi 377 | # has custom warning color? 378 | if [ "${force_warn}" = "1" ]; then 379 | _color="${_color_warn}" 380 | echo "${_color}" 381 | return 382 | fi 383 | 384 | 385 | # has custom good color? 386 | if [ "${force_good}" = "1" ]; then 387 | _color="${_color_good}" 388 | fi 389 | 390 | # has good color? 391 | for (( i=0; i < ${#tg_placeholder[@]}; i++ )); do 392 | 393 | if [ "${tg_sign[$i]}" = "=" ] || [ "${tg_sign[$i]}" = "!=" ]; then 394 | pval="${!tg_placeholder[$i]}" 395 | else 396 | pval="$( echo "${!tg_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 397 | fi 398 | 399 | if [ "${tg_sign[$i]}" = "<" ]; then 400 | if [ "${pval}" -lt "${tg_value[$i]}" ]; then 401 | _color="${_color_good}" 402 | fi 403 | elif [ "${tg_sign[$i]}" = "=" ]; then 404 | if [[ "${pval}" =~ ${tg_value[$i]} ]]; then 405 | _color="${_color_good}" 406 | fi 407 | elif [ "${tg_sign[$i]}" = "!=" ]; then 408 | if [[ "${pval}" != "${tg_value[$i]}" ]]; then 409 | _color="${_color_good}" 410 | fi 411 | elif [ "${tg_sign[$i]}" = ">" ]; then 412 | if [ "${pval}" -gt "${tg_value[$i]}" ]; then 413 | _color="${_color_good}" 414 | fi 415 | fi 416 | done 417 | # has info color? 418 | for (( i=0; i < ${#ti_placeholder[@]}; i++ )); do 419 | 420 | if [ "${ti_sign[$i]}" = "=" ] || [ "${ti_sign[$i]}" = "!=" ]; then 421 | pval="${!ti_placeholder[$i]}" 422 | else 423 | pval="$( echo "${!ti_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 424 | fi 425 | 426 | if [ "${ti_sign[$i]}" = "<" ]; then 427 | if [ "${pval}" -lt "${ti_value[$i]}" ]; then 428 | _color="${_color_info}" 429 | fi 430 | elif [ "${ti_sign[$i]}" = "=" ]; then 431 | if [[ "${pval}" =~ ${ti_value[$i]} ]]; then 432 | _color="${_color_info}" 433 | fi 434 | elif [ "${ti_sign[$i]}" = "!=" ]; then 435 | if [[ "${pval}" != "${ti_value[$i]}" ]]; then 436 | _color="${_color_info}" 437 | fi 438 | elif [ "${ti_sign[$i]}" = ">" ]; then 439 | if [ "${pval}" -gt "${ti_value[$i]}" ]; then 440 | _color="${_color_info}" 441 | fi 442 | fi 443 | done 444 | # has warning color? 445 | for (( i=0; i < ${#tw_placeholder[@]}; i++ )); do 446 | 447 | if [ "${tw_sign[$i]}" = "=" ] || [ "${tw_sign[$i]}" = "!=" ]; then 448 | pval="${!tw_placeholder[$i]}" 449 | else 450 | pval="$( echo "${!tw_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 451 | fi 452 | 453 | if [ "${tw_sign[$i]}" = "<" ]; then 454 | if [ "${pval}" -lt "${tw_value[$i]}" ]; then 455 | _color="${color_warn}" 456 | fi 457 | elif [ "${tw_sign[$i]}" = "=" ]; then 458 | if [[ "${pval}" =~ ${tw_value[$i]} ]]; then 459 | _color="${_color_warn}" 460 | fi 461 | elif [ "${tw_sign[$i]}" = "!=" ]; then 462 | if [[ "${pval}" != "${tw_value[$i]}" ]]; then 463 | _color="${_color_warn}" 464 | fi 465 | elif [ "${tw_sign[$i]}" = ">" ]; then 466 | if [ "${pval}" -gt "${tw_value[$i]}" ]; then 467 | _color="${_color_warn}" 468 | fi 469 | fi 470 | done 471 | 472 | 473 | # has critical color? 474 | for (( i=0; i < ${#tc_placeholder[@]}; i++ )); do 475 | 476 | if [ "${tc_sign[$i]}" = "=" ] || [ "${tc_sign[$i]}" = "!=" ]; then 477 | pval="${!tc_placeholder[$i]}" 478 | else 479 | pval="$( echo "${!tc_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 480 | fi 481 | 482 | if [ "${tc_sign[$i]}" = "<" ]; then 483 | if [ "${pval}" -lt "${tc_value[$i]}" ]; then 484 | _color="${_color_crit}" 485 | fi 486 | elif [ "${tc_sign[$i]}" = "=" ]; then 487 | if [[ "${pval}" =~ ${tc_value[$i]} ]]; then 488 | _color="${_color_crit}" 489 | fi 490 | elif [ "${tc_sign[$i]}" = "!=" ]; then 491 | if [[ "${pval}" != "${tc_value[$i]}" ]]; then 492 | _color="${_color_crit}" 493 | fi 494 | elif [ "${tc_sign[$i]}" = ">" ]; then 495 | if [ "${pval}" -gt "${tc_value[$i]}" ]; then 496 | _color="${_color_crit}" 497 | fi 498 | fi 499 | done 500 | 501 | 502 | echo "${_color}" 503 | } 504 | 505 | 506 | ### 507 | ### Replace custom stuff in format string 508 | ### 509 | replace_placeholders() { 510 | local _format="${1}" 511 | local _search 512 | local _replace 513 | 514 | # Select format based on extended placeholders 515 | for (( i=0; i < ${#fe_placeholder[@]}; i++ )); do 516 | 517 | if [ "${fe_sign[$i]}" = "=" ] || [ "${fe_sign[$i]}" = "!=" ]; then 518 | pval="${!fe_placeholder[$i]}" 519 | else 520 | pval="$( echo "${!fe_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 521 | fi 522 | 523 | if [ "${fe_sign[$i]}" = "<" ]; then 524 | if [ "${pval}" -lt "${fe_value[$i]}" ]; then 525 | _format="${fe_format[$i]}" 526 | fi 527 | elif [ "${fe_sign[$i]}" = "=" ]; then 528 | if [[ "${pval}" =~ ${fe_value[$i]} ]]; then 529 | _format="${fe_format[$i]}" 530 | fi 531 | elif [ "${fe_sign[$i]}" = "!=" ]; then 532 | if [[ "${pval}" != "${fe_value[$i]}" ]]; then 533 | _format="${fe_format[$i]}" 534 | fi 535 | elif [ "${fe_sign[$i]}" = ">" ]; then 536 | if [ "${pval}" -gt "${fe_value[$i]}" ]; then 537 | _format="${fe_format[$i]}" 538 | fi 539 | fi 540 | done 541 | 542 | 543 | 544 | # Replace placeholders in $format 545 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 546 | _search="${format_nodes[$i]}" 547 | _replace="${!format_vars[$i]}" 548 | _format="${_format/${_search}/${_replace}}" 549 | done 550 | echo "${_format}" 551 | } 552 | 553 | 554 | ### 555 | ### Replace colors in format string 556 | ### 557 | replace_colors() { 558 | local _format="${1}" 559 | local _color="${2}" 560 | local _color_def="${3}" 561 | local _color_good="${4}" 562 | local _color_warn="${5}" 563 | local _color_crit="${6}" 564 | local _color_info="${7}" 565 | 566 | _format="${_format/'{color}'/${_color}}" 567 | _format="${_format/'{color_def}'/${_color_def}}" 568 | _format="${_format/'{color_good}'/${_color_good}}" 569 | _format="${_format/'{color_warn}'/${_color_warn}}" 570 | _format="${_format/'{color_crit}'/${_color_crit}}" 571 | _format="${_format/'{color_info}'/${_color_info}}" 572 | 573 | echo "${_format}" 574 | } 575 | 576 | 577 | 578 | ################################################################################ 579 | # 580 | # MAIN ENTRY POINT 581 | # 582 | ################################################################################ 583 | 584 | # Enable/Disable threshold argument 585 | if [ "${has_threshold}" = "1" ]; then 586 | th_chk="" 587 | else 588 | th_chk="__THRESHOLD_DISABLED__" 589 | fi 590 | 591 | 592 | while [ $# -gt 0 ]; do 593 | case "$1" in 594 | ### 595 | ### Extended formats 596 | ### 597 | -fe) 598 | # 1/4 Check placeholder 599 | shift 600 | if [ "${1}" = "" ]; then 601 | echo "Error, -fe

- no placeholder specified." 602 | echo "Type ${appname} -h for help" 603 | exit 1 604 | fi 605 | f=0 606 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 607 | if [ "${format_nodes[$i]}" = "${1}" ]; then 608 | f=1 609 | break 610 | fi 611 | done 612 | if [ "${f}" = "0" ]; then 613 | echo "Error, -fe '${1}' no such placeholder." 614 | echo "Type ${appname} -h for help" 615 | exit 1 616 | fi 617 | fe_placeholder+=("${format_vars[$i]}") 618 | 619 | # 2/4 Check sign 620 | shift 621 | if [ "${1}" = "" ]; then 622 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - sign argyment is empty." 623 | echo "Type ${appname} -h for help" 624 | exit 1 625 | fi 626 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 627 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 628 | echo "Type ${appname} -h for help" 629 | exit 1 630 | fi 631 | fe_sign+=("${1}") 632 | 633 | # 3/4 Check value 634 | shift 635 | if [ "${1}" = "" ]; then 636 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is empty." 637 | echo "Type ${appname} -h for help" 638 | exit 1 639 | fi 640 | if [ "${fe_sign[${#fe_sign[@]}-1]}" = ">" ] || [ "${fe_sign[${#fe_sign[@]}-1]}" = "<" ]; then 641 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 642 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is not a number." 643 | echo "Type ${appname} -h for help" 644 | exit 1 645 | fi 646 | fi 647 | fe_value+=("${1}") 648 | 649 | # 4/4 Check placeholder string 650 | shift 651 | fe_format+=("${1}") 652 | ;; 653 | ### 654 | ### Threshold good 655 | ### 656 | "-tg${th_chk}") 657 | # 1/3 Check placeholder 658 | shift 659 | if [ "${1}" = "" ]; then 660 | echo "Error, -tg

- no placeholder specified." 661 | echo "Type ${appname} -h for help" 662 | exit 1 663 | fi 664 | f=0 665 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 666 | if [ "${format_nodes[$i]}" = "${1}" ]; then 667 | f=1 668 | break 669 | fi 670 | done 671 | if [ "${f}" = "0" ]; then 672 | echo "Error, -tg '${1}' no such placeholder." 673 | echo "Type ${appname} -h for help" 674 | exit 1 675 | fi 676 | tg_placeholder+=("${format_vars[$i]}") 677 | 678 | # 2/3 Check sign 679 | shift 680 | if [ "${1}" = "" ]; then 681 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 682 | echo "Type ${appname} -h for help" 683 | exit 1 684 | fi 685 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 686 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 687 | echo "Type ${appname} -h for help" 688 | exit 1 689 | fi 690 | tg_sign+=("${1}") 691 | 692 | # 3/3 Check value 693 | shift 694 | if [ "${1}" = "" ]; then 695 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argyment is empty." 696 | echo "Type ${appname} -h for help" 697 | exit 1 698 | fi 699 | if [ "${tg_sign[${#tg_sign[@]}-1]}" = ">" ] || [ "${tg_sign[${#tg_sign[@]}-1]}" = "<" ]; then 700 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 701 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argument is not a number." 702 | echo "Type ${appname} -h for help" 703 | exit 1 704 | fi 705 | fi 706 | tg_value+=("${1}") 707 | ;; 708 | ### 709 | ### Threshold info 710 | ### 711 | "-ti${th_chk}") 712 | # 1/3 Check placeholder 713 | shift 714 | if [ "${1}" = "" ]; then 715 | echo "Error, -ti

- no placeholder specified." 716 | echo "Type ${appname} -h for help" 717 | exit 1 718 | fi 719 | f=0 720 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 721 | if [ "${format_nodes[$i]}" = "${1}" ]; then 722 | f=1 723 | break 724 | fi 725 | done 726 | if [ "${f}" = "0" ]; then 727 | echo "Error, -ti '${1}' no such placeholder." 728 | echo "Type ${appname} -h for help" 729 | exit 1 730 | fi 731 | ti_placeholder+=("${format_vars[$i]}") 732 | 733 | # 2/3 Check sign 734 | shift 735 | if [ "${1}" = "" ]; then 736 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 737 | echo "Type ${appname} -h for help" 738 | exit 1 739 | fi 740 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 741 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 742 | echo "Type ${appname} -h for help" 743 | exit 1 744 | fi 745 | ti_sign+=("${1}") 746 | 747 | # 3/3 Check value 748 | shift 749 | if [ "${1}" = "" ]; then 750 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argyment is empty." 751 | echo "Type ${appname} -h for help" 752 | exit 1 753 | fi 754 | if [ "${ti_sign[${#ti_sign[@]}-1]}" = ">" ] || [ "${ti_sign[${#ti_sign[@]}-1]}" = "<" ]; then 755 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 756 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argument is not a number." 757 | echo "Type ${appname} -h for help" 758 | exit 1 759 | fi 760 | fi 761 | ti_value+=("${1}") 762 | ;; 763 | ### 764 | ### Threshold warning 765 | ### 766 | "-tw${th_chk}") 767 | # 1/3 Check placeholder 768 | shift 769 | if [ "${1}" = "" ]; then 770 | echo "Error, -tw

- no placeholder specified." 771 | echo "Type ${appname} -h for help" 772 | exit 1 773 | fi 774 | f=0 775 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 776 | if [ "${format_nodes[$i]}" = "${1}" ]; then 777 | f=1 778 | break 779 | fi 780 | done 781 | if [ "${f}" = "0" ]; then 782 | echo "Error, -tw '${1}' no such placeholder." 783 | echo "Type ${appname} -h for help" 784 | exit 1 785 | fi 786 | tw_placeholder+=("${format_vars[$i]}") 787 | 788 | # 2/3 Check sign 789 | shift 790 | if [ "${1}" = "" ]; then 791 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 792 | echo "Type ${appname} -h for help" 793 | exit 1 794 | fi 795 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 796 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 797 | echo "Type ${appname} -h for help" 798 | exit 1 799 | fi 800 | tw_sign+=("${1}") 801 | 802 | # 3/3 Check value 803 | shift 804 | if [ "${1}" = "" ]; then 805 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argyment is empty." 806 | echo "Type ${appname} -h for help" 807 | exit 1 808 | fi 809 | if [ "${tw_sign[${#tw_sign[@]}-1]}" = ">" ] || [ "${tw_sign[${#tw_sign[@]}-1]}" = "<" ]; then 810 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 811 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argument is not a number." 812 | echo "Type ${appname} -h for help" 813 | exit 1 814 | fi 815 | fi 816 | tw_value+=("${1}") 817 | ;; 818 | ### 819 | ### Threshold critical 820 | ### 821 | "-tc${th_chk}") 822 | # 1/3 Check placeholder 823 | shift 824 | if [ "${1}" = "" ]; then 825 | echo "Error, -tc

- no placeholder specified." 826 | echo "Type ${appname} -h for help" 827 | exit 1 828 | fi 829 | f=0 830 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 831 | if [ "${format_nodes[$i]}" = "${1}" ]; then 832 | f=1 833 | break 834 | fi 835 | done 836 | if [ "${f}" = "0" ]; then 837 | echo "Error, -tc '${1}' no such placeholder." 838 | echo "Type ${appname} -h for help" 839 | exit 1 840 | fi 841 | tc_placeholder+=("${format_vars[$i]}") 842 | 843 | # 2/3 Check sign 844 | shift 845 | if [ "${1}" = "" ]; then 846 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 847 | echo "Type ${appname} -h for help" 848 | exit 1 849 | fi 850 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 851 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 852 | echo "Type ${appname} -h for help" 853 | exit 1 854 | fi 855 | tc_sign+=("${1}") 856 | 857 | # 3/3 Check value 858 | shift 859 | if [ "${1}" = "" ]; then 860 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argyment is empty." 861 | echo "Type ${appname} -h for help" 862 | exit 1 863 | fi 864 | if [ "${tc_sign[${#tc_sign[@]}-1]}" = ">" ] || [ "${tc_sign[${#tc_sign[@]}-1]}" = "<" ]; then 865 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 866 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argument is not a number." 867 | echo "Type ${appname} -h for help" 868 | exit 1 869 | fi 870 | fi 871 | tc_value+=("${1}") 872 | ;; 873 | ### 874 | ### Format overwrite 875 | ### 876 | -f) 877 | shift 878 | if [ "${1}" = "" ]; then 879 | echo "Error, -f requires a string" 880 | echo "Type ${appname} -h for help" 881 | exit 1 882 | fi 883 | format="${1}" 884 | ;; 885 | ### 886 | ### Disable pango markup output 887 | ### 888 | -np) 889 | pango=0 890 | ;; 891 | ### 892 | ### Color overwrites 893 | ### 894 | -cd) 895 | # default color 896 | shift 897 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 898 | echo "Error, invalid color string: ${1}" 899 | echo "Type ${appname} -h for help" 900 | exit 1 901 | fi 902 | color_def="${1}" 903 | ;; 904 | -cg) 905 | # good color 906 | shift 907 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 908 | echo "Error, invalid color string: ${1}" 909 | echo "Type ${appname} -h for help" 910 | exit 1 911 | fi 912 | color_good="${1}" 913 | ;; 914 | -cw) 915 | # warning color 916 | shift 917 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 918 | echo "Error, invalid color string: ${1}" 919 | echo "Type ${appname} -h for help" 920 | exit 1 921 | fi 922 | color_warn="${1}" 923 | ;; 924 | -cc) 925 | # critical color 926 | shift 927 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 928 | echo "Error, invalid color string: ${1}" 929 | echo "Type ${appname} -h for help" 930 | exit 1 931 | fi 932 | color_crit="${1}" 933 | ;; 934 | -ci) 935 | # info color 936 | shift 937 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 938 | echo "Error, invalid color string: ${1}" 939 | echo "Type ${appname} -h for help" 940 | exit 1 941 | fi 942 | color_info="${1}" 943 | ;; 944 | ### 945 | ### System options 946 | ### 947 | -h) 948 | print_usage 949 | exit 0 950 | ;; 951 | -v) 952 | print_version 953 | exit 0 954 | ;; 955 | ### 956 | ### Unknown/Custom option 957 | ### 958 | *) 959 | 960 | ### 961 | ### Evaluate user-specified arguments 962 | ### 963 | found=0 964 | if [ "${#arg_params}" -gt "0" ]; then 965 | for (( i=0; i<${#arg_params[@]}; i++ )); do 966 | if [ "${arg_params[$i]}" = "${1}" ]; then 967 | shift 968 | var_name="${arg_vars[$i]}" 969 | eval "${var_name}=\"${1}\"" 970 | found=1 971 | break; 972 | fi 973 | done 974 | fi 975 | 976 | ### 977 | ### Unknown option 978 | ### 979 | if [ "${found}" = "0" ]; then 980 | echo "Invalid argument: '${1}'" 981 | echo "Type ${appname} -h for help" 982 | exit 1 983 | fi 984 | ;; 985 | esac 986 | shift 987 | done 988 | 989 | 990 | 991 | 992 | ### 993 | ### Call custom function 994 | ### 995 | custom_action 996 | 997 | ### 998 | ### Get final output color (based on custom specs) 999 | ### 1000 | color="$( get_status_color "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1001 | 1002 | ### 1003 | ### Format (colors) 1004 | ### 1005 | format="$( replace_colors "${format}" "${color}" "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1006 | 1007 | ### 1008 | ### Format (custom) 1009 | ### 1010 | format="$( replace_placeholders "${format}" )" 1011 | 1012 | 1013 | ### 1014 | ### Output pango or plain style? 1015 | ### 1016 | if [ "${pango}" = "1" ]; then 1017 | if [ "${format}" != "" ]; then 1018 | echo "${format}" 1019 | fi 1020 | else 1021 | echo "${format}" # Long output 1022 | echo "${format}" # short output 1023 | echo "\\${color}" # color code '\#RRGGBB' 1024 | fi 1025 | 1026 | exit 0 1027 | -------------------------------------------------------------------------------- /modules/backlight: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" 4 | 5 | ################################################################################ 6 | # 7 | # CUSTOM VARIABLES 8 | # 9 | ################################################################################ 10 | 11 | ############################################################ 12 | # Variables 13 | ############################################################ 14 | 15 | ### 16 | ### Custom Defines 17 | ### 18 | appname="backlight" 19 | provider="${BLOCK_INSTANCE:-intel_backlight}" 20 | format="☼ {percent}%" 21 | 22 | 23 | ### 24 | ### Thresholds 25 | ### 26 | 27 | # Enable 28 | has_threshold=1 29 | 30 | # Depending on the conditions in your custom_action() 31 | # you can force the output to be critical or warning 32 | # Set these vars to 1 (in custom_action) 33 | force_crit=0 34 | force_warn=0 35 | force_good=0 36 | 37 | 38 | ### 39 | ### Additional arguments 40 | ### 41 | arg_params=( 42 | "-p" 43 | ) 44 | arg_vars=( 45 | "provider" 46 | ) 47 | arg_desc_val=( 48 | "" 49 | ) 50 | arg_desc_long=( 51 | "Choose the backlight provider under /sys/class/backlight/. This can also be set via i3blocks 'instance=' value. If neither is set, it will default to 'intel_backlight'." 52 | ) 53 | 54 | 55 | ### 56 | ### Format placeholders 57 | ### 58 | 59 | # bash variable names 60 | format_vars=( 61 | percent 62 | ) 63 | 64 | # Format placeholders 65 | format_nodes=( 66 | "{percent}" 67 | ) 68 | 69 | # Format description (for help display) 70 | format_descs=( 71 | "Percent of brightness" 72 | ) 73 | 74 | # Format examples (for help display) 75 | format_examples=( 76 | "-f \" {percent}%\"" 77 | ) 78 | 79 | 80 | ############################################################ 81 | # custom_actio function 82 | ############################################################ 83 | 84 | ### 85 | ### Evaluate disk space 86 | ### 87 | custom_action() { 88 | local path 89 | local max 90 | local cur 91 | 92 | path="/sys/class/backlight" 93 | 94 | if [ ! -d "${path}/${provider}" ]; then 95 | echo "Error, provider '${provider}' does not exist in: ${path}" 96 | exit 1 97 | fi 98 | max="$( cat "${path}/${provider}/max_brightness" )" 99 | cur="$( cat "${path}/${provider}/actual_brightness" )" 100 | percent="$(( 100 * cur / max ))" 101 | } 102 | 103 | 104 | 105 | 106 | ### 107 | ### 108 | ### 109 | ### D O N O T E D I T A F T E R H E R E 110 | ### 111 | ### 112 | ### 113 | 114 | 115 | 116 | ################################################################################ 117 | # 118 | # BUILT-IN VARIABLES 119 | # 120 | ################################################################################ 121 | 122 | ### 123 | ### General default values 124 | ### 125 | color_def="#666666" # gray 126 | color_good="#88b090" # green 127 | color_warn="#ccdc90" # yellow 128 | color_crit="#e89393" # red 129 | color_info="#fce94f" # bright yellow 130 | 131 | ### 132 | ### Extended format arrays 133 | ### 134 | fe_placeholder=() 135 | fe_sign=() 136 | fe_value=() 137 | fe_format=() 138 | 139 | 140 | ### 141 | ### Threshold arrays 142 | ### 143 | tg_placeholder=() 144 | tg_sign=() 145 | tg_value=() 146 | 147 | ti_placeholder=() 148 | ti_sign=() 149 | ti_value=() 150 | 151 | tw_placeholder=() 152 | tw_sign=() 153 | tw_value=() 154 | 155 | tc_placeholder=() 156 | tc_sign=() 157 | tc_value=() 158 | 159 | 160 | ### 161 | ### Use of pango markup? 162 | ### 163 | pango=1 164 | 165 | 166 | ### 167 | ### source configuration file if it exists 168 | ### 169 | if [ -f "${HOME}/.config/i3blocks-modules/conf" ]; then 170 | # shellcheck disable=SC1090 171 | . "${HOME}/.config/i3blocks-modules/conf" 172 | fi 173 | 174 | 175 | ### 176 | ### i3blocks vars info 177 | ### https://vivien.github.io/i3blocks/ 178 | ### 179 | # name= ${BLOCK_NAME} 180 | # instace= ${BLOCK_INSTANCE} 181 | # button= ${BLOCK_BUTTON} 182 | # x-coor ${BLOCK_X} 183 | # y-coor ${BLOCK_Y} 184 | 185 | 186 | ################################################################################ 187 | # 188 | # BUILT-IN FUNCTIONS 189 | # 190 | ################################################################################ 191 | 192 | ### 193 | ### System functions 194 | ### 195 | print_usage() { 196 | custom_args="" 197 | 198 | # program specific arguments 199 | for (( i=0; i<${#arg_params[@]}; i++ )); do 200 | custom_args="${custom_args}[${arg_params[$i]} ${arg_desc_val[$i]}] " 201 | done 202 | 203 | # Show/Hide threshold 204 | if [ "${has_threshold}" = "1" ]; then 205 | custom_args="${custom_args}[-tg|-ti|-tw|-tc

] " 206 | fi 207 | 208 | echo "Usage: ${appname} [-f ] [-fe

] ${custom_args}[-np] [-cd|-cg|-cw|-cc|-ci ]" 209 | echo " ${appname} -h" 210 | echo " ${appname} -v" 211 | echo 212 | 213 | if [ "${#custom_args}" -gt "0" ]; then 214 | echo "Optional variables:" 215 | echo "--------------------------------------------------------------------------------" 216 | 217 | for (( i=0; i<${#arg_params[@]}; i++ )); do 218 | printf " %-13s%s\\n" "${arg_params[$i]} ${arg_desc_val[$i]}" "${arg_desc_long[$i]}" 219 | done 220 | echo 221 | fi 222 | 223 | if [ "${has_threshold}" = "1" ]; then 224 | echo "Optional threshold arguments:" 225 | echo "--------------------------------------------------------------------------------" 226 | echo "You can optionally enable threshold checking against any placeholder value." 227 | echo "This enables the colorizing of the final output depending on any met" 228 | echo "conditions specified." 229 | echo "Default is not to use any threshold" 230 | echo "You can use unlimited number of threshold for each type." 231 | echo 232 | 233 | echo " -tg

Enable threshold for 'good status'" 234 | echo " -ti

Enable threshold for 'info status'" 235 | echo " -tw

Enable threshold for 'warn status'" 236 | echo " -tc

Enable threshold for 'crit status'" 237 | echo 238 | echo " Explanation:" 239 | echo "

is the placeholder value you want to check against." 240 | printf " valid placeholders: " 241 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 242 | printf "%s" "${format_nodes[$i]} " 243 | done 244 | printf "\\n" 245 | echo " Note 1: placeholder values will be converted to integers" 246 | echo " Any decimal places will simply be cut off." 247 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 248 | echo " no placeholder will be converted." 249 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 250 | echo " no placeholder will be converted." 251 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 252 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 253 | echo " you want to check the threshold placeholder against." 254 | echo " The integer number you want to check against the placeholder." 255 | echo " The string you want to check against the placeholder." 256 | echo " You can only use a string when in equal mode '='." 257 | echo " You can also use regex here." 258 | echo 259 | echo " Examples:" 260 | echo " 1. Check if value of ${format_nodes[0]} < 50, then format using the good color" 261 | echo " -tg '${format_nodes[0]}' '<' 50" 262 | echo 263 | echo " 2. Check if value of ${format_nodes[0]} > 90, then format using the warn color" 264 | echo " -tw '${format_nodes[0]}' '>' 90" 265 | echo 266 | echo " 3. Check if value of ${format_nodes[0]} equals the string 'foo', then format using the info color" 267 | echo " -ti '${format_nodes[0]}' '=' 'foo'" 268 | echo 269 | echo " 4. Check if value of ${format_nodes[0]} equals the regex '^[0-9]+\$', then format using the info color" 270 | echo " -ti '${format_nodes[0]}' '=' '^[0-9]+\$'" 271 | echo 272 | fi 273 | 274 | echo "Optional markup (pango):" 275 | echo "--------------------------------------------------------------------------------" 276 | echo " -np Disable pango markup" 277 | echo 278 | 279 | echo "Optional color arguments:" 280 | echo "--------------------------------------------------------------------------------" 281 | echo "If not specified, script default colors are used" 282 | echo "If config file with color codes is present in:" 283 | echo "'${HOME}/.config/i3blocks-modules/conf', these colors will be used." 284 | echo 285 | echo " -cd Default color (hexadecimal color code)" 286 | echo " Default value is: ${color_def}" 287 | echo " -cg Good color (hexadecimal color code)" 288 | echo " Default value is: ${color_good}" 289 | echo " -cw Warning color (hexadecimal color code)" 290 | echo " Default value is: ${color_warn}" 291 | echo " -cc Critical color (hexadecimal color code)" 292 | echo " Default value is: ${color_crit}" 293 | echo " -ci Info color (hexadecimal color code)" 294 | echo " Default value is: ${color_info}" 295 | echo 296 | 297 | echo "Optional Format placeholders:" 298 | echo "--------------------------------------------------------------------------------" 299 | echo " Available color placeholders:" 300 | echo " (Use with pango disabled for custom markup) building" 301 | echo " {color} Current active color depending on thresholds" 302 | echo " {color_def} Default color" 303 | echo " {color_good} Good color" 304 | echo " {color_warn} Warning color" 305 | echo " {color_crit} Critical color" 306 | echo " {color_info} Info color" 307 | echo " Format example:" 308 | echo " -np -f \"Colored text\"" 309 | echo 310 | 311 | echo " Available specific placeholders:" 312 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 313 | printf " %-15s%s\\n" "${format_nodes[$i]}" "${format_descs[$i]}" 314 | done 315 | 316 | echo " Format example:" 317 | for (( i=0; i<${#format_examples[@]}; i++ )); do 318 | printf " %s\\n" "${format_examples[$i]}" 319 | done 320 | echo " Default:" 321 | echo " -f \"${format}\"" 322 | echo 323 | 324 | echo "Optional extended Format output:" 325 | echo "--------------------------------------------------------------------------------" 326 | echo "You can conditionally set your output text depending on the value of any placeholder." 327 | echo "For example, If you have a placeholder {status} that either is 'up' or 'down', you" 328 | echo "can specify different outputs for 'up' and for 'down'." 329 | echo "Usage" 330 | echo " -fe

" 331 | echo 332 | echo " Format example:" 333 | echo " -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}'" 334 | echo " Explanation:" 335 | echo "

is the placeholder value you want to check against." 336 | printf " valid placeholders: " 337 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 338 | printf "%s" "${format_nodes[$i]} " 339 | done 340 | printf "\\n" 341 | echo " Note 1: placeholder values will be converted to integers" 342 | echo " Any decimal places will simply be cut off." 343 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 344 | echo " no placeholder will be converted." 345 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 346 | echo " no placeholder will be converted." 347 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 348 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 349 | echo " you want to check the threshold placeholder against." 350 | echo " The integer number you want to check against the placeholder." 351 | echo " The string you want to check against the placeholder." 352 | echo " You can only use a string when in equal mode '='." 353 | echo " You can also use regex here." 354 | echo " Is the format string that should be displayed under the above condition." 355 | echo " Of course you can also use placeholders here ;-)." 356 | } 357 | 358 | print_version() { 359 | echo "${appname} v1.8 by cytopia" 360 | echo "https://github.com/cytopia/i3blocks-modules" 361 | } 362 | 363 | ### 364 | ### Decide about final output color color 365 | ### 366 | get_status_color() { 367 | local _color_def="${1}" 368 | local _color_good="${2}" 369 | local _color_warn="${3}" 370 | local _color_crit="${4}" 371 | local _color_info="${5}" 372 | 373 | # final color 374 | local _color="${_color_def}" 375 | 376 | local pval 377 | 378 | # has custom critical color? 379 | if [ "${force_crit}" = "1" ]; then 380 | _color="${_color_crit}" 381 | echo "${_color}" 382 | return 383 | fi 384 | # has custom warning color? 385 | if [ "${force_warn}" = "1" ]; then 386 | _color="${_color_warn}" 387 | echo "${_color}" 388 | return 389 | fi 390 | 391 | 392 | # has custom good color? 393 | if [ "${force_good}" = "1" ]; then 394 | _color="${_color_good}" 395 | fi 396 | 397 | # has good color? 398 | for (( i=0; i < ${#tg_placeholder[@]}; i++ )); do 399 | 400 | if [ "${tg_sign[$i]}" = "=" ] || [ "${tg_sign[$i]}" = "!=" ]; then 401 | pval="${!tg_placeholder[$i]}" 402 | else 403 | pval="$( echo "${!tg_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 404 | fi 405 | 406 | if [ "${tg_sign[$i]}" = "<" ]; then 407 | if [ "${pval}" -lt "${tg_value[$i]}" ]; then 408 | _color="${_color_good}" 409 | fi 410 | elif [ "${tg_sign[$i]}" = "=" ]; then 411 | if [[ "${pval}" =~ ${tg_value[$i]} ]]; then 412 | _color="${_color_good}" 413 | fi 414 | elif [ "${tg_sign[$i]}" = "!=" ]; then 415 | if [[ "${pval}" != "${tg_value[$i]}" ]]; then 416 | _color="${_color_good}" 417 | fi 418 | elif [ "${tg_sign[$i]}" = ">" ]; then 419 | if [ "${pval}" -gt "${tg_value[$i]}" ]; then 420 | _color="${_color_good}" 421 | fi 422 | fi 423 | done 424 | # has info color? 425 | for (( i=0; i < ${#ti_placeholder[@]}; i++ )); do 426 | 427 | if [ "${ti_sign[$i]}" = "=" ] || [ "${ti_sign[$i]}" = "!=" ]; then 428 | pval="${!ti_placeholder[$i]}" 429 | else 430 | pval="$( echo "${!ti_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 431 | fi 432 | 433 | if [ "${ti_sign[$i]}" = "<" ]; then 434 | if [ "${pval}" -lt "${ti_value[$i]}" ]; then 435 | _color="${_color_info}" 436 | fi 437 | elif [ "${ti_sign[$i]}" = "=" ]; then 438 | if [[ "${pval}" =~ ${ti_value[$i]} ]]; then 439 | _color="${_color_info}" 440 | fi 441 | elif [ "${ti_sign[$i]}" = "!=" ]; then 442 | if [[ "${pval}" != "${ti_value[$i]}" ]]; then 443 | _color="${_color_info}" 444 | fi 445 | elif [ "${ti_sign[$i]}" = ">" ]; then 446 | if [ "${pval}" -gt "${ti_value[$i]}" ]; then 447 | _color="${_color_info}" 448 | fi 449 | fi 450 | done 451 | # has warning color? 452 | for (( i=0; i < ${#tw_placeholder[@]}; i++ )); do 453 | 454 | if [ "${tw_sign[$i]}" = "=" ] || [ "${tw_sign[$i]}" = "!=" ]; then 455 | pval="${!tw_placeholder[$i]}" 456 | else 457 | pval="$( echo "${!tw_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 458 | fi 459 | 460 | if [ "${tw_sign[$i]}" = "<" ]; then 461 | if [ "${pval}" -lt "${tw_value[$i]}" ]; then 462 | _color="${color_warn}" 463 | fi 464 | elif [ "${tw_sign[$i]}" = "=" ]; then 465 | if [[ "${pval}" =~ ${tw_value[$i]} ]]; then 466 | _color="${_color_warn}" 467 | fi 468 | elif [ "${tw_sign[$i]}" = "!=" ]; then 469 | if [[ "${pval}" != "${tw_value[$i]}" ]]; then 470 | _color="${_color_warn}" 471 | fi 472 | elif [ "${tw_sign[$i]}" = ">" ]; then 473 | if [ "${pval}" -gt "${tw_value[$i]}" ]; then 474 | _color="${_color_warn}" 475 | fi 476 | fi 477 | done 478 | 479 | 480 | # has critical color? 481 | for (( i=0; i < ${#tc_placeholder[@]}; i++ )); do 482 | 483 | if [ "${tc_sign[$i]}" = "=" ] || [ "${tc_sign[$i]}" = "!=" ]; then 484 | pval="${!tc_placeholder[$i]}" 485 | else 486 | pval="$( echo "${!tc_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 487 | fi 488 | 489 | if [ "${tc_sign[$i]}" = "<" ]; then 490 | if [ "${pval}" -lt "${tc_value[$i]}" ]; then 491 | _color="${_color_crit}" 492 | fi 493 | elif [ "${tc_sign[$i]}" = "=" ]; then 494 | if [[ "${pval}" =~ ${tc_value[$i]} ]]; then 495 | _color="${_color_crit}" 496 | fi 497 | elif [ "${tc_sign[$i]}" = "!=" ]; then 498 | if [[ "${pval}" != "${tc_value[$i]}" ]]; then 499 | _color="${_color_crit}" 500 | fi 501 | elif [ "${tc_sign[$i]}" = ">" ]; then 502 | if [ "${pval}" -gt "${tc_value[$i]}" ]; then 503 | _color="${_color_crit}" 504 | fi 505 | fi 506 | done 507 | 508 | 509 | echo "${_color}" 510 | } 511 | 512 | 513 | ### 514 | ### Replace custom stuff in format string 515 | ### 516 | replace_placeholders() { 517 | local _format="${1}" 518 | local _search 519 | local _replace 520 | 521 | # Select format based on extended placeholders 522 | for (( i=0; i < ${#fe_placeholder[@]}; i++ )); do 523 | 524 | if [ "${fe_sign[$i]}" = "=" ] || [ "${fe_sign[$i]}" = "!=" ]; then 525 | pval="${!fe_placeholder[$i]}" 526 | else 527 | pval="$( echo "${!fe_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 528 | fi 529 | 530 | if [ "${fe_sign[$i]}" = "<" ]; then 531 | if [ "${pval}" -lt "${fe_value[$i]}" ]; then 532 | _format="${fe_format[$i]}" 533 | fi 534 | elif [ "${fe_sign[$i]}" = "=" ]; then 535 | if [[ "${pval}" =~ ${fe_value[$i]} ]]; then 536 | _format="${fe_format[$i]}" 537 | fi 538 | elif [ "${fe_sign[$i]}" = "!=" ]; then 539 | if [[ "${pval}" != "${fe_value[$i]}" ]]; then 540 | _format="${fe_format[$i]}" 541 | fi 542 | elif [ "${fe_sign[$i]}" = ">" ]; then 543 | if [ "${pval}" -gt "${fe_value[$i]}" ]; then 544 | _format="${fe_format[$i]}" 545 | fi 546 | fi 547 | done 548 | 549 | 550 | 551 | # Replace placeholders in $format 552 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 553 | _search="${format_nodes[$i]}" 554 | _replace="${!format_vars[$i]}" 555 | _format="${_format/${_search}/${_replace}}" 556 | done 557 | echo "${_format}" 558 | } 559 | 560 | 561 | ### 562 | ### Replace colors in format string 563 | ### 564 | replace_colors() { 565 | local _format="${1}" 566 | local _color="${2}" 567 | local _color_def="${3}" 568 | local _color_good="${4}" 569 | local _color_warn="${5}" 570 | local _color_crit="${6}" 571 | local _color_info="${7}" 572 | 573 | _format="${_format/'{color}'/${_color}}" 574 | _format="${_format/'{color_def}'/${_color_def}}" 575 | _format="${_format/'{color_good}'/${_color_good}}" 576 | _format="${_format/'{color_warn}'/${_color_warn}}" 577 | _format="${_format/'{color_crit}'/${_color_crit}}" 578 | _format="${_format/'{color_info}'/${_color_info}}" 579 | 580 | echo "${_format}" 581 | } 582 | 583 | 584 | 585 | ################################################################################ 586 | # 587 | # MAIN ENTRY POINT 588 | # 589 | ################################################################################ 590 | 591 | # Enable/Disable threshold argument 592 | if [ "${has_threshold}" = "1" ]; then 593 | th_chk="" 594 | else 595 | th_chk="__THRESHOLD_DISABLED__" 596 | fi 597 | 598 | 599 | while [ $# -gt 0 ]; do 600 | case "$1" in 601 | ### 602 | ### Extended formats 603 | ### 604 | -fe) 605 | # 1/4 Check placeholder 606 | shift 607 | if [ "${1}" = "" ]; then 608 | echo "Error, -fe

- no placeholder specified." 609 | echo "Type ${appname} -h for help" 610 | exit 1 611 | fi 612 | f=0 613 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 614 | if [ "${format_nodes[$i]}" = "${1}" ]; then 615 | f=1 616 | break 617 | fi 618 | done 619 | if [ "${f}" = "0" ]; then 620 | echo "Error, -fe '${1}' no such placeholder." 621 | echo "Type ${appname} -h for help" 622 | exit 1 623 | fi 624 | fe_placeholder+=("${format_vars[$i]}") 625 | 626 | # 2/4 Check sign 627 | shift 628 | if [ "${1}" = "" ]; then 629 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - sign argyment is empty." 630 | echo "Type ${appname} -h for help" 631 | exit 1 632 | fi 633 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 634 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 635 | echo "Type ${appname} -h for help" 636 | exit 1 637 | fi 638 | fe_sign+=("${1}") 639 | 640 | # 3/4 Check value 641 | shift 642 | if [ "${1}" = "" ]; then 643 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is empty." 644 | echo "Type ${appname} -h for help" 645 | exit 1 646 | fi 647 | if [ "${fe_sign[${#fe_sign[@]}-1]}" = ">" ] || [ "${fe_sign[${#fe_sign[@]}-1]}" = "<" ]; then 648 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 649 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is not a number." 650 | echo "Type ${appname} -h for help" 651 | exit 1 652 | fi 653 | fi 654 | fe_value+=("${1}") 655 | 656 | # 4/4 Check placeholder string 657 | shift 658 | fe_format+=("${1}") 659 | ;; 660 | ### 661 | ### Threshold good 662 | ### 663 | "-tg${th_chk}") 664 | # 1/3 Check placeholder 665 | shift 666 | if [ "${1}" = "" ]; then 667 | echo "Error, -tg

- no placeholder specified." 668 | echo "Type ${appname} -h for help" 669 | exit 1 670 | fi 671 | f=0 672 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 673 | if [ "${format_nodes[$i]}" = "${1}" ]; then 674 | f=1 675 | break 676 | fi 677 | done 678 | if [ "${f}" = "0" ]; then 679 | echo "Error, -tg '${1}' no such placeholder." 680 | echo "Type ${appname} -h for help" 681 | exit 1 682 | fi 683 | tg_placeholder+=("${format_vars[$i]}") 684 | 685 | # 2/3 Check sign 686 | shift 687 | if [ "${1}" = "" ]; then 688 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 689 | echo "Type ${appname} -h for help" 690 | exit 1 691 | fi 692 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 693 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 694 | echo "Type ${appname} -h for help" 695 | exit 1 696 | fi 697 | tg_sign+=("${1}") 698 | 699 | # 3/3 Check value 700 | shift 701 | if [ "${1}" = "" ]; then 702 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argyment is empty." 703 | echo "Type ${appname} -h for help" 704 | exit 1 705 | fi 706 | if [ "${tg_sign[${#tg_sign[@]}-1]}" = ">" ] || [ "${tg_sign[${#tg_sign[@]}-1]}" = "<" ]; then 707 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 708 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argument is not a number." 709 | echo "Type ${appname} -h for help" 710 | exit 1 711 | fi 712 | fi 713 | tg_value+=("${1}") 714 | ;; 715 | ### 716 | ### Threshold info 717 | ### 718 | "-ti${th_chk}") 719 | # 1/3 Check placeholder 720 | shift 721 | if [ "${1}" = "" ]; then 722 | echo "Error, -ti

- no placeholder specified." 723 | echo "Type ${appname} -h for help" 724 | exit 1 725 | fi 726 | f=0 727 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 728 | if [ "${format_nodes[$i]}" = "${1}" ]; then 729 | f=1 730 | break 731 | fi 732 | done 733 | if [ "${f}" = "0" ]; then 734 | echo "Error, -ti '${1}' no such placeholder." 735 | echo "Type ${appname} -h for help" 736 | exit 1 737 | fi 738 | ti_placeholder+=("${format_vars[$i]}") 739 | 740 | # 2/3 Check sign 741 | shift 742 | if [ "${1}" = "" ]; then 743 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 744 | echo "Type ${appname} -h for help" 745 | exit 1 746 | fi 747 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 748 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 749 | echo "Type ${appname} -h for help" 750 | exit 1 751 | fi 752 | ti_sign+=("${1}") 753 | 754 | # 3/3 Check value 755 | shift 756 | if [ "${1}" = "" ]; then 757 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argyment is empty." 758 | echo "Type ${appname} -h for help" 759 | exit 1 760 | fi 761 | if [ "${ti_sign[${#ti_sign[@]}-1]}" = ">" ] || [ "${ti_sign[${#ti_sign[@]}-1]}" = "<" ]; then 762 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 763 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argument is not a number." 764 | echo "Type ${appname} -h for help" 765 | exit 1 766 | fi 767 | fi 768 | ti_value+=("${1}") 769 | ;; 770 | ### 771 | ### Threshold warning 772 | ### 773 | "-tw${th_chk}") 774 | # 1/3 Check placeholder 775 | shift 776 | if [ "${1}" = "" ]; then 777 | echo "Error, -tw

- no placeholder specified." 778 | echo "Type ${appname} -h for help" 779 | exit 1 780 | fi 781 | f=0 782 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 783 | if [ "${format_nodes[$i]}" = "${1}" ]; then 784 | f=1 785 | break 786 | fi 787 | done 788 | if [ "${f}" = "0" ]; then 789 | echo "Error, -tw '${1}' no such placeholder." 790 | echo "Type ${appname} -h for help" 791 | exit 1 792 | fi 793 | tw_placeholder+=("${format_vars[$i]}") 794 | 795 | # 2/3 Check sign 796 | shift 797 | if [ "${1}" = "" ]; then 798 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 799 | echo "Type ${appname} -h for help" 800 | exit 1 801 | fi 802 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 803 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 804 | echo "Type ${appname} -h for help" 805 | exit 1 806 | fi 807 | tw_sign+=("${1}") 808 | 809 | # 3/3 Check value 810 | shift 811 | if [ "${1}" = "" ]; then 812 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argyment is empty." 813 | echo "Type ${appname} -h for help" 814 | exit 1 815 | fi 816 | if [ "${tw_sign[${#tw_sign[@]}-1]}" = ">" ] || [ "${tw_sign[${#tw_sign[@]}-1]}" = "<" ]; then 817 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 818 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argument is not a number." 819 | echo "Type ${appname} -h for help" 820 | exit 1 821 | fi 822 | fi 823 | tw_value+=("${1}") 824 | ;; 825 | ### 826 | ### Threshold critical 827 | ### 828 | "-tc${th_chk}") 829 | # 1/3 Check placeholder 830 | shift 831 | if [ "${1}" = "" ]; then 832 | echo "Error, -tc

- no placeholder specified." 833 | echo "Type ${appname} -h for help" 834 | exit 1 835 | fi 836 | f=0 837 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 838 | if [ "${format_nodes[$i]}" = "${1}" ]; then 839 | f=1 840 | break 841 | fi 842 | done 843 | if [ "${f}" = "0" ]; then 844 | echo "Error, -tc '${1}' no such placeholder." 845 | echo "Type ${appname} -h for help" 846 | exit 1 847 | fi 848 | tc_placeholder+=("${format_vars[$i]}") 849 | 850 | # 2/3 Check sign 851 | shift 852 | if [ "${1}" = "" ]; then 853 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 854 | echo "Type ${appname} -h for help" 855 | exit 1 856 | fi 857 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 858 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 859 | echo "Type ${appname} -h for help" 860 | exit 1 861 | fi 862 | tc_sign+=("${1}") 863 | 864 | # 3/3 Check value 865 | shift 866 | if [ "${1}" = "" ]; then 867 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argyment is empty." 868 | echo "Type ${appname} -h for help" 869 | exit 1 870 | fi 871 | if [ "${tc_sign[${#tc_sign[@]}-1]}" = ">" ] || [ "${tc_sign[${#tc_sign[@]}-1]}" = "<" ]; then 872 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 873 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argument is not a number." 874 | echo "Type ${appname} -h for help" 875 | exit 1 876 | fi 877 | fi 878 | tc_value+=("${1}") 879 | ;; 880 | ### 881 | ### Format overwrite 882 | ### 883 | -f) 884 | shift 885 | if [ "${1}" = "" ]; then 886 | echo "Error, -f requires a string" 887 | echo "Type ${appname} -h for help" 888 | exit 1 889 | fi 890 | format="${1}" 891 | ;; 892 | ### 893 | ### Disable pango markup output 894 | ### 895 | -np) 896 | pango=0 897 | ;; 898 | ### 899 | ### Color overwrites 900 | ### 901 | -cd) 902 | # default color 903 | shift 904 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 905 | echo "Error, invalid color string: ${1}" 906 | echo "Type ${appname} -h for help" 907 | exit 1 908 | fi 909 | color_def="${1}" 910 | ;; 911 | -cg) 912 | # good color 913 | shift 914 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 915 | echo "Error, invalid color string: ${1}" 916 | echo "Type ${appname} -h for help" 917 | exit 1 918 | fi 919 | color_good="${1}" 920 | ;; 921 | -cw) 922 | # warning color 923 | shift 924 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 925 | echo "Error, invalid color string: ${1}" 926 | echo "Type ${appname} -h for help" 927 | exit 1 928 | fi 929 | color_warn="${1}" 930 | ;; 931 | -cc) 932 | # critical color 933 | shift 934 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 935 | echo "Error, invalid color string: ${1}" 936 | echo "Type ${appname} -h for help" 937 | exit 1 938 | fi 939 | color_crit="${1}" 940 | ;; 941 | -ci) 942 | # info color 943 | shift 944 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 945 | echo "Error, invalid color string: ${1}" 946 | echo "Type ${appname} -h for help" 947 | exit 1 948 | fi 949 | color_info="${1}" 950 | ;; 951 | ### 952 | ### System options 953 | ### 954 | -h) 955 | print_usage 956 | exit 0 957 | ;; 958 | -v) 959 | print_version 960 | exit 0 961 | ;; 962 | ### 963 | ### Unknown/Custom option 964 | ### 965 | *) 966 | 967 | ### 968 | ### Evaluate user-specified arguments 969 | ### 970 | found=0 971 | if [ "${#arg_params}" -gt "0" ]; then 972 | for (( i=0; i<${#arg_params[@]}; i++ )); do 973 | if [ "${arg_params[$i]}" = "${1}" ]; then 974 | shift 975 | var_name="${arg_vars[$i]}" 976 | eval "${var_name}=\"${1}\"" 977 | found=1 978 | break; 979 | fi 980 | done 981 | fi 982 | 983 | ### 984 | ### Unknown option 985 | ### 986 | if [ "${found}" = "0" ]; then 987 | echo "Invalid argument: '${1}'" 988 | echo "Type ${appname} -h for help" 989 | exit 1 990 | fi 991 | ;; 992 | esac 993 | shift 994 | done 995 | 996 | 997 | 998 | 999 | ### 1000 | ### Call custom function 1001 | ### 1002 | custom_action 1003 | 1004 | ### 1005 | ### Get final output color (based on custom specs) 1006 | ### 1007 | color="$( get_status_color "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1008 | 1009 | ### 1010 | ### Format (colors) 1011 | ### 1012 | format="$( replace_colors "${format}" "${color}" "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1013 | 1014 | ### 1015 | ### Format (custom) 1016 | ### 1017 | format="$( replace_placeholders "${format}" )" 1018 | 1019 | 1020 | ### 1021 | ### Output pango or plain style? 1022 | ### 1023 | if [ "${pango}" = "1" ]; then 1024 | if [ "${format}" != "" ]; then 1025 | echo "${format}" 1026 | fi 1027 | else 1028 | echo "${format}" # Long output 1029 | echo "${format}" # short output 1030 | echo "\\${color}" # color code '\#RRGGBB' 1031 | fi 1032 | 1033 | exit 0 1034 | -------------------------------------------------------------------------------- /modules/online: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" 4 | 5 | ################################################################################ 6 | # 7 | # CUSTOM PLUGIN SECTION 8 | # 9 | ################################################################################ 10 | 11 | ############################################################ 12 | # Variables 13 | ############################################################ 14 | 15 | ### 16 | ### Custom Defines 17 | ### 18 | appname="online" 19 | time="${BLOCK_INSTANCE:-%H:%M}" 20 | format=" {status_or_ip}" 21 | 22 | 23 | ### 24 | ### Thresholds 25 | ### 26 | 27 | # Enable 28 | has_threshold=1 29 | 30 | # Depending on the conditions in your custom_action() 31 | # you can force the output to be critical or warning 32 | # Set these vars to 1 (in custom_action) 33 | force_crit=0 34 | force_warn=0 35 | force_good=0 36 | 37 | 38 | ### 39 | ### Additional arguments 40 | ### 41 | arg_params=( 42 | ) 43 | arg_vars=( 44 | ) 45 | arg_desc_val=( 46 | ) 47 | arg_desc_long=( 48 | ) 49 | 50 | 51 | ### 52 | ### Format placeholders 53 | ### 54 | 55 | # bash variable names 56 | format_vars=( 57 | status 58 | status_or_ip 59 | ip 60 | country 61 | city 62 | ) 63 | 64 | # Format placeholders 65 | format_nodes=( 66 | "{status}" 67 | "{status_or_ip}" 68 | "{ip}" 69 | "{country}" 70 | "{city}" 71 | ) 72 | 73 | # Format description (for help display) 74 | format_descs=( 75 | "'online' or 'offline'" 76 | "IP address or 'offline'" 77 | "IP address" 78 | "Country" 79 | "City" 80 | ) 81 | 82 | # Format examples (for help display) 83 | format_examples=( 84 | "-f \" {status_or_ip} ({city})\"" 85 | ) 86 | 87 | 88 | ############################################################ 89 | # custom_actio function 90 | ############################################################ 91 | 92 | ### 93 | ### Evaluate disk space 94 | ### 95 | custom_action() { 96 | if ! command -v wget > /dev/null 2>&1; then 97 | echo "Error, wget is required for thi module." 98 | fi 99 | 100 | local _data 101 | 102 | if ! _data="$( wget -qO- ip-api.com/line/?fields=country,city,query )"; then 103 | ip= 104 | country= 105 | city= 106 | status="offline" 107 | status_or_ip="${status}" 108 | 109 | # Set fail status 110 | force_crit=1 111 | else 112 | ip="$( echo "${_data}" | sed '3q;d' )" 113 | country="$( echo "${_data}" | sed '1q;d' )" 114 | city="$( echo "${_data}" | sed '2q;d' )" 115 | status="online" 116 | status_or_ip="${ip}" 117 | fi 118 | 119 | 120 | } 121 | 122 | 123 | 124 | 125 | ### 126 | ### 127 | ### 128 | ### D O N O T E D I T A F T E R H E R E 129 | ### 130 | ### 131 | ### 132 | 133 | 134 | 135 | ################################################################################ 136 | # 137 | # BUILT-IN VARIABLES 138 | # 139 | ################################################################################ 140 | 141 | ### 142 | ### General default values 143 | ### 144 | color_def="#666666" # gray 145 | color_good="#88b090" # green 146 | color_warn="#ccdc90" # yellow 147 | color_crit="#e89393" # red 148 | color_info="#fce94f" # bright yellow 149 | 150 | ### 151 | ### Extended format arrays 152 | ### 153 | fe_placeholder=() 154 | fe_sign=() 155 | fe_value=() 156 | fe_format=() 157 | 158 | 159 | ### 160 | ### Threshold arrays 161 | ### 162 | tg_placeholder=() 163 | tg_sign=() 164 | tg_value=() 165 | 166 | ti_placeholder=() 167 | ti_sign=() 168 | ti_value=() 169 | 170 | tw_placeholder=() 171 | tw_sign=() 172 | tw_value=() 173 | 174 | tc_placeholder=() 175 | tc_sign=() 176 | tc_value=() 177 | 178 | 179 | ### 180 | ### Use of pango markup? 181 | ### 182 | pango=1 183 | 184 | 185 | ### 186 | ### source configuration file if it exists 187 | ### 188 | if [ -f "${HOME}/.config/i3blocks-modules/conf" ]; then 189 | # shellcheck disable=SC1090 190 | . "${HOME}/.config/i3blocks-modules/conf" 191 | fi 192 | 193 | 194 | ### 195 | ### i3blocks vars info 196 | ### https://vivien.github.io/i3blocks/ 197 | ### 198 | # name= ${BLOCK_NAME} 199 | # instace= ${BLOCK_INSTANCE} 200 | # button= ${BLOCK_BUTTON} 201 | # x-coor ${BLOCK_X} 202 | # y-coor ${BLOCK_Y} 203 | 204 | 205 | ################################################################################ 206 | # 207 | # BUILT-IN FUNCTIONS 208 | # 209 | ################################################################################ 210 | 211 | ### 212 | ### System functions 213 | ### 214 | print_usage() { 215 | custom_args="" 216 | 217 | # program specific arguments 218 | for (( i=0; i<${#arg_params[@]}; i++ )); do 219 | custom_args="${custom_args}[${arg_params[$i]} ${arg_desc_val[$i]}] " 220 | done 221 | 222 | # Show/Hide threshold 223 | if [ "${has_threshold}" = "1" ]; then 224 | custom_args="${custom_args}[-tg|-ti|-tw|-tc

] " 225 | fi 226 | 227 | echo "Usage: ${appname} [-f ] [-fe

] ${custom_args}[-np] [-cd|-cg|-cw|-cc|-ci ]" 228 | echo " ${appname} -h" 229 | echo " ${appname} -v" 230 | echo 231 | 232 | if [ "${#custom_args}" -gt "0" ]; then 233 | echo "Optional variables:" 234 | echo "--------------------------------------------------------------------------------" 235 | 236 | for (( i=0; i<${#arg_params[@]}; i++ )); do 237 | printf " %-13s%s\\n" "${arg_params[$i]} ${arg_desc_val[$i]}" "${arg_desc_long[$i]}" 238 | done 239 | echo 240 | fi 241 | 242 | if [ "${has_threshold}" = "1" ]; then 243 | echo "Optional threshold arguments:" 244 | echo "--------------------------------------------------------------------------------" 245 | echo "You can optionally enable threshold checking against any placeholder value." 246 | echo "This enables the colorizing of the final output depending on any met" 247 | echo "conditions specified." 248 | echo "Default is not to use any threshold" 249 | echo "You can use unlimited number of threshold for each type." 250 | echo 251 | 252 | echo " -tg

Enable threshold for 'good status'" 253 | echo " -ti

Enable threshold for 'info status'" 254 | echo " -tw

Enable threshold for 'warn status'" 255 | echo " -tc

Enable threshold for 'crit status'" 256 | echo 257 | echo " Explanation:" 258 | echo "

is the placeholder value you want to check against." 259 | printf " valid placeholders: " 260 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 261 | printf "%s" "${format_nodes[$i]} " 262 | done 263 | printf "\\n" 264 | echo " Note 1: placeholder values will be converted to integers" 265 | echo " Any decimal places will simply be cut off." 266 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 267 | echo " no placeholder will be converted." 268 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 269 | echo " no placeholder will be converted." 270 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 271 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 272 | echo " you want to check the threshold placeholder against." 273 | echo " The integer number you want to check against the placeholder." 274 | echo " The string you want to check against the placeholder." 275 | echo " You can only use a string when in equal mode '='." 276 | echo " You can also use regex here." 277 | echo 278 | echo " Examples:" 279 | echo " 1. Check if value of ${format_nodes[0]} < 50, then format using the good color" 280 | echo " -tg '${format_nodes[0]}' '<' 50" 281 | echo 282 | echo " 2. Check if value of ${format_nodes[0]} > 90, then format using the warn color" 283 | echo " -tw '${format_nodes[0]}' '>' 90" 284 | echo 285 | echo " 3. Check if value of ${format_nodes[0]} equals the string 'foo', then format using the info color" 286 | echo " -ti '${format_nodes[0]}' '=' 'foo'" 287 | echo 288 | echo " 4. Check if value of ${format_nodes[0]} equals the regex '^[0-9]+\$', then format using the info color" 289 | echo " -ti '${format_nodes[0]}' '=' '^[0-9]+\$'" 290 | echo 291 | fi 292 | 293 | echo "Optional markup (pango):" 294 | echo "--------------------------------------------------------------------------------" 295 | echo " -np Disable pango markup" 296 | echo 297 | 298 | echo "Optional color arguments:" 299 | echo "--------------------------------------------------------------------------------" 300 | echo "If not specified, script default colors are used" 301 | echo "If config file with color codes is present in:" 302 | echo "'${HOME}/.config/i3blocks-modules/conf', these colors will be used." 303 | echo 304 | echo " -cd Default color (hexadecimal color code)" 305 | echo " Default value is: ${color_def}" 306 | echo " -cg Good color (hexadecimal color code)" 307 | echo " Default value is: ${color_good}" 308 | echo " -cw Warning color (hexadecimal color code)" 309 | echo " Default value is: ${color_warn}" 310 | echo " -cc Critical color (hexadecimal color code)" 311 | echo " Default value is: ${color_crit}" 312 | echo " -ci Info color (hexadecimal color code)" 313 | echo " Default value is: ${color_info}" 314 | echo 315 | 316 | echo "Optional Format placeholders:" 317 | echo "--------------------------------------------------------------------------------" 318 | echo " Available color placeholders:" 319 | echo " (Use with pango disabled for custom markup) building" 320 | echo " {color} Current active color depending on thresholds" 321 | echo " {color_def} Default color" 322 | echo " {color_good} Good color" 323 | echo " {color_warn} Warning color" 324 | echo " {color_crit} Critical color" 325 | echo " {color_info} Info color" 326 | echo " Format example:" 327 | echo " -np -f \"Colored text\"" 328 | echo 329 | 330 | echo " Available specific placeholders:" 331 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 332 | printf " %-15s%s\\n" "${format_nodes[$i]}" "${format_descs[$i]}" 333 | done 334 | 335 | echo " Format example:" 336 | for (( i=0; i<${#format_examples[@]}; i++ )); do 337 | printf " %s\\n" "${format_examples[$i]}" 338 | done 339 | echo " Default:" 340 | echo " -f \"${format}\"" 341 | echo 342 | 343 | echo "Optional extended Format output:" 344 | echo "--------------------------------------------------------------------------------" 345 | echo "You can conditionally set your output text depending on the value of any placeholder." 346 | echo "For example, If you have a placeholder {status} that either is 'up' or 'down', you" 347 | echo "can specify different outputs for 'up' and for 'down'." 348 | echo "Usage" 349 | echo " -fe

" 350 | echo 351 | echo " Format example:" 352 | echo " -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}'" 353 | echo " Explanation:" 354 | echo "

is the placeholder value you want to check against." 355 | printf " valid placeholders: " 356 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 357 | printf "%s" "${format_nodes[$i]} " 358 | done 359 | printf "\\n" 360 | echo " Note 1: placeholder values will be converted to integers" 361 | echo " Any decimal places will simply be cut off." 362 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 363 | echo " no placeholder will be converted." 364 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 365 | echo " no placeholder will be converted." 366 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 367 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 368 | echo " you want to check the threshold placeholder against." 369 | echo " The integer number you want to check against the placeholder." 370 | echo " The string you want to check against the placeholder." 371 | echo " You can only use a string when in equal mode '='." 372 | echo " You can also use regex here." 373 | echo " Is the format string that should be displayed under the above condition." 374 | echo " Of course you can also use placeholders here ;-)." 375 | } 376 | 377 | print_version() { 378 | echo "${appname} v1.8 by cytopia" 379 | echo "https://github.com/cytopia/i3blocks-modules" 380 | } 381 | 382 | ### 383 | ### Decide about final output color color 384 | ### 385 | get_status_color() { 386 | local _color_def="${1}" 387 | local _color_good="${2}" 388 | local _color_warn="${3}" 389 | local _color_crit="${4}" 390 | local _color_info="${5}" 391 | 392 | # final color 393 | local _color="${_color_def}" 394 | 395 | local pval 396 | 397 | # has custom critical color? 398 | if [ "${force_crit}" = "1" ]; then 399 | _color="${_color_crit}" 400 | echo "${_color}" 401 | return 402 | fi 403 | # has custom warning color? 404 | if [ "${force_warn}" = "1" ]; then 405 | _color="${_color_warn}" 406 | echo "${_color}" 407 | return 408 | fi 409 | 410 | 411 | # has custom good color? 412 | if [ "${force_good}" = "1" ]; then 413 | _color="${_color_good}" 414 | fi 415 | 416 | # has good color? 417 | for (( i=0; i < ${#tg_placeholder[@]}; i++ )); do 418 | 419 | if [ "${tg_sign[$i]}" = "=" ] || [ "${tg_sign[$i]}" = "!=" ]; then 420 | pval="${!tg_placeholder[$i]}" 421 | else 422 | pval="$( echo "${!tg_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 423 | fi 424 | 425 | if [ "${tg_sign[$i]}" = "<" ]; then 426 | if [ "${pval}" -lt "${tg_value[$i]}" ]; then 427 | _color="${_color_good}" 428 | fi 429 | elif [ "${tg_sign[$i]}" = "=" ]; then 430 | if [[ "${pval}" =~ ${tg_value[$i]} ]]; then 431 | _color="${_color_good}" 432 | fi 433 | elif [ "${tg_sign[$i]}" = "!=" ]; then 434 | if [[ "${pval}" != "${tg_value[$i]}" ]]; then 435 | _color="${_color_good}" 436 | fi 437 | elif [ "${tg_sign[$i]}" = ">" ]; then 438 | if [ "${pval}" -gt "${tg_value[$i]}" ]; then 439 | _color="${_color_good}" 440 | fi 441 | fi 442 | done 443 | # has info color? 444 | for (( i=0; i < ${#ti_placeholder[@]}; i++ )); do 445 | 446 | if [ "${ti_sign[$i]}" = "=" ] || [ "${ti_sign[$i]}" = "!=" ]; then 447 | pval="${!ti_placeholder[$i]}" 448 | else 449 | pval="$( echo "${!ti_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 450 | fi 451 | 452 | if [ "${ti_sign[$i]}" = "<" ]; then 453 | if [ "${pval}" -lt "${ti_value[$i]}" ]; then 454 | _color="${_color_info}" 455 | fi 456 | elif [ "${ti_sign[$i]}" = "=" ]; then 457 | if [[ "${pval}" =~ ${ti_value[$i]} ]]; then 458 | _color="${_color_info}" 459 | fi 460 | elif [ "${ti_sign[$i]}" = "!=" ]; then 461 | if [[ "${pval}" != "${ti_value[$i]}" ]]; then 462 | _color="${_color_info}" 463 | fi 464 | elif [ "${ti_sign[$i]}" = ">" ]; then 465 | if [ "${pval}" -gt "${ti_value[$i]}" ]; then 466 | _color="${_color_info}" 467 | fi 468 | fi 469 | done 470 | # has warning color? 471 | for (( i=0; i < ${#tw_placeholder[@]}; i++ )); do 472 | 473 | if [ "${tw_sign[$i]}" = "=" ] || [ "${tw_sign[$i]}" = "!=" ]; then 474 | pval="${!tw_placeholder[$i]}" 475 | else 476 | pval="$( echo "${!tw_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 477 | fi 478 | 479 | if [ "${tw_sign[$i]}" = "<" ]; then 480 | if [ "${pval}" -lt "${tw_value[$i]}" ]; then 481 | _color="${color_warn}" 482 | fi 483 | elif [ "${tw_sign[$i]}" = "=" ]; then 484 | if [[ "${pval}" =~ ${tw_value[$i]} ]]; then 485 | _color="${_color_warn}" 486 | fi 487 | elif [ "${tw_sign[$i]}" = "!=" ]; then 488 | if [[ "${pval}" != "${tw_value[$i]}" ]]; then 489 | _color="${_color_warn}" 490 | fi 491 | elif [ "${tw_sign[$i]}" = ">" ]; then 492 | if [ "${pval}" -gt "${tw_value[$i]}" ]; then 493 | _color="${_color_warn}" 494 | fi 495 | fi 496 | done 497 | 498 | 499 | # has critical color? 500 | for (( i=0; i < ${#tc_placeholder[@]}; i++ )); do 501 | 502 | if [ "${tc_sign[$i]}" = "=" ] || [ "${tc_sign[$i]}" = "!=" ]; then 503 | pval="${!tc_placeholder[$i]}" 504 | else 505 | pval="$( echo "${!tc_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 506 | fi 507 | 508 | if [ "${tc_sign[$i]}" = "<" ]; then 509 | if [ "${pval}" -lt "${tc_value[$i]}" ]; then 510 | _color="${_color_crit}" 511 | fi 512 | elif [ "${tc_sign[$i]}" = "=" ]; then 513 | if [[ "${pval}" =~ ${tc_value[$i]} ]]; then 514 | _color="${_color_crit}" 515 | fi 516 | elif [ "${tc_sign[$i]}" = "!=" ]; then 517 | if [[ "${pval}" != "${tc_value[$i]}" ]]; then 518 | _color="${_color_crit}" 519 | fi 520 | elif [ "${tc_sign[$i]}" = ">" ]; then 521 | if [ "${pval}" -gt "${tc_value[$i]}" ]; then 522 | _color="${_color_crit}" 523 | fi 524 | fi 525 | done 526 | 527 | 528 | echo "${_color}" 529 | } 530 | 531 | 532 | ### 533 | ### Replace custom stuff in format string 534 | ### 535 | replace_placeholders() { 536 | local _format="${1}" 537 | local _search 538 | local _replace 539 | 540 | # Select format based on extended placeholders 541 | for (( i=0; i < ${#fe_placeholder[@]}; i++ )); do 542 | 543 | if [ "${fe_sign[$i]}" = "=" ] || [ "${fe_sign[$i]}" = "!=" ]; then 544 | pval="${!fe_placeholder[$i]}" 545 | else 546 | pval="$( echo "${!fe_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 547 | fi 548 | 549 | if [ "${fe_sign[$i]}" = "<" ]; then 550 | if [ "${pval}" -lt "${fe_value[$i]}" ]; then 551 | _format="${fe_format[$i]}" 552 | fi 553 | elif [ "${fe_sign[$i]}" = "=" ]; then 554 | if [[ "${pval}" =~ ${fe_value[$i]} ]]; then 555 | _format="${fe_format[$i]}" 556 | fi 557 | elif [ "${fe_sign[$i]}" = "!=" ]; then 558 | if [[ "${pval}" != "${fe_value[$i]}" ]]; then 559 | _format="${fe_format[$i]}" 560 | fi 561 | elif [ "${fe_sign[$i]}" = ">" ]; then 562 | if [ "${pval}" -gt "${fe_value[$i]}" ]; then 563 | _format="${fe_format[$i]}" 564 | fi 565 | fi 566 | done 567 | 568 | 569 | 570 | # Replace placeholders in $format 571 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 572 | _search="${format_nodes[$i]}" 573 | _replace="${!format_vars[$i]}" 574 | _format="${_format/${_search}/${_replace}}" 575 | done 576 | echo "${_format}" 577 | } 578 | 579 | 580 | ### 581 | ### Replace colors in format string 582 | ### 583 | replace_colors() { 584 | local _format="${1}" 585 | local _color="${2}" 586 | local _color_def="${3}" 587 | local _color_good="${4}" 588 | local _color_warn="${5}" 589 | local _color_crit="${6}" 590 | local _color_info="${7}" 591 | 592 | _format="${_format/'{color}'/${_color}}" 593 | _format="${_format/'{color_def}'/${_color_def}}" 594 | _format="${_format/'{color_good}'/${_color_good}}" 595 | _format="${_format/'{color_warn}'/${_color_warn}}" 596 | _format="${_format/'{color_crit}'/${_color_crit}}" 597 | _format="${_format/'{color_info}'/${_color_info}}" 598 | 599 | echo "${_format}" 600 | } 601 | 602 | 603 | 604 | ################################################################################ 605 | # 606 | # MAIN ENTRY POINT 607 | # 608 | ################################################################################ 609 | 610 | # Enable/Disable threshold argument 611 | if [ "${has_threshold}" = "1" ]; then 612 | th_chk="" 613 | else 614 | th_chk="__THRESHOLD_DISABLED__" 615 | fi 616 | 617 | 618 | while [ $# -gt 0 ]; do 619 | case "$1" in 620 | ### 621 | ### Extended formats 622 | ### 623 | -fe) 624 | # 1/4 Check placeholder 625 | shift 626 | if [ "${1}" = "" ]; then 627 | echo "Error, -fe

- no placeholder specified." 628 | echo "Type ${appname} -h for help" 629 | exit 1 630 | fi 631 | f=0 632 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 633 | if [ "${format_nodes[$i]}" = "${1}" ]; then 634 | f=1 635 | break 636 | fi 637 | done 638 | if [ "${f}" = "0" ]; then 639 | echo "Error, -fe '${1}' no such placeholder." 640 | echo "Type ${appname} -h for help" 641 | exit 1 642 | fi 643 | fe_placeholder+=("${format_vars[$i]}") 644 | 645 | # 2/4 Check sign 646 | shift 647 | if [ "${1}" = "" ]; then 648 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - sign argyment is empty." 649 | echo "Type ${appname} -h for help" 650 | exit 1 651 | fi 652 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 653 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 654 | echo "Type ${appname} -h for help" 655 | exit 1 656 | fi 657 | fe_sign+=("${1}") 658 | 659 | # 3/4 Check value 660 | shift 661 | if [ "${1}" = "" ]; then 662 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is empty." 663 | echo "Type ${appname} -h for help" 664 | exit 1 665 | fi 666 | if [ "${fe_sign[${#fe_sign[@]}-1]}" = ">" ] || [ "${fe_sign[${#fe_sign[@]}-1]}" = "<" ]; then 667 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 668 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is not a number." 669 | echo "Type ${appname} -h for help" 670 | exit 1 671 | fi 672 | fi 673 | fe_value+=("${1}") 674 | 675 | # 4/4 Check placeholder string 676 | shift 677 | fe_format+=("${1}") 678 | ;; 679 | ### 680 | ### Threshold good 681 | ### 682 | "-tg${th_chk}") 683 | # 1/3 Check placeholder 684 | shift 685 | if [ "${1}" = "" ]; then 686 | echo "Error, -tg

- no placeholder specified." 687 | echo "Type ${appname} -h for help" 688 | exit 1 689 | fi 690 | f=0 691 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 692 | if [ "${format_nodes[$i]}" = "${1}" ]; then 693 | f=1 694 | break 695 | fi 696 | done 697 | if [ "${f}" = "0" ]; then 698 | echo "Error, -tg '${1}' no such placeholder." 699 | echo "Type ${appname} -h for help" 700 | exit 1 701 | fi 702 | tg_placeholder+=("${format_vars[$i]}") 703 | 704 | # 2/3 Check sign 705 | shift 706 | if [ "${1}" = "" ]; then 707 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 708 | echo "Type ${appname} -h for help" 709 | exit 1 710 | fi 711 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 712 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 713 | echo "Type ${appname} -h for help" 714 | exit 1 715 | fi 716 | tg_sign+=("${1}") 717 | 718 | # 3/3 Check value 719 | shift 720 | if [ "${1}" = "" ]; then 721 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argyment is empty." 722 | echo "Type ${appname} -h for help" 723 | exit 1 724 | fi 725 | if [ "${tg_sign[${#tg_sign[@]}-1]}" = ">" ] || [ "${tg_sign[${#tg_sign[@]}-1]}" = "<" ]; then 726 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 727 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argument is not a number." 728 | echo "Type ${appname} -h for help" 729 | exit 1 730 | fi 731 | fi 732 | tg_value+=("${1}") 733 | ;; 734 | ### 735 | ### Threshold info 736 | ### 737 | "-ti${th_chk}") 738 | # 1/3 Check placeholder 739 | shift 740 | if [ "${1}" = "" ]; then 741 | echo "Error, -ti

- no placeholder specified." 742 | echo "Type ${appname} -h for help" 743 | exit 1 744 | fi 745 | f=0 746 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 747 | if [ "${format_nodes[$i]}" = "${1}" ]; then 748 | f=1 749 | break 750 | fi 751 | done 752 | if [ "${f}" = "0" ]; then 753 | echo "Error, -ti '${1}' no such placeholder." 754 | echo "Type ${appname} -h for help" 755 | exit 1 756 | fi 757 | ti_placeholder+=("${format_vars[$i]}") 758 | 759 | # 2/3 Check sign 760 | shift 761 | if [ "${1}" = "" ]; then 762 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 763 | echo "Type ${appname} -h for help" 764 | exit 1 765 | fi 766 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 767 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 768 | echo "Type ${appname} -h for help" 769 | exit 1 770 | fi 771 | ti_sign+=("${1}") 772 | 773 | # 3/3 Check value 774 | shift 775 | if [ "${1}" = "" ]; then 776 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argyment is empty." 777 | echo "Type ${appname} -h for help" 778 | exit 1 779 | fi 780 | if [ "${ti_sign[${#ti_sign[@]}-1]}" = ">" ] || [ "${ti_sign[${#ti_sign[@]}-1]}" = "<" ]; then 781 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 782 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argument is not a number." 783 | echo "Type ${appname} -h for help" 784 | exit 1 785 | fi 786 | fi 787 | ti_value+=("${1}") 788 | ;; 789 | ### 790 | ### Threshold warning 791 | ### 792 | "-tw${th_chk}") 793 | # 1/3 Check placeholder 794 | shift 795 | if [ "${1}" = "" ]; then 796 | echo "Error, -tw

- no placeholder specified." 797 | echo "Type ${appname} -h for help" 798 | exit 1 799 | fi 800 | f=0 801 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 802 | if [ "${format_nodes[$i]}" = "${1}" ]; then 803 | f=1 804 | break 805 | fi 806 | done 807 | if [ "${f}" = "0" ]; then 808 | echo "Error, -tw '${1}' no such placeholder." 809 | echo "Type ${appname} -h for help" 810 | exit 1 811 | fi 812 | tw_placeholder+=("${format_vars[$i]}") 813 | 814 | # 2/3 Check sign 815 | shift 816 | if [ "${1}" = "" ]; then 817 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 818 | echo "Type ${appname} -h for help" 819 | exit 1 820 | fi 821 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 822 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 823 | echo "Type ${appname} -h for help" 824 | exit 1 825 | fi 826 | tw_sign+=("${1}") 827 | 828 | # 3/3 Check value 829 | shift 830 | if [ "${1}" = "" ]; then 831 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argyment is empty." 832 | echo "Type ${appname} -h for help" 833 | exit 1 834 | fi 835 | if [ "${tw_sign[${#tw_sign[@]}-1]}" = ">" ] || [ "${tw_sign[${#tw_sign[@]}-1]}" = "<" ]; then 836 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 837 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argument is not a number." 838 | echo "Type ${appname} -h for help" 839 | exit 1 840 | fi 841 | fi 842 | tw_value+=("${1}") 843 | ;; 844 | ### 845 | ### Threshold critical 846 | ### 847 | "-tc${th_chk}") 848 | # 1/3 Check placeholder 849 | shift 850 | if [ "${1}" = "" ]; then 851 | echo "Error, -tc

- no placeholder specified." 852 | echo "Type ${appname} -h for help" 853 | exit 1 854 | fi 855 | f=0 856 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 857 | if [ "${format_nodes[$i]}" = "${1}" ]; then 858 | f=1 859 | break 860 | fi 861 | done 862 | if [ "${f}" = "0" ]; then 863 | echo "Error, -tc '${1}' no such placeholder." 864 | echo "Type ${appname} -h for help" 865 | exit 1 866 | fi 867 | tc_placeholder+=("${format_vars[$i]}") 868 | 869 | # 2/3 Check sign 870 | shift 871 | if [ "${1}" = "" ]; then 872 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 873 | echo "Type ${appname} -h for help" 874 | exit 1 875 | fi 876 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 877 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 878 | echo "Type ${appname} -h for help" 879 | exit 1 880 | fi 881 | tc_sign+=("${1}") 882 | 883 | # 3/3 Check value 884 | shift 885 | if [ "${1}" = "" ]; then 886 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argyment is empty." 887 | echo "Type ${appname} -h for help" 888 | exit 1 889 | fi 890 | if [ "${tc_sign[${#tc_sign[@]}-1]}" = ">" ] || [ "${tc_sign[${#tc_sign[@]}-1]}" = "<" ]; then 891 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 892 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argument is not a number." 893 | echo "Type ${appname} -h for help" 894 | exit 1 895 | fi 896 | fi 897 | tc_value+=("${1}") 898 | ;; 899 | ### 900 | ### Format overwrite 901 | ### 902 | -f) 903 | shift 904 | if [ "${1}" = "" ]; then 905 | echo "Error, -f requires a string" 906 | echo "Type ${appname} -h for help" 907 | exit 1 908 | fi 909 | format="${1}" 910 | ;; 911 | ### 912 | ### Disable pango markup output 913 | ### 914 | -np) 915 | pango=0 916 | ;; 917 | ### 918 | ### Color overwrites 919 | ### 920 | -cd) 921 | # default color 922 | shift 923 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 924 | echo "Error, invalid color string: ${1}" 925 | echo "Type ${appname} -h for help" 926 | exit 1 927 | fi 928 | color_def="${1}" 929 | ;; 930 | -cg) 931 | # good color 932 | shift 933 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 934 | echo "Error, invalid color string: ${1}" 935 | echo "Type ${appname} -h for help" 936 | exit 1 937 | fi 938 | color_good="${1}" 939 | ;; 940 | -cw) 941 | # warning color 942 | shift 943 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 944 | echo "Error, invalid color string: ${1}" 945 | echo "Type ${appname} -h for help" 946 | exit 1 947 | fi 948 | color_warn="${1}" 949 | ;; 950 | -cc) 951 | # critical color 952 | shift 953 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 954 | echo "Error, invalid color string: ${1}" 955 | echo "Type ${appname} -h for help" 956 | exit 1 957 | fi 958 | color_crit="${1}" 959 | ;; 960 | -ci) 961 | # info color 962 | shift 963 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 964 | echo "Error, invalid color string: ${1}" 965 | echo "Type ${appname} -h for help" 966 | exit 1 967 | fi 968 | color_info="${1}" 969 | ;; 970 | ### 971 | ### System options 972 | ### 973 | -h) 974 | print_usage 975 | exit 0 976 | ;; 977 | -v) 978 | print_version 979 | exit 0 980 | ;; 981 | ### 982 | ### Unknown/Custom option 983 | ### 984 | *) 985 | 986 | ### 987 | ### Evaluate user-specified arguments 988 | ### 989 | found=0 990 | if [ "${#arg_params}" -gt "0" ]; then 991 | for (( i=0; i<${#arg_params[@]}; i++ )); do 992 | if [ "${arg_params[$i]}" = "${1}" ]; then 993 | shift 994 | var_name="${arg_vars[$i]}" 995 | eval "${var_name}=\"${1}\"" 996 | found=1 997 | break; 998 | fi 999 | done 1000 | fi 1001 | 1002 | ### 1003 | ### Unknown option 1004 | ### 1005 | if [ "${found}" = "0" ]; then 1006 | echo "Invalid argument: '${1}'" 1007 | echo "Type ${appname} -h for help" 1008 | exit 1 1009 | fi 1010 | ;; 1011 | esac 1012 | shift 1013 | done 1014 | 1015 | 1016 | 1017 | 1018 | ### 1019 | ### Call custom function 1020 | ### 1021 | custom_action 1022 | 1023 | ### 1024 | ### Get final output color (based on custom specs) 1025 | ### 1026 | color="$( get_status_color "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1027 | 1028 | ### 1029 | ### Format (colors) 1030 | ### 1031 | format="$( replace_colors "${format}" "${color}" "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1032 | 1033 | ### 1034 | ### Format (custom) 1035 | ### 1036 | format="$( replace_placeholders "${format}" )" 1037 | 1038 | 1039 | ### 1040 | ### Output pango or plain style? 1041 | ### 1042 | if [ "${pango}" = "1" ]; then 1043 | if [ "${format}" != "" ]; then 1044 | echo "${format}" 1045 | fi 1046 | else 1047 | echo "${format}" # Long output 1048 | echo "${format}" # short output 1049 | echo "\\${color}" # color code '\#RRGGBB' 1050 | fi 1051 | 1052 | exit 0 1053 | -------------------------------------------------------------------------------- /modules/memory: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:${PATH}" 4 | 5 | ################################################################################ 6 | # 7 | # CUSTOM PLUGIN SECTION 8 | # 9 | ################################################################################ 10 | 11 | ############################################################ 12 | # Variables 13 | ############################################################ 14 | 15 | ### 16 | ### Custom Defines 17 | ### 18 | appname="memory" 19 | #time="${BLOCK_INSTANCE:-%H:%M}" 20 | format=" {used}{uunit} iB used" 21 | 22 | 23 | ### 24 | ### Thresholds 25 | ### 26 | 27 | # Enable 28 | has_threshold=1 29 | 30 | # Depending on the conditions in your custom_action() 31 | # you can force the output to be critical or warning 32 | # Set these vars to 1 (in custom_action) 33 | force_crit=0 34 | force_warn=0 35 | force_good=0 36 | 37 | 38 | ### 39 | ### Additional arguments 40 | ### 41 | arg_params=( 42 | ) 43 | arg_vars=( 44 | ) 45 | arg_desc_val=( 46 | ) 47 | arg_desc_long=( 48 | ) 49 | 50 | 51 | ### 52 | ### Format placeholders 53 | ### 54 | 55 | # bash variable names 56 | format_vars=( 57 | used 58 | free 59 | total 60 | pused 61 | pfree 62 | uunit 63 | funit 64 | tunit 65 | ) 66 | 67 | # Format placeholders 68 | format_nodes=( 69 | "{used}" 70 | "{free}" 71 | "{total}" 72 | "{pused}" 73 | "{pfree}" 74 | "{uunit}" 75 | "{funit}" 76 | "{tunit}" 77 | ) 78 | 79 | # Format description (for help display) 80 | format_descs=( 81 | "Used memory" 82 | "Free memory" 83 | "Total memory" 84 | "Percent of used memory" 85 | "Percent of free memory" 86 | "Unit of used memory (K, M, G...)" 87 | "Unit of free memory (K, M, G...)" 88 | "Unit of total memory (K, M, G...)" 89 | ) 90 | 91 | # Format examples (for help display) 92 | format_examples=( 93 | "-f \" {used} {punit}\"" 94 | "-f \"RAM: {used} {punit} of {total} {tunit} ({pused}%)\"" 95 | ) 96 | 97 | 98 | ############################################################ 99 | # custom_actio function 100 | ############################################################ 101 | 102 | ### 103 | ### Evaluate disk space 104 | ### 105 | custom_action() { 106 | local _dump 107 | _dump="$( free | grep 'Mem' || true )" 108 | 109 | total="$( echo "${_dump}" | awk '{printf("%.2f\n", $2/(1024*1024))}' )" 110 | free="$( echo "${_dump}" | awk '{printf("%.2f\n", $7/(1024*1024))}' )" 111 | used="$( awk -v total="${total}" -v free="${free}" 'BEGIN {printf("%.2f\n", total-free)}' )" 112 | pused="$( awk -v total="${total}" -v used="${used}" 'BEGIN {printf("%0.f\n", 100*used/total)}' )" 113 | pfree="$( awk -v total="${total}" -v free="${free}" 'BEGIN {printf("%0.f\n", 100*free/total)}' )" 114 | uunit="G" 115 | funit="G" 116 | tunit="G" 117 | } 118 | 119 | 120 | 121 | 122 | ### 123 | ### 124 | ### 125 | ### D O N O T E D I T A F T E R H E R E 126 | ### 127 | ### 128 | ### 129 | 130 | 131 | 132 | ################################################################################ 133 | # 134 | # BUILT-IN VARIABLES 135 | # 136 | ################################################################################ 137 | 138 | ### 139 | ### General default values 140 | ### 141 | color_def="#666666" # gray 142 | color_good="#88b090" # green 143 | color_warn="#ccdc90" # yellow 144 | color_crit="#e89393" # red 145 | color_info="#fce94f" # bright yellow 146 | 147 | ### 148 | ### Extended format arrays 149 | ### 150 | fe_placeholder=() 151 | fe_sign=() 152 | fe_value=() 153 | fe_format=() 154 | 155 | 156 | ### 157 | ### Threshold arrays 158 | ### 159 | tg_placeholder=() 160 | tg_sign=() 161 | tg_value=() 162 | 163 | ti_placeholder=() 164 | ti_sign=() 165 | ti_value=() 166 | 167 | tw_placeholder=() 168 | tw_sign=() 169 | tw_value=() 170 | 171 | tc_placeholder=() 172 | tc_sign=() 173 | tc_value=() 174 | 175 | 176 | ### 177 | ### Use of pango markup? 178 | ### 179 | pango=1 180 | 181 | 182 | ### 183 | ### source configuration file if it exists 184 | ### 185 | if [ -f "${HOME}/.config/i3blocks-modules/conf" ]; then 186 | # shellcheck disable=SC1090 187 | . "${HOME}/.config/i3blocks-modules/conf" 188 | fi 189 | 190 | 191 | ### 192 | ### i3blocks vars info 193 | ### https://vivien.github.io/i3blocks/ 194 | ### 195 | # name= ${BLOCK_NAME} 196 | # instace= ${BLOCK_INSTANCE} 197 | # button= ${BLOCK_BUTTON} 198 | # x-coor ${BLOCK_X} 199 | # y-coor ${BLOCK_Y} 200 | 201 | 202 | ################################################################################ 203 | # 204 | # BUILT-IN FUNCTIONS 205 | # 206 | ################################################################################ 207 | 208 | ### 209 | ### System functions 210 | ### 211 | print_usage() { 212 | custom_args="" 213 | 214 | # program specific arguments 215 | for (( i=0; i<${#arg_params[@]}; i++ )); do 216 | custom_args="${custom_args}[${arg_params[$i]} ${arg_desc_val[$i]}] " 217 | done 218 | 219 | # Show/Hide threshold 220 | if [ "${has_threshold}" = "1" ]; then 221 | custom_args="${custom_args}[-tg|-ti|-tw|-tc

] " 222 | fi 223 | 224 | echo "Usage: ${appname} [-f ] [-fe

] ${custom_args}[-np] [-cd|-cg|-cw|-cc|-ci ]" 225 | echo " ${appname} -h" 226 | echo " ${appname} -v" 227 | echo 228 | 229 | if [ "${#custom_args}" -gt "0" ]; then 230 | echo "Optional variables:" 231 | echo "--------------------------------------------------------------------------------" 232 | 233 | for (( i=0; i<${#arg_params[@]}; i++ )); do 234 | printf " %-13s%s\\n" "${arg_params[$i]} ${arg_desc_val[$i]}" "${arg_desc_long[$i]}" 235 | done 236 | echo 237 | fi 238 | 239 | if [ "${has_threshold}" = "1" ]; then 240 | echo "Optional threshold arguments:" 241 | echo "--------------------------------------------------------------------------------" 242 | echo "You can optionally enable threshold checking against any placeholder value." 243 | echo "This enables the colorizing of the final output depending on any met" 244 | echo "conditions specified." 245 | echo "Default is not to use any threshold" 246 | echo "You can use unlimited number of threshold for each type." 247 | echo 248 | 249 | echo " -tg

Enable threshold for 'good status'" 250 | echo " -ti

Enable threshold for 'info status'" 251 | echo " -tw

Enable threshold for 'warn status'" 252 | echo " -tc

Enable threshold for 'crit status'" 253 | echo 254 | echo " Explanation:" 255 | echo "

is the placeholder value you want to check against." 256 | printf " valid placeholders: " 257 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 258 | printf "%s" "${format_nodes[$i]} " 259 | done 260 | printf "\\n" 261 | echo " Note 1: placeholder values will be converted to integers" 262 | echo " Any decimal places will simply be cut off." 263 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 264 | echo " no placeholder will be converted." 265 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 266 | echo " no placeholder will be converted." 267 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 268 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 269 | echo " you want to check the threshold placeholder against." 270 | echo " The integer number you want to check against the placeholder." 271 | echo " The string you want to check against the placeholder." 272 | echo " You can only use a string when in equal mode '='." 273 | echo " You can also use regex here." 274 | echo 275 | echo " Examples:" 276 | echo " 1. Check if value of ${format_nodes[0]} < 50, then format using the good color" 277 | echo " -tg '${format_nodes[0]}' '<' 50" 278 | echo 279 | echo " 2. Check if value of ${format_nodes[0]} > 90, then format using the warn color" 280 | echo " -tw '${format_nodes[0]}' '>' 90" 281 | echo 282 | echo " 3. Check if value of ${format_nodes[0]} equals the string 'foo', then format using the info color" 283 | echo " -ti '${format_nodes[0]}' '=' 'foo'" 284 | echo 285 | echo " 4. Check if value of ${format_nodes[0]} equals the regex '^[0-9]+\$', then format using the info color" 286 | echo " -ti '${format_nodes[0]}' '=' '^[0-9]+\$'" 287 | echo 288 | fi 289 | 290 | echo "Optional markup (pango):" 291 | echo "--------------------------------------------------------------------------------" 292 | echo " -np Disable pango markup" 293 | echo 294 | 295 | echo "Optional color arguments:" 296 | echo "--------------------------------------------------------------------------------" 297 | echo "If not specified, script default colors are used" 298 | echo "If config file with color codes is present in:" 299 | echo "'${HOME}/.config/i3blocks-modules/conf', these colors will be used." 300 | echo 301 | echo " -cd Default color (hexadecimal color code)" 302 | echo " Default value is: ${color_def}" 303 | echo " -cg Good color (hexadecimal color code)" 304 | echo " Default value is: ${color_good}" 305 | echo " -cw Warning color (hexadecimal color code)" 306 | echo " Default value is: ${color_warn}" 307 | echo " -cc Critical color (hexadecimal color code)" 308 | echo " Default value is: ${color_crit}" 309 | echo " -ci Info color (hexadecimal color code)" 310 | echo " Default value is: ${color_info}" 311 | echo 312 | 313 | echo "Optional Format placeholders:" 314 | echo "--------------------------------------------------------------------------------" 315 | echo " Available color placeholders:" 316 | echo " (Use with pango disabled for custom markup) building" 317 | echo " {color} Current active color depending on thresholds" 318 | echo " {color_def} Default color" 319 | echo " {color_good} Good color" 320 | echo " {color_warn} Warning color" 321 | echo " {color_crit} Critical color" 322 | echo " {color_info} Info color" 323 | echo " Format example:" 324 | echo " -np -f \"Colored text\"" 325 | echo 326 | 327 | echo " Available specific placeholders:" 328 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 329 | printf " %-15s%s\\n" "${format_nodes[$i]}" "${format_descs[$i]}" 330 | done 331 | 332 | echo " Format example:" 333 | for (( i=0; i<${#format_examples[@]}; i++ )); do 334 | printf " %s\\n" "${format_examples[$i]}" 335 | done 336 | echo " Default:" 337 | echo " -f \"${format}\"" 338 | echo 339 | 340 | echo "Optional extended Format output:" 341 | echo "--------------------------------------------------------------------------------" 342 | echo "You can conditionally set your output text depending on the value of any placeholder." 343 | echo "For example, If you have a placeholder {status} that either is 'up' or 'down', you" 344 | echo "can specify different outputs for 'up' and for 'down'." 345 | echo "Usage" 346 | echo " -fe

" 347 | echo 348 | echo " Format example:" 349 | echo " -fe '{status}' '=' 'up' 'It works ;-)' -fe '{status}' '!=' 'up' 'status is: {status}'" 350 | echo " Explanation:" 351 | echo "

is the placeholder value you want to check against." 352 | printf " valid placeholders: " 353 | for (( i=0; i<${#format_nodes[@]}; i++ )); do 354 | printf "%s" "${format_nodes[$i]} " 355 | done 356 | printf "\\n" 357 | echo " Note 1: placeholder values will be converted to integers" 358 | echo " Any decimal places will simply be cut off." 359 | echo " Note 2: In equal mode ( '=') is a string regex comparison and" 360 | echo " no placeholder will be converted." 361 | echo " Note 3: In unequal mode ( '!=') is a string comparison and" 362 | echo " no placeholder will be converted." 363 | echo " Note 3: In equal mode ( '=') regex is allowed :-)" 364 | echo " must either be '<', '>', '=' or '!=' depending on what direction" 365 | echo " you want to check the threshold placeholder against." 366 | echo " The integer number you want to check against the placeholder." 367 | echo " The string you want to check against the placeholder." 368 | echo " You can only use a string when in equal mode '='." 369 | echo " You can also use regex here." 370 | echo " Is the format string that should be displayed under the above condition." 371 | echo " Of course you can also use placeholders here ;-)." 372 | } 373 | 374 | print_version() { 375 | echo "${appname} v1.8 by cytopia" 376 | echo "https://github.com/cytopia/i3blocks-modules" 377 | } 378 | 379 | ### 380 | ### Decide about final output color color 381 | ### 382 | get_status_color() { 383 | local _color_def="${1}" 384 | local _color_good="${2}" 385 | local _color_warn="${3}" 386 | local _color_crit="${4}" 387 | local _color_info="${5}" 388 | 389 | # final color 390 | local _color="${_color_def}" 391 | 392 | local pval 393 | 394 | # has custom critical color? 395 | if [ "${force_crit}" = "1" ]; then 396 | _color="${_color_crit}" 397 | echo "${_color}" 398 | return 399 | fi 400 | # has custom warning color? 401 | if [ "${force_warn}" = "1" ]; then 402 | _color="${_color_warn}" 403 | echo "${_color}" 404 | return 405 | fi 406 | 407 | 408 | # has custom good color? 409 | if [ "${force_good}" = "1" ]; then 410 | _color="${_color_good}" 411 | fi 412 | 413 | # has good color? 414 | for (( i=0; i < ${#tg_placeholder[@]}; i++ )); do 415 | 416 | if [ "${tg_sign[$i]}" = "=" ] || [ "${tg_sign[$i]}" = "!=" ]; then 417 | pval="${!tg_placeholder[$i]}" 418 | else 419 | pval="$( echo "${!tg_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 420 | fi 421 | 422 | if [ "${tg_sign[$i]}" = "<" ]; then 423 | if [ "${pval}" -lt "${tg_value[$i]}" ]; then 424 | _color="${_color_good}" 425 | fi 426 | elif [ "${tg_sign[$i]}" = "=" ]; then 427 | if [[ "${pval}" =~ ${tg_value[$i]} ]]; then 428 | _color="${_color_good}" 429 | fi 430 | elif [ "${tg_sign[$i]}" = "!=" ]; then 431 | if [[ "${pval}" != "${tg_value[$i]}" ]]; then 432 | _color="${_color_good}" 433 | fi 434 | elif [ "${tg_sign[$i]}" = ">" ]; then 435 | if [ "${pval}" -gt "${tg_value[$i]}" ]; then 436 | _color="${_color_good}" 437 | fi 438 | fi 439 | done 440 | # has info color? 441 | for (( i=0; i < ${#ti_placeholder[@]}; i++ )); do 442 | 443 | if [ "${ti_sign[$i]}" = "=" ] || [ "${ti_sign[$i]}" = "!=" ]; then 444 | pval="${!ti_placeholder[$i]}" 445 | else 446 | pval="$( echo "${!ti_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 447 | fi 448 | 449 | if [ "${ti_sign[$i]}" = "<" ]; then 450 | if [ "${pval}" -lt "${ti_value[$i]}" ]; then 451 | _color="${_color_info}" 452 | fi 453 | elif [ "${ti_sign[$i]}" = "=" ]; then 454 | if [[ "${pval}" =~ ${ti_value[$i]} ]]; then 455 | _color="${_color_info}" 456 | fi 457 | elif [ "${ti_sign[$i]}" = "!=" ]; then 458 | if [[ "${pval}" != "${ti_value[$i]}" ]]; then 459 | _color="${_color_info}" 460 | fi 461 | elif [ "${ti_sign[$i]}" = ">" ]; then 462 | if [ "${pval}" -gt "${ti_value[$i]}" ]; then 463 | _color="${_color_info}" 464 | fi 465 | fi 466 | done 467 | # has warning color? 468 | for (( i=0; i < ${#tw_placeholder[@]}; i++ )); do 469 | 470 | if [ "${tw_sign[$i]}" = "=" ] || [ "${tw_sign[$i]}" = "!=" ]; then 471 | pval="${!tw_placeholder[$i]}" 472 | else 473 | pval="$( echo "${!tw_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 474 | fi 475 | 476 | if [ "${tw_sign[$i]}" = "<" ]; then 477 | if [ "${pval}" -lt "${tw_value[$i]}" ]; then 478 | _color="${color_warn}" 479 | fi 480 | elif [ "${tw_sign[$i]}" = "=" ]; then 481 | if [[ "${pval}" =~ ${tw_value[$i]} ]]; then 482 | _color="${_color_warn}" 483 | fi 484 | elif [ "${tw_sign[$i]}" = "!=" ]; then 485 | if [[ "${pval}" != "${tw_value[$i]}" ]]; then 486 | _color="${_color_warn}" 487 | fi 488 | elif [ "${tw_sign[$i]}" = ">" ]; then 489 | if [ "${pval}" -gt "${tw_value[$i]}" ]; then 490 | _color="${_color_warn}" 491 | fi 492 | fi 493 | done 494 | 495 | 496 | # has critical color? 497 | for (( i=0; i < ${#tc_placeholder[@]}; i++ )); do 498 | 499 | if [ "${tc_sign[$i]}" = "=" ] || [ "${tc_sign[$i]}" = "!=" ]; then 500 | pval="${!tc_placeholder[$i]}" 501 | else 502 | pval="$( echo "${!tc_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 503 | fi 504 | 505 | if [ "${tc_sign[$i]}" = "<" ]; then 506 | if [ "${pval}" -lt "${tc_value[$i]}" ]; then 507 | _color="${_color_crit}" 508 | fi 509 | elif [ "${tc_sign[$i]}" = "=" ]; then 510 | if [[ "${pval}" =~ ${tc_value[$i]} ]]; then 511 | _color="${_color_crit}" 512 | fi 513 | elif [ "${tc_sign[$i]}" = "!=" ]; then 514 | if [[ "${pval}" != "${tc_value[$i]}" ]]; then 515 | _color="${_color_crit}" 516 | fi 517 | elif [ "${tc_sign[$i]}" = ">" ]; then 518 | if [ "${pval}" -gt "${tc_value[$i]}" ]; then 519 | _color="${_color_crit}" 520 | fi 521 | fi 522 | done 523 | 524 | 525 | echo "${_color}" 526 | } 527 | 528 | 529 | ### 530 | ### Replace custom stuff in format string 531 | ### 532 | replace_placeholders() { 533 | local _format="${1}" 534 | local _search 535 | local _replace 536 | 537 | # Select format based on extended placeholders 538 | for (( i=0; i < ${#fe_placeholder[@]}; i++ )); do 539 | 540 | if [ "${fe_sign[$i]}" = "=" ] || [ "${fe_sign[$i]}" = "!=" ]; then 541 | pval="${!fe_placeholder[$i]}" 542 | else 543 | pval="$( echo "${!fe_placeholder[$i]}" | grep -oE '[0-9]*' | head -1 )" 544 | fi 545 | 546 | if [ "${fe_sign[$i]}" = "<" ]; then 547 | if [ "${pval}" -lt "${fe_value[$i]}" ]; then 548 | _format="${fe_format[$i]}" 549 | fi 550 | elif [ "${fe_sign[$i]}" = "=" ]; then 551 | if [[ "${pval}" =~ ${fe_value[$i]} ]]; then 552 | _format="${fe_format[$i]}" 553 | fi 554 | elif [ "${fe_sign[$i]}" = "!=" ]; then 555 | if [[ "${pval}" != "${fe_value[$i]}" ]]; then 556 | _format="${fe_format[$i]}" 557 | fi 558 | elif [ "${fe_sign[$i]}" = ">" ]; then 559 | if [ "${pval}" -gt "${fe_value[$i]}" ]; then 560 | _format="${fe_format[$i]}" 561 | fi 562 | fi 563 | done 564 | 565 | 566 | 567 | # Replace placeholders in $format 568 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 569 | _search="${format_nodes[$i]}" 570 | _replace="${!format_vars[$i]}" 571 | _format="${_format/${_search}/${_replace}}" 572 | done 573 | echo "${_format}" 574 | } 575 | 576 | 577 | ### 578 | ### Replace colors in format string 579 | ### 580 | replace_colors() { 581 | local _format="${1}" 582 | local _color="${2}" 583 | local _color_def="${3}" 584 | local _color_good="${4}" 585 | local _color_warn="${5}" 586 | local _color_crit="${6}" 587 | local _color_info="${7}" 588 | 589 | _format="${_format/'{color}'/${_color}}" 590 | _format="${_format/'{color_def}'/${_color_def}}" 591 | _format="${_format/'{color_good}'/${_color_good}}" 592 | _format="${_format/'{color_warn}'/${_color_warn}}" 593 | _format="${_format/'{color_crit}'/${_color_crit}}" 594 | _format="${_format/'{color_info}'/${_color_info}}" 595 | 596 | echo "${_format}" 597 | } 598 | 599 | 600 | 601 | ################################################################################ 602 | # 603 | # MAIN ENTRY POINT 604 | # 605 | ################################################################################ 606 | 607 | # Enable/Disable threshold argument 608 | if [ "${has_threshold}" = "1" ]; then 609 | th_chk="" 610 | else 611 | th_chk="__THRESHOLD_DISABLED__" 612 | fi 613 | 614 | 615 | while [ $# -gt 0 ]; do 616 | case "$1" in 617 | ### 618 | ### Extended formats 619 | ### 620 | -fe) 621 | # 1/4 Check placeholder 622 | shift 623 | if [ "${1}" = "" ]; then 624 | echo "Error, -fe

- no placeholder specified." 625 | echo "Type ${appname} -h for help" 626 | exit 1 627 | fi 628 | f=0 629 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 630 | if [ "${format_nodes[$i]}" = "${1}" ]; then 631 | f=1 632 | break 633 | fi 634 | done 635 | if [ "${f}" = "0" ]; then 636 | echo "Error, -fe '${1}' no such placeholder." 637 | echo "Type ${appname} -h for help" 638 | exit 1 639 | fi 640 | fe_placeholder+=("${format_vars[$i]}") 641 | 642 | # 2/4 Check sign 643 | shift 644 | if [ "${1}" = "" ]; then 645 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - sign argyment is empty." 646 | echo "Type ${appname} -h for help" 647 | exit 1 648 | fi 649 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 650 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 651 | echo "Type ${appname} -h for help" 652 | exit 1 653 | fi 654 | fe_sign+=("${1}") 655 | 656 | # 3/4 Check value 657 | shift 658 | if [ "${1}" = "" ]; then 659 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is empty." 660 | echo "Type ${appname} -h for help" 661 | exit 1 662 | fi 663 | if [ "${fe_sign[${#fe_sign[@]}-1]}" = ">" ] || [ "${fe_sign[${#fe_sign[@]}-1]}" = "<" ]; then 664 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 665 | echo "Error, -fe '{${fe_placeholder[${#fe_placeholder[@]}-1]}}' '${fe_sign[${#fe_sign[@]}-1]}' '${1}' - value argument is not a number." 666 | echo "Type ${appname} -h for help" 667 | exit 1 668 | fi 669 | fi 670 | fe_value+=("${1}") 671 | 672 | # 4/4 Check placeholder string 673 | shift 674 | fe_format+=("${1}") 675 | ;; 676 | ### 677 | ### Threshold good 678 | ### 679 | "-tg${th_chk}") 680 | # 1/3 Check placeholder 681 | shift 682 | if [ "${1}" = "" ]; then 683 | echo "Error, -tg

- no placeholder specified." 684 | echo "Type ${appname} -h for help" 685 | exit 1 686 | fi 687 | f=0 688 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 689 | if [ "${format_nodes[$i]}" = "${1}" ]; then 690 | f=1 691 | break 692 | fi 693 | done 694 | if [ "${f}" = "0" ]; then 695 | echo "Error, -tg '${1}' no such placeholder." 696 | echo "Type ${appname} -h for help" 697 | exit 1 698 | fi 699 | tg_placeholder+=("${format_vars[$i]}") 700 | 701 | # 2/3 Check sign 702 | shift 703 | if [ "${1}" = "" ]; then 704 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 705 | echo "Type ${appname} -h for help" 706 | exit 1 707 | fi 708 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 709 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 710 | echo "Type ${appname} -h for help" 711 | exit 1 712 | fi 713 | tg_sign+=("${1}") 714 | 715 | # 3/3 Check value 716 | shift 717 | if [ "${1}" = "" ]; then 718 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argyment is empty." 719 | echo "Type ${appname} -h for help" 720 | exit 1 721 | fi 722 | if [ "${tg_sign[${#tg_sign[@]}-1]}" = ">" ] || [ "${tg_sign[${#tg_sign[@]}-1]}" = "<" ]; then 723 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 724 | echo "Error, -tg '{${tg_placeholder[${#tg_placeholder[@]}-1]}}' '${tg_sign[${#tg_sign[@]}-1]}' '${1}' - value argument is not a number." 725 | echo "Type ${appname} -h for help" 726 | exit 1 727 | fi 728 | fi 729 | tg_value+=("${1}") 730 | ;; 731 | ### 732 | ### Threshold info 733 | ### 734 | "-ti${th_chk}") 735 | # 1/3 Check placeholder 736 | shift 737 | if [ "${1}" = "" ]; then 738 | echo "Error, -ti

- no placeholder specified." 739 | echo "Type ${appname} -h for help" 740 | exit 1 741 | fi 742 | f=0 743 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 744 | if [ "${format_nodes[$i]}" = "${1}" ]; then 745 | f=1 746 | break 747 | fi 748 | done 749 | if [ "${f}" = "0" ]; then 750 | echo "Error, -ti '${1}' no such placeholder." 751 | echo "Type ${appname} -h for help" 752 | exit 1 753 | fi 754 | ti_placeholder+=("${format_vars[$i]}") 755 | 756 | # 2/3 Check sign 757 | shift 758 | if [ "${1}" = "" ]; then 759 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 760 | echo "Type ${appname} -h for help" 761 | exit 1 762 | fi 763 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 764 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 765 | echo "Type ${appname} -h for help" 766 | exit 1 767 | fi 768 | ti_sign+=("${1}") 769 | 770 | # 3/3 Check value 771 | shift 772 | if [ "${1}" = "" ]; then 773 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argyment is empty." 774 | echo "Type ${appname} -h for help" 775 | exit 1 776 | fi 777 | if [ "${ti_sign[${#ti_sign[@]}-1]}" = ">" ] || [ "${ti_sign[${#ti_sign[@]}-1]}" = "<" ]; then 778 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 779 | echo "Error, -ti '{${ti_placeholder[${#ti_placeholder[@]}-1]}}' '${ti_sign[${#ti_sign[@]}-1]}' '${1}' - value argument is not a number." 780 | echo "Type ${appname} -h for help" 781 | exit 1 782 | fi 783 | fi 784 | ti_value+=("${1}") 785 | ;; 786 | ### 787 | ### Threshold warning 788 | ### 789 | "-tw${th_chk}") 790 | # 1/3 Check placeholder 791 | shift 792 | if [ "${1}" = "" ]; then 793 | echo "Error, -tw

- no placeholder specified." 794 | echo "Type ${appname} -h for help" 795 | exit 1 796 | fi 797 | f=0 798 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 799 | if [ "${format_nodes[$i]}" = "${1}" ]; then 800 | f=1 801 | break 802 | fi 803 | done 804 | if [ "${f}" = "0" ]; then 805 | echo "Error, -tw '${1}' no such placeholder." 806 | echo "Type ${appname} -h for help" 807 | exit 1 808 | fi 809 | tw_placeholder+=("${format_vars[$i]}") 810 | 811 | # 2/3 Check sign 812 | shift 813 | if [ "${1}" = "" ]; then 814 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 815 | echo "Type ${appname} -h for help" 816 | exit 1 817 | fi 818 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 819 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 820 | echo "Type ${appname} -h for help" 821 | exit 1 822 | fi 823 | tw_sign+=("${1}") 824 | 825 | # 3/3 Check value 826 | shift 827 | if [ "${1}" = "" ]; then 828 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argyment is empty." 829 | echo "Type ${appname} -h for help" 830 | exit 1 831 | fi 832 | if [ "${tw_sign[${#tw_sign[@]}-1]}" = ">" ] || [ "${tw_sign[${#tw_sign[@]}-1]}" = "<" ]; then 833 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 834 | echo "Error, -tw '{${tw_placeholder[${#tw_placeholder[@]}-1]}}' '${tw_sign[${#tw_sign[@]}-1]}' '${1}' - value argument is not a number." 835 | echo "Type ${appname} -h for help" 836 | exit 1 837 | fi 838 | fi 839 | tw_value+=("${1}") 840 | ;; 841 | ### 842 | ### Threshold critical 843 | ### 844 | "-tc${th_chk}") 845 | # 1/3 Check placeholder 846 | shift 847 | if [ "${1}" = "" ]; then 848 | echo "Error, -tc

- no placeholder specified." 849 | echo "Type ${appname} -h for help" 850 | exit 1 851 | fi 852 | f=0 853 | for (( i=0; i < ${#format_nodes[@]}; i++ )); do 854 | if [ "${format_nodes[$i]}" = "${1}" ]; then 855 | f=1 856 | break 857 | fi 858 | done 859 | if [ "${f}" = "0" ]; then 860 | echo "Error, -tc '${1}' no such placeholder." 861 | echo "Type ${appname} -h for help" 862 | exit 1 863 | fi 864 | tc_placeholder+=("${format_vars[$i]}") 865 | 866 | # 2/3 Check sign 867 | shift 868 | if [ "${1}" = "" ]; then 869 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - sign argument is empty." 870 | echo "Type ${appname} -h for help" 871 | exit 1 872 | fi 873 | if [ "${1}" != "<" ] && [ "${1}" != ">" ] && [ "${1}" != "=" ] && [ "${1}" != "!=" ]; then 874 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${1}' - invalid sign: '${1}'." 875 | echo "Type ${appname} -h for help" 876 | exit 1 877 | fi 878 | tc_sign+=("${1}") 879 | 880 | # 3/3 Check value 881 | shift 882 | if [ "${1}" = "" ]; then 883 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argyment is empty." 884 | echo "Type ${appname} -h for help" 885 | exit 1 886 | fi 887 | if [ "${tc_sign[${#tc_sign[@]}-1]}" = ">" ] || [ "${tc_sign[${#tc_sign[@]}-1]}" = "<" ]; then 888 | if ! printf "%d" "${1}" >/dev/null 2>&1; then 889 | echo "Error, -tc '{${tc_placeholder[${#tc_placeholder[@]}-1]}}' '${tc_sign[${#tc_sign[@]}-1]}' '${1}' - value argument is not a number." 890 | echo "Type ${appname} -h for help" 891 | exit 1 892 | fi 893 | fi 894 | tc_value+=("${1}") 895 | ;; 896 | ### 897 | ### Format overwrite 898 | ### 899 | -f) 900 | shift 901 | if [ "${1}" = "" ]; then 902 | echo "Error, -f requires a string" 903 | echo "Type ${appname} -h for help" 904 | exit 1 905 | fi 906 | format="${1}" 907 | ;; 908 | ### 909 | ### Disable pango markup output 910 | ### 911 | -np) 912 | pango=0 913 | ;; 914 | ### 915 | ### Color overwrites 916 | ### 917 | -cd) 918 | # default color 919 | shift 920 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 921 | echo "Error, invalid color string: ${1}" 922 | echo "Type ${appname} -h for help" 923 | exit 1 924 | fi 925 | color_def="${1}" 926 | ;; 927 | -cg) 928 | # good color 929 | shift 930 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 931 | echo "Error, invalid color string: ${1}" 932 | echo "Type ${appname} -h for help" 933 | exit 1 934 | fi 935 | color_good="${1}" 936 | ;; 937 | -cw) 938 | # warning color 939 | shift 940 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 941 | echo "Error, invalid color string: ${1}" 942 | echo "Type ${appname} -h for help" 943 | exit 1 944 | fi 945 | color_warn="${1}" 946 | ;; 947 | -cc) 948 | # critical color 949 | shift 950 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 951 | echo "Error, invalid color string: ${1}" 952 | echo "Type ${appname} -h for help" 953 | exit 1 954 | fi 955 | color_crit="${1}" 956 | ;; 957 | -ci) 958 | # info color 959 | shift 960 | if ! echo "${1}" | grep -qE '#[0-9a-fA-F]{6}' >/dev/null 2>&1; then 961 | echo "Error, invalid color string: ${1}" 962 | echo "Type ${appname} -h for help" 963 | exit 1 964 | fi 965 | color_info="${1}" 966 | ;; 967 | ### 968 | ### System options 969 | ### 970 | -h) 971 | print_usage 972 | exit 0 973 | ;; 974 | -v) 975 | print_version 976 | exit 0 977 | ;; 978 | ### 979 | ### Unknown/Custom option 980 | ### 981 | *) 982 | 983 | ### 984 | ### Evaluate user-specified arguments 985 | ### 986 | found=0 987 | if [ "${#arg_params}" -gt "0" ]; then 988 | for (( i=0; i<${#arg_params[@]}; i++ )); do 989 | if [ "${arg_params[$i]}" = "${1}" ]; then 990 | shift 991 | var_name="${arg_vars[$i]}" 992 | eval "${var_name}=\"${1}\"" 993 | found=1 994 | break; 995 | fi 996 | done 997 | fi 998 | 999 | ### 1000 | ### Unknown option 1001 | ### 1002 | if [ "${found}" = "0" ]; then 1003 | echo "Invalid argument: '${1}'" 1004 | echo "Type ${appname} -h for help" 1005 | exit 1 1006 | fi 1007 | ;; 1008 | esac 1009 | shift 1010 | done 1011 | 1012 | 1013 | 1014 | 1015 | ### 1016 | ### Call custom function 1017 | ### 1018 | custom_action 1019 | 1020 | ### 1021 | ### Get final output color (based on custom specs) 1022 | ### 1023 | color="$( get_status_color "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1024 | 1025 | ### 1026 | ### Format (colors) 1027 | ### 1028 | format="$( replace_colors "${format}" "${color}" "${color_def}" "${color_good}" "${color_warn}" "${color_crit}" "${color_info}" )" 1029 | 1030 | ### 1031 | ### Format (custom) 1032 | ### 1033 | format="$( replace_placeholders "${format}" )" 1034 | 1035 | 1036 | ### 1037 | ### Output pango or plain style? 1038 | ### 1039 | if [ "${pango}" = "1" ]; then 1040 | if [ "${format}" != "" ]; then 1041 | echo "${format}" 1042 | fi 1043 | else 1044 | echo "${format}" # Long output 1045 | echo "${format}" # short output 1046 | echo "\\${color}" # color code '\#RRGGBB' 1047 | fi 1048 | 1049 | exit 0 1050 | --------------------------------------------------------------------------------