From 34ffddbd4bdc54b8be2002910099bd6e596966a6 Mon Sep 17 00:00:00 2001 From: ndwarshuis Date: Thu, 17 Jun 2021 17:21:38 -0400 Subject: [PATCH] ADD more plugins to tmux --- dot_config/tmux/plugins/executable_common | 39 ++++++++++++++++++ dot_config/tmux/plugins/executable_cpu_perc | 36 ++--------------- dot_config/tmux/plugins/executable_mem_perc | 6 +++ dot_config/tmux/plugins/executable_network | 44 +++++++++++++++++++++ dot_config/tmux/tmux.conf.tmpl | 33 +++++++++++++--- 5 files changed, 119 insertions(+), 39 deletions(-) create mode 100644 dot_config/tmux/plugins/executable_common mode change 100755 => 100644 dot_config/tmux/plugins/executable_cpu_perc create mode 100644 dot_config/tmux/plugins/executable_mem_perc create mode 100644 dot_config/tmux/plugins/executable_network diff --git a/dot_config/tmux/plugins/executable_common b/dot_config/tmux/plugins/executable_common new file mode 100644 index 0000000..17c7ca4 --- /dev/null +++ b/dot_config/tmux/plugins/executable_common @@ -0,0 +1,39 @@ +#!/bin/bash + +cache_vals(){ + echo "$1" > "$3" + echo -n "$2" >> "$3" +} + +cached_eval(){ + local dir + local a0 + local t0 + local a1 + local t1 + dir="/tmp/$1" + a1="$2" + t1="$3" + if [ -f "$dir" ]; then + a0="$(head -n1 "$dir")" + t0="$(tail -n+2 "$dir")" + cache_vals "$a1" "$t1" "$dir" + awk -v a1="$a1" \ + -v t1="$t1" \ + -v a0="$a0" \ + -v t0="$t0" \ + 'BEGIN {print (a1-a0)/(t1-t0)}' + else + cache_vals "$a1" "$t1" "$dir" + echo -n 0 + fi +} + +fmt_percent(){ + awk "$(printf '%s' \ + '{x=$1*100} END { '\ + 'if (x>=100) printf("%d%% ", x); '\ + 'else if (x>=10) printf("%.1f%%", x); '\ + 'else printf("%.2f%%", x)'\ + '}')" +} diff --git a/dot_config/tmux/plugins/executable_cpu_perc b/dot_config/tmux/plugins/executable_cpu_perc old mode 100755 new mode 100644 index 58667bd..eef7698 --- a/dot_config/tmux/plugins/executable_cpu_perc +++ b/dot_config/tmux/plugins/executable_cpu_perc @@ -1,41 +1,11 @@ #!/bin/bash -cache_vals(){ - echo "$1" > "$3" - echo -n "$2" >> "$3" -} - -cached_eval(){ - local dir - local a0 - local t0 - local a1 - local t1 - dir="/tmp/tmux_cpu_perc" - a1="$1" - t1="$2" - if [ -f "$dir" ]; then - a0="$(head -n1 "$dir")" - t0="$(tail -n+2 "$dir")" - cache_vals "$a1" "$t1" "$dir" - awk -v a1="$a1" \ - -v t1="$t1" \ - -v a0="$a0" \ - -v t0="$t0" \ - 'BEGIN {print 100*(a1-a0)/(t1-t0)}' - else - cache_vals "$a1" "$t1" "$dir" - echo -n 0 - fi -} +this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +source "$this_dir/common" get_cpu(){ grep 'cpu ' /proc/stat | \ awk '{a=($2+$4)}; {t=($2+$4+$5)} END {printf("%s %s", a, t)}' } -cpu_percentage_format="%.2f%%" - -cached_eval $(get_cpu) \ - | awk -v f="$cpu_percentage_format" \ - '{usage=$NF} END {printf(f, usage)}' +cached_eval "cpu_perc" $(get_cpu) | fmt_percent diff --git a/dot_config/tmux/plugins/executable_mem_perc b/dot_config/tmux/plugins/executable_mem_perc new file mode 100644 index 0000000..aa3f331 --- /dev/null +++ b/dot_config/tmux/plugins/executable_mem_perc @@ -0,0 +1,6 @@ +#!/bin/bash + +this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +source "$this_dir/common" + +free | grep Mem | awk '{ printf("%.5f", $3/$2) }' | fmt_percent diff --git a/dot_config/tmux/plugins/executable_network b/dot_config/tmux/plugins/executable_network new file mode 100644 index 0000000..2018c35 --- /dev/null +++ b/dot_config/tmux/plugins/executable_network @@ -0,0 +1,44 @@ +#!/bin/bash + +iface="$1" +cmd="$2" + +net_format="%3d" + +this_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" +source "$this_dir/common" + +get_bits() { + awk '{print($1*8)}' "$1" +} + +get_down(){ + get_bits "/sys/class/net/$iface/statistics/rx_bytes" +} + +get_up(){ + get_bits "/sys/class/net/$iface/statistics/tx_bytes" +} + +get_time() { + date +%s.%N +} + +if [[ "$cmd" == "down" ]]; then + bits="$(get_down)" +elif [[ "$cmd" == "up" ]]; then + bits="$(get_up)" +else + echo "Invalid command" + exit 1 +fi + +cached_eval "network_$cmd" "$bits" "$(get_time)" \ + | awk -v f="$net_format" \ + "$(printf '%s' \ + '{x=$1} END { '\ + 'if (x>=1073741824) printf(f " Gib/s", x/1073741824); '\ + 'else if (x>=1048576) printf(f " Mib/s", x/1048576); '\ + 'else if (x>=1024) printf(f " Kib/s", x/1024); '\ + 'else printf(f " bit/s", x)'\ + '}')" diff --git a/dot_config/tmux/tmux.conf.tmpl b/dot_config/tmux/tmux.conf.tmpl index 19f7f6e..fd05330 100644 --- a/dot_config/tmux/tmux.conf.tmpl +++ b/dot_config/tmux/tmux.conf.tmpl @@ -28,8 +28,8 @@ bind | split-window -h # window navigation bind -n M-Tab next-window bind -n M-BTab previous-window -bind -n C-M-Tab swap-window -t +1 \; next-window -bind -n C-M-BTab swap-window -t -1 \; previous-window +bind -n M-] swap-window -t +1 \; next-window +bind -n M-[ swap-window -t -1 \; previous-window bind -n M-| choose-window "join-pane -h -t '%%'" bind -n M-- choose-window "join-pane -v -t '%%'" @@ -117,12 +117,33 @@ bind -T off F12 \ set -u window-status-current-style \;\ refresh-client -S -mem_perc="free | grep Mem | awk '{ printf(\"%.2f%%\", $3/$2 * 100.0) }'" - -set -g status-right "CPU: #($XDG_CONFIG_HOME/tmux/plugins/cpu_perc) $status_sep RAM: #($mem_perc) " - +# if on local machine, put bar at top, if not put bar at bottom with some nice +# stats that the local machine already has via conky {{- if eq .chezmoi.hostname "petrucci4prez" }} set -g status-position top {{- else }} + +# TODO this is super annoying +{{- if eq .chezmoi.hostname "portnoy4prez" }} +iface="enp0s31f6" +{{- else if eq .chezmoi.hostname "mangini4mayor" }} +iface="enp3s0" +{{- else if eq .chezmoi.hostname "harrison4hegemon" }} +iface="eth0" +{{- else if eq .chezmoi.hostname "peart4prez" }} +iface="ens3" +{{- else if eq .chezmoi.hostname "myung4mayor" }} +iface="extern1" +{{- end }} + +down="#($XDG_CONFIG_HOME/tmux/plugins/network $iface down)" +up="#($XDG_CONFIG_HOME/tmux/plugins/network $iface up)" +net="NET(↑/↓): $up$down" +cpu="CPU: #($XDG_CONFIG_HOME/tmux/plugins/cpu_perc)" +mem="RAM: #($XDG_CONFIG_HOME/tmux/plugins/mem_perc)" + set -g status-position bottom +set -g status-right-length 64 +set -g status-right "$net $status_sep $cpu $status_sep $mem " + {{ end }}