jrmarino / synth

Next D/Ports build tool for live systems (Alternative for Portmaster and Portupgrade tools)
ISC License
255 stars 15 forks source link

Feature Request: zsh autocompletion #12

Closed basbebe closed 8 years ago

basbebe commented 8 years ago

zsh / bash autocompletion would be great to have

yermulnik commented 8 years ago

+1

jrmarino commented 8 years ago

I don't use zsh (I've seen a basic howto but never actually used it myself).

Can you explain exactly what this autocompletion would do in context of synth? what would it be used for, adding cat/portname combinations on build commands?

finally, do either one of you know how to implement it? If so, maybe you could provide a pull request?

Since I don't know what the autocompletions are supposed to do, I'm at a loss of how to begin to implement it (or even if it needs to be an option or installed by default)

basbebe commented 8 years ago

bash and zsh autocomplete commands by pressing [TAB]. So if a user entered synth and then pressed [TAB], a list of the available commands would appear (just as using synth help). If the user entered synth conf[TAB], the command would autocomplete to synth configure. synth install security/libres[TAB] would give security/libressland security/libressl-develto choose and so on.

I'm sorry but I don't know how to implement it.

a quick introduction: https://scottlinux.com/2011/08/19/quick-intro-to-zsh-auto-complete/

http://zsh.sourceforge.net/Doc/Release/Completion-System.html

maybe this points in the right direction: https://blog.jcoglan.com/2013/02/12/tab-completion-for-your-command-line-apps/

yermulnik commented 8 years ago

For example, you can take a look at completions supplied along with ports-mgmt/portupgrade port:

%%EXAMPLESDIR%%/bash/complete.sample
%%EXAMPLESDIR%%/tcsh/complete.sample
jrmarino commented 8 years ago

fyi, I intend to support this but it will be post-1.00. I need help too because mogrify examples on internet don't work for me.

basbebe commented 8 years ago

yay! Looking forward to it!

Maybe this helps a little bit? https://github.com/churchers/vm-bhyve#autocomplete

jrmarino commented 8 years ago

the problem is that zshell doesn't work for me. I've installed it, tried to use it, but the examples on the internet for command completion fail. this "mogrify" thing, zsh doesn't get it.

If I can't use it myself, I can't test it.

basbebe commented 8 years ago

try that: install zsh, start a zsh shell. then enter pkg [SPACE], [TAB]. Then you should see all the commands that pkg offers. This also works with bash.

This also works: freebsd-update -[SPACE][TAB]

yermulnik commented 8 years ago

@jrmarino, if there's a forum, where synth is discussed, I think you can just ask for contribution - there're a lot of people using completions with zsh and bash and a lot of people who know how to create those completions.

jrmarino commented 8 years ago

bax, it doesn't work for me, at least not in DragonFly.

When I hit [SPACE] followed by [TAB], the first time i get a list of files/directories (ls e.g. /usr/ports/ports-mgmt/pkg) and the subsequent tabs just cycle through those.

no commands, just files.

jrmarino commented 8 years ago

i can't make it work in freebsd either, with either zsh or bash.

basbebe commented 8 years ago

that's strange… dose cd[TAB] work?

basbebe commented 8 years ago

I think it worked right away for me… maybe you have to enable it (there should be a setup screen the first time you use it): http://unix.stackexchange.com/questions/108136/run-the-zsh-first-use-wizard http://wp.edsel.nu/2010/06/03/howto-make-a-nice-zsh-command-prompt/

and maybe another resource as to how to implement it in synth: https://github.com/freebsd/portmaster/blob/master/files/zsh-completions

yermulnik commented 8 years ago

FreeBSD + Bash: install shells/bash-completion and follow instructions from pkg-message. By the way, there are Perl ports to deal with Bash completions:

shells/p5-Bash-Completion Extensible system to provide bash completion
shells/p5-Term-Bash-Completion-Generator Generate bash completion scripts
jrmarino commented 8 years ago

this is coming back to me now. zsh had similar pkg-message that was equally cryptic. nothing works out of the box. It's pretty horrible actually.

I'm just running bash manually, so does .bashrc even work?

jrmarino commented 8 years ago

bash doesn't work for me. i modified .bashrc which didn't exist, predictably it didn't work.

zsh pkg-description has this to do $ autoload -U compinstall $ compinstall I tried it and got a huge cryptical menu list.

this is really crap for me. Please provide step by step instructions for somebody that doesn't have it install and doesn't use it. The previous instructions assume way too much prior install and knowledge. So far I'm pretty unimpressed with the amount of hoop jumping involved. It definitely doesn't "just work"

yermulnik commented 8 years ago

You just have to source that file mentioned in pkg-message. You can find "stock" completions in /usr/local/share/bash-completion/completions/. Those will work right after sourcing /usr/local/share/bash-completion/bash_completion.sh. Additional completions (portmaster, pkg etc) could be sourced manually if shipped with the port or if downloaded from the net.

jrmarino commented 8 years ago

that makes bash work, thx

yermulnik commented 8 years ago

As an example:

