added a dedicated pager library
This commit is contained in:
+3
-15
@@ -4,9 +4,10 @@
|
||||
# Created by Luxferre in 2025, released into public domain
|
||||
|
||||
from deck.input import input
|
||||
from deck.pager import print_paged
|
||||
|
||||
class DeckChat:
|
||||
def __init__(self, start_state={}, chat_prefix='> ', command_prefix='/', paginate=0):
|
||||
def __init__(self, start_state={}, chat_prefix='> ', command_prefix='/'):
|
||||
"""Init the chat class"""
|
||||
self.command_prefix = command_prefix # prefix that commands start with
|
||||
self.chat_prefix = chat_prefix # prefix to write before input
|
||||
@@ -14,7 +15,6 @@ class DeckChat:
|
||||
'default': lambda m,s: (m,s) # echo handler by default
|
||||
}
|
||||
self.state = start_state # internal state object (impl-specific)
|
||||
self.paginate_n = paginate # paginate the responses exceeding N>0 lines
|
||||
|
||||
def command(self, command, handler):
|
||||
"""
|
||||
@@ -43,15 +43,6 @@ class DeckChat:
|
||||
res = (cmd, arg)
|
||||
return res
|
||||
|
||||
def _paginate_output(self, text):
|
||||
"""Splits output for small terminals."""
|
||||
lines = text.split('\n')
|
||||
for i in range(0, len(lines), self.paginate_n):
|
||||
chunk = lines[i:i + self.paginate_n]
|
||||
for line in chunk: print(line)
|
||||
if i + self.paginate_n < len(lines):
|
||||
input("-- Press Enter --")
|
||||
|
||||
def start(self):
|
||||
"""Start the chat loop"""
|
||||
while True:
|
||||
@@ -60,10 +51,7 @@ class DeckChat:
|
||||
if len(input_msg) == 0: continue
|
||||
cmd, msg = self._detect_command(input_msg)
|
||||
output, self.state = self.command_handlers[cmd](msg, self.state)
|
||||
if self.paginate_n > 0:
|
||||
self._paginate_output(output.strip())
|
||||
else:
|
||||
print(output)
|
||||
print_paged(output.strip())
|
||||
if self.state is None:
|
||||
break
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
|
||||
Reference in New Issue
Block a user