fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
3c468054b
master
1# Execute an `iwctl ... list` command and parse output 2function __iwctl_filter -w iwctl 3# set results "iwctl $cmd list | tail -n +5" 4# if test -n "$empty" 5# set -a results "| string match --invert '*$empty*'" 6# end 7# eval "$results" | awk '{print $2}' 8# awk does not work on multiline entries, therefore we use string match, 9# which has the added benefit of filtering out the `No devices in ...` lines 10 11argparse -u'all-columns&' --$argv 12 13# remove color escape sequences 14set -l results( iwctl $argv_opts --$argv | string replace -ra'\e\[[\d;]+m' '' ) 15# calculate column widths 16set -l headers$results [ 3 ] 17# We exploit the fact that all column labels will have >2 space to the left, and inside column labels there is always only one space. 18set -l leading_ws( string match -r"^ *" --$headers | string length) 19set -l column_widths( string match -a -r'(?<= )\S.*?(?: (?=\S)|$)' --$headers | string length) 20 21if set -ql _flag_all_columns 22for linein ( string match" *" --$results [ 5 .. ] | string sub -s( math $leading_ws +1 )) 23for column_widthin $column_widths 24printf %s\t ( string sub -l$column_width --$line | string trim -r) 25set line( string sub -s( math $column_width +1 ) --$line ) 26end 27printf "\n" 28end 29else if set -q column_widths[ 1 ] 30# only take lines starting with ` `, i.e., no `No devices ...` 31# then take the first column as substring 32string match" *" $results [ 5 .. ] | string sub -s( math $leading_ws +1 ) -l$column_widths [ 1 ] | string trim -r 33end 34end 35 36function __iwctl_match_subcoms 37set -l match( string split --no-empty" " --$argv ) 38 39set argv( commandline -pxc) 40# iwctl allows to specify arguments for username, password, passphrase and dont-ask regardless of any following commands 41argparse -u'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask --$argv 42set argv$argv [ 2 .. ] 43 44if test ( count $argv ) != ( count $match ) 45return 1 46end 47 48while set -q argv[ 1 ] 49string match -q --$match [ 1 ] $argv [ 1 ] 50or return 1 51set -e match[ 1 ] argv[ 1 ] 52end 53end 54 55function __iwctl_connect 56set argv( commandline -pxc) 57# remove all options 58argparse -u'u/username=' 'p/password=' 'P/passphrase=' v/dont-ask --$argv 59# station name should now be the third argument (`iwctl station <wlan>`) 60for networkin ( __iwctl_filter station$argv [ 3 ] get-networks rssi-dbms --all-columns) 61set network( string split\t --$network ) 62set -l strength" $network [ 3 ] " 63# This follows iwctls display of * to **** 64# https://git.kernel.org/pub/scm/network/wireless/iwd.git/tree/client/station.c?id=4a0a97379008489daa108c9bc0a4204c1ae9c6a8#n380 65if test $strength -ge -6000 66set strength4 67else if test $strength -ge -6700 68set strength3 69else if test $strength -ge -7500 70set strength2 71else 72set strength1 73end 74 75printf "%s\t[%s] - %s\n" " $network [ 1 ] " ( string repeat -n$strength '*' | string pad -rw4 -c -) " $network [ 2 ] " 76end 77end 78 79# The `empty` messages in case we want to go back to using those 80# set ad_hoc '(__iwctl_filter ad-hoc "No devices in Ad-Hoc mode available.")' 81# set adapter '(__iwctl_filter adapter)' 82# set ap '(__iwctl_filter ap "No devices in access point mode available.")' 83# set device '(__iwctl_filter device)' 84# set dpp '(__iwctl_filter dpp "No DPP-capable devices available")' 85# set known_networks '(__iwctl_filter known-networks)' 86# set station '(__iwctl_filter station "No devices in Station mode available.")' 87# set wsc '(__iwctl_filter wsc "No WSC-capable devices available")' 88 89complete -f iwctl 90 91# Options 92complete -c iwctl -s h -l help 93complete -c iwctl -s p -l password -rf 94complete -c iwctl -s u -l username -rf 95complete -c iwctl -s P -l passphrase -rf 96complete -c iwctl -s v -l dont-ask -d"Don't ask for missing credentials" 97 98# Subcommand 99complete -c iwctl -n __iwctl_match_subcoms\ 100 -a"ad-hoc adapter ap debug device dpp exit help known-networks quit station version wsc" 101 102# ad-hoc 103complete -c iwctl -n'__iwctl_match_subcoms ad-hoc' -a list -d"List devices in Ad-Hoc mode" 104complete -c iwctl -n'__iwctl_match_subcoms ad-hoc' -a"(__iwctl_filter ad-hoc list)" 105complete -c iwctl -n'__iwctl_match_subcoms "ad-hoc *"' -n'not __iwctl_match_subcoms ad-hoc list' -a start -d"Start or join an Ad-Hoc network" 106complete -c iwctl -n'__iwctl_match_subcoms "ad-hoc *"' -n'not __iwctl_match_subcoms ad-hoc list' -a start_open -d"Start of join an open Ad-Hoc network" 107complete -c iwctl -n'__iwctl_match_subcoms "ad-hoc *"' -n'not __iwctl_match_subcoms ad-hoc list' -a stop -d"Leave an Ad-Hoc network" 108 109# adapter 110complete -c iwctl -n'__iwctl_match_subcoms adapter' -a list -d"List adapters" 111complete -c iwctl -n'__iwctl_match_subcoms adapter' -a"(__iwctl_filter adapter list)" 112complete -c iwctl -n'__iwctl_match_subcoms "adapter *"' -n'not __iwctl_match_subcoms adapter list' -a show -d"Show adapter info" 113complete -c iwctl -n'__iwctl_match_subcoms "adapter *"' -n'not __iwctl_match_subcoms adapter list' -a set-property -d"Set property" 114# TODO implement completions for `properties`, i.e. all rows with `*` in first column 115 116# ap 117complete -c iwctl -n'__iwctl_match_subcoms ap' -a list -d"List devices in AP mode" 118complete -c iwctl -n'__iwctl_match_subcoms ap' -a"(__iwctl_filter ap list)" 119complete -c iwctl -n'__iwctl_match_subcoms "ap *"' -n'not __iwctl_match_subcoms ap list' -a start -d"Start an access point" 120complete -c iwctl -n'__iwctl_match_subcoms "ap *"' -n'not __iwctl_match_subcoms ap list' -a start-profile -d"Start an access point based on a disk profile" 121complete -c iwctl -n'__iwctl_match_subcoms "ap *"' -n'not __iwctl_match_subcoms ap list' -a stop -d"Stop a started access point" 122complete -c iwctl -n'__iwctl_match_subcoms "ap *"' -n'not __iwctl_match_subcoms ap list' -a show -d"Show AP info" 123complete -c iwctl -n'__iwctl_match_subcoms "ap *"' -n'not __iwctl_match_subcoms ap list' -a get-networks -d"Get network list after scanning" 124 125# debug 126complete -c iwctl -n'__iwctl_match_subcoms "debug *"' -a connect -d"Connect to a specific BSS" 127complete -c iwctl -n'__iwctl_match_subcoms "debug *"' -a roam -d"Roam to a BSS" 128complete -c iwctl -n'__iwctl_match_subcoms "debug *"' -a get-networks -d"Get networks" 129complete -c iwctl -n'__iwctl_match_subcoms "debug *"' -a autoconnect -d"Set autoconnect property" 130complete -c iwctl -n'__iwctl_match_subcoms "debug * autoconnect"' -a"on off" -d"Set autoconnect property" 131 132# device 133complete -c iwctl -n'__iwctl_match_subcoms device' -a list -d"List devices" 134complete -c iwctl -n'__iwctl_match_subcoms device' -a"(__iwctl_filter device list)" 135complete -c iwctl -n'__iwctl_match_subcoms "device *"' -n'not __iwctl_match_subcoms device list' -a show -d"Show device info" 136complete -c iwctl -n'__iwctl_match_subcoms "device *"' -n'not __iwctl_match_subcoms device list' -a set-property -d"Set property" 137# TODO implement completions for `properties`, i.e. all rows with `*` in first column 138 139# dpp 140complete -c iwctl -n'__iwctl_match_subcoms dpp' -a list -d"List DPP-capable devices" 141complete -c iwctl -n'__iwctl_match_subcoms dpp' -a"(__iwctl_filter dpp list)" 142complete -c iwctl -n'__iwctl_match_subcoms "dpp *"' -n'not __iwctl_match_subcoms dpp list' -a start-enrollee -d"Starts a DPP Enrollee" 143complete -c iwctl -n'__iwctl_match_subcoms "dpp *"' -n'not __iwctl_match_subcoms dpp list' -a start-configurator -d"Starts a DPP Configurator" 144complete -c iwctl -n'__iwctl_match_subcoms "dpp *"' -n'not __iwctl_match_subcoms dpp list' -a stop -d"Aborts a DPP operations" 145complete -c iwctl -n'__iwctl_match_subcoms "dpp *"' -n'not __iwctl_match_subcoms dpp list' -a show -d"Show the DPP state" 146 147# known-networks 148# TODO Does not support SSIDs ending/starting on whitespace. Not sure how to fix. 149complete -c iwctl -n'__iwctl_match_subcoms known-networks' -a list -d"List known networks" 150complete -c iwctl -n'__iwctl_match_subcoms known-networks' -a"(__iwctl_filter known-networks list)" 151complete -c iwctl -n'__iwctl_match_subcoms "known-networks *"' -n'not __iwctl_match_subcoms known-networks list' -a forget -d"Forget a known network" 152complete -c iwctl -n'__iwctl_match_subcoms "known-networks *"' -n'not __iwctl_match_subcoms known-networks list' -a show -d"Show known network" 153complete -c iwctl -n'__iwctl_match_subcoms "known-networks *"' -n'not __iwctl_match_subcoms known-networks list' -a set-property -d"Set property" 154 155# station 156complete -c iwctl -n'__iwctl_match_subcoms station' -a list -d"List devices in Station mode" 157complete -c iwctl -n'__iwctl_match_subcoms station' -a"(__iwctl_filter station list)" 158complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a connect -d"Connect to network" 159complete -c iwctl -n'__iwctl_match_subcoms "station * connect"' -a"(__iwctl_connect)" -d"Connect to network" --keep-order 160complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a connect-hidden -d"Connect to hidden network" 161complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a disconnect -d Disconnect 162complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a get-networks -d"Get networks" 163complete -c iwctl -n'__iwctl_match_subcoms "station * get-networks"' -a"rssi-dbms rssi-bars" 164complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a get-hidden-access-points -d"Get hidden APs" 165complete -c iwctl -n'__iwctl_match_subcoms "station * get-hidden-access-points"' -a rssi-dbms 166complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a scan -d"Scan for networks" 167complete -c iwctl -n'__iwctl_match_subcoms "station *"' -n'not __iwctl_match_subcoms station list' -a show -d"Show station info" 168 169# wsc 170complete -c iwctl -n'__iwctl_match_subcoms wsc' -a list -d"List WSC-capable devices" 171complete -c iwctl -n'__iwctl_match_subcoms wsc' -a"(__iwctl_filter wsc list)" 172complete -c iwctl -n'__iwctl_match_subcoms "wsc *"' -n'not __iwctl_match_subcoms wsc list' -a push-button -d"PushButton Mode" 173complete -c iwctl -n'__iwctl_match_subcoms "wsc *"' -n'not __iwctl_match_subcoms wsc list' -a start-user-pin -d"PIN mode" 174complete -c iwctl -n'__iwctl_match_subcoms "wsc *"' -n'not __iwctl_match_subcoms wsc list' -a start-pin -d"PIN mode with generated PIN" 175complete -c iwctl -n'__iwctl_match_subcoms "wsc *"' -n'not __iwctl_match_subcoms wsc list' -a cancel -d"Aborts WSC operations"