即使将scrollok设置为true,ncurses窗口滚动也不起作用

时间:2018-12-30 17:17:16

标签: c ncurses

我正在使用带有Ranger文件管理器之类的UI用C编写ncurses文件管理器。 到目前为止,这是我的代码:

#include <stdio.h>
#include <dirent.h>
#include <curses.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>


/*
    Creates a new window with dimensions `height` and `width` starting at `starty` and `startx`
*/
WINDOW *create_newwin(int height, int width, int starty, int startx)
{   
    WINDOW *local_win;

    local_win = newwin(height, width, starty, startx);

    return local_win;
}


/*
    Returns number of files in `char* directory`
*/
int getNumberofFiles(char* directory)
{
    int len=0;
    DIR *pDir;
    struct dirent *pDirent;

    pDir = opendir (directory);
    if (pDir == NULL) {
        printf ("Cannot open directory '%s'\n", directory);
        exit(0);
    }

    while ((pDirent = readdir(pDir)) != NULL) {
        // Skip hidden files
      if(pDirent->d_name[0] != '.' )
        len++;
    }
    return len;
}


/*
    Stores all the file names in `char* directory` to `char *target[]`
*/
void getFiles(char* directory, char* target[])
{
    int i = 0;
    DIR *pDir;
    struct dirent *pDirent;

    pDir = opendir (directory);
    if (pDir == NULL) {
        printf ("Cannot open directory '%s'\n", directory);
        exit(0);
    }

    while ((pDirent = readdir(pDir)) != NULL) {
        // Skip hidden files
        if(pDirent->d_name[0] != '.')
          target[i++] = strdup(pDirent->d_name);
    }

    closedir (pDir);
}


int main(int argc, char* argv[])
{
    // To store number of files in directory
    int len=0;
    // Counter variable
    int i = 0;
    // Direcotry to be opened
    char* dir;

    // Get UID of user
    uid_t uid = getuid();
    // Get home directory of user from UID
    struct passwd *info = getpwuid(uid);

    // No Path is given in arguments
    // Set Path as $HOME
    if(argc == 1)
    {
        dir = info->pw_dir;
    }

    // Path is given in arguments
    // Set Path as the argument
    else if(argc == 2)
    {
        dir = argv[1];

        // Relative Path Given
        if(dir[0] != '/')
        {
            // Add path of $HOME before the Relative Path
            char temp[250] = "";
            strcat(temp,info->pw_dir);
            strcat(temp,"/");
            strcat(temp,dir);
            dir = temp;
        }   
    }
    // Incorrect Useage
    else
    {
        printf("Incorrect Useage\n");
        exit(0);
    }

    // Get number of files in the home directory
    len = getNumberofFiles(dir);

    // ncurses initialization
    initscr();
    raw();
    noecho();
    curs_set(0);

    // Shows current directory
    WINDOW *current_win;
    // Shows child directory preview
    WINDOW *preview_win;
    int startx, starty, midx, midy, maxx, maxy;

    // Index of currently selected item in `char* directories`
    int selection = 0;
    char ch;
    do 
    {
        len = getNumberofFiles(dir);
        char* directories[len];
        getFiles(dir, directories);

        getmaxyx(stdscr, maxy, maxx);

        // Make the two windows side-by-side
        current_win = create_newwin(maxy, maxx/2+3, 0, 0);
        preview_win = create_newwin(maxy, maxx/2 -1, 0, maxx/2 + 1);

        // Print all the elements and highlight the selection
        for( i=0; i<len; i++ )
        {
            if(i==selection)
                wattron(current_win, A_STANDOUT);
            else
                wattroff(current_win, A_STANDOUT);
            wmove(current_win,i+1,2);
            wprintw(current_win, "%s\n", directories[i]);
        }

        char* selected_file = directories[selection];
        char next_dir[250] = "";
        char prev_dir[250] = "";
        char *p;

        // Get path of parent directory
        strcat(prev_dir, dir);
        p = strrchr(dir,'/');
        prev_dir[p-dir] = '\0';

        // Parent directory is root
        if(prev_dir[0] != '/')
            prev_dir[0] = '/';

        // Get path of child directory
        strcat(next_dir, dir);
        strcat(next_dir, "/");
        strcat(next_dir, directories[selection]);
        int len_preview = getNumberofFiles(next_dir);
        char* next_directories[len_preview];
        getFiles(next_dir, next_directories);

        for( i=0; i<len_preview; i++ )
        {
            wmove(preview_win,i+1,2);
            wprintw(preview_win, "%s\n", next_directories[i]);
        }

        wattroff(current_win, A_STANDOUT);
        box(current_win,0,0);
        box(preview_win,0,0);
        wrefresh(current_win);
        wrefresh(preview_win);

        // Keybindings
        switch( ch = getch() ) {
            case 'k':
                selection--;
                selection = ( selection < 0 ) ? len-1 : selection;
                break;
            case 'j':
                selection++;
                selection = ( selection > len-1 ) ? 0 : selection;
                break;
            case 'l':
                strcpy(dir, next_dir);
                selection = 0;
                break;
            case 'h':
                strcpy(dir, prev_dir);
                selection = 0;
                break;
            case 'g':
                selection = 0;
                break;
            case 'G':
                selection = len-1;
                break;
        }

        // Free Memory
        for( i=0; i<len_preview; i++ )
        {
            free(next_directories[i]);
        }

        for( i=0; i<len; i++ )
        {
            free(directories[i]);
        }
    } while( ch != 'q');

    endwin();


    return 0;
}

