-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrowse_packages
executable file
·159 lines (137 loc) · 3.31 KB
/
browse_packages
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
#!/bin/bash
#
# Browse installed packages along with info about them.
#
# Dependencies: coreutils, fzf, less, pacman, pacman-contrib, sed
set -o nounset -o pipefail
readonly L_OPTS="help,version"
readonly OPTS="hVv"
readonly CMD="${0##*/}"
readonly USAGE="${CMD} [OPTION]..."
readonly USAGE_ERR_MSG=$(
cat <<END
Usage: ${USAGE}
Try '${CMD} --help' for more information.
END
)
readonly VERSION_INFO=$(
cat <<END
${CMD} (scripts) 1.0.0
This is free and unencumbered software released into the public domain.
For more information, please refer to <https://unlicense.org/>.
END
)
readonly HELP=$(
cat <<END
SYNOPSIS
${USAGE}
DESCRIPTION
Browse installed packages along with info about them.
Press ENTER on any package for a detailed view that includes files.
OPTIONS
-h, --help
display this help and exit
-V, -v, --version
display version and exit
END
)
#######################################
# Align command according to the 'pacman --info' format
# Arguments:
# cmd - command to align
# name - first column of the format (every character before ':')
#######################################
function align_cmd() {
local -r cmd="$1"
local -r name="$2"
echo "${cmd} | sed '1s/^/${name}: /' | sed '2,\$s/^/ /'"
}
#######################################
# Main function
# Globals:
# CMD - script basename
# HELP - help message
# L_OPTS - long non-positional options (e.g. --help)
# OPTS - short non-positional options (e.g. --h)
# USAGE_ERR_MSG - usage error message
#######################################
function main() {
local opts
opts=$(
getopt \
--options=${OPTS} --longoptions=${L_OPTS} \
--name "${CMD}" \
-- "$@"
)
if (($? != 0)); then
echo "${USAGE_ERR_MSG}"
exit 2
fi
eval set -- "${opts}"
local need_help=false
local need_version=false
while true; do
case "$1" in
-h | --help)
need_help=true
shift
break
;;
-V | -v | --version)
need_version=true
shift
break
;;
--)
shift
break
;;
esac
done
if [[ $need_help == true ]]; then
echo "${HELP}"
exit 0
fi
if [[ $need_version == true ]]; then
echo "${VERSION_INFO}"
exit 0
fi
if (($# != 0)); then
echo "${CMD}: expected 0 arguments; got $#"
echo "${USAGE_ERR_MSG}"
exit 2
fi
local -r files_list_cmd=$(
align_cmd "pacman --query --list --quiet {}" "Package Files "
)
local -r files_status_cmd=$(
align_cmd "LC_ALL=C pacman --query --check --check {} 2>&1" \
"Files Status "
)
local -r package_info_cmd="LC_ALL=C pacman --query --info {} \
| head --bytes=-1"
local -r reversed_tree_cmd=$(
align_cmd "pactree --optional=-1 --reverse {}" "Reversed Tree "
)
local -r preview_cmd="${package_info_cmd} ; ${reversed_tree_cmd}"
local -r on_enter_cmd="\
{ \
${package_info_cmd}; \
${reversed_tree_cmd}; \
${files_list_cmd}; \
${files_status_cmd}; \
} | less"
pacman --query --quiet \
| fzf --preview "${preview_cmd}" \
--bind "enter:execute(${on_enter_cmd})" \
--no-mouse \
--preview-window=right,70% \
--reverse
declare -ir exit_code=$?
if ((exit_code == 130)); then
exit 0
else
exit ${exit_code}
fi
}
main "$@"