-
Notifications
You must be signed in to change notification settings - Fork 1
/
flatpak-search-install
executable file
·123 lines (108 loc) · 2.81 KB
/
flatpak-search-install
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
#!/usr/bin/env bash
tab=$'\t'
fzf_args=(
"--delimiter=${tab}"
'--with-nth=1,2'
'--cycle'
'--header=Flatpak Installer'
'--preview=printf "%-8s: %s\n" package {1} app-id {3} version {4} branch {5} branch {6}'
'--preview-label=Package Information'
'--preview-window=top,5'
)
debug_echo() { [[ -z ${verbose:+'is_set'} ]] || printf ':: %s\n' "$1" >&2; }
help_msg() {
cat <<EOF
usage: flatpak-search-install [-h] [-v] [-f | -r REMOTE] [QUERY]
Interactively install a flatpak with fzf.
Command-line Flags:
-h --help Show this message
-v --verbose Show short debug messages
-f Shorthand for '-r flathub'
-r REMOTE Filter only apps in REMOTE
EOF
}
declare -a query
declare remote
while [[ -n $1 ]]; do
case "$1" in
-h | --help)
help_msg
exit 0
;;
-r)
remotes=$(flatpak remotes | cut -f 1)
if grep -q "$2" <<<"$remotes"; then
remote=$2
else
printf '%s is not a valid remote.\n' "$2" >&2
exit 1
fi
shift 2
;;
-f)
remote='flathub'
shift
;;
-v | --verbose)
verbose=1
shift
;;
-*)
printf 'Unknown flag: %s\n' "$1" >&2
help_msg >&2
exit 3
;;
*)
query+=("$1")
shift
;;
esac
done
# If no query provided from cli, ask for input
if [[ ${query[*]} == '' ]]; then
read -rp 'Search: ' -a query
debug_echo "query=(${query[*]})"
fi
# Perform the search with flatpak
debug_echo "Performing search"
if raw_search=$(flatpak search -- "${query[@]}"); then
debug_echo "Search complete"
else
echo 'Could not search flatpak.' >&2
exit 2
fi
# Exit if search has no results
if [[ $raw_search == 'No matches found' ]]; then
echo 'No matches found.' >&2
exit 1
fi
# If a remote is given with '-r', filter for it
if [[ -n $remote ]]; then
debug_echo "Filtering results by remote='${remote}'"
raw_search=$(awk -F "$tab" '$6 == "'"${remote}"'" { print }' <<<"$raw_search")
# Exit if search has no results after filter
if [[ $raw_search == '' ]]; then
echo 'No matches found.' >&2
exit 1
fi
fi
declare -i max_len=0
mapfile -t names < <(awk -F "$tab" '{print $1}' <<<"$raw_search")
for name in "${names[@]}"; do
[[ "${#name}" -gt "$max_len" ]] && max_len=${#name}
done
debug_echo "Longest name is ${max_len} chars."
# Run fzf; output "${reponame}\n${app_id}//${pkgver}\n${pkgname}"; collect into $pkg_info array
mapfile -t pkg_info < <(fzf --tabstop "$(($max_len + 2))" "${fzf_args[@]}" <<<"$raw_search" | awk -v 'FS=\t' -v 'OFS=\n' '{ print $6, $3 "//" $5, $1 }')
if [[ -z ${pkg_info[*]} ]]; then
# User exited without picking anything
echo 'No program selected.'
exit 0
elif [[ -z ${pkg_info[2]} ]]; then
# No pkgname means no pkg available
# Should not be reachable
echo 'No matches found.' >&2
exit 1
fi
# Install the package
exec flatpak install -- "${pkg_info[0]}" "${pkg_info[1]}"