[READ-ONLY] Mirror of https://github.com/jmrplens/ShellFish-Widgets. Bash scripts to send information from the server to the widget.
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #9 from jmrplens/better_colors

Better color interp and update readme

authored by

José M. Requena Plens and committed by
GitHub
(Sep 8, 2024, 1:28 AM +0200) a4e5ba24 114f9a9f

+54 -27
+19 -4
README.md
··· 8 8 9 9 The file will be added to the home folder of your user on the server. If it is different from root, modify the path in the third line of the script `source /root/.shellfishrc` to the correct one `source /your_user/.shellfishrc`. 10 10 11 + ## Dependencies 12 + 13 + Your server must have the following tools installed: 14 + 15 + - `curl`: To send the information to the widget. [More info](https://curl.se/) 16 + - `jq`: To process the JSON response. [More info](https://stedolan.github.io/jq/) 17 + 11 18 ## Usage 12 19 13 20 All the widgets that will be added to this repository will be used in the same way. If there is any difference, it will be indicated in its section. 14 21 15 22 1. Download the .sh file to your server, for example (you can change the path `/opt/shellfish_widgets` to any other: 16 - ```bash 23 + 24 + ```shell 17 25 mkdir /opt/shellfish_widgets 18 26 cd /opt/shellfish_widgets 19 27 wget https://github.com/jmrplens/ShellFish-Widgets/raw/main/small_widget_A.sh 20 28 ``` 29 + 21 30 2. Enable its execution: 22 - ``` 31 + 32 + ```shell 23 33 chmod +x /opt/shellfish_widgets/small_widget_A.sh 24 34 ``` 35 + 25 36 3. Run it: 26 - ``` 37 + 38 + ```shell 27 39 /opt/shellfish_widgets/small_widget_A.sh --server_name Example 28 40 ``` 41 + 29 42 With this, you will have already sent the information to the widget you set on your iOS. If you want, you can configure some details: 30 43 - **Server name**: This is the name that will appear on the widget, if it is not configured, the hostame of the server will be used `--server_name Example`. 31 44 - **Disk**: If you want to send the used space of a disk other than the main one `--disk /volumeX`. ··· 33 46 - **Target**: To send/create the information to a specific widget, indicate the widget's reference `--target Small_A`. 34 47 35 48 4. (Optional) Add it to the crontab (in this example, it runs every 10 minutes) to have updated information periodically: 36 - ``` 49 + 50 + ```shell 37 51 { crontab -l; echo "*/10 * * * * /opt/shellfish_widgets/small_widget_A.sh"; } | crontab - 38 52 ``` 53 + 39 54 or `crontab -e` and add a new line with `*/10 * * * * /opt/shellfish_widgets/small_widget_A.sh` 40 55 41 56 ### Small Widgets
+35 -23
small_widget_A.sh
··· 1 1 #!/bin/bash 2 2 3 3 # Load to make widget command available 4 + # shellcheck source=/dev/null 4 5 source /root/.shellfishrc 5 6 6 7 # Default values ··· 27 28 CPU_TEMP_SENSOR=${CPU_TEMP_SENSOR:-$DEFAULT_CPU_TEMP_SENSOR} 28 29 TARGET=${TARGET:-$DEFAULT_TARGET} 29 30 31 + # Function to calculate the interpolated color between green (min) and red (max) 32 + calculate_color() { 33 + local value=$1 34 + local min_value=$2 35 + local max_value=$3 36 + 37 + # If value is lee than min_value, clip it to min_value 38 + if (( value < min_value )); then 39 + value=$min_value 40 + fi 41 + 42 + # Normalize value between 0 and 1 43 + local range=$((max_value - min_value)) 44 + local normalized_value=$((100 * (value - min_value) / range)) 45 + 46 + if (( normalized_value <= 33 )); then 47 + # Interpolating between green (#00FF00) and yellow (#FFFF00) 48 + local red=$((255 * normalized_value / 33)) 49 + printf "#%02XFF00\n" $red 50 + elif (( normalized_value <= 66 )); then 51 + # Interpolating between yellow (#FFFF00) and orange (#FFA500) 52 + local green=$((255 - (90 * (normalized_value - 33) / 33))) 53 + printf "#FFA5%02X\n" $green 54 + else 55 + # Interpolating between orange (#FFA500) and red (#FF0000) 56 + local green=$((165 - (165 * (normalized_value - 66) / 34))) 57 + printf "#FF%02X00\n" $green 58 + fi 59 + } 60 + 30 61 # Try to automatically detect the main disk if it's not defined 31 62 if [ -z "$DISK" ]; then 32 63 # Attempt 1: lsblk 33 64 DISK=$(lsblk -J -o NAME,MOUNTPOINT | jq -r '.blockdevices[] | select(.children != null) | .children[] | select(.mountpoint == "/") | .name') 34 - 65 + 35 66 # Attempt 2: df (fallback method) 36 67 if [ -z "$DISK" ]; then 37 68 DISK=$(df / | grep -Eo '^/dev/[a-zA-Z0-9]+' | cut -d'/' -f3) ··· 44 75 fi 45 76 fi 46 77 47 - # Function to interpolate colors in hexadecimal (green to red) 48 - calculate_color() { 49 - local value=$1 50 - local min_value=$2 51 - local max_value=$3 52 - 53 - if (( value <= min_value )); then 54 - echo "#00FF00" # Green 55 - elif (( value >= max_value )); then 56 - echo "#FF0000" # Red 57 - else 58 - if (( value < (max_value + min_value) / 2 )); then 59 - echo "#FFFF00" # Yellow 60 - else 61 - echo "#FFA500" # Orange 62 - fi 63 - fi 64 - } 65 78 66 79 # 1. Memory usage as a percentage 67 80 if [[ -f /proc/meminfo ]]; then ··· 81 94 82 95 # 2. Disk usage as a percentage 83 96 disk_info_percent=$(df /dev/"${DISK}" -h --output=pcent | tail -1 | tr -d ' %') 84 - disk_total_size=$(df /dev/"${DISK}" -h --output=size | tail -1)B 85 97 disk_used_size=$(df /dev/"${DISK}" -h --output=used | tail -1)B 86 98 87 99 # String for disk usage as a percentage ··· 154 166 cpu_string="${cpu_usage}%" 155 167 156 168 # 5. Color calculations based on percentages 157 - cpu_color=$(calculate_color "$cpu_usage" 20 90) 158 - memory_color=$(calculate_color "$mem_percent" 20 90) 159 - disk_color=$(calculate_color "$disk_info_percent" 20 90) 169 + cpu_color=$(calculate_color "$cpu_usage" 10 95) 170 + memory_color=$(calculate_color "$mem_percent" 10 95) 171 + disk_color=$(calculate_color "$disk_info_percent" 20 95) 160 172 temp_color=$(calculate_color "$cpu_temp" 45 90) 161 173 162 174 # Widget command using SF Symbols and strings in the correct format with colors