在Python中,如何处理比Curses窗口长的行?
我希望我的curses应用程序在迭代时显示当前文件的绝对路径。这些可能比窗口长,运行到下一行。如果下一个文件路径较短,则不会覆盖长度差异,从而导致字符串损坏。解决此问题的最佳实践方法是什么 编辑:MacOSX上的Python 3示例代码在Python中,如何处理比Curses窗口长的行?,python,curses,Python,Curses,我希望我的curses应用程序在迭代时显示当前文件的绝对路径。这些可能比窗口长,运行到下一行。如果下一个文件路径较短,则不会覆盖长度差异,从而导致字符串损坏。解决此问题的最佳实践方法是什么 编辑:MacOSX上的Python 3示例代码 from os import walk import curses from os import path stdscr = curses.initscr() curses.noecho() for root, dirs, files in walk("/Us
from os import walk
import curses
from os import path
stdscr = curses.initscr()
curses.noecho()
for root, dirs, files in walk("/Users"):
for file in files:
file_path = path.join(root, file)
stdscr.addstr(0, 0, "Scanning: {0}".format(file_path))
stdscr.clearok(1)
stdscr.refresh()
假设您不想使用窗口,最简单的解决方案是:
addstr
,不要在行中写入超过合适长度的字符,以及from scandir import walk
import curses
from os import path
try:
stdscr = curses.initscr()
curses.noecho()
_, width = stdscr.getmaxyx()
for root, dirs, files in walk("/Users"):
for file in files:
file_path = path.join(root, file)
stdscr.addnstr(0, 0, "Scanning: {0}".format(file_path), width-1)
stdscr.clrtoeol()
stdscr.clearok(1)
stdscr.refresh()
finally:
curses.endwin()
如果您想通过创建一个大于全屏的窗口并将其剪切到终端来实现此目的,请阅读。对于一个简单的案例行,它不会更简单,但对于更复杂的案例,它可能是您想要的:
from scandir import walk
import curses
from os import path
try:
stdscr = curses.initscr()
curses.noecho()
height, width = stdscr.getmaxyx()
win = curses.newpad(height, 16383)
for root, dirs, files in walk("/Users"):
for file in files:
file_path = path.join(root, file)
win.addstr(0, 0, "Scanning: {0}".format(file_path))
win.clrtoeol()
win.clearok(1)
win.refresh(0, 0, 0, 0, height-1, width-1)
finally:
curses.endwin()
这取决于文本的显示方式。请提供一个简短、完整的程序来演示此问题。听起来您要么需要截断字符串,要么创建一个足够大的滚动窗口来容纳字符串,要么在字符串后清除为EOL。我不确定三者中的哪一个;作为@Robᵩ 说,如果没有看到您的代码,理想情况下也没有对您的程序进行更完整的描述,很难猜测。@abarnet添加了示例代码请给我们实际的工作示例代码;您至少需要从操作系统导入路径
或此处的其他内容。此外,a不应该包括与问题无关的第三方依赖关系;为什么不直接用os.walk
而不是scandir.walk
来演示它呢?