简体   繁体   English

如何在“top”linux命令中进行控制台输入?

[英]How to do console input like in the “top” linux command?

So, the linux top command has the real time-like loop with console output (nothing fancy), but it uses non-blocking console input which doesn't display typed character in the command line. 因此,linux top命令具有与控制台输出类似的实时循环(没什么特别的),但它使用非阻塞控制台输入,它不在命令行中显示类型字符。 How it's done? 怎么做的? Is there any library for it, do they use threads? 有没有它的库,他们使用线程吗? I need to write an linux app with the same style (used via ssh) and I have no idea how to do that input (cin in separate thread isn't the solution, top uses something other). 我需要编写一个具有相同风格的linux应用程序(通过ssh使用),我不知道如何做这个输入(cin在单独的线程中不是解决方案, top使用其他东西)。

One solution is to use an implementation of curses . 一种解决方案是使用curses的实现。

I don't know how top does it. 我不知道它有多么top

Another option, not using curses: 另一种选择,不使用curses:

#include <stdio.h>
//#include <unistd.h>
#include <termios.h>
//#include <sys/ioctl.h>
//#include <sys/time.h>
//#include <sys/types.h>
#include <fcntl.h>

//------------------------------------------------------------------------------
// getkey() returns the next char in the stdin buffer if available, otherwise
//          it returns -1 immediately.
//
int getkey(void)
{
    char ch;
    int error;
    struct termios oldAttr, newAttr;
    int oldFlags, newFlags;
    struct timeval tv;
    int fd = fileno(stdin);
    tcgetattr(fd, &oldAttr);
    newAttr = oldAttr;
    oldFlags = fcntl(fd, F_GETFL, 0);

    newAttr.c_iflag = 0; /* input mode */
    newAttr.c_oflag = 0; /* output mode */
    newAttr.c_lflag &= ~ICANON; /* line settings */
    newAttr.c_cc[VMIN] = 1; /* minimum chars to wait for */
    newAttr.c_cc[VTIME] = 1; /* minimum wait time */

    // Set stdin to nonblocking, noncanonical input
    fcntl(fd, F_SETFL, O_NONBLOCK);
    error=tcsetattr(fd, TCSANOW, &newAttr);

    tv.tv_sec = 0;
    tv.tv_usec = 10000; // small 0.01 msec delay
    select(1, NULL, NULL, NULL, &tv);

    if (error == 0)
        error=(read(fd, &ch, 1) != 1); // get char from stdin

    // Restore original settings
    error |= tcsetattr(fd, TCSANOW, &oldAttr);
    fcntl(fd, F_SETFL, oldFlags);

    return (error ? -1 : (int) ch);
}

int main()
{
    int c,n=0;
    printf("Hello, world!\nPress any key to exit. I'll wait for 4 keypresses.\n\n");
    while (n<4)
    {
        //printf("."); // uncomment this to print a dot on each loop iteration
        c = getkey();
        if (c >= 0)
        {
            printf("You pressed '%c'\n", c);
            ++n;
        }
    }
}

I'm sorry that I can't take full credit for this, as I pulled it off the 'net many years back. 对不起,我不能完全相信这一点,因为我把它从多年前的网上拉下来了。 I think I've tweaked it a little, but it's mostly unchanged from where I got it. 我想我已经稍微调整了一下,但是从我得到它的地方来看它几乎没有变化。 Unfortunately I didn't add a comment indicating where I found it. 不幸的是我没有添加评论表明我找到了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM