aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--man/cpt-search.141
-rwxr-xr-xsrc/cpt-search66
2 files changed, 89 insertions, 18 deletions
diff --git a/man/cpt-search.1 b/man/cpt-search.1
index f59f7b9..299fed6 100644
--- a/man/cpt-search.1
+++ b/man/cpt-search.1
@@ -5,8 +5,15 @@
.Nd search for cpt packages
.Sh SYNOPSIS
.Nm
-.Op Fl dso
-.Op Ar query
+.Op Fl ds
+.Ar package...
+.Nm
+.Fl o
+.Op Fl ds
+.Nm
+.Fl q
+.Op Fl Fds
+.Ar query
.Sh DESCRIPTION
.Nm
can be used to search packages. Glob characters can also be used in the search.
@@ -17,6 +24,10 @@ The options are as follows:
Do not search the installed package database.
.It Fl s , -single
Only show the first instance of a package.
+.It Fl q , -query
+Search packages making use of package descriptions.
+.It Fl F , -fixed
+Run query mode interpreting the given pattern as a fixed string
.It Fl o , -others
Use the current directory as the package and show other instances of that
package.
@@ -24,9 +35,31 @@ package.
Show help message
.It Fl v , -version
Print version information
-.It Fl -verbose
-Be more verbose
.El
+.Pp
+The program has three modes of operations. The default operation is to search
+for the packages given as positional arguments.
+.Pp
+If the
+.Fl o
+flag is specified,
+.Nm
+will use the name of the current directory to search for instances of other
+packages with the same name.
+.Pp
+If the
+.Fl q
+flag is specified,
+.Nm
+will search through the name and description of packages using the given
+.Ar query ,
+and run a case-insensitive search through
+.Xr grep 1 .
+If additionally the
+.Fl F
+flag is given, the given
+.Ar query
+will be considered a fixed string.
.Sh EXAMPLES
Below are usage examples for
.Nm ,
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