saury/saury

177 lines
4.9 KiB
Fish
Executable File

#!/usr/bin/fish
set saury_version v0.0.1
set saury_name (basename (status -f))
set saury_authors "Les De Ridder <les@lesderid.net>"
set has_pacman (type pacman ^ /dev/null > /dev/null; and echo 1)
set has_makepkg (type makepkg ^ /dev/null > /dev/null; and echo 1)
set aurweb_rpc_base_url "https://aur.archlinux.org/rpc/"
set aurweb_rpc_version 5
function print_prefix
echo -n "$saury_name: "
end
function error
print_prefix
set_color red
echo $argv
set_color normal
exit 1
end
function niy
print_prefix
set_color cyan
echo "not implemented yet ($argv)"
set_color normal
exit 1
end
function need_root
if not test (id -u) -eq 0
error "you cannot perform this operation unless you are root"
end
end
function print_usage
function print_operation
echo -e "\t"(status -f) $argv
end
echo 'usage:' (status -f) '<operation> [...]'
echo 'operations:'
print_operation "{-h --help}"
print_operation "{-V --version}"
print_operation "{-A --aur-sync} [options] [package(s)]"
end
function print_version
echo $saury_name $saury_version
echo "Copyright © 2019 $saury_authors"
echo
echo "This program may be freely redistributed under the terms of" \
"the University of Illinois/NCSA Open Source License." | fold -sw 60
end
function aur_sync
niy "install package '$argv'"
end
function aur_search
set query $argv
if test (string length "$query") -lt 1
error "search query must be at least 2 characters long"
end
set rpc_response (curl -s "$aurweb_rpc_base_url/?v=$aurweb_rpc_version&type=search&by=name-desc&arg=$query")
set rpc_response_type (echo $rpc_response | jq -r '.type')
if test "$rpc_response_type" = "search"
set results (echo $rpc_response | jq -r '.results | map(.Name, .PackageBase, .Description, .Version, .OutOfDate != null, .NumVotes, (.Popularity * 100 | round / 100))[]')
for i in (seq (math (count $results) / 7))
set name $results[(math $i \* 7 - 6)]
set package_base $results[(math $i \* 7 - 5)]
set description $results[(math $i \* 7 - 4)]
set package_version $results[(math $i \* 7 - 3)]
set out_of_date $results[(math $i \* 7 - 2)]
set votes $results[(math $i \* 7 - 1)]
set popularity $results[(math $i \* 7)]
set_color --bold
echo -n (set_color CC3399)aur/(set_color white)$name
echo -n ' '
if test $out_of_date = "true"; set_color CC334D; else; set_color 33CC66; end
echo -n $package_version
if test "$name" != "$package_base"
set_color 3399CC
echo -n " ($package_base)"
end
set -x fish_term24bit 1
echo -n (set_color white)' ('(set_color CC6733)"+$votes"(set_color white)' | '(set_color CC6733)$popularity(set_color white)')'
echo
set_color normal
echo \t$description
end
else if test "$rpc_response_type" = "error"
set rpc_error (echo $rpc_response | jq -r '.error')
error "an RPC error occurred ("(string trim -c '.' (string lower $rpc_error))")"
else
error "an unexpected RPC response was received"
end
end
function aur_sysupgrade
niy "upgrade installed AUR packages"
end
function on_start
tabs -4
# Detect 24-bit colour support
# (Source: https://github.com/fish-shell/fish-shell/blob/619a248a3525b13ccd12f815cdb2d373e7372292/share/config.fish#L18)
if not set -q STY
and not string match -q -- 'eterm*' $TERM
and begin
set -q KONSOLE_PROFILE_NAME
or string match -q -- "*:*" $ITERM_SESSION_ID
or string match -q -- "st-*" $TERM
or test -n "$VTE_VERSION" -a "$VTE_VERSION" -ge 3600
or test "$COLORTERM" = truecolor -o "$COLORTERM" = 24bit
end
set -q fish_term24bit
or set -g fish_term24bit 1
end
end
function on_exit
tabs -8
end
function main
set aur_sync_options 's/search' 'u/sysupgrade'
set aur_sync_exclusive_options 's,u'
argparse -n $saury_name -x 'V,A' -x $aur_sync_exclusive_options 'h/help' 'V/version' 'A/aur-sync' $aur_sync_options -- $argv ^| sed 's/:/':(set_color red)'/;s/$/'(set_color normal)'/' | head -1
if not set --local --query argv; exit 1; end
if set --query _flag_help
print_usage
else if set --query _flag_version
print_version
else if set --query _flag_aur_sync
if test $_flag_search
aur_search $argv
else if test $_flag_sysupgrade
aur_sysupgrade
else
if not test $argv
error "no targets specified"
else
aur_sync $argv
end
end
else
error "no operation specified (use -h for help)"
end
end
on_start
main $argv
on_exit