GNU Readline: how to clear the input line?

前端 未结 6 1388
你的背包
你的背包 2021-01-05 14:35

I use GNU Readline in the \"select\" fashion, by registering a callback function like so:

rl_callback_handler_install(\"\", on_readline_input);
6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-05 15:27

    After quite a lot of hacking I was able to get this mechanism. I hope other people will find it useful. It does not even use select(), but I hope you will get the point.

    #include 
        #include 
        #include 
        #include 
        #include 
    
        const char const* prompt = "PROMPT> ";
    
        void printlog(int c) {
            char* saved_line;
            int saved_point;
            saved_point = rl_point;
            saved_line = rl_copy_text(0, rl_end);
            rl_set_prompt("");
            rl_replace_line("", 0);
            rl_redisplay();
            printf("Message: %d\n", c);
            rl_set_prompt(prompt);
            rl_replace_line(saved_line, 0);
            rl_point = saved_point;
            rl_redisplay();
            free(saved_line);
        }
    
    
        void handle_line(char* ch) {
            printf("%s\n", ch);
            add_history(ch);
        }
    
        int main() {
            int c = 1;
    
            printf("Start.\n");
            rl_callback_handler_install(prompt, handle_line);
    
            while (1) {
                if (((++c) % 5) == 0) {
                    printlog(c);
                }
    
                usleep(10);
                rl_callback_read_char();
            }
            rl_callback_handler_remove();
        }
    

提交回复
热议问题