GNU Readline(libreadline):异步显示输出消息 [英] GNU Readline (libreadline): Displaying output message asynchronously

查看:61
本文介绍了GNU Readline(libreadline):异步显示输出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用readline(阻塞)进行用户输入的同时,我想将文本行从另一个线程异步输出到控制台.此外,我希望从控制台中删除readline提示符和当前的部分输入行,写入输出行,然后恢复readline提示符和部分用户行-以便使输出看起来像写在上面"提示.

While using readline (blocking) for user input, I would like to output lines of text to the console asynchronously from another thread. Further, I would like that the readline prompt and current partial input line be removed from the console, the output line written, then the readline prompt and the partial user line restored - so as to give the appearance that the output was written "above" the prompt.

通过阅读线重新显示功能(或其他方式)的什么组合可以实现?

By what combination of readline redisplay functions (or otherwise) can this be achieved?

(重新显示功能文档: http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC35 )

(Redisplay function documentation: http://cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC35)

问题演示:

    #include <readline/readline.h>
    #include <readline/history.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <pthread.h>

    bool run = true;

    void* log_thread(void*)
    {
            while (run)
            {
                    sleep(1);
                    // WHAT TO DO HERE?
                    write(1, "tick\n", 5);
            }
    }

    int main()
    {
            pthread_t t;
            pthread_create(&t, 0, log_thread, 0);

            while (true)
            {
                    char* p = readline("? ");
                    free(p);

                    if (!p)
                            break;
            }

            run = false;
            pthread_join(t,0);
    }

构建:

$ g++ -pthread -lreadline test.cpp
$ ./a.out

观察到的输出 :(输入"foo \ nbar \ n"输入缓慢)

observed output: (input "foo\nbar\n" typed slowly)

? tick
ftick
otick
otick

? tick
tick
bartick
tick

? tick
^C

所需的输出 :(输入"foo \ nbar \ n"输入缓慢)

desired output: (input "foo\nbar\n" typed slowly)

tick
tick
tick
tick
tick
? foo

tick
tick
tick
tick
tick
? bar

tick
? ^C

推荐答案

我正在我的程序omphalos的控制台版本中进行此操作( https://github.com/dankamongmen/omphalos/blob/master/src/ui/tty/tty.c .

I'm doing this in the console version of my program omphalos (https://github.com/dankamongmen/omphalos). This particular code comes from https://github.com/dankamongmen/omphalos/blob/master/src/ui/tty/tty.c.

我有:

// Call whenever we generate output, so that the prompt is updated
static inline void
wake_input_thread(void){
    if(input_tid){
            pthread_kill(*input_tid,SIGWINCH);
            rl_redisplay(); // FIXME probably need call from readline contex
    }
    pthread_mutex_unlock(&promptlock);

}

static inline void
clear_for_output(FILE *fp){
    fputc('\r',fp);
}

每当要打印某些内容时,它将获取锁定并调用clear_for_output(),将光标移动到当前行的开头.如有必要,它可以在此时通过调用rl_set_prompt()来更改提示.完成后,它将调用ake_input_thread(),释放锁并导致重新显示.

Whenever something wants to print, it takes the lock and calls clear_for_output(), moving the cursor to the beginning of the current line. It can change the prompt at this time if necessary, by calling rl_set_prompt(). When done, it calls wake_input_thread(), releasing the lock and causing a redisplay.

我不确定这是否适用于您输入了多行文本并对此表示怀疑的情况,并且不关心此时此刻可能会出现什么令人沮丧的新错误,因此您可以自己尝试一下.

I'm not sure if this works in the case where you've typed more than a line of text in, and doubt it, and don't care to formally discover what's likely a new and depressing bug right this moment, so you can experiment with that yourself.

这篇关于GNU Readline(libreadline):异步显示输出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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