forked from minhdanh/tmux-network-speed
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.sh
More file actions
126 lines (111 loc) · 3.3 KB
/
Copy pathhelpers.sh
File metadata and controls
126 lines (111 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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
# 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" ]
}