NCurses 初始化而不清除屏幕

发布于 2024-07-15 03:19:47 字数 469 浏览 7 评论 0原文

我正在编写一个类似于 shell 的程序。 启动后,会出现提示,您需要输入一些特定于应用程序的命令。

到目前为止,这工作得很好。 但是,我想像 Bash 一样添加对命令历史记录的支持,以便用户可以点击向上或向下箭头并查看以前输入的命令。

我已经包含了 ncurses 库,并且使用 getch() 做了一个简单的 hello world 测试,以确保正确报告向上和向下箭头。

让我困扰的是,我似乎需要调用 initscr() 来清除屏幕,以便我使用 getch() 。

好吧,问题是:

有谁知道如何使用 ncurses getch() 函数而不先调用 initscr() 吗? 如果没有,我可以让它不清除屏幕吗? 基本上,我希望 getch() 的行为与 getchar() 相同,如果这有意义的话。

提前致谢!

编辑:我认为最好的例子是Python如何在交互模式下运行。

I am writing a program that's similar to a shell. Once started up, there's a prompt and you enter in some app-specific commands.

So far this works just fine. However, I want to add support for command history like in Bash, so the user could hit the up or down arrow and see previously entered commands.

I have included the ncurses library, and I have done a simple hello world test with getch() to make sure the up and down arrows are reported correctly.

The thing that's bothering me is that it seems to be a requirement that I call initscr() which will clear the screen in order for me to use getch().

OKAY SO THE QUESTION IS:

Does anybody know a way to use ncurses getch() function without calling initscr() first? If not, can I make it not clear the screen? Basically, I'm looking to have getch() act the same as getchar(), if that makes sense.

Thanks in advance!

EDIT: I think the best example of this is how Python runs in interactive mode.

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

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

发布评论

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

评论(5

原来分手还会想你 2024-07-22 03:19:47

Curses 希望完全控制屏幕,并优化对屏幕的写入,尤其是在慢速串行线路上。 为此,它需要知道屏幕上有什么,而对于大多数终端来说,唯一合理的方法是从空屏幕开始并跟踪您写入终端的内容。

因此,我怀疑 (n)curses 对于你的 shell 来说是错误的工具。 您可能需要在抽象层上走一步,并使用 terminfo 和来自终端(标准输入)的非阻塞读取。

(这不是很有帮助。抱歉。)

Curses wants to fully control the screen, and to optimize writes to the screen, especially over slow serial lines. To do this, it needs to know what is on the screen, and the only reasonable way to do that with most terminals is to start from an empty screen and keep track of what you write to the terminal.

Thus, I suspect (n)curses is the wrong tool for your shell. You probably need to go down a step on the abstraction layer and use terminfo and non-blocking reads from the terminal (standard input) instead.

(This is not very helpful. Sorry.)

亽野灬性zι浪 2024-07-22 03:19:47

使用像 readline() 这样的接口可能比求助更简单到成熟的 ncurses。

It might be simpler to use an interface like readline() rather than resorting to full-blown ncurses.

还给你自由 2024-07-22 03:19:47

您只需从 rlwrap 中调用您的程序即可轻松获得该功能。 。

You could just call your program from within rlwrap and have the functionality without the pain...

一场春暖 2024-07-22 03:19:47

这是关于此的另一个讨论。 提供的解决方案是:

  1. “‘filter()’函数允许您将curses用作单行。”
  2. “您可以用 C 语言编写等效的内容,使用 setupterm 来获取
    终端数据,以及用于格式化和输出的 tparm、tputs。”

当然还有第三个选项来获取 ncurses 源代码并对其进行修改,这样它就不再清除屏幕了。

Here is another discussion about this. The provided solutions are:

  1. "The 'filter()' function lets you use curses as a single-line."
  2. "You can write something equivalent in C, using setupterm to get the
    terminal data, and tparm, tputs for formatting and output."

Of course there is the third option to get the ncurses source code and modify it so it doesn't clear the screen anymore.

青萝楚歌 2024-07-22 03:19:47

您是否考虑过创建一个缺少清除屏幕序列的自定义 terminfo 或 termcap 条目的简单权宜之计,然后在运行程序之前将终端设置切换到该顺序? 您也可以在 ncurses 中使用 newterm() 和 set_term()。 过去使用 termcap 会更容易,因为您可以包含另一个终端并覆盖它的一些功能。

Have you considered the simple expedient of creating a custom terminfo or termcap entry lacking a sequence to clear the screen and then switching your terminal setting to that right before running your program? You could also just use newterm() and set_term() in ncurses. This used to be easier with termcap, since you could include another terminal and override some of its capabilities.

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