Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I get (and set) current bash cursor position when using python readline?
    primarykey
    data
    text
    <p>I have a python script that takes manages the stdin, stdout, and stderr of any application and allows for readline to be inserted gracefully. Think of any application that has lots of console output, but also accepts commands from stdin.</p> <p>In any case, my script uses these two functions:</p> <pre><code>def blank_current_readline(): # Next line said to be reasonably portable for various Unixes (rows,cols) = struct.unpack('hh', fcntl.ioctl(sys.stdout, termios.TIOCGWINSZ,'1234')) text_len = len(readline.get_line_buffer())+2 # ANSI escape sequences (All VT100 except ESC[0G) sys.stdout.write('\x1b[2K') # Clear current line sys.stdout.write('\x1b[1A\x1b[2K'*(text_len/cols)) # Move cursor up and clear line sys.stdout.write('\x1b[0G') # Move to start of line def print_line(line): global cmd_state blank_current_readline() print line, sys.stdout.write(cmd_state["prompt"] + readline.get_line_buffer()) sys.stdout.flush() </code></pre> <p>When handling stdout, I call print_line(). This blanks whatever the user might be typing, prints the line, then restores the user's input text. This all happens without the user noticing a thing.</p> <p><strong>The problem</strong> occurs when the cursor is not at the end of whatever input the user is typing. When the cursor is in the middle of the test and a line is printed, the cursor will automatically be placed at the end of the input. To solve this, I want to do something like this in print_line:</p> <pre><code>def print_line(line): global cmd_state cursorPos = getCurrentCursorPos() #Doesn't exist blank_current_readline() print line, sys.stdout.write(cmd_state["prompt"] + readline.get_line_buffer()) sys.stdout.setCurrentCursorPos(cursorPos) #Doesn't exist sys.stdout.flush() </code></pre> <p>Edit: To try and visualize what I have written:</p> <p>The terminal looks like this:</p> <pre><code>---------------------------------------------- | | | | | &lt;scolling command output here&gt; | | | | &lt;scolling command output here&gt; | | | |: &lt;user inputted text here&gt; | ---------------------------------------------- </code></pre> <p>So the output text is constantly scrolling as new logs are coming through. At the same time, the user is currently editing and writing a new command that will be inserted once the hit enter. So it looks like the python console, but with output always being appended.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload