clarkperkins / click-shell

An extension to click that easily turns your click app into a shell utility
BSD 3-Clause "New" or "Revised" License
90 stars 17 forks source link

Support for changeable prompt #1

Closed clarkperkins closed 4 years ago

clarkperkins commented 8 years ago

We want to be able for the cmd prompt line to be able to dynamically change during runtime.

brrzap commented 7 years ago

i'm currently using this hack for dynamic prompts:

shell = make_click_shell(...)
# save shell object before shell.cmdloop()
click.get_current_context().meta['myshellobj'] = shell
shell.cmdloop()   

# update prompt later
click.get_current_context().meta['myshellobj'].prompt = newprompt

works in my use case, which doesn't change the prompt all that often.

dan1994 commented 5 years ago

I managed to make this work in a very simple manner by changing the get_prompt method in ClickCmd:

def get_prompt(self):
    try:
        return self.ctx.obj['prompt']
    except KeyError:
        return self.prompt

In this solution, the user can set a value to the prompt by settingctx.obj['prompt'] which will take precedence over self.prompt.

Does this seem like a good option?

clarkperkins commented 4 years ago

I've decided to implement this by allowing the prompt argument to be either a string or a callable. If it's a callable, it will be called each time a prompt is printed, and the return value will be the prompt. I think that should be flexible enough to fix pretty much anything!

It's implemented in #18.

dan1994 commented 4 years ago

Awesome! Thanks!