基本上,有两个并排渲染的窗口。一个是current_win,它显示当前目录中的所有文件,另一个是preview_win,它显示所选目录的子目录中的所有文件。

问题是,当当前目录中有很多文件时,当选择超出窗口范围时,窗口将不会滚动。我试过了 scrollok(current_win, TRUE);,但它会自动滚动到右下角并保持这种状态。

如何使其起作用?

(也是出于某种原因,在打开编译的二进制文件后,直到按下某个键,shell仍为空白)

2 个答案:

答案 0 :(得分:0)

使用在窗口中滚动非常简单。当您写完末尾时,它将在底部插入空白行。而已。顶部的线消失了吗?它消失了。没有保留在某些屏幕外缓冲区中。一去不复返。这不是某种交互式双向滚动。它不知道您的“选择”在哪里,也无法对这些信息做任何事情。

要进行反向滚动,可以使用scrollok。像向前滚动一样,它只是在顶部添加一个空白行。重绘该行中的信息是您的责任。

诅咒pad可能会更容易使用更高级别的界面。

创建一个足以容纳整个列表的垫,然后用wscrl绘制顶部。当选择内容移出可见区域时,请使用更新的坐标再次调用prefresh,以告诉它显示包括新选择内容的打击垫的一部分。

答案 1 :(得分:0)

在阅读示例时,该块存在问题:

    for( i=0; i<len; i++ )
    {
        if(i==selection)
            wattron(current_win, A_STANDOUT);
        else
            wattroff(current_win, A_STANDOUT);
        wmove(current_win,i+1,2);
        wprintw(current_win, "%s\n", directories[i]);
    }

如果 len 值大于窗口中的行数,则 wmove 调用将失败-将光标留在 wprintw 调用将其放置。如果不启用scrollok,它将位于窗口的右下方。但这(启用了scrollok)仍然是 current_win 的最后一行(在这种情况下为左下)。如果您拨出wmove个电话并启用scrollok,则结果将比您预期的还要多。

preview_win 的循环中存在相同的问题。

最后一个问题问一个空白的屏幕:那是因为在这一块中

    wrefresh(current_win);
    wrefresh(preview_win);

    // Keybindings
    switch( ch = getch() ) {

getch 呼叫执行

wrefresh(stdscr);

将覆盖刚刚绘制的窗口。您可以将wgetch更改为

switch ( ch = wgetch(current_win) ) {

stdscr 不会碍事。