17:42:31 [yz@nb][w:10][j:0][~]> echo $SHELL
/usr/local/bin/bash
17:42:37 [yz@nb][w:10][j:0][~]> bash --version | head -1
GNU bash, version 4.3.42(1)-release (i386-portbld-freebsd10.2)
17:42:41 [yz@nb][w:10][j:0][~]> . /usr/local/share/bash-completion/bash_completion.sh
17:42:43 [yz@nb][w:10][j:0][~]> portupgrade -<TAB>
-A   -C   -F   -M   -O   -PP  -S   -a   -c   -h   -k   -m   -o   -q   -s   -v   -x
-B   -D   -L   -N   -P   -R   -W   -b   -f   -i   -l   -n   -p   -r   -u   -w   -y
jrmarino commented 8 years ago

what about this one for bash?

https://www.dragonflybsd.org/~marino/_synth.bash

I think you just install it in /usr/local/etc/bash-completion.d/ with mode 755 and the next time bash logs in it is ready.

jrmarino commented 8 years ago

zsh is still a disaster though. Per pkg-message:

autoload -U compinstall (no response) compinstall

I haven't found any existing completion definitions.
If you have some already defined by compinstall, edit the name of the
file where these can be found.  Note that this will only work if they
are exactly the form in which compinstall inserted them.  If you leave
the line as it is, or empty, I won't search.
file> /root/.zshrc

I don't know what the hell to do with that. both curl and pkg have zsh completions at /usr/local/share/zsh/site-functions

I am not reading zshcompsys(1) manpage as suggested by pkg-message. how does anyone get started with this cryptic mess?

jrmarino commented 8 years ago

i finally got zsh completions working and I produced this for synth: https://www.dragonflybsd.org/~marino/_synth

zsh pretended it did not exist until l reinstall poudriere with zsh extensions and then it recognized it. I guess it was cached or something but it was HIGHLY frustrating to say the least.

basbebe commented 8 years ago

@jrmarino THAT I can imagine! Glad you got it working.

If you like it, you could have a look here: https://github.com/robbyrussell/oh-my-zsh or https://github.com/sorin-ionescu/prezto

http://mikebuss.com/2014/02/02/a-beautiful-productive-terminal-experience/

looking forward to the next version

jrmarino commented 8 years ago

can you test it now? Just copy to /usr/local/share/zsh/site-functions

(you might have to install something like poudriere to make it get recognized though)

basbebe commented 8 years ago

works so far!

suggestions for even further improvement:

when I enter synth status[SPACE][TAB] I see the correct message to enter a port or a file's location.

It would be cool if autocompletion would be active when I started to enter a path (e.g. /us[TAB] should autocomplete to /usr/ and so on). Most programs do this. Also it would be great if autocompletion would work if I entered a port (e.g. www/ng would autocomplete to www/nginx and www/nginx-devel.

Portmaster and some other programs' autocompletion do this, I think the needed lines can be found in portmaster's implementation linked to in one of my posts above.

jrmarino commented 8 years ago

Most programs do this.

it did before. the auto-completion broke it

Also it would be great if autocompletion would work if I entered a port (e.g. www/ng would autocomplete to www/nginx and www/nginx-devel. Portmaster and some other programs' autocompletion do this, I think the needed lines can be found in portmaster's implementation linked to in one of my posts above.

maybe you can dig them out? at this time I have no idea how to do that

jrmarino commented 8 years ago

i think i can fix at least the file path (first issue above)

basbebe commented 8 years ago

That should be the needed part:

case "$state" in
  pkgs)
    if [ $with_pkgng -eq 1 ]; then
      array=($(pkg info -aq))
    else
      array=("${PKG_DBDIR:-/var/db/pkg}"/*(/:t))
    fi
    _describe -t packages "installed ports" array
    ;;
  ports)
    _files -X "%Bports%b" -W "${PORTSDIR:-/usr/ports}" -/
    ;;
  pkgs_ports)
    _tags packages ports
    while _tags; do
      if _requested packages; then
        if [ $with_pkgng -eq 1 ]; then
          array=($(pkg info -aq))
        else
          array=("${PKG_DBDIR:-/var/db/pkg}"/*(/:t))
        fi
        _describe -t packages "installed ports" array && ret=0
      fi
      if _requested ports; then
        _files -X "%Bports%b" -W "${PORTSDIR:-/usr/ports}" -/ && ret=0
      fi
      (( ret )) || return ret
    done
    ;;
esc

I don't know what to make of it either, though. Maybe I have some time to check it out next week

jrmarino commented 8 years ago

how about this one? https://raw.githubusercontent.com/jrmarino/synth/master/shell-completers/_synth

basbebe commented 8 years ago

like a charm!

jrmarino commented 8 years ago

it's not perfect. during the rework of bash I realized some thing. zsh shouldn't let you add a path after the first port. e.g.

synth just-build (at this port allow file or port origin) synth just-build file (at this port, allow nothing more) synth just-build port-origin1 (at this point, only allow port origins)

jrmarino commented 8 years ago

These drafts were released with version 0.99_7, request done.