我如何处理比 Python 中的 Curses window 长的行?
How do I deal with lines that are longer than the Curses window in Python?
我希望我的 curses 应用程序在迭代时显示它正在使用的当前文件的绝对路径。这些可以比 window、运行 长到下一行。如果下一个文件路径较短,则不会覆盖长度差异,从而导致字符串损坏。解决此问题的最佳实践方法是什么?
编辑:Python Mac OS X
上的 3 个示例代码
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()
假设您不想使用 window,最简单的解决方案是:
例如:
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()
如果您想通过创建大于全屏 window 并将其剪切到终端来实现此目的,请继续阅读 newpad
。对于一个简单的案例行,它不会更简单,但对于更复杂的案例,它可能就是你要找的:
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()
我希望我的 curses 应用程序在迭代时显示它正在使用的当前文件的绝对路径。这些可以比 window、运行 长到下一行。如果下一个文件路径较短,则不会覆盖长度差异,从而导致字符串损坏。解决此问题的最佳实践方法是什么?
编辑:Python Mac OS X
上的 3 个示例代码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()
假设您不想使用 window,最简单的解决方案是:
例如:
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()
如果您想通过创建大于全屏 window 并将其剪切到终端来实现此目的,请继续阅读 newpad
。对于一个简单的案例行,它不会更简单,但对于更复杂的案例,它可能就是你要找的:
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()