2 # Copyright (c) 2014 Zhong Jianxin <azuwis@gmail.com>
4 # Permission is hereby granted, free of charge, to any person obtaining a copy
5 # of this software and associated documentation files (the "Software"), to deal
6 # in the Software without restriction, including without limitation the rights
7 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 # copies of the Software, and to permit persons to whom the Software is
9 # furnished to do so, subject to the following conditions:
11 # The above copyright notice and this permission notice shall be included in
12 # all copies or substantial portions of the Software.
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # Thank Stefan Breunig for the original implementation, see
23 # contrib/measure-net-speed.bash.
25 # i3status.conf should contain:
27 # output_format = i3bar
30 # i3 config looks like this:
32 # status_command exec /usr/share/doc/i3status/contrib/net-speed
42 ifaces=$(ls /sys/class/net | grep -E '^(eth|wlan)')
44 # Interval must be the same as in i3status.conf
47 if [ -z "$XDG_CONFIG_HOME" ]; then
48 XDG_CONFIG_HOME="${HOME}/.config"
52 if [ -f "${XDG_CONFIG_HOME}/i3status/config" ]; then
53 i3status_conf="${XDG_CONFIG_HOME}/i3status/config"
54 elif [ -f ~/.i3status.conf ]; then
55 i3status_conf=~/.i3status.conf
57 i3status_conf="/etc/i3status.conf"
60 if [ -f "$i3status_conf" ]; then
61 interval=$(grep -o '^[[:space:]]*interval[[:space:]]*=[[:space:]]*[[:digit:]]\+' $i3status_conf | grep -o '[[:digit:]]\+')
64 if [ x"$interval" = x ]; then
74 local kib=$(( byte >> 10 ))
75 if [ "$kib" -gt 1024 ]; then
76 local mib_int=$(( kib >> 10 ))
77 local mib_dec=$(( kib % 1024 * 976 / 10000 ))
78 if [ "$mib_dec" -lt 10 ]; then
81 echo "${mib_int}.${mib_dec}M"
90 for iface in $ifaces; do
93 local stat="/sys/class/net/${iface}/statistics"
94 read tmp_rx < "${stat}/rx_bytes"
95 read tmp_tx < "${stat}/tx_bytes"
99 rate="$(readable $(( (rx - last_rx) / interval )))↓ $(readable $(( (tx - last_tx) / interval )))↑"
110 i3status | (read line && echo "$line" && read line && echo "$line" && read line && echo "$line" && update_rate && while :
114 echo ",[{\"full_text\":\"${rate}\" },${line#,\[}" || exit 1