prompt-toolkit / python-prompt-toolkit

Library for building powerful interactive command line applications in Python
https://python-prompt-toolkit.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
9.37k stars 716 forks source link

Completer example #203

Open blark opened 8 years ago

blark commented 8 years ago

It would be really nice to have a basic and well documented completer example. I hacked something together from the other apps around based on prompt toolkit, it's here: https://gist.github.com/blark/46d6befc51909c7db4e0 But this only completes a list of words.

What I'm really interested in is context dependent recommendations... changing the recommendation based on the last word typed. I could probably figure out a way to do it, but the code would be ugly -- I'd love to see a clean example. THANKS!

jonathanslenders commented 8 years ago

I'll put it on my to-do list. Thank for the request!

blark commented 8 years ago

Thank you so much. I think I figured it out, I updated my gist (https://gist.github.com/blark/46d6befc51909c7db4e0) it now does context-dependent completions, but I'm probably doing it poorly. I would love to hear your feedback... Or if I'm not doing it poorly I can add it as an example and send a pull request :)

ghost commented 7 years ago

@jonathanslenders I too am interested in such a feature. I'd also expand on @blark's request to recommend based on sequence of all words leading to the current word. This would allow building a complete command hierarchy and context.

For example, you could feed in a dictionary of commands:

colors = ["red", "green", "blue", "cyan", "magenta", "yellow"]
styles = ["normal", "bright"]

commands = {
    "set": {
        "color": {
            "fore": colors,
            "back": colors,
        },
        "style": styles
    },
    "get": {
        "color": ["fore", "back"],
        "style": None
    },
    "show": ["colors", "styles"]
}

prompt = prompt(">>", completer=NestedCompleter(commands))

I'd like to know if you are currently considering/working/fancy a PR on something like this?

jonathanslenders commented 7 years ago

Hi @pshirali,

This functionality for the example you give is definitely useful. However, this is something that should be possible to build on top of prompt_toolkit, rather than inside of prompt_toolkit. It would be good for this to start as a separate project that implement the Completer interface. If it's proven to work well and useful for many people, then maybe we can merge it inside.

Actually, I also prefer to keep prompt_toolkit (the "core") to stay as small as possible, but have other people create tools around that.

Jonathan

jonathanslenders commented 7 years ago

@blark: I noticed that it is a year ago that I replied, sorry about that. Is the question still relevant?

blark commented 7 years ago

I learn best by example, I always like to see examples of best practice to understand how to start building something correctly. I have managed to figure out a solution, but would still love to see a very simple example of multi-level completions in the repo. But maybe that's just me...

BlueRhino commented 5 years ago

I implemented a NestedCompleter.It's here:https://gitee.com/BlueRhino/codes/q9yvef1whnmagdk3z0us259