From 8f90ed6d4992f51bb445555d6e826442d36be4f2 Mon Sep 17 00:00:00 2001 From: hal9000 Date: Sun, 23 Aug 2020 17:29:03 -0400 Subject: [PATCH 1/4] linux wifi widget --- internal/p10k.zsh | 93 +++++++++++++++++++++++++++++++++-------------- log.txt | 1 + 2 files changed, 66 insertions(+), 28 deletions(-) create mode 100644 log.txt diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 390d7884..59a10758 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4903,12 +4903,19 @@ prompt_wifi() { (( _p9k__has_upglob )) || typeset -g "_p9k__segment_val_${_p9k__prompt_side}[_p9k__segment_index]"=$_p9k__prompt[len+1,-1] } + _p9k_prompt_wifi_init() { - if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport ]]; then + if [[ -x /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport || -f /proc/net/wireless ]]; then typeset -g _p9k__wifi_on= typeset -g P9K_WIFI_LAST_TX_RATE= typeset -g P9K_WIFI_SSID= - typeset -g P9K_WIFI_LINK_AUTH= + + # possible refactor to set link_auth only on darwin + # or also possible to simply set linux link_auth to empty string and leave this scope as-is + if [[ $_p9k_os == OSX ]]; then + typeset -g P9K_WIFI_LINK_AUTH= + fi + typeset -g P9K_WIFI_RSSI= typeset -g P9K_WIFI_NOISE= typeset -g P9K_WIFI_BARS= @@ -4918,27 +4925,64 @@ _p9k_prompt_wifi_init() { fi } -_p9k_prompt_wifi_compute() { - _p9k_worker_async _p9k_prompt_wifi_async _p9k_prompt_wifi_sync -} - _p9k_prompt_wifi_async() { - local airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport local last_tx_rate ssid link_auth rssi noise bars on out line v state - { - [[ -x $airport ]] || return 0 - out="$($airport -I)" || return 0 - for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do - v=${line#*: } - case $line[1,-$#v-3] in - agrCtlRSSI) rssi=$v;; - agrCtlNoise) noise=$v;; - state) state=$v;; - lastTxRate) last_tx_rate=$v;; - link\ auth) link_auth=$v;; - SSID) ssid=$v;; - esac - done + + { + if [[ $_p9k_os == OSX ]]; then + local airport=/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport + [[ -x $airport ]] || return 0 + out="$($airport -I)" || return 0 + for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do + v=${line#*: } + case $line[1,-$#v-3] in + agrCtlRSSI) rssi=$v;; + agrCtlNoise) noise=$v;; + state) state=$v;; + lastTxRate) last_tx_rate=$v;; + link\ auth) link_auth=$v;; + SSID) ssid=$v;; + esac + done + + elif [[ $_p9k_os == (Linux|Android)]]; then + # iw tools only output 'noise' from a dump that requires superuser and a background service to be running, which probably isn't, so a separate process is needed + # /proc/net/wireless displays noise level up to date, w/o requiring superuser + local proc_less=/proc/net/wireless + [[ -f $proc_less ]] || return 0 + + # this method using iw is over 10x faster than the network manager method in benchmarking + # it's possible some systems are still using 'wireless_tools' (iw's ancestor) but they are long deprecated anyway + local device="$(iw dev | grep 'Interface ' | cut -d\ -f2)" || return 0 + out="$(iw dev $device link)" || return 0 + + # 'running' state guaranteed by 'device' and 'proc_less' var assignment + state='running' + + local proc_out="$(grep $device $proc_less | tr -s ' ')" || return 0 + # using cut is more performant than awk,sed,perl, but I haven't timed against zsh expansion pattern + rssi="${$($proc_out | cut -d\ -f4)%.*}" + noise="$($proc_out | cut -d\ -f5)" + + # it's possible to get boolean from iw to check authorization status from a dump, but getting the method (if any) requires superuser + link_auth="" + + for line in ${${${(f)out}##[[:space:]]#}%%[[:space:]]#}; do + v=${line#*: } + case ${line[1,-$#v-3]} in + SSID) ssid=$v;; + + # formatting here to remove ' dBm' from tail + # note 'rssi' is also assigned up above, but this replacement pattern might be faster than using cut - needs benchmarking + signal) rssi=${v//[^0-9-]/};; + + # formatting here to transform from 'xxx.x MBit/s MCS xx short GI' to 'xxx.x' where x's are integers + # benchmarking shows grep here is extremely fast (under .1 msc even on older hw), but you can change to native zsh pattern if you want + tx\ bitrate) last_tx_rate=$(echo $v | grep -o "^[0-9]+.[0-9]");; + esac + done + fi + if [[ $state != running || $rssi != (0|-<->) || $noise != (0|-<->) ]]; then rssi= noise= @@ -4991,13 +5035,6 @@ _p9k_prompt_wifi_async() { } } -_p9k_prompt_wifi_sync() { - if [[ -n $REPLY ]]; then - eval $REPLY - _p9k_worker_reply $REPLY - fi -} - function _p9k_asdf_check_meta() { [[ -n $_p9k_asdf_meta_sig ]] || return [[ -z $^_p9k_asdf_meta_non_files(#qN) ]] || return diff --git a/log.txt b/log.txt new file mode 100644 index 00000000..e050bad0 --- /dev/null +++ b/log.txt @@ -0,0 +1 @@ +2020/08/23 17:20:27 Micro started From d62961131c57de97bb766d8df41ba83f9cb0ee85 Mon Sep 17 00:00:00 2001 From: David Date: Sun, 23 Aug 2020 17:36:08 -0400 Subject: [PATCH 2/4] Delete log.txt --- log.txt | 1 - 1 file changed, 1 deletion(-) delete mode 100644 log.txt diff --git a/log.txt b/log.txt deleted file mode 100644 index e050bad0..00000000 --- a/log.txt +++ /dev/null @@ -1 +0,0 @@ -2020/08/23 17:20:27 Micro started From 16cb58d15f5bb602bf7a0d09dcf6011544a8fc0e Mon Sep 17 00:00:00 2001 From: sys-lectern Date: Tue, 25 Aug 2020 16:04:47 -0400 Subject: [PATCH 3/4] minor fixes and syntax improvements --- internal/p10k.zsh | 14 +++++++------- log.txt | 1 - 2 files changed, 7 insertions(+), 8 deletions(-) delete mode 100644 log.txt diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 59a10758..985d417a 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -4945,15 +4945,15 @@ _p9k_prompt_wifi_async() { esac done - elif [[ $_p9k_os == (Linux|Android)]]; then + elif [[ $_p9k_os == Linux]]; then # iw tools only output 'noise' from a dump that requires superuser and a background service to be running, which probably isn't, so a separate process is needed # /proc/net/wireless displays noise level up to date, w/o requiring superuser local proc_less=/proc/net/wireless [[ -f $proc_less ]] || return 0 - # this method using iw is over 10x faster than the network manager method in benchmarking - # it's possible some systems are still using 'wireless_tools' (iw's ancestor) but they are long deprecated anyway - local device="$(iw dev | grep 'Interface ' | cut -d\ -f2)" || return 0 + # this method using iw is over 10x faster than the network manager method in benchmarking + # it's possible some systems are still using 'wireless_tools' (iw's ancestor) but they are long deprecated anyway + local device="$(cut -d\ -f2 <<< $(iw dev | grep Interface))" || return 0 out="$(iw dev $device link)" || return 0 # 'running' state guaranteed by 'device' and 'proc_less' var assignment @@ -4961,8 +4961,8 @@ _p9k_prompt_wifi_async() { local proc_out="$(grep $device $proc_less | tr -s ' ')" || return 0 # using cut is more performant than awk,sed,perl, but I haven't timed against zsh expansion pattern - rssi="${$($proc_out | cut -d\ -f4)%.*}" - noise="$($proc_out | cut -d\ -f5)" + rssi="${$(cut -d\ -f4 <<< $proc_out)%.*}" + noise="$(cut -d\ -f5 <<< $proc_out)" # it's possible to get boolean from iw to check authorization status from a dump, but getting the method (if any) requires superuser link_auth="" @@ -4978,7 +4978,7 @@ _p9k_prompt_wifi_async() { # formatting here to transform from 'xxx.x MBit/s MCS xx short GI' to 'xxx.x' where x's are integers # benchmarking shows grep here is extremely fast (under .1 msc even on older hw), but you can change to native zsh pattern if you want - tx\ bitrate) last_tx_rate=$(echo $v | grep -o "^[0-9]+.[0-9]");; + tx\ bitrate) last_tx_rate=$(cut -d\ -f1 <<< $v);; esac done fi diff --git a/log.txt b/log.txt deleted file mode 100644 index e050bad0..00000000 --- a/log.txt +++ /dev/null @@ -1 +0,0 @@ -2020/08/23 17:20:27 Micro started From 6853fcd8e5d1e082ebce26942847743367a7c2a1 Mon Sep 17 00:00:00 2001 From: sys-lectern Date: Tue, 25 Aug 2020 16:13:41 -0400 Subject: [PATCH 4/4] minor fixes and syntax improvements --- internal/p10k.zsh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/p10k.zsh b/internal/p10k.zsh index 985d417a..24d756ac 100644 --- a/internal/p10k.zsh +++ b/internal/p10k.zsh @@ -5035,6 +5035,13 @@ _p9k_prompt_wifi_async() { } } +function _p9k_prompt_wifi_sync() { + if [[ -n $REPLY ]]; then + eval $REPLY + _p9k_worker_reply $REPLY + fi +} + function _p9k_asdf_check_meta() { [[ -n $_p9k_asdf_meta_sig ]] || return [[ -z $^_p9k_asdf_meta_non_files(#qN) ]] || return