Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changing the prompt string dynamically #417

Open
mainrs opened this issue Jul 18, 2020 · 2 comments · May be fixed by #772
Open

Changing the prompt string dynamically #417

mainrs opened this issue Jul 18, 2020 · 2 comments · May be fixed by #772

Comments

@mainrs
Copy link

mainrs commented Jul 18, 2020

Hey!

I have a rather "special" use case. I want the user to enter a string of a specific length n. For convenience I wanted to show the remaining characters that they have while typing. Something like this, with >> being the prompt:

(56) >> User already typed stuff

Is this somehow possible?

@gwenn
Copy link
Collaborator

gwenn commented Jul 18, 2020

No, you cannot but you may use highlight_pompt as a workaround with some limitations:
#369 (comment)

@gwenn
Copy link
Collaborator

gwenn commented Nov 7, 2021

dmlary added a commit to dmlary/rustyline that referenced this issue Apr 6, 2024
rustyline doesn't currently support changing the prompt while in the
core readline loop.  There are a number of open PRs and issues for this
functionality, but all of them appear to be stalled for more than a
year.

Looking at kkawakam#696 and 4ec26e8, the traditional appoach to this is to
provide a reference to a trait object (`Prompt` or `ToString`), but with
that appoach there's no way to cause the prompt to be redrawn for a
change without user input.  This means for these appoaches the prompt
could change without being displayed to the user.

There's an existing mechanism to allow another async task/thread to push
input into the core readline loop, the `ExternalPrinter`.

In this commit, I expand `ExternalPrinter` to add `set_prompt()`.  With
various plumbing, this function results in `wait_for_input` to return
`Cmd::SetPrompt(String)`.

One of key change here is `State.prompt` changes from `&str`
to `String`.  There is a performance hit here from the copy, but
rustyline would need to prompt and receive input hundreds of times per
second for the copy to have a noticable performance inpact.

Added examples/dynamic_prompt.rs to demonstrate the functionality.

Closes kkawakam#417

Related kkawakam#208, kkawakam#372, kkawakam#369, kkawakam#417, kkawakam#598, kkawakam#696
@dmlary dmlary linked a pull request Apr 6, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants