用于向python应用程序添加类似“more”的分页功能的库
more-or-less的Python项目详细描述
此库使向应用程序添加more分页变得容易
这意味着它将打印一整页输出, 之后,它将提示用户如何继续
它支持多个continue操作,并完全控制输入的位置 来自,以及输出的打印位置
可用的继续操作
这个库支持所有常用的continue操作,比如另一个页面的空间, 输入一行或多行
要查看所有可用的操作,请在--More--提示符下按h:
------------------------------------------------------------------------------- Most commands can optionally be preceded by an integer argument k. The default values are printed in brackets. A star (*) indicates the value of k becomes the new default. ------------------------------------------------------------------------------- <space> Display next k lines of text [current screen size] <return> Display next k lines of text [1]* q or Q or <interrupt> Exit from more = Display current line number l Toggle printing line number on every line [currently disabled] /<regular expression> Search for kth occurrence of the regular expression [1] n Search for kth occurrence of the last regular expression [1] . Repeat previous command h or ? Display this help text -------------------------------------------------------------------------------
如何使用此库
您只需调用paginate,并传入输入行:
importmore_or_lessmore_or_less.paginate(input=iterator_or_queue)
这将使用终端的屏幕高度,并将文本打印到stdout。
是否要使用stdout以外的其他内容?只需输入要使用的输出
more_or_less.paginate(input=iterator,output=stderr)
这可以是任何具有write和flush方法的对象
classMyCustomOutput(object):defwrite(self,text):passdefflush(self):passdefpaginate(iterator):more_or_less.paginate(input=iterator,output=myCustomOutput())
要使用比终端会话更高的屏幕高度,请传入一个ScreenDimensions对象,如内置的FixedSizeScreen:
importmore_or_lessmore_or_less.paginate(input=iterator,screen_dimensions=more_or_less.FixedSizeScreen(height=25))
最后,要异步运行分页器,只需传入asynchronous=True
more_or_less.paginate(input=iterator,asynchronous=True)
如果使用queue.Queue作为输入,这尤其有用。 在这种情况下,使用more_or_less.END_OF_INPUT让我们的分页器知道它应该停止, 并加入返回的对象以等待分页器结束:
my_queue=queue.Queue()controller=more_or_less.paginate(input=my_queue,asynchronous=True)my_queue.put('first line\n')my_queue.put('second line\n')# Signal we're donemy_queue.put(more_or_less.END_OF_INPUT)# Wait for the pagination to completecontroller.join()
高级主题
添加插件
在more提示符下执行的所有操作(多页的空间、多行的输入等等)都是使用插件安装的。
这使得通过创建自己的^{tt15}来扩展功能变得很容易$ 并通过more_or_less.add_plugin安装它。
例如,请参见我们的rainbow plugin和rainbow toggle。
更改提示读取器
当输出暂停等待继续操作时, 我们从stdin读取此操作。
这可以通过传入自定义的Input对象来更改。 例如,当我们点击更多提示时,下面的代码将自动搜索"the search pattern":
classMyCustomInput(more_or_less.Input):defprompt(self,message):# Return a full line of input.# Used for example after typing a '/'return"the search pattern"defget_character(self,message):# Returns a single input characterreturn'/'more_or_less.paginate(input=iterator,prompt=MyCustomInput())