forked from beeryardtech/tmux-net-speed
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathhelpers.sh
More file actions
150 lines (123 loc) · 2.99 KB
/
helpers.sh
File metadata and controls
150 lines (123 loc) · 2.99 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash -
##
# Varialbes
##
DOWNLOAD_FILE="/tmp/tmux_net_speed.download"
UPLOAD_FILE="/tmp/tmux_net_speed.upload"
get_tmux_option() {
local option=$1
local default_value=$2
local option_value="$(tmux show-option -gqv "$option")"
if [[ -z "$option_value" ]]; then
echo "$default_value"
else
echo "$option_value"
fi
}
set_tmux_option() {
local option=$1
local value=$2
tmux set-option -gq "$option" "$value"
}
get_velocity()
{
local new_value=$1
local old_value=$2
# Consts
local THOUSAND=1024
local MILLION=1048576
local interval=$(get_tmux_option 'status-interval' 5)
local vel=$(( ( new_value - old_value ) / interval ))
local vel_kb=$(( vel / THOUSAND ))
local vel_mb=$(( vel / MILLION ))
if [[ $vel_mb != 0 ]] ; then
echo -n "$vel_mb MB/s"
elif [[ $vel_kb != 0 ]] ; then
echo -n "$vel_kb KB/s";
else
echo -n "$vel B/s";
fi
}
# Reads from value from file. If file does not exist,
# is empty, or not readable, starts back at 0
read_file()
{
local path="$1"
local fallback_val=0
# File exists and is readdable?
if [[ ! -f "$path" ]] ; then
echo $fallback_val
return 1
elif [[ ! -r "$path" ]]; then
echo $fallback_val
return 1
fi
# Does the file have content?
tmp=$(< "$path")
if [[ "x${tmp}" == "x" ]] ; then
echo $fallback_val
return 1
fi
# Now return known value
echo $tmp
}
# Update values in file
write_file()
{
local path="$1"
local val="$2"
# TODO Add error checking
echo "$val" > "$path"
}
get_interfaces()
{
local interfaces=$(get_tmux_option @net_speed_interfaces "")
if [[ -z "$interfaces" ]] ; then
if [[ -n is_osx ]]
then
interfaces=$(netstat -i | tail +2 | sed -E 's/ +/ /g' | cut -d ' ' -f 1 | sort -u | grep "en")
else
for interface in /sys/class/net/*; do
interfaces+=$(echo $(basename $interface) " ");
done
fi
fi
# Do not quote the variable. This way will handle trailing whitespace
echo -n $interfaces
}
sum_speed()
{
local column=$1
declare -a interfaces=$(get_interfaces)
local line=""
local val=0
for intf in ${interfaces[@]} ; do
line=$(get_speed $intf)
speed="$(echo -n $line | cut -d' ' -f $column)"
let val+=${speed:=0}
done
echo $val
}
get_speed()
{
local interface=$1
if [[ -n is_osx ]]
then
line="$(netstat -b -i -I "$interface" | tail +2 | sed -E 's/ +/ /g' | cut -d ' ' -f 1,7,10 | uniq )"
down=$(echo -n $line | cut -d ' ' -f 2)
up=$(echo -n $line | cut -d ' ' -f 3)
echo "$down 0 0 0 0 0 0 0 $up"
else
echo $(cat /proc/net/dev | grep "$interface" | cut -d':' -f 2)
fi
}
is_osx() {
[ $(uname) == "Darwin" ]
}
is_cygwin() {
command -v WMIC > /dev/null
}
command_exists() {
local command="$1"
type "$command" >/dev/null 2>&1
}