import curses
import curses.textpad
import time
import math
[docs]class Screen(object):
UP = -1
DOWN = 1
[docs] def __init__(self, items):
""" Initialize the screen window
Attributes
window: A full curses screen window
width: The width of `window`
height: The height of `window`
max_lines: Maximum visible line count for `result_window`
top: Available top line position for current page (used on scrolling)
bottom: Available bottom line position for whole pages (as length of items)
current: Current highlighted line number (as window cursor)
page: Total page count which being changed corresponding to result of a query (starts from 0)
┌--------------------------------------┐
|1. Item |
|--------------------------------------| <- top = 1
|2. Item |
|3. Item |
|4./Item///////////////////////////////| <- current = 3
|5. Item |
|6. Item |
|7. Item |
|8. Item | <- max_lines = 7
|--------------------------------------|
|9. Item |
|10. Item | <- bottom = 10
| |
| | <- page = 1 (0 and 1)
└--------------------------------------┘
Returns
None
"""
self.window = None
self.width = 0
self.height = 0
self.init_curses()
self.items = items
self.max_lines = curses.LINES
self.top = 0
self.bottom = len(self.items)
self.current = 0
self.page = self.bottom // self.max_lines
self.run()
[docs] def init_curses(self):
"""Setup the curses"""
self.window = curses.initscr()
self.window.nodelay(1)
self.window.keypad(True)
curses.noecho()
curses.cbreak()
curses.start_color()
curses.use_default_colors()
curses.init_pair(1, curses.COLOR_WHITE,-1)
curses.init_pair(2, curses.COLOR_WHITE ,55)
self.current = curses.color_pair(2)
self.height, self.width = self.window.getmaxyx()
self.index=0
[docs] def run(self):
"""Continue running the TUI until get interrupted"""
try:
self.input_stream()
except KeyboardInterrupt:
pass
finally:
curses.endwin()
[docs] def paging(self, direction):
"""Paging the window when pressing left/right arrow keys"""
current_page = (self.top + self.current) // self.max_lines
next_page = current_page + direction
# The last page may have fewer items than max lines,
# so we should adjust the current cursor position as maximum item count on last page
if next_page == self.page:
self.current = min(self.current, self.bottom % self.max_lines - 1)
# Page up
# if current page is not a first page, page up is possible
# top position can not be negative, so if top position is going to be negative, we should set it as 0
if (direction == self.UP) and (current_page > 0):
self.top = max(0, self.top - self.max_lines)
return
# Page down
# if current page is not a last page, page down is possible
if (direction == self.DOWN) and (current_page < self.page):
self.top += self.max_lines
return
def batch(self,iterable, n=1):
l = len(iterable)
for ndx in range(0, l, n):
yield iterable[ndx:min(ndx + n, l)]
[docs] def display(self):
"""Display the items on window"""
self.window.erase()
self.height, self.width = self.window.getmaxyx()
self.max_lines = self.height
self.bottom = len(self.items)
items=self.items.copy()
for idx in range(len(items)):
for i in range(math.ceil(len(items[idx])/self.width)-1):
items.insert(idx+1+i,"")
idx=idx+i
try:
for idx, item in enumerate(items[self.top:self.top + self.max_lines]):
# Highlight the current cursor line
if len(item)<=self.width*(self.height-idx):
if idx == self.current:
self.window.addstr(idx, 0, item, curses.color_pair(2))
else:
self.window.addstr(idx, 0, item, curses.color_pair(1))
except:
self.window.addstr(0, 0, "!ERROR WINDOW TOO SMALL", curses.color_pair(1))
self.window.refresh()
[docs]def main():
items = []
title="""
██████╗ ██████╗ ██╗ █████╗ ██████╗ ███████╗██████╗ ███╗ ██╗
██╔══██╗██╔══██╗██║ ██╔══██╗██╔══██╗ ██╔════╝██╔══██╗████╗ ██║
██║ ██║██████╔╝██║ ███████║██████╔╝ ███████╗██║ ██║██╔██╗ ██║
██║ ██║██╔══██╗██║ ██╔══██║██╔══██╗ ╚════██║██║ ██║██║╚██╗██║
██████╔╝██████╔╝███████╗██║ ██║██████╔╝ ███████║██████╔╝██║ ╚████║Autour:Lu-Yi-Hsun
╚═════╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝╚═════╝ ╚══════╝╚═════╝ ╚═╝ ╚═══╝
----------------------------------------------------------------------------------
"""
for i in title.split("\n"):
items.append(i)
items.append("s1-s3:delay:22mssss ")
items.append("s1-s3:delay:22ms")
items.append("s1-s3:delay:22ferferms")
items.append("s1-s3:delay:22ms")
print(type(items))
Screen(items)
if __name__ == '__main__':
main()