kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.53k stars 176 forks source link

User-specified continuation prompt #793

Closed zao111222333 closed 1 month ago

zao111222333 commented 1 month ago

I believe the user-specified continuation prompt is a crucial feature, especially in scenarios where indentation is important (such as Python shell). I have implemented this feature by replacing the '\n' character in multi-line input with \n+continuation_prompt in display phase (in Renderer::refresh_line). This approach can cause a misalignment between the displayed cursor position and the actual editing position. To address this, I used State::continuation_prompt_move_cursor to correct these misalignments.

Additionally, I have added an example. You can run it with the following command:

cargo run --release --example continuation_prompt --features="custom-bindings derive"

And the demo results: image

However, the current implementation only meets the minimum use case (i.e., continuation prompt works when pasting multi-line text into rustyline), but I have still encountered some bugs:

Since I'm not very familiar with rustyline, I want to share my current implementation and seek help and suggestions here. Thank you!

gwenn commented 1 month ago

See #372 And https://github.com/gwenn/rustyline-notes/blob/master/src/design.md

zao111222333 commented 1 month ago

See #372 And https://github.com/gwenn/rustyline-notes/blob/master/src/design.md

Hi, I agree with "split the input by line" mentioned in design.md, the implementation here is essentially split the input in renderer (soft wrapped), and then re-align cursor.

And I'd like to contribute for continuation prompt if the new Vec<(Range,Style)> API is finished.

gwenn commented 1 month ago

We might be able to fix the Highlighter API like isocline. And also check how isocline handles continuation prompt:

gwenn commented 1 month ago

Also I don't want you to waste your time. So maybe you should check if some alternatives already supports continuation prompt. For example: https://github.com/nushell/reedline/issues/112

zao111222333 commented 1 month ago

Also I don't want you to waste your time. So maybe you should check if some alternatives already supports continuation prompt. For example: nushell/reedline#112

Thank you a lot for your time and information, I will have a try for those alternatives. Also I will still try to implement this feature for rustyline.

gwenn commented 4 weeks ago

For the highlighting part, we might use this library: https://crates.io/crates/ansi-str

A library which provides a set of methods to work with ANSI strings It's an agnostic library in regard to different color libraries.

This method seems to do the job: https://docs.rs/ansi-str/latest/ansi_str/fn.get_blocks.html

gwenn commented 3 weeks ago

Just in case if you are still interested, Could you please have a look at: https://github.com/kkawakam/rustyline/pull/795/commits/6b34d32cb43561856d538bc729ef8cfd0d7de5aa ? I guess it should be feasible like the Split iterator but I am not sure.

zao111222333 commented 3 weeks ago

Just in case if you are still interested,

Could you please have a look at:

https://github.com/kkawakam/rustyline/pull/795/commits/6b34d32cb43561856d538bc729ef8cfd0d7de5aa

?

I guess it should be feasible like the Split iterator but I am not sure.

Thank you for informing, I will try it :)

gwenn commented 3 weeks ago

Just in case if you are still interested, Could you please have a look at: 6b34d32 ? I guess it should be feasible like the Split iterator but I am not sure.

Thank you for informing, I will try it :)

Sorry I mean help me to fix it if you can... (you cannot give it a try because it doesn't type-check)

zao111222333 commented 3 weeks ago

Just in case if you are still interested, Could you please have a look at: 6b34d32 ? I guess it should be feasible like the Split iterator but I am not sure.

Thank you for informing, I will try it :)

Sorry I mean help me to fix it if you can... (you cannot give it a try because it doesn't type-check)

Oh I see, sure I can help to try how to fix it (might be this weekend). Thanks!