sharkdp / shell-functools

Functional programming tools for the shell
MIT License
1.19k stars 49 forks source link

Add shell-completion for available function names #22

Open sharkdp opened 6 years ago

sharkdp commented 6 years ago
ls | map <TAB>
rpdeo commented 6 years ago

here is a sample completion function for bash

#!/bin/bash
# bash completion for shell-functools

ft_functions="$(ft-functions | sed -E 's/\[[0-9]+m//g' | cut -f1 -d' ')"

function _complete_ft() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="-h --help -c --column"

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
    else
        COMPREPLY=( $(compgen -W "${ft_functions}" -- ${cur}) )
        return 0
    fi
}
complete -F _complete_ft map
complete -F _complete_ft filter
complete -F _complete_ft foldl

Install to /etc/bash_completions.d/shell-functools.sh See here for an intro: https://debian-administration.org/article/317/An_introduction_to_bash_completion_part_2

Interesting project..., cheers!

sharkdp commented 6 years ago

@rpdeo Cool, thanks!

We should probably add an option to ft-functions that only outputs the function names without any ANSI codes and type annotations (actually, it would be super-cool to additionally show the type annotations in the tab-completion without pasting it on the command line, in a similar way how command line options are shown with the help text).

rpdeo commented 6 years ago

You are welcome!

I have not looked at the python code yet to fully understand the Command class, but am pretty sure it can be extended well with python modules like prompt_toolkit, cliff and click. If I come up with something along those lines that might be worth merging, will let you know.