12 个版本
0.3.3 | 2024 年 1 月 4 日 |
---|---|
0.3.2 | 2023 年 4 月 30 日 |
0.3.1 | 2023 年 1 月 7 日 |
0.3.0 | 2020 年 1 月 20 日 |
0.1.2 | 2018 年 11 月 11 日 |
#317 在 Unix APIs 中
用于 notnow
27KB
372 行
rline
rline 是一个提供对 libreadline
的 "备用接口" 便捷封装的 crate。它提供了 libreadline
的优点(它的普遍性、强大的文本输入支持以及可配置性),同时将实际字符输入留给用户(或开发者)。
这对于希望根据自己的条件处理输入,并且只利用 libreadline
的一部分文本输入需求的基于终端(但不一定是基于命令行)的应用程序来说,可以是一个强大的工具。但对于通常根本不使用基于文件流输入(或终端)的图形应用程序来说,这也使得它们能够使用 libreadline
来提供通常仅在命令行上才能体验到的输入体验。
使用方法
将 crate 集成到应用程序或库中通常包含以下部分
// Create a "context" for interacting with libreadline. Contexts are
// isolated from each other, such that you can keep input state and
// history around on a per-object basis (for example, per text input
// field).
let mut rl = rline::Readline::new();
// ...
// Feed data to libreadline and check what the result is. Globally
// configured settings (e.g., via /etc/inputrc) are honored. The result
// is either a completed line or `None`, if editing is still in
// progress.
if let Some(line) = rl.feed(&raw_input) {
// We got a completed line. Work with to it.
my_process(&line);
} else {
// Editing is still in progress. Depending on the use-case we may want
// to look at the current line input so far as well as the cursor
// position, for example, to update the screen accordingly.
rl.peek(|line_so_far, cursor| {
my_display(&line_so_far, cursor)
});
};
// ...
// If the user supplied text out-of-band, e.g., by pasting it via a
// cursor based input device, the libreadline state can be adjusted
// accordingly:
let new_line = CStr::from_bytes_with_nul(b"copied-and-pasted\0").unwrap();
let new_cursor = 6;
let clear_undo = true;
rl.reset(new_line, new_cursor, clear_undo);
请注意,rline 需要 libreadline
在系统上可用。它不支持如 libedit
这样的替代行编辑实现。
示例
一个 可编译示例 展示了如何使用 crate 为基于命令行的应用程序提供行编辑支持。
notnow 程序(可选)使用 rline crate 在其基于终端的 UI 中输入文本。另一个示例集成可以在那里找到。
依赖关系
~60KB