使用占位符从用户获取输入

huangapple go评论78阅读模式
英文:

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", ""))?;

huangapple
  • 本文由 发表于 2023年8月4日 05:13:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831638.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定