ack3 icon indicating copy to clipboard operation
ack3 copied to clipboard

Bash completion script?

Open boillodmanuel opened this issue 4 years ago • 8 comments

Hello,

I'm looking for a bash completion script. Does it exist?

boillodmanuel avatar Jan 26 '21 14:01 boillodmanuel

Take a look in the dev/ directory in the repo. There's a shell script that should create one. I don't know anything about it, though. It looks like it wants a Util.pm which looks like it's in t/.

I'm interested to know how it goes. If it needs updates, I'll be glad to put them in.

petdance avatar Jan 26 '21 15:01 petdance

I found this file /dev/generate-completion-scripts.pl

I'm not sure about how execute it.

It tried the following (under bash, not zsh):

$ wget https://github.com/beyondgrep/ack3/blob/dev/dev/generate-completion-scripts.pl
$ chmod u+x generate-completion-scripts.pl
$ ./generate-completion-scripts.pl

# Error message from max os

Can\'t locate Template.pm in @INC (you may need to install the Template module) (@INC contains: t blib/lib /usr/local/Cellar/perl/5.32.0/lib/perl5/site_perl/5.32.0/darwin-thread-multi-2level /usr/local/Cellar/perl/5.32.0/lib/perl5/site_perl/5.32.0 /usr/local/Cellar/perl/5.32.0/lib/perl5/5.32.0/darwin-thread-multi-2level /usr/local/Cellar/perl/5.32.0/lib/perl5/5.32.0 /usr/local/lib/perl5/site_perl/5.32.0) at ./generate-completion-scripts.pl line 8.
BEGIN failed--compilation aborted at ./generate-completion-scripts.pl line 8.

# Try to install missing module
$ apt-get install libtemplate-perl

$  ./generate-completion-scripts.pl

# Error message from debian jessie (docker)
Can\'t locate Util.pm in @INC (you may need to install the Util module) (@INC contains: t blib/lib /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.20.2 /usr/local/share/perl/5.20.2 /usr/lib/x86_64-linux-gnu/perl5/5.20 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.20 /usr/share/perl/5.20 /usr/local/lib/site_perl .) at ./generate-completion-scripts.pl line 9.
BEGIN failed--compilation aborted at ./generate-completion-scripts.pl line 9.

# I don't kwow how to fix that!

boillodmanuel avatar Jan 26 '21 17:01 boillodmanuel

The first complaint is that it can't find Template.pm, which is the Template Toolkit module. You'll need to install that.

You'll also need to clone the entire repo, not just that one script, because it uses the Util.pm module in the repo.

Then run perl dev/generate-completion-scripts.pl completion.bash and you should have a file created for you. I don't know how accurate it is.

petdance avatar Jan 26 '21 17:01 petdance

Definitively can't generate it :

Using docker run -it --rm debian:jessie

apt update
apt install -y git
git clone https://github.com/beyondgrep/ack3.git
cd ack3/

perl dev/generate-completion-scripts.pl completion.bash
# ERROR with Template module
# Install `cpan Template::Toolkit` or `cpan Template` didn't fix it

apt-get install -y libtemplate-perl

perl dev/generate-completion-scripts.pl completion.bash

# ERROR
# Can't locate File/Next.pm in @INC (you may need to install the File::Next module) (@INC contains: t blib/lib /etc/perl /usr/local/lib/x86_64-linux-gnu/perl/5.20.2 /usr/local/share/perl/5.20.2 /usr/lib/x86_64-linux-gnu/perl5/5.20 /usr/share/perl5 /usr/lib/x86_64-linux-gnu/perl/5.20 /usr/share/perl/5.20 /usr/local/lib/site_perl .) at t/Util.pm line 12.
# BEGIN failed--compilation aborted at t/Util.pm line 12.
# Compilation failed in require at dev/generate-completion-scripts.pl line 9.
# BEGIN failed--compilation aborted at dev/generate-completion-scripts.pl line 9.

boillodmanuel avatar Jan 26 '21 17:01 boillodmanuel

If all you want is to be able to run it once and get the output, here you go.

declare -g -a _ack_options
declare -g -a _ack_types=()

