使用 python readline 时如何获取(并设置)当前 bash 光标位置?
我有一个 python 脚本,它可以管理任何应用程序的 stdin、stdout 和 stderr,并允许优雅地插入 readline。想象一下任何具有大量控制台输出但也接受来自标准输入的命令的应用程序。
无论如何,我的脚本使用这两个函数:
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()
处理 stdout 时,我调用 print_line()。这会清空用户可能输入的任何内容,打印该行,然后恢复用户的输入文本。这一切都是在用户没有注意到的情况下发生的。
当光标不在用户键入的任何输入的末尾时,就会出现问题。当光标位于测试中间并打印一行时,光标将自动放置在输入的末尾。为了解决这个问题,我想在 print_line 中做这样的事情:
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()
编辑:尝试可视化我所写的内容:
终端看起来像这样:
----------------------------------------------
| |
| |
| <scolling command output here> |
| |
| <scolling command output here> |
| |
|: <user inputted text here> |
----------------------------------------------
因此,随着新日志的出现,输出文本不断滚动。与此同时,用户当前正在编辑和编写一个新命令,一旦按下回车键就会插入该命令。所以它看起来像 python 控制台,但总是附加输出。
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.
In any case, my script uses these two functions:
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()
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.
The problem 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:
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()
Edit: To try and visualize what I have written:
The terminal looks like this:
----------------------------------------------
| |
| |
| <scolling command output here> |
| |
| <scolling command output here> |
| |
|: <user inputted text here> |
----------------------------------------------
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以建议Python诅咒吗?
这是基本操作方法
另外,
我在这里找到了终端控制器: 使用 terminfo 进行便携式彩色输出和打印光标控制。它看起来比站点名称所建议的更便携(评论中提到了 MacOS - 尽管有所更改)。
这是一个使用示例,显示进度条:
Might I suggest Python curses?
Here is the Basic how-to
Alternatively
I found Terminal Controller here: Using terminfo for portable color output & cursor control. It looks to be more portable than the sitename would suggest (MacOS mentioned in the comments - though with changes).
Here is a usage example, displaying a progress bar: