如何实现类似 bash 的 Tab 补全?

发布于 2024-08-06 11:43:51 字数 129 浏览 5 评论 0原文

我试图确定系统如何将字符打印到标准输入,即如何打印用户可以删除的字符以及如果用户按“Enter”键则将其视为输入的字符。

我碰巧使用的是 C,但如果解决方案与语言相关,我会感到非常惊讶。

感谢您的任何见解! :D

I'm trying to determine how the system prints characters to standard input -- that is, how it prints characters which the user can delete and which are considered input if the user hits "Enter."

I happen to be using C, but I would be very surprised if the solution were language-dependent.

Thanks for any insights! : D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你曾走过我的故事 2024-08-13 11:43:51

正如 iny 所说,bash 使用 readline 作为输入。源代码可以在此处找到,并且有一个名为complete的文件。 c.

为了回答你的问题,我认为它们实际上并没有打印到标准输入。 Readline 包含某种缓冲区,用于存储用户正在编辑的行的内容,并将完成打印到其中。当用户按下回车键时,缓冲区的内容将被发送到任何想要读取一行的程序,并且在 bash 的情况下,会传递到标准输入中。 (Readline 不会执行此操作 - 使用 readline 的其他程序可能只是将值存储到字符串中以供以后使用。)

As iny says, bash uses readline for its input. The source is available here, and there's a file called complete.c.

To answer your question, I don't think they're actually printed to standard input. Readline contains some kind of buffer for the contents of the line the user is editing, and completion prints into this. When the user presses enter, the contents of the buffer are sent to whatever program wanted to read a line, and in the case of bash, passed along into standard input. (Readline doesn't do this - other programs which use readline might simply store the value into a string for later use.)

一片旧的回忆 2024-08-13 11:43:51

有几个人指出 bash 使用 readline,这是事实,但我认为您真正要问的是它如何能够在您按 Enter 键之前看到您输入的内容。

答案是 ttys(即:终端)可以切换到“原始模式”,其中终端的输入处理被禁用,然后您将看到输入的每个字符。这也禁用了键入字符的自动回显。

有关详细信息,请参阅有关从文件或终端读取单个字符的指南。

Several people have pointed out that bash uses readline, which is true, but I think what you're really asking is how is it able to see what you've typed before you hit enter.

The answer is that ttys (ie: terminals) can be switched into "raw mode", where input processing of the terminal is disabled, and then you'll see every character as it comes in. This also disables automatic echoing of typed characters.

See this guide on Reading a single character from a file or a terminal for more info.

笑叹一世浮沉 2024-08-13 11:43:51

它使用 readline 库来处理输入,readline 提供历史记录和完成信息。

要实际实现完成,需要访问键盘输入处理。完成必须能够修改它所使用的缓冲区。之后,它只是查看当前输入并检查发现了哪些补全。实际的完成逻辑可以通过多种方式工作。

It uses readline library to handle the input and readline provides the history and the completion.

To actually implement completion, access to the keyboard input handling is needed. The completion must be able to modify the buffer used by it. After that it is just about looking at the current input and checking what completions is found. The actual completion logic can work in many ways.

遗忘曾经 2024-08-13 11:43:51

下面是一个通过 readline 实现 Tab 补全的 C 代码片段:

http://github.com/rupa/el

Here's a C snippet that implements tab completion via readline:

http://github.com/rupa/el

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文