在ncurses中实现文本滚动的推荐方法是什么? [英] What is the recommended way to implement text scrolling in ncurses?

查看:166
本文介绍了在ncurses中实现文本滚动的推荐方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现ncurses应用,该应用的文字滚动更少.推荐的方法是什么?

这就是我所知道的:

  1. 您可以使用scroll将文本缓冲区上移或下移1行.但是,如果向下滚动,最终将在顶部留下一个空白行;如果向上滚动,最终将在底部出现空白行,您将需要重新粉刷自己.
  2. Ncurses为您进行自动换行,当我必须确定在步骤1中必须重画的行时,这弄乱了我的数学.

我想我可以重新实现自动换行并保留所有后包装行的数组,但这似乎是一个常见问题,因此可能会有更好的方法.

解决方案

答案取决于您拥有多少文本,以及您在屏幕上处理数据的其他用途.但是通常首选使用 pad 功能显示可滚动文本的方式.这不仅是 ncurses 功能,而且大多数curses的实现都支持该功能(即1980年代后期的所有功能).

键盘就像一个窗口,但其大小不限于当前的屏幕大小.取而代之的是,数据通过一个视图显示,该视图在平板电脑中的位置可以轻松更改.

ncurses-examples中有两个相关的示例程序: padview.c 使用一个在其上绘制了整个文件的垫,并使用该垫功能根据需要,上下左右移动视图.

在这些程序中,show_all函数执行实际的绘制,而 view 的讨论作为比较ncurses和slang库的示例.

I am trying to implement an ncurses app with text scrolling like less. What is the recommended way to do this?

Here's what I know:

  1. You can use scroll to move the text buffer up or down by 1 row. However, you'll end up with one blank line at the top if you scroll down, or at the bottom if you scroll up, which you'll have to repaint yourself.
  2. Ncurses does wordwrap for you, which messes up my math when I have to determine what line I have to repaint in step 1.

I guess I could re-implement wordwrap myself and keep an array of all the post-wrapped lines, but this seems like a common problem, so there might be a better way.

解决方案

The answer depends on how much text you have, and what other uses you are making of the data on the screen. But usually the pad feature is preferred way to display scrollable text. This is not only an ncurses feature, but is supported by most implementations of curses (i.e., anything after the late 1980s).

A pad is like a window, but its size is not limited to the current screen size. Instead, the data is shown through a view whose position within the pad can be easily changed.

There are two sample programs in ncurses-examples which are relevant: view.c displays a file by writing its contents onto a window, while padview.c uses a pad on which the entire file is drawn, and uses the pad-functions for moving the view around as needed, up/down, and left/right.

In those programs, the show_all function does the actual drawing, and is about a third as long for padview.c (35 lines) compared to the equivalent for view.c (94 lines).

Further reading: discussion of view as an example for comparing ncurses and slang libraries.

这篇关于在ncurses中实现文本滚动的推荐方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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