aboutsummaryrefslogtreecommitdiff
path: root/src/cpt-search
blob: 9eeac751fad6f6955459ab026db0f67900f308fb (plain)
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
#!/bin/sh -ef
# Search for a package

parser_definition() {
    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 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 compact
}

if [ -f ./cpt-lib ]; then . ./cpt-lib; else . cpt-lib; fi


# 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