boon

unix terminal framework
git clone https://git.ce9e.org/boon.git

commit
ff31820a9442da499a6834fca7e232ef7078a73c
parent
65a60933fd4552a52890caefdd614211296de8dd
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-07-11 15:47
add title support

Diffstat

M boon.py 33 ++++++++++++++++++++++-----------

1 files changed, 22 insertions, 11 deletions


diff --git a/boon.py b/boon.py

@@ -80,6 +80,17 @@ def fullscreen():
   80    80 		sys.stdout.flush()
   81    81 
   82    82 
   -1    83 @contextmanager
   -1    84 def title(s):
   -1    85 	sys.stdout.write('\033]2;%s\a' % s)
   -1    86 	sys.stdout.flush()
   -1    87 	try:
   -1    88 		yield
   -1    89 	finally:
   -1    90 		sys.stdout.write('\033]2;%s\a' % '')
   -1    91 		sys.stdout.flush()
   -1    92 
   -1    93 
   83    94 def getch():
   84    95 	# NOTE: bytes might contain more than one key
   85    96 	fd = sys.stdin.fileno()
@@ -95,16 +106,13 @@ def getch():
   95   106 		return os.read(fd, 8).decode('ascii')
   96   107 
   97   108 
   98    -1 # https://github.com/tartley/colorama/blob/master/colorama/ansi.py
   99    -1 # def set_title(title):
  100    -1 # 	return OSC + '2;' + title + BEL
  101    -1 
  102    -1 
  103   109 def cursor_move(x, y):
  104   110 	sys.stdout.write(get_cap('cup', y, x))
  105   111 
  106   112 
  107   113 class App:
   -1   114 	title = ''
   -1   115 
  108   116 	def __init__(self):
  109   117 		self.old_lines = []
  110   118 		signal.signal(signal.SIGWINCH, self.on_resize)
@@ -131,12 +139,13 @@ class App:
  131   139 
  132   140 	def run(self):
  133   141 		with fullscreen():
  134    -1 			self.on_resize()
  135    -1 			while True:
  136    -1 				key = getch()
  137    -1 				if key:
  138    -1 					self.on_key(key)
  139    -1 					self.update()
   -1   142 			with title(self.title):
   -1   143 				self.on_resize()
   -1   144 				while True:
   -1   145 					key = getch()
   -1   146 					if key:
   -1   147 						self.on_key(key)
   -1   148 						self.update()
  140   149 
  141   150 	def render(self):
  142   151 		return []
@@ -146,6 +155,8 @@ class App:
  146   155 
  147   156 
  148   157 class Example(App):
   -1   158 	title = 'Example'
   -1   159 
  149   160 	def __init__(self):
  150   161 		super().__init__()
  151   162 		self.keys = ['f', 'b', 'z']