_ack_options=(
  "--ackrc" \
  "--after-context" \
  "--bar" \
  "--before-context" \
  "--break" \
  "--cathy" \
  "--color" \
  "--color-filename" \
  "--color-lineno" \
  "--color-match" \
  "--colour" \
  "--column" \
  "--context" \
  "--count" \
  "--create-ackrc" \
  "--dump" \
  "--env" \
  "--files-from" \
  "--files-with-matches" \
  "--files-without-matches" \
  "--filter" \
  "--flush" \
  "--follow" \
  "--group" \
  "--heading" \
  "--help" \
  "--help-types" \
  "--help-colors" \
  "--help-rgb-colors" \
  "--ignore-ack-defaults" \
  "--ignore-case" \
  "--ignore-dir" \
  "--ignore-directory" \
  "--ignore-file" \
  "--invert-match" \
  "--literal" \
  "--man" \
  "--match" \
  "--max-count" \
  "--no-filename" \
  "--no-recurse" \
  "--nobreak" \
  "--nocolor" \
  "--nocolour" \
  "--nocolumn" \
  "--noenv" \
  "--nofilter" \
  "--nofollow" \
  "--nogroup" \
  "--noheading" \
  "--noignore-dir" \
  "--noignore-directory" \
  "--nopager" \
  "--nosmart-case" \
  "--output" \
  "--pager" \
  "--passthru" \
  "--print0" \
  "--recurse" \
  "--show-types" \
  "--smart-case" \
  "--sort-files" \
  "--thpppt" \
  "--type" \
  "--type-add" \
  "--type-del" \
  "--type-set" \
  "--version" \
  "--with-filename" \
  "--word-regexp" \
  "-1" \
  "-A" \
  "-B" \
  "-C" \
  "-H" \
  "-L" \
  "-Q" \
  "-R" \
  "-S" \
  "-c" \
  "-f" \
  "-g" \
  "-h" \
  "-i" \
  "-l" \
  "-m" \
  "-n" \
  "-o" \
  "-r" \
  "-s" \
  "-v" \
  "-w" \
  "-x" \
)

function __setup_ack() {
    local type

    while read LINE; do
        case $LINE in
            --*)
                type="${LINE%% *}"
                type=${type/--\[no\]/}
                _ack_options[ ${#_ack_options[@]} ]="--$type"
                _ack_options[ ${#_ack_options[@]} ]="--no$type"
                _ack_types[ ${#_ack_types[@]} ]="$type"
            ;;
        esac
    done < <(ack --help-types)
}
__setup_ack
unset -f __setup_ack

function _ack_complete() {
    local current_word
    local pattern

    current_word=${COMP_WORDS[$COMP_CWORD]}

    if [[ "$current_word" == -* ]]; then
        pattern="${current_word}*"
        for option in ${_ack_options[@]}; do
            if [[ "$option" == $pattern ]]; then
                COMPREPLY[ ${#COMPREPLY[@]} ]=$option
            fi
        done
    else
        local previous_word
        previous_word=${COMP_WORDS[$(( $COMP_CWORD - 1 ))]}
        if [[ "$previous_word" == "=" ]]; then
            previous_word=${COMP_WORDS[$(( $COMP_CWORD - 2 ))]}
        fi

        if [ "$previous_word" == '--type' -o "$previous_word" == '--notype' ]; then
            pattern="${current_word}*"
            for type in ${_ack_types[@]}; do
                if [[ "$type" == $pattern ]]; then
                    COMPREPLY[ ${#COMPREPLY[@]} ]=$type
                fi
            done
        fi
    fi
}

complete -o default -F _ack_complete ack ack2 ack3 ack-grep

petdance avatar Jan 26 '21 18:01 petdance

which you'd install as /etc/bash_completion.d/ack if i understand correctly.

(I get path completion out of the box so haven't played with this, but providing hints and completion on --options spellings could be useful!)

Do we need to add this scripts pre-reqs to metadata as dev pre-reqs ?

HOW TO should probably be a FAQ item.

n1vux avatar Jan 26 '21 20:01 n1vux

If somebody wants to take that on, go ahead. As far as I'm concerned, dev/ isn't really supported.

petdance avatar Jan 26 '21 23:01 petdance

I tried the completion script and it works well. It's limited to the argument name but that's a good beginning. I noticed that -t is missing.

It worth to be added in source code and mentioned in install documentation

$ ack -<tab>
--ackrc                  --env                    --ignore-directory       --nogroup                --type-add               -f
--after-context          --files-from             --ignore-file            --noheading              --type-del               -g
--bar                    --files-with-matches     --invert-match           --noignore-dir           --type-set               -h
--before-context         --files-without-matches  --literal                --noignore-directory     --version                -i
--break                  --filter                 --man                    --nopager                --with-filename          -l
--cathy                  --flush                  --match                  --nosmart-case           --word-regexp            -m
--color                  --follow                 --max-count              --output                 -1                       -n
--color-filename         --group                  --no-filename            --pager                  -A                       -o
--color-lineno           --heading                --no-recurse             --passthru               -B                       -r
--color-match            --help                   --nobreak                --print0                 -C                       -s
--colour                 --help-colors            --nocolor                --recurse                -H                       -v
--column                 --help-rgb-colors        --nocolour               --show-types             -L                       -w
--context                --help-types             --nocolumn               --smart-case             -Q                       -x
--count                  --ignore-ack-defaults    --noenv                  --sort-files             -R
--create-ackrc           --ignore-case            --nofilter               --thpppt                 -S
--dump                   --ignore-dir             --nofollow               --type                   -c

boillodmanuel avatar Jan 27 '21 20:01 boillodmanuel