Python 使curses程序输出在程序退出后保留在终端回滚历史记录中

Python 使curses程序输出在程序退出后保留在终端回滚历史记录中,python,ncurses,curses,Python,Ncurses,Curses,我对诅咒很陌生,所以我正在用python尝试一些不同的东西 我已经初始化了窗口并为窗口对象设置了scrollok。我可以添加字符串,滚动可以使addstr()在窗口末尾没有任何错误 我想要的是在程序完成后,能够在终端程序(本例中为tmux或KDE Konsole)中的程序输出中向后滚动 在我的代码中,如果跳过endwin()调用,至少可以看到输出,但是终端需要一个重置调用才能返回到操作状态 此外,即使在程序运行时,在curses窗口向下滚动后,我也无法在Konsole中向后滚动以查看初始输出 #

我对诅咒很陌生,所以我正在用python尝试一些不同的东西

我已经初始化了窗口并为窗口对象设置了scrollok。我可以添加字符串,滚动可以使addstr()在窗口末尾没有任何错误

我想要的是在程序完成后,能够在终端程序(本例中为tmux或KDE Konsole)中的程序输出中向后滚动

在我的代码中,如果跳过endwin()调用,至少可以看到输出,但是终端需要一个重置调用才能返回到操作状态

此外,即使在程序运行时,在curses窗口向下滚动后,我也无法在Konsole中向后滚动以查看初始输出

#!/usr/bin/env python2
import curses
import time
win = curses.initscr()
win.scrollok(True)
(h,w)=win.getmaxyx()
h = h + 10
while h > 0:
    win.addstr("[h=%d] This is a sample string.  After 1 second, it will be lost\n" % h)
    h = h - 1
    win.refresh()
    time.sleep(0.05)
time.sleep(1.0)
curses.endwin()

对于此任务,我建议您使用pad():

pad就像一个窗口,只是它不受屏幕大小的限制,并且不一定与屏幕的特定部分相关联。[…]一次屏幕上将只显示窗口的一部分。[……]

为了在使用完curses后将pad的内容留在控制台上,我将从pad读回内容,结束curses并将内容写入标准输出

下面的代码实现了您所描述的功能

#!/usr/bin/env python2

import curses
import time

# Create curses screen
scr = curses.initscr()
scr.keypad(True)
scr.refresh()
curses.noecho()

# Get screen width/height
height,width = scr.getmaxyx()

# Create a curses pad (pad size is height + 10)
mypad_height = height + 10
mypad = curses.newpad(mypad_height, width);
mypad.scrollok(True)
mypad_pos = 0
mypad_refresh = lambda: mypad.refresh(mypad_pos, 0, 0, 0, height-1, width)
mypad_refresh()

# Fill the window with text (note that 5 lines are lost forever)
for i in range(0, height + 15):
    mypad.addstr("{0} This is a sample string...\n".format(i))
    if i > height: mypad_pos = min(i - height, mypad_height - height)
    mypad_refresh()
    time.sleep(0.05)

# Wait for user to scroll or quit
running = True
while running:
    ch = scr.getch()
    if ch == curses.KEY_DOWN and mypad_pos < mypad_height - height:
        mypad_pos += 1
        mypad_refresh()
    elif ch == curses.KEY_UP and mypad_pos > 0:
        mypad_pos -= 1
        mypad_refresh()
    elif ch < 256 and chr(ch) == 'q':
        running = False

# Store the current contents of pad
mypad_contents = []
for i in range(0, mypad_height):
    mypad_contents.append(mypad.instr(i, 0))

# End curses
curses.endwin()

# Write the old contents of pad to console
print '\n'.join(mypad_contents)
#/usr/bin/env蟒蛇2
进口诅咒
导入时间
#创建诅咒屏幕
scr=curses.initscr()
scr.键盘(正确)
scr.refresh()
诅咒
#获取屏幕宽度/高度
高度,宽度=scr.getmaxyx()
#创建诅咒焊盘(焊盘大小为高度+10)
mypad_高度=高度+10
mypad=curses.newpad(mypad\u高度、宽度);
mypad.scrollok(True)
mypad_pos=0
mypad_refresh=lambda:mypad.refresh(mypad_位置,0,0,0,高度-1,宽度)
mypad_刷新()
#用文本填充窗口(请注意,5行将永远丢失)
对于范围(0,高度+15)内的i:
mypad.addstr(“{0}这是一个示例字符串…\n”。格式(i))
如果i>height:mypad\u pos=min(i-高度,mypad\u高度-高度)
mypad_刷新()
睡眠时间(0.05)
#等待用户滚动或退出
运行=真
运行时:
ch=scr.getch()
如果ch==curses.KEY\u DOWN和mypad\u pos0:
mypad_位置-=1
mypad_刷新()
elif ch<256且chr(ch)=‘q’:
运行=错误
#存储pad的当前内容
mypad_内容=[]
对于范围内的i(0,mypad_高度):
mypad_contents.append(mypad.instr(i,0))
#结束诅咒
诅咒
#将pad的旧内容写入控制台
打印“\n”。加入(mypad\u内容)