GNU Readline: how to clear the input line?

前端 未结 6 1333
你的背包
你的背包 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:13

    With spaces? Try to print "\b \b" for each character you want to "delete" rather than a single '\b'.


    Edit

    How it works
    Suppose you have written "Hello, world!" to the display device and you want to replace "world!" with "Jim."

    Hello, world!
                 ^ /* active position */ /* now write "\b \b" */
                   /* '\b' moves the active position back;
                   // ' ' writes a space (erases the '!')
                   // and another '\b' to go back again */
    Hello, world
                ^ /* active position */ /* now write "\b \b" again */
    Hello, worl
               ^ /* active position */ /* now write "\b \b" 4 times ... */
    Hello, 
           ^ /* active position */ /* now write "Jim." */
    Hello, Jim.
               ^ /* active position */
    

    Portability
    I'm not sure, but the Standard specifically describes the behaviour of '\b' and '\r' as has been described in answers to your question.

    Section 5.2.2 Character display semantics

    > 1   The active position is that location on a display device where the next character output by
    >     the fputc function would appear. The intent of writing a printing character (as defined
    >     by the isprint function) to a display device is to display a graphic representation of
    >     that character at the active position and then advance the active position to the next
    >     position on the current line. The direction of writing is locale-specific. If the active
    >     position is at the final position of a line (if there is one), the behavior of the display devic e
    >     is unspecified.
    >  
    > 2   Alphabetic escape sequences representing nongraphic characters in the execution
    >     character set are intended to produce actions on display devices as follows:
    >     \a (alert) Produces an audible or visible alert without changing the active position.
    >     \b (backspace) Moves the active position to the previous position on the current line. If
    >        the active position is at the initial position of a line, the behavior of the display
    >        device is unspecified.
    >     \f ( form feed) Moves the active position to the initial position at the start of the next
    >        logical page.
    >     \n (new line) Moves the active position to the initial position of the next line.
    >     \r (carriage return) Moves the active position to the initial position of the current line.
    >     \t (horizontal tab) Moves the active position to the next horizontal tabulation position
    >        on the current line. If the active position is at or past the last defined horizontal
    >        tabulation position, the behavior of the display device is unspecified.
    >     \v (vertical tab) Moves the active position to the initial position of the next vertical
    >         tabulation position. If the active position is at or past the last defined vertical
    >         tabulation position, the behavior of the display device is unspecified.
    >  
    > 3   Each of these escape sequences shall produce a unique implementation-defined value
    >     which can be stored in a single char object. The external representations in a text file
    >     need not be identical to the internal representations, and are outside the scope of this
    >     International Standard.
    

提交回复
热议问题