├── net-speed.tmux └── scripts ├── helpers.sh └── net-speed.sh /net-speed.tmux: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | SDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) 4 | source "$SDIR/scripts/helpers.sh" 5 | 6 | upload_speed_format() { 7 | get_tmux_option @upload_speed_format "%%7s" 8 | } 9 | download_speed_format() { 10 | get_tmux_option @download_speed_format "%%7s" 11 | } 12 | 13 | upload_speed="#($SDIR/scripts/net-speed.sh tx_bytes '$(upload_speed_format)')" 14 | download_speed="#($SDIR/scripts/net-speed.sh rx_bytes '$(download_speed_format)')" 15 | 16 | upload_interpolation="\#{upload_speed}" 17 | download_interpolation="\#{download_speed}" 18 | 19 | do_interpolation() { 20 | local input=$1 21 | local result="" 22 | 23 | result=${input/$upload_interpolation/$upload_speed} 24 | result=${result/$download_interpolation/$download_speed} 25 | 26 | echo "$result" 27 | } 28 | 29 | update_tmux_option() { 30 | local option=$1 31 | local option_value=$(get_tmux_option "$option") 32 | set_tmux_option "$option" "$(do_interpolation "$option_value")" 33 | } 34 | 35 | main() { 36 | update_tmux_option "status-right" 37 | update_tmux_option "status-left" 38 | } 39 | main 40 | -------------------------------------------------------------------------------- /scripts/helpers.sh: -------------------------------------------------------------------------------- 1 | set_tmux_option() { 2 | local option="$1" 3 | local value="$2" 4 | tmux set-option -gq "$option" "$value" 5 | } 6 | 7 | get_tmux_option() { 8 | local option=$1 9 | local default_value=$2 10 | local option_value="$(tmux show-option -gqv "$option")" 11 | 12 | [[ -z "$option_value" ]] && echo $default_value || echo $option_value 13 | } 14 | 15 | get_os() { 16 | case "$OSTYPE" in 17 | linux*) echo "linux" ;; 18 | darwin*) echo "osx" ;; 19 | solaris*) echo "solaris" ;; 20 | freebsd*) echo "freebsd" ;; 21 | netbsd*) echo "netbsd" ;; 22 | openbsd*) echo "openbsd" ;; 23 | bsd*) echo "bsd" ;; 24 | msys*) echo "windows" ;; 25 | *) echo "unknown" ;; 26 | esac 27 | } 28 | 29 | sum_column() { 30 | awk '{sum += $1;}END{printf("%.0f",sum);}' 31 | } 32 | 33 | # ref: https://unix.stackexchange.com/a/98790 @John 34 | bytestohuman() { 35 | local L_BYTES="${1:-0}" 36 | local L_BASE="${2:-1024}" 37 | (awk -v bytes="${L_BYTES}" -v base="${L_BASE}" 'function human(x, base) { 38 | if(base!=1024)base=1000 39 | 40 | s="BKMGTEPYZ" 41 | while (x>=base && length(s)>1) 42 | {x/=base; s=substr(s,2)} 43 | s=substr(s,1,1) 44 | xf=((s=="B")?"%4d": (x > 99 ? "%4d" : "%4.1f")) 45 | return sprintf( (xf "%s\n"), x, s) 46 | } 47 | BEGIN{print human(bytes, base)}') 48 | } 49 | -------------------------------------------------------------------------------- /scripts/net-speed.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | SDIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) 4 | source "$SDIR/helpers.sh" 5 | 6 | # $1: rx_bytes/tx_bytes 7 | get_bytes() { 8 | case $(get_os) in 9 | osx) 10 | netstat -ibn | sort -u -k1,1 | grep ':' | grep -Ev '^(lo|docker).*' | 11 | awk '{rx += $7;tx += $10;}END{print "rx_bytes "rx,"\ntx_bytes "tx}' | 12 | grep "$1" | awk '{print $2}' 13 | ;; 14 | linux) 15 | for file in /sys/class/net/*; do 16 | [[ $file =~ .*/(lo|docker.*) ]] || cat "$file/statistics/$1" 17 | done | sum_column 2>/dev/null 18 | ;; 19 | freebsd) 20 | netstat -ibnW | sort -u -k1,1 | grep ':' | grep -Ev '^lo.*' | 21 | awk '{rx += $8;tx += $11;}END{print "rx_bytes "rx,"\ntx_bytes "tx}' | 22 | grep "$1" | awk '{print $2}' 23 | ;; 24 | netbsd|openbsd) 25 | netstat -ibn | sort -u -k1,1 | grep ':' | grep -Ev '^lo.*' | 26 | awk '{rx += $5;tx += $6;}END{print "rx_bytes "rx,"\ntx_bytes "tx}' | 27 | grep "$1" | awk '{print $2}' 28 | ;; 29 | *) 30 | echo 0 31 | ;; 32 | esac 33 | } 34 | 35 | # $1: rx_bytes/tx_bytes 36 | get_speed() { 37 | local pre cur diff speed pre_var 38 | pre_var="@netspeed_$1" 39 | cur=$(get_bytes "$1") 40 | pre=$(get_tmux_option "$pre_var" "$cur") 41 | diff=$((cur - pre)) 42 | (( diff < 0 )) && diff=0 43 | speed=$(bytestohuman $diff) 44 | # speed=$(numfmt --to=iec --padding=7 $diff) 45 | echo "${speed}/s" 46 | set_tmux_option "$pre_var" "$cur" 47 | } 48 | 49 | # $1: tx_bytes/tx_bytes 50 | # $2: format 51 | main() { 52 | printf "${2:-%8s}" "$(get_speed "$1")" 53 | } 54 | 55 | main "$@" 56 | --------------------------------------------------------------------------------