-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(colors): Adding settings to change color when speed threshold is… #4
base: master
Are you sure you want to change the base?
Changes from all commits
6e291b7
d587582
fd7ac45
64ad470
863c3ec
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,79 @@ | ||
# tmux-network-speed | ||
|
||
(Previously `tmux-macos-network-speed`) | ||
|
||
Tmux plugin to monitor network stats. Inspired by https://github.com/tmux-plugins/tmux-net-speed | ||
|
||
## Installation with [Tmux Plugin Manager](https://github.com/tmux-plugins/tpm) | ||
|
||
Add this to `.tmux.conf`: | ||
``` | ||
|
||
```sh | ||
set -g @plugin 'minhdanh/tmux-network-speed' | ||
``` | ||
|
||
Also add `#{network_speed}` to your left/right status bar. | ||
For example: | ||
|
||
``` | ||
```sh | ||
set -g status-right '#{prefix_highlight} #{network_speed} | CPU: #{cpu_icon}#{cpu_percentage} | %a %Y-%m-%d %H:%M' | ||
``` | ||
|
||
Then hit `<prefix> + I` to install the plugin. | ||
|
||
Sample output: | ||
|
||
 | ||
 | ||
|
||
## Options | ||
|
||
### Network Interface | ||
|
||
In case you want to monitor a network interface other than `en0`, set `network_speed_interface` to the name of that network interface: | ||
|
||
``` | ||
```sh | ||
set -g @network_speed_interface 'enp37s0' | ||
``` | ||
|
||
### Colors | ||
|
||
Colors for download and upload are supported: | ||
|
||
``` | ||
```sh | ||
set -g @network_speed_download_color '#[fg=green]' | ||
set -g @network_speed_upload_color '#[fg=yellow]' | ||
``` | ||
|
||
### Speed Format | ||
|
||
You can also set the format for the speed, it accepts any format string that `printf` supports: | ||
``` | ||
|
||
```sh | ||
set -g @network_speed_format '%05.2f' | ||
``` | ||
|
||
### High-Speed Threshold and Color | ||
|
||
You can set a threshold speed above which the color will change to indicate high speed. The default threshold is 1.0 MB/s. | ||
|
||
Set the high-speed threshold: | ||
|
||
```sh | ||
set -g @network_speed_threshold '1.0' | ||
``` | ||
|
||
Set the color for high speed: | ||
|
||
```sh | ||
set -g @network_speed_high_color '#[fg=red]' | ||
``` | ||
|
||
### High-Speed Threshold Unit | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just one minor thing, I think this section should be merged into "### High-Speed Threshold and Color" as they're both about the threshold and color. |
||
|
||
You can specify the unit for the high-speed threshold. Supported units are `KB/s`, `MB/s`, and `GB/s`. The default unit is `MB/s`. | ||
|
||
Set the high-speed threshold unit: | ||
|
||
```sh | ||
set -g @network_speed_threshold_unit 'MB/s' | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,126 @@ | ||
#!/bin/bash - | ||
|
||
# Function to get a tmux option or return a default value if the option is not set | ||
get_tmux_option() { | ||
local option="$1" | ||
local default_value="$2" | ||
local option_value="$(tmux show-option -gqv "$option")" | ||
# Check if the option is not set, return the default value if so | ||
if [ -z "$option_value" ]; then | ||
echo "$default_value" | ||
else | ||
echo "$option_value" | ||
fi | ||
} | ||
|
||
# Function to calculate the speed in KB/s or MB/s | ||
get_speed() { | ||
# Constants for conversion | ||
local THOUSAND=1024 | ||
local MILLION=1048576 | ||
local BILLION=1073741824 | ||
|
||
local new=$1 | ||
local current=$2 | ||
local interval=$3 | ||
local vel=0 | ||
|
||
# Get the format string for speed display from tmux options | ||
local format_string=$(get_tmux_option '@network_speed_format' "%05.2f") | ||
|
||
# Calculate the speed if the current value is not zero | ||
if [ ! "$current" -eq "0" ]; then | ||
vel=$(echo "$(($new - $current)) $interval" | awk '{print ($1 / $2)}') | ||
fi | ||
|
||
# Convert speed to KB/s, MB/s, and GB/s | ||
local vel_kb=$(echo "$vel" $THOUSAND | awk '{print ($1 / $2)}') | ||
local vel_mb=$(echo "$vel" $MILLION | awk '{print ($1 / $2)}') | ||
local vel_gb=$(echo "$vel" $BILLION | awk '{print ($1 / $2)}') | ||
|
||
# Check if speed is greater than 99.99 KB/s, then display in MB/s or GB/s | ||
if (($(echo "$vel_kb > 1024" | bc -l))); then | ||
if (($(echo "$vel_mb > 1024" | bc -l))); then | ||
local vel_gb_f=$(printf $format_string $vel_gb) | ||
printf "%s GB/s" $vel_gb_f | ||
else | ||
local vel_mb_f=$(printf $format_string $vel_mb) | ||
printf "%s MB/s" $vel_mb_f | ||
fi | ||
else | ||
local vel_kb_f=$(printf $format_string $vel_kb) | ||
printf "%s KB/s" $vel_kb_f | ||
fi | ||
} | ||
|
||
# Function to get the current network speed output for the given interface | ||
get_speed_output() { | ||
local interface="$1" | ||
|
||
# Check if the operating system is macOS | ||
if is_osx; then | ||
# Use netstat for macOS to get transmitted and received bytes | ||
netstat -bn -I $network_interface | grep "<Link#" | awk '{print $7 " " $10}' | ||
else | ||
# Use /proc/net/dev for Linux to get transmitted and received bytes | ||
cat /proc/net/dev | grep $network_interface | awk '{print $2 " " $10}' | ||
fi | ||
} | ||
|
||
# Function to get the appropriate color based on the speed | ||
get_speed_color() { | ||
local speed=$1 | ||
local threshold=$2 | ||
local threshold_unit=$3 | ||
local default_color=$4 | ||
local high_color=$5 | ||
|
||
# Convert threshold to KB/s for comparison | ||
case "$threshold_unit" in | ||
"KB/s") | ||
threshold_kb=$(echo "$threshold" | awk '{print $1}') | ||
;; | ||
"MB/s") | ||
threshold_kb=$(echo "$threshold * 1024" | bc -l) | ||
;; | ||
"GB/s") | ||
threshold_kb=$(echo "$threshold * 1048576" | bc -l) | ||
;; | ||
*) | ||
threshold_kb=$(echo "$threshold" | awk '{print $1}') | ||
;; | ||
esac | ||
Comment on lines
+79
to
+92
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I understand this correctly, in case we don't set the threshold, then threshold_kb will be 0. Then the evaluation in line 115-117 always results to high_color being used:
Is that correct? I just want to make sure if the threshold is 0, then we use the default color. |
||
|
||
# Extract the speed value and unit | ||
local speed_value=$(echo $speed | awk '{print $1}') | ||
local speed_unit=$(echo $speed | awk '{print $2}') | ||
|
||
# Convert speed to KB/s for comparison | ||
case "$speed_unit" in | ||
"KB/s") | ||
speed_kb=$(echo "$speed_value" | awk '{print $1}') | ||
;; | ||
"MB/s") | ||
speed_kb=$(echo "$speed_value * 1024" | bc -l) | ||
;; | ||
"GB/s") | ||
speed_kb=$(echo "$speed_value * 1048576" | bc -l) | ||
;; | ||
*) | ||
speed_kb=$(echo "$speed_value" | awk '{print $1}') | ||
;; | ||
esac | ||
|
||
# If speed in KB/s is greater than the threshold in KB/s, use high color | ||
if (($(echo "$speed_kb > $threshold_kb" | bc -l))); then | ||
echo "$high_color" | ||
else | ||
# Otherwise, use the default color | ||
echo "$default_color" | ||
fi | ||
} | ||
|
||
# Function to check if the operating system is macOS | ||
is_osx() { | ||
[ $(uname) == "Darwin" ] | ||
[ $(uname) == "Darwin" ] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,64 @@ | ||
#!/bin/bash - | ||
|
||
CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | ||
# Get the directory of the current script | ||
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
|
||
# Source the helper functions from helpers.sh | ||
source "$CURRENT_DIR/helpers.sh" | ||
|
||
default_download_color="#[fg=green]" | ||
default_upload_color="#[fg=yellow]" | ||
# Retrieve tmux options for upload and download colors, set default if not found | ||
default_upload_color="$(get_tmux_option "@network_speed_upload_color" "#[fg=yellow]")" | ||
default_download_color="$(get_tmux_option "@network_speed_download_color" "#[fg=green]")" | ||
|
||
get_speed() | ||
{ | ||
# Consts | ||
local THOUSAND=1024 | ||
local MILLION=1048576 | ||
|
||
local new=$1 | ||
local current=$2 | ||
local interval=$3 | ||
local vel=0 | ||
|
||
local format_string=$(get_tmux_option '@network_speed_format' "%05.2f") | ||
|
||
if [ ! "$current" -eq "0" ]; then | ||
vel=$(echo "$(( $new - $current )) $interval" | awk '{print ($1 / $2)}') | ||
fi | ||
|
||
local vel_kb=$(echo "$vel" $THOUSAND | awk '{print ($1 / $2)}') | ||
local vel_mb=$(echo "$vel" $MILLION | awk '{print ($1 / $2)}') | ||
|
||
result=$(printf "%05.2f > 99.99\n" $vel_kb | bc -l) | ||
if [[ $result == 1 ]]; then | ||
local vel_mb_f=$(printf $format_string $vel_mb) | ||
printf "%s MB/s" $vel_mb_f | ||
else | ||
local vel_kb_f=$(printf $format_string $vel_kb) | ||
printf "%s KB/s" $vel_kb_f | ||
fi | ||
} | ||
# Retrieve tmux options for speed threshold, unit and high speed color, set default if not found | ||
threshold_speed="$(get_tmux_option "@network_speed_threshold" "0")" | ||
threshold_unit="$(get_tmux_option "@network_speed_threshold_unit" "MB/s")" | ||
high_speed_color="$(get_tmux_option "@network_speed_high_color" "#[fg=red]")" | ||
|
||
# Retrieve the network interface and current transmitted (tx) and received (rx) byte counts from tmux options | ||
network_interface=$(get_tmux_option "@network_speed_interface" "en0") | ||
current_tx=$(get_tmux_option "@network_speed_tx" 0) | ||
current_rx=$(get_tmux_option "@network_speed_rx" 0) | ||
|
||
# Get the current speed output for the network interface | ||
speed_output=$(get_speed_output $network_interface) | ||
new_rx=$(echo "$speed_output" | awk '{print $1}') | ||
new_tx=$(echo "$speed_output" | awk '{print $2}') | ||
new_rx=$(echo "$speed_output" | awk '{print $1}') # Extract the new received bytes | ||
new_tx=$(echo "$speed_output" | awk '{print $2}') # Extract the new transmitted bytes | ||
|
||
# Update tmux options with the new transmitted and received byte counts | ||
tmux set-option -gq "@network_speed_tx" $new_tx | ||
tmux set-option -gq "@network_speed_rx" $new_rx | ||
|
||
# Get the current time in seconds since epoch | ||
cur_time=$(date +%s) | ||
|
||
# Retrieve the last update times for tx and rx from tmux options, default to 0 if not found | ||
last_update_time_tx=$(get_tmux_option '@network_speed_last_update_time_tx' 0) | ||
interval_tx=$((cur_time - last_update_time_tx)) | ||
last_update_time_rx=$(get_tmux_option '@network_speed_last_update_time_rx' 0) | ||
interval_rx=$((cur_time - last_update_time_rx)) | ||
|
||
# Calculate upload speed if the interval is non-zero, otherwise use the last recorded speed | ||
if [ $interval_tx -eq 0 ]; then | ||
upload_speed=$(get_tmux_option '@network_speed_last_speed_tx') | ||
upload_speed=$(get_tmux_option '@network_speed_last_speed_tx') | ||
else | ||
upload_speed=$(get_speed $new_tx $current_tx $interval_tx) | ||
tmux set-option -gq "@network_speed_last_speed_tx" "$upload_speed" | ||
tmux set-option -gq "@network_speed_last_update_time_tx" $(date +%s) | ||
upload_speed=$(get_speed $new_tx $current_tx $interval_tx) | ||
tmux set-option -gq "@network_speed_last_speed_tx" "$upload_speed" | ||
tmux set-option -gq "@network_speed_last_update_time_tx" $(date +%s) | ||
fi | ||
|
||
# Calculate download speed if the interval is non-zero, otherwise use the last recorded speed | ||
if [ $interval_rx -eq 0 ]; then | ||
download_speed=$(get_tmux_option '@network_speed_last_speed_rx') | ||
download_speed=$(get_tmux_option '@network_speed_last_speed_rx') | ||
else | ||
download_speed=$(get_speed $new_rx $current_rx $interval_rx) | ||
tmux set-option -gq "@network_speed_last_speed_rx" "$download_speed" | ||
tmux set-option -gq "@network_speed_last_update_time_rx" $(date +%s) | ||
download_speed=$(get_speed $new_rx $current_rx $interval_rx) | ||
tmux set-option -gq "@network_speed_last_speed_rx" "$download_speed" | ||
tmux set-option -gq "@network_speed_last_update_time_rx" $(date +%s) | ||
fi | ||
|
||
download_color=$(get_tmux_option "@network_speed_download_color" "$default_download_color") | ||
upload_color=$(get_tmux_option "@network_speed_upload_color" "$default_upload_color") | ||
# Determine the color for download and upload speeds based on the threshold, unit, and high speed color settings | ||
download_color=$(get_speed_color "$download_speed" "$threshold_speed" "$threshold_unit" "$default_download_color" "$high_speed_color") | ||
upload_color=$(get_speed_color "$upload_speed" "$threshold_speed" "$threshold_unit" "$default_upload_color" "$high_speed_color") | ||
|
||
printf "%s↓ %s %s↑ %s#[fg=default]" "$download_color" "$download_speed" "$upload_color" "$upload_speed" | ||
# Print the download and upload speeds with the appropriate colors | ||
printf "%s↓ %s#[fg=default] %s↑ %s#[fg=default]" "$download_color" "$download_speed" "$upload_color" "$upload_speed" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be updated also I guess.
We don't want to have a default value for the threshold 😄