英文:
Getting Input from User with placeholder
问题
我目前正在用Rust编写一个简单的记事本应用程序。
在主循环中,用户被要求插入、删除或更改行。
当用户更改一行时,我想预填充行的内容,以便用户更容易编辑行。
类似于这样(下划线表示终端光标。用户在选择行后没有输入任何内容):
内容:
1. 测试
2. 123
操作(更改、插入、删除) --> C
哪一行 --> 2
--> 123_
我找到了一些可能有帮助的Crossterm和Termion包。Ncurses也可能有帮助,但我宁愿不使用它。
英文:
I'm currently writing a simple Notepad app in Rust.
During the main loop, the user is asked to insert, delete, or change lines.
When the user changes a line, I'd like to pre-fill the content of the line so that the user can edit a line easier.
Something like this (The underscore represents the terminal cursor. The user has not made any input after selecting the line):
Content:
1. Test
2. 123
Action(Change, Insert, Delete) --> C
Which line --> 2
--> 123_
I've found some crates that could help: Crossterm and Termion. Ncurses could help too, but I'd prefer not to use it.
答案1
得分: 0
如果你不想使用完整的 TUI 框架,比如 ratatui
,或者更低级的像 termion
这样处理终端的 crate,你仍然可以使用 rustyline
来进行基本的带行编辑的输入。具体用于编辑预填充行的情况,你可以使用 readline_with_initial
:
let mut rl = rustyline::DefaultEditor::new()?;
let line = rl.readline_with_initial("-->", ("123", ""))?;
英文:
If you don't want to use a full TUI crate like ratatui
or a lower-level "curses"-like terminal handling crate like termion
, you can still use rustyline
for basic input with line editing. Specifically for editing a pre-filled line, you would use readline_with_initial
:
let mut rl = rustyline::DefaultEditor::new()?;
let line = rl.readline_with_initial ("-->", ("123", ""))?;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论