1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
|
'''This module contains helper functions for Fish.'''
from . import helpers
_FISH_QUERY = helpers.FishFunction('fish_query', r'''
# ===========================================================================
#
# This function implements the parsing of options and positionals in the Fish shell.
#
# Usage: __fish_query <OPTIONS> <COMMAND> [ARGS...]
#
# The first argument is a comma-separated list of options that the parser should know about.
# Short options (-o), long options (--option), and old-style options (-option) are supported.
#
# If an option takes an argument, it is suffixed by '='.
# If an option takes an optional argument, it is suffixed by '=?'.
#
# For example:
# __fish_query '-f,--flag,-old-style,--with-arg=,--with-optional=?' [...]
#
# Here, -f, --flag and -old-style don't take options, --with-arg requires an
# argument and --with-optional takes an optional argument.
#
# COMMANDS
# positional_contains <NUM> <WORDS...>
# Checks if the positional argument number NUM is one of WORDS.
# NUM counts from one.
#
# has_option [WITH_INCOMPLETE] <OPTIONS...>
# Checks if an option given in OPTIONS is passed on commandline.
# If an option requires an argument, this command returns true only if the
# option includes an argument. If 'WITH_INCOMPLETE' is specified, it also
# returns true for options missing their arguments.
#
# option_is <OPTIONS...> -- <VALUES...>
# Checks if any option in OPTIONS has a value of VALUES.
#
# num_of_positionals [<OPERATOR> <NUMBER>]
# Checks the number of positional arguments.
# If no arguments are provided, print the total count of positional arguments.
# If two arguments are provided, the first argument should be one of
# the comparison operators: '-lt', '-le', '-eq', '-ne', '-gt', '-ge'.
# Returns 0 if the count of positional arguments matches the
# specified NUMBER according to the comparison operator, otherwise returns 1.
#
# ===========================================================================
set -l positionals
set -l having_options
set -l option_values
#ifdef DEBUG
switch (count $argv)
case 0
echo '%FUNCNAME%: missing OPTIONS argument' >&2
return 1
case 1
echo '%FUNCNAME%: missing COMMAND' >&2
return 1
end
#endif
set -l options $argv[1]
set -e argv[1]
set -l cmd $argv[1]
set -e argv[1]
set -l my_cache_key "$(commandline -b) $options"
if test "$__QUERY_CACHE_KEY" = "$my_cache_key"
set positionals $__QUERY_CACHE_POSITIONALS
set having_options $__QUERY_CACHE_HAVING_OPTIONS
set option_values $__QUERY_CACHE_OPTION_VALUES
else
# =========================================================================
# Parsing of OPTIONS argument
# =========================================================================
#ifdef short_options
set -l short_opts_with_arg
set -l short_opts_without_arg
set -l short_opts_with_optional_arg
#endif
#ifdef long_options
set -l long_opts_with_arg
set -l long_opts_without_arg
set -l long_opts_with_optional_arg
#endif
#ifdef old_options
set -l old_opts_with_arg
set -l old_opts_without_arg
set -l old_opts_with_optional_arg
#endif
set -l option
if test -n "$options"
for option in (string split -- ',' $options)
if false
true
#ifdef long_options
else if string match -qr -- '^--.+=$' $option
set -a long_opts_with_arg (string replace -- '=' '' $option)
else if string match -qr -- '^--.+=\?$' $option
set -a long_opts_with_optional_arg (string replace -- '=?' '' $option)
else if string match -qr -- '^--.+$' $option
set -a long_opts_without_arg $option
#endif
#ifdef short_options
else if string match -qr -- '^-.=$' $option
set -a short_opts_with_arg (string replace -- '=' '' $option)
else if string match -qr -- '^-.=\?$' $option
set -a short_opts_with_optional_arg (string replace -- '=?' '' $option)
else if string match -qr -- '^-.$' $option
set -a short_opts_without_arg $option
#endif
#ifdef old_options
else if string match -qr -- '^-..+=$' $option
set -a old_opts_with_arg (string replace -- '=' '' $option)
else if string match -qr -- '^-..+=\?$' $option
set -a old_opts_with_optional_arg (string replace -- '=?' '' $option)
else if string match -qr -- '^-..+$' $option
set -a old_opts_without_arg $option
#endif
end
end
end
# =========================================================================
# Parsing of options and positionals
# =========================================================================
set -l cmdline (commandline -poc)
set -l cmdline_count (count $cmdline)
set -l argi 2 # cmdline[1] is command name
while test $argi -le $cmdline_count
set -l arg "$cmdline[$argi]"
set -l have_trailing_arg (test $argi -lt $cmdline_count && echo true || echo false)
switch $arg
case '-'
set -a positionals -
case '--'
for argi in (seq (math $argi + 1) $cmdline_count)
set -a positionals $cmdline[$argi]
end
break
case '--*=*'
set -l split (string split -m 1 -- '=' $arg)
set -a having_options $split[1]
set -a option_values "$split[2]"
case '--*'
#ifdef long_options
if contains -- $arg $long_opts_with_arg
if $have_trailing_arg
set -a having_options $arg
set -a option_values $cmdline[(math $argi + 1)]
set argi (math $argi + 1)
end
else
set -a having_options $arg
set -a option_values ''
end
#endif
case '-*'
set -l end_of_parsing false
#ifdef old_options
if string match -q -- '*=*' $arg
set -l split (string split -m 1 -- '=' $arg)
if contains -- $split[1] $old_opts_with_arg $old_opts_with_optional_arg
set -a having_options $split[1]
set -a option_values "$split[2]"
set end_of_parsing true
end
else if contains -- $arg $old_opts_with_arg
set end_of_parsing true
if $have_trailing_arg
set -a having_options $arg
set -a option_values $cmdline[(math $argi + 1)]
set argi (math $argi + 1)
end
else if contains -- $arg $old_opts_without_arg $old_opts_with_optional_arg
set -a having_options $arg
set -a option_values ''
set end_of_parsing true
end
#endif
#ifdef short_options
set -l arg_length (string length -- $arg)
set -l i 2
while not $end_of_parsing; and test $i -le $arg_length
set -l option "-$(string sub -s $i -l 1 -- $arg)"
set -l trailing_chars "$(string sub -s (math $i + 1) -- $arg)"
if contains -- $option $short_opts_without_arg
set -a having_options $option
set -a option_values ''
else if contains -- $option $short_opts_with_arg
set end_of_parsing true
if test -n "$trailing_chars"
set -a having_options $option
set -a option_values $trailing_chars
else if $have_trailing_arg
set -a having_options $option
set -a option_values $cmdline[(math $argi + 1)]
set argi (math $argi + 1)
end
else if contains -- $option $short_opts_with_optional_arg
set end_of_parsing true
set -a having_options $option
set -a option_values "$trailing_chars" # may be empty
end
set i (math $i + 1)
end
#endif
case '*'
set -a positionals $arg
end
set argi (math $argi + 1)
end
set -g __QUERY_CACHE_POSITIONALS $positionals
set -g __QUERY_CACHE_HAVING_OPTIONS $having_options
set -g __QUERY_CACHE_OPTION_VALUES $option_values
set -g __QUERY_CACHE_KEY $my_cache_key
end
# ===========================================================================
# Commands
# ===========================================================================
switch $cmd
#ifdef positional_contains
case 'positional_contains'
if test (count $argv) -eq 0
echo '%FUNCNAME%: positional_contains: argv[3]: missing number' >&2
return 1
end
set -l positional_num $argv[1]
set -e argv[1]
contains -- $positionals[$positional_num] $argv && return 0 || return 1
#endif
#ifdef has_option
case 'has_option'
#ifdef with_incomplete
set -l with_incomplete false
if test $argv[1] = 'WITH_INCOMPLETE'
set with_incomplete true
set -e argv[1]
end
#endif
for option in $having_options
contains -- $option $argv && return 0
end
#ifdef with_incomplete
if $with_incomplete
set -l tokens (commandline -po)
set -e tokens[1]
for option in $argv
if test 2 -eq (string length -- $option)
set option (string sub -l 1 -s 2 -- $option)
string match -q -r -- "^-[A-z0-9]*$option\$" $tokens[-2] && return 0
string match -q -r -- "^-[A-z0-9]*$option.*\$" $tokens[-1] && return 0
else
string match -q -r -- "^$option\$" $tokens[-2] && return 0
string match -q -r -- "^$option(=.*)?\$" $tokens[-1] && return 0
contains -- $tokens[-1] $argv && return 0
end
end
end
#endif
return 1
#endif
#ifdef num_of_positionals
case 'num_of_positionals'
switch (count $argv)
case 0
count $positionals
case 1
echo '%FUNCNAME%: num_of_positionals: $argv[1]: missing operand' >&2
return 1
case 2
if contains -- $argv[1] -lt -le -eq -ne -gt -ge;
test (count $positionals) $argv[1] $argv[2] && return 0 || return 1
else
echo '%FUNCNAME%: num_of_positionals: $argv[1]: unknown operator' >&2
return 1
end
case '*'
echo '%FUNCNAME%: num_of_positionals: too many arguments' >&2
return 1
end
#endif
#ifdef option_is
case 'option_is'
set -l options
set -l values
for arg in $argv
set -e argv[1]
if string match -q -- -- $arg
set values $argv
break
else
set -a options $arg
end
end
if test (count $values) -eq 0
echo '%FUNCNAME%: missing values' >&2
return 1
end
set -l i (count $having_options)
while test $i -ge 1
if contains -- $having_options[$i] $options
if contains -- $option_values[$i] $values
return 0
end
end
set i (math $i - 1)
end
return 1
#endif
#ifdef DEBUG
case '*'
echo '%FUNCNAME%: argv[2]: invalid command' >&2
return 1
#endif
end
''')
_FISH_COMPLETE_FILEDIR = helpers.FishFunction('fish_complete_filedir', r'''
# Function for completing files or directories
#
# Options:
# -d|--description=DESC The description for completed entries
# -c|--comp=STR Complete STR instead of current command line argument
# -D|--directories Only complete directories
# -C|--cd=DIR List contents in DIR
#
# This function is made out of /usr/share/fish/functions/__fish_complete_directories.fish
argparse --max-args 0 'd/description=' 'c/comp=' 'D/directories' 'C/cd=' -- $argv || return 1
set -l comp
set -l desc
if set -q _flag_description[1]
set desc $_flag_description
else if set -g _flag_directories
set desc 'Directory'
end
if set -q _flag_comp[1]
set comp $_flag_comp
else
set comp (commandline -ct | string replace -r -- '^-[^=]*=' '')
end
if set -q _flag_cd[1]
pushd $_flag_cd || return 1
end
set -l files (complete -C"'' $comp")
if set -q _flag_cd[1]
popd
end
if set -q files[1]
if set -q _flag_directories[1]
set files (printf '%s\n' $files | string match -r '.*/$')
end
printf '%s\n' $files\t"$desc"
end
''')
_HISTORY = helpers.FishFunction('history', r'''
builtin history | command grep -E -o -- $argv[1]
''')
# =============================================================================
# Bonus
# =============================================================================
_NET_INTERFACES_LIST = helpers.FishFunction('net_interfaces_list', r'''
if test -d /sys/class/net
command ls /sys/class/net
else if ifconfig -l &>/dev/null
command ifconfig -l # BSD / macOS
else
command ifconfig 2>/dev/null | command awk '/^[a-z0-9]/ {print $1}' | command sed 's/://'
end''')
_TIMEZONE_LIST = helpers.FishFunction('timezone_list', r'''
if ! command timedatectl list-timezones 2>/dev/null
command find /usr/share/zoneinfo -type f |\
command sed 's|/usr/share/zoneinfo/||g' |\
command grep -E -v '^(posix|right)'
end''')
_ALSA_LIST_CARDS = helpers.FishFunction('alsa_list_cards', r'''
set -l card
for card in (command aplay -l | string match -r '^card [0-9]+: [^,]+')
set card (string replace 'card ' '' $card)
set -l split (string split ': ' $card)
if set -q split[2]
printf "%s\t%s\n" $split[1] $split[2]
end
end''')
_ALSA_LIST_DEVICES = helpers.FishFunction('alsa_list_devices', r'''
set -l card
for card in (command aplay -l | string match -r '^card [0-9]+: [^,]+')
set card (string replace 'card ' '' $card)
set -l split (string split ': ' $card)
if set -q split[2]
printf "hw:%s\t%s\n" $split[1] $split[2]
end
end''')
class FishHelpers(helpers.GeneralHelpers):
'''Class holding helper functions for Fish.'''
def __init__(self, function_prefix):
super().__init__(function_prefix, helpers.FishFunction)
self.add_function(_FISH_QUERY)
self.add_function(_FISH_COMPLETE_FILEDIR)
self.add_function(_HISTORY)
self.add_function(_NET_INTERFACES_LIST)
self.add_function(_TIMEZONE_LIST)
self.add_function(_ALSA_LIST_CARDS)
self.add_function(_ALSA_LIST_DEVICES)
|