saury/saury

260 lines
7.6 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 " "(status -f) $argv
end
echo 'usage:' (status -f) '[options] <operation> [...]'
echo 'operations:'
print_operation "{-h --help}"
print_operation "{-V --version}"
print_operation "{-A --aur-sync} [options] [package(s)]"
echo 'options:'
echo ' -c, --color=<auto/always/never>'
echo ' -v, --verbose'
echo ' -q, --quiet'
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 2
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"
if not test $_flag_quiet
set results (echo $rpc_response | \
jq -r '.results | sort_by(.NumVotes, (.Name | explode | map(-.))) | reverse |
map(.Name, .PackageBase, .Description, .Version,
.OutOfDate != null, .NumVotes, (.Popularity * 100 | round / 100))[]')
if test $_flag_verbose
begin
set -l IFS
set installed_packages (pacman -Q)
end
end
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 (string join -- "" \
(set_color CC3399) "aur/" \
(set_color normal; set_color --bold) (string replace -a -i -- \
"$query" \
(string join -- "" (set_color 33CCB3) \
"$query" \
(set_color normal; set_color --bold)) \
"$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
echo -n (string join -- "" \
(set_color normal; set_color --bold) ' (' \
(set_color CC6733) "+$votes" \
(set_color normal; set_color --bold) ' | ' \
(set_color CC6733) "$popularity" \
(set_color normal; set_color --bold) ')')
if test $_flag_verbose
# not shown by default because it's quite slow
set installed (echo $installed_packages | grep -E "^$name ")
if test "$installed"
echo -n ' [installed'
set installed_version (echo $installed | cut -d' ' -f2)
if test "$package_version" != "$installed_version"
echo -n ": $installed_version"
end
echo -n ']'
end
end
echo
set_color normal
echo (string replace -a -i -- \
"$query" \
(string join -- "" (set_color 33CCB3) \
"$query" \
(set_color normal)) \
" $description")
end
else
set results (echo $rpc_response | \
jq -r '.results | sort_by(.NumVotes, (.Name | explode | map(-.))) | reverse |
map(.Name)[]')
for package in $results
echo $package
end
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
# 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 -g fish_term24bit 1
end
if not test -t 1
function set_color
end
end
end
function main
set -le argv
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' 'c/color=?' 'v/verbose' 'q/quiet' $aur_sync_options -- $argv \
^| sed (string join '' 's/:/:' (set_color red) '/;s/$/' (set_color normal) '/') | head -1
if not set --local --query argv
# if argparse didn't locally set $argv, it encountered an error
exit 1
end
if set --query _flag_color
if test "$_flag_color" = "always"
functions -e set_color
else if test "$_flag_color" = "never"
function set_color
end
end
end
if test $_flag_verbose
set -g _flag_verbose $_flag_verbose
end
if test $_flag_quiet
set -g _flag_quiet $_flag_quiet
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