所以这就是交易,这个基本程序获取文件的内容并显示它。另外,使用 wsetscrreg() 我创建了一个滚动部分,一切都很完美,但问题是当我滚动并且文本离开屏幕时它会完全消失,这不是滚动的工作原理。我正在阅读文档,但我没有任何线索?有什么建议吗?
#include "window.h"
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
WINDOW *
new_window (char *file)
{
WINDOW *win;
int start_x, start_y;
getmaxyx (stdscr, start_y, start_x);
win = newwin (start_y, start_x - 2, 1, 1);
mvprintw (1, 3, "%s", file);
scrollok (win, TRUE);
wsetscrreg (win, 0, start_y);
box (win, 0, 0);
wrefresh (win);
refresh ();
return win;
}
void
init_window ()
{
initscr ();
cbreak ();
noecho ();
start_color ();
curs_set (0);
init_pair (1, COLOR_WHITE, COLOR_BLACK);
refresh ();
}
void
end_window (WINDOW *win)
{
delwin (win);
endwin ();
}
// "█"
//
void
load_file (WINDOW *win, char *path, int max_x)
{
FILE *f;
char c;
int x = 1;
int y = 2;
f = fopen (path, "r");
if (f == NULL)
{
mvwprintw (win, 0, 0,
"Error: File not found or cannot be opened.");
wrefresh (win);
return;
}
while ((c = fgetc (f)) != EOF)
{
if (c == '\n')
{
y++;
x = 0;
}
else if (x > max_x - 5)
{
x = 1;
y++;
mvwprintw (win, y, x, "%c", c);
}
else if (c != '\n')
{
mvwprintw (win, y, x, "%c", c);
}
x++;
}
fclose (f);
wrefresh (win);
}
int
main (int argc, char **argv)
{
WINDOW *win;
int max_y, max_x, scrollPos = 0;
char ch;
init_window ();
win = new_window (argv[1]);
getmaxyx (win, max_y, max_x);
load_file (win, argv[1], max_x);
while ((ch = wgetch (win)) != 'q')
{
if (ch == 'j')
{
wscrl (win, 1);
}
if (ch == 'k')
{
wscrl (win, -1);
}
}
end_window (win);
return 0;
}
我认为一种解决方案可以是在用户滚动时使用一些缓冲区来逐行显示和删除行,但该实现似乎比实际问题复杂得多,而且我认为答案与之相比非常简单。