aboutsummaryrefslogtreecommitdiff
path: root/src/cpt-search
diff options
context:
space:
mode:
authormerakor <cem@ckyln.com>2021-08-10 23:31:25 +0000
committermerakor <cem@ckyln.com>2021-08-10 23:31:25 +0000
commitce9541cf425c005f48901ab5781e0f56437ad8b2 (patch)
tree36cea41e8e1291c3829ffd7728e8e27934afa35c /src/cpt-search
parent4efb2ffed359b16fbb63002373547d78b1e3cece (diff)
downloadcpt-ce9541cf425c005f48901ab5781e0f56437ad8b2.tar.gz
cpt-search: add description searching mode
FossilOrigin-Name: 065485d1faf9a5ec3ba5951049e2ac7882f1a3cb5587dc713f8c32a347a92b5e
Diffstat (limited to 'src/cpt-search')
-rwxr-xr-xsrc/cpt-search66
1 files changed, 52 insertions, 14 deletions
diff --git a/src/cpt-search b/src/cpt-search
index fbe0b04..9eeac75 100755
--- a/src/cpt-search
+++ b/src/cpt-search
@@ -2,26 +2,64 @@
# Search for a package
parser_definition() {
- setup REST help:usage -- "usage: ${0##*/} [pkg...]"
+ setup REST help:usage -- "usage: ${0##*/} [-dFoqs] [pkg...]"
msg -- '' 'Options:'
flag SEARCH_PATH -d "on:$CPT_PATH" -- "Do not search the installed package database"
flag all -s --single init:=1 on:'' -- "Only show the first instance of a package"
- flag others -o --others -- "Use the current directory as the package" \
+ flag mode -q --query on:2 -- "Search packages making use of package descriptions"
+ flag fflag -F --fixed -- "Run query mode interpreting the given pattern as a" \
+ "fixed string"
+ flag mode -o --others on:1 -- "Use the current directory as the package" \
"and show other instances"
- global_options
+ global_options compact
}
if [ -f ./cpt-lib ]; then . ./cpt-lib; else . cpt-lib; fi
-# The 'all' variable is set by the option parser.
-# shellcheck disable=2154
-case $others in
- '') for pkg; do pkg_find "$pkg" "${all:+all}"; done ;;
- *) pkg_find "${PWD##*/}" all |
- while read -r pkg_dir; do case $pkg_dir in
- "$PWD") ;;
- *) printf '%s\n' "$pkg_dir"
- [ "$all" ] || exit 0
- esac
- done
+
+# The 'all' and 'mode' variables are set by the option parser, and are never
+# modified in the subshell.
+# shellcheck disable=2154,2030,2031
+case $mode in
+ '')
+ # Default mode of operation.
+ for pkg; do pkg_find "$pkg" "${all:+all}"; done
+ ;;
+ 1)
+ # Use the current directory as the package and show other instances.
+ pkg_find "${PWD##*/}" all |
+ while read -r pkg_dir; do
+ case $pkg_dir in
+ "$PWD") ;;
+ *) printf '%s\n' "$pkg_dir"
+ [ "$all" ] || exit 0
+ esac
+ done
+ ;;
+ 2)
+ # Make a partial string search using the name and the description of all
+ # packages. This is a "pretty information" mode, and its output is not
+ # meant to be used in scripting. There is a whole library meant for
+ # scripting.
+ pkg_find \* all |
+ while read -r pkg_dir; do
+ name=${pkg_dir##*/}
+ desc=$(pkg_query_meta "$pkg_dir" description ||:)
+
+ # We pipe the name and description to the given query and
+ # continue if it's not a match
+ printf '%s %s\n' "$name" "$desc" |
+ "$grep" "-iq${fflag:+F}" -- "$1" || continue
+
+ read -r ver rel < "$pkg_dir/version"
+ printf '%b%s%b@%s %s-%s\n %s\n\n' \
+ "$colorb" "$name" "$colre" \
+ "$pkg_dir" \
+ "$ver" "$rel" \
+ "$desc"
+
+ # I don't know why someone use the '-s' flag on this operation
+ # mode, but go ahead.
+ [ "$all" ] || exit 0
+ done
esac