如何更改termios配置,以便当用户按下< tab>时getc()立即返回钥匙? [英] How to change termios configuration, so that getc() immediately returns when user presses <tab> key?

查看:94
本文介绍了如何更改termios配置,以便当用户按下< tab>时getc()立即返回钥匙?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的CLI应用程序实现自动完成功能.仅当输入以下字符列表时,才返回getc()的默认行为:NEW_LINE或EOF.我想将TAB添加到此列表中,以便可以触发自动完成算法.

I want to implement auto-completion feature for my CLI application. The default behavior of getc() is returning only when the following list of characters are entered: NEW_LINE or EOF. I want to add TAB to this list so that I can trigger my auto-completion algorithm.

有没有办法做到这一点,例如使用termios?编辑行库(http://www.thrysoee.dk/editline/)可以执行此操作,但是我不知道它是如何执行的?

Is there a way to do it, for instance, using termios? The editline library (http://www.thrysoee.dk/editline/) can do it but I could not figure how it does?

推荐答案

在"UNIX环境中的高级编程"第二版中,终端IO的处理大约需要40页. (termios.c_cc [EOL]和termios.c_cc [EOL2])字符具有其他字符,例如\ n.

Handling of terminal IO takes about 40 pages in the second edition of "Advanced Programming in the UNIX Environment"... Rapidly, you can set the eol and eol2 (termios.c_cc[EOL] and termios.c_cc[EOL2]) characters to have additional characters behaving like \n.

您甚至可以尝试使用stty

You can even try this with stty

$ cat -
abc\tdef
abc\tdef
^d
$stty eol ^i
abc\tabc\tdef
def
^d

如何在程序中进行操作的示例(在实践中,请不要忘记进行错误处理并在结束时,挂起时,在发出信号时等等,以恢复原始状态……这就是为什么使用打包的库来执行此操作的原因更好,对于健壮的应用程序,有很多细节可以解决问题.

Example of how to do it in a program (in practice, don't forget error handling and restoring the original state at end, when suspended, when signaled, and so on... that's why using a packaged library to do it is better, there are a lot of details to get right for a robust application).

struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_cc[EOL] = '\t';
tcsetattr(STDION_FILENO, TCSAFLUSH, &term);

这篇关于如何更改termios配置,以便当用户按下< tab>时getc()立即返回钥匙?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