我在 Python3 中有以下基本的 curses 实现。
#!/usr/bin/env python3
import curses
import time
from curses import wrapper
stdscr = curses.initscr() # required
curses.noecho() # don't show keyboard input
curses.cbreak() # don't require enter to send input
stdscr.keypad(True)
def main(stdscr):
# curses.newwin(5, 10, 7, 20)
stdscr.addstr("SUMMON SHOGGOTHS")
stdscr.addstr(20, 30, "Razzmatazz")
stdscr.refresh()
time.sleep(3)
wrapper(main)
# Unwind
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
几乎所有事情都如我所料发生:Shoggoths 被召唤,Razzes 被 matazzed,但是当我输入 git status 时,我的换行符被打破了。
在 stty -a 显示之前和之后进行比较:
5c5
< iflags: -istrip icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -iutf8
---
> iflags: -istrip -icrnl -inlcr -igncr ixon -ixoff ixany imaxbel -iutf8
7c7
< oflags: opost onlcr -oxtabs -onocr -onlret
---
> oflags: opost -onlcr -oxtabs -onocr -onlret
在研究了这些选项之后,我发现发出 stty onlcr 可以修复终端。然而,我很惊讶,因为我认为 curses.endwin()会重置我:
De-initialize the library, and return terminal to normal status.
我认为这可能是 iTerm2 中的一个问题,所以我尝试使用 Terminal.app。这产生了相同的行为。
我很困惑还有其他一些重置技术吗?我在基于 C 的实现中看到,stty 数据通常被保存到一个结构中以供恢复……这可能是一种追求的途径。
感谢您的帮助!
请您参考如下方法:
可能的问题是这个(参见 source code ):
- 诅咒
wrapper方法执行initscr和endwin。 - 当您的脚本从
wrapper退出时,curses 库返回shell 模式 - 更改
curses.nocbreak()是多余的,可能会导致 curses 库的当前终端模式从 程序模式 - 调用
endwin会更新错误的设置。
我只是删除标记为“# Unwind”的 block 。
