boon

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

commit
e4c90edd275833b1c1a0b9c7f5138e1785077fae
parent
b71d288ac9b9964d53c9b75237253a326970e399
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-07-11 15:10
simplify styling

Diffstat

M boon.py 51 +++++++++++++++++++++++++--------------------------

1 files changed, 25 insertions, 26 deletions


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

@@ -100,30 +100,29 @@ def getch():
  100   100 # 	return OSC + '2;' + title + BEL
  101   101 
  102   102 
  103    -1 def print_line(line):
  104    -1 	if not isinstance(line, list):
  105    -1 		line = [line]
  106    -1 	for span in line:
  107    -1 		if isinstance(span, tuple):
  108    -1 			s, attrs = span
  109    -1 		else:
  110    -1 			s, attrs = span, {}
  111    -1 
  112    -1 		if attrs.get(bold):
  113    -1 			write('bold')
  114    -1 		if attrs.get('reverse'):
  115    -1 			write('rev')
  116    -1 		if attrs.get('underline'):
  117    -1 			write('smul')
  118    -1 		if attrs.get('italic'):
  119    -1 			write('sitm')
  120    -1 		if 'fg' in attrs:
  121    -1 			write('setaf', attrs['fg'])
  122    -1 		if 'bg' in attrs:
  123    -1 			write('setab', attrs['bg'])
  124    -1 		print(s, end=' ')
  125    -1 		sys.stdout.flush()
  126    -1 		write('sgr0')
   -1   103 def style(
   -1   104 	s,
   -1   105 	bold=False,
   -1   106 	rev=False,
   -1   107 	underline=False,
   -1   108 	italic=False,
   -1   109 	fg=None,
   -1   110 	bg=None,
   -1   111 ):
   -1   112 	styles = ''
   -1   113 	if bold:
   -1   114 		styles += get_cap('bold')
   -1   115 	if rev:
   -1   116 		styles += get_cap('rev')
   -1   117 	if underline:
   -1   118 		styles += get_cap('smul')
   -1   119 	if italic:
   -1   120 		styles += get_cap('sitm')
   -1   121 	if fg is not None:
   -1   122 		styles += get_cap('setaf', fg)
   -1   123 	if bg is not None:
   -1   124 		styles += get_cap('setab', bg)
   -1   125 	return styles + s + get_cap('sgr0')
  127   126 
  128   127 
  129   128 class App:
@@ -138,7 +137,7 @@ class App:
  138   137 				continue
  139   138 			write('cup', i, 0)
  140   139 			write('el')
  141    -1 			print_line(line)
   -1   140 			print(line)
  142   141 
  143   142 		# clear rest of screen
  144   143 		write('cup', len(lines), 0)
@@ -182,7 +181,7 @@ class Example(App):
  182   181 
  183   182 	def render(self):
  184   183 		for key in self.keys:
  185    -1 			yield [(key, {'fg': 13}), 'test']
   -1   184 			yield style(str(key), fg=13) + 'test'
  186   185 
  187   186 
  188   187 if __name__ == '__main__':