boon

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

commit
60d5ddaaf6bd3aed4d3c2bbe79333ead1d56f175
parent
ad1d1911cbc38bb91d9a19814c8746949f967fcf
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2022-06-19 07:08
add more examples

Diffstat

A examples/colors.py 12 ++++++++++++
R example.py -> examples/example.py 0
A examples/getpass.py 13 +++++++++++++
A examples/hello_world.py 14 ++++++++++++++
A examples/layout.py 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

5 files changed, 115 insertions, 0 deletions


diff --git a/examples/colors.py b/examples/colors.py

@@ -0,0 +1,12 @@
   -1     1 import boon
   -1     2 
   -1     3 def print_colors(start, end):
   -1     4 	for i in range(start, end):
   -1     5 		print(boon.get_cap('setab', i) + '{:>4}'.format(i), end='')
   -1     6 	print(boon.get_cap('sgr0'))
   -1     7 
   -1     8 print_colors(0, 8)
   -1     9 print_colors(8, 16)
   -1    10 for start in range(16, 232, 6):
   -1    11 	print_colors(start, start + 6)
   -1    12 print_colors(232, 256)

diff --git a/example.py b/examples/example.py

diff --git a/examples/getpass.py b/examples/getpass.py

@@ -0,0 +1,13 @@
   -1     1 import sys
   -1     2 import termios
   -1     3 from boon import tty_restore
   -1     4 
   -1     5 def getpass(prompt):
   -1     6 	fd = sys.stdin.fileno()
   -1     7 	with tty_restore(fd):
   -1     8 		flags = termios.tcgetattr(fd)
   -1     9 		flags[3] &= ~termios.ECHO
   -1    10 		termios.tcsetattr(fd, termios.TCSADRAIN, flags)
   -1    11 		return input(prompt)
   -1    12 
   -1    13 print(getpass('Password: '))

diff --git a/examples/hello_world.py b/examples/hello_world.py

@@ -0,0 +1,14 @@
   -1     1 import boon
   -1     2 
   -1     3 
   -1     4 class Example(boon.App):
   -1     5 	def render(self, rows, cols):
   -1     6 		yield 'Hello World'
   -1     7 
   -1     8 	def on_key(self, key):
   -1     9 		if key == 'q':
   -1    10 			self.running = False
   -1    11 
   -1    12 
   -1    13 example = Example()
   -1    14 example.run()

diff --git a/examples/layout.py b/examples/layout.py

@@ -0,0 +1,76 @@
   -1     1 import textwrap
   -1     2 import boon
   -1     3 
   -1     4 LOREM = """Aspernatur dolor quas explicabo adipisci. Velit possimus
   -1     5 eveniet odio quo quia. Earum quia vel officia et ea. Dolores quia et est
   -1     6 recusandae rem neque et. Magnam voluptates ut nulla commodi laboriosam
   -1     7 vitae.
   -1     8 
   -1     9 Quod facilis nobis nihil blanditiis. Minima voluptatum alias ut rerum
   -1    10 numquam eos. Aspernatur dolorem omnis quae cumque fugiat reiciendis vel.
   -1    11 Deserunt necessitatibus sit at voluptates aut id omnis. Aspernatur odit
   -1    12 impedit delectus. Voluptatem ducimus totam repudiandae quisquam quis aut
   -1    13 fugiat.
   -1    14 
   -1    15 In a est iste. Molestiae qui consequatur ut aspernatur alias temporibus
   -1    16 est delectus. A ut cum omnis eaque. Veritatis fugit rem vel."""
   -1    17 
   -1    18 
   -1    19 def fix_width(s, w):
   -1    20 	return '{0:{1}.{1}}'.format(s, w)
   -1    21 
   -1    22 
   -1    23 def columns(rows, cols, sep=''):
   -1    24 	cols = [(iter(i), w) for i, w in cols]
   -1    25 	for _ in range(rows):
   -1    26 		yield sep.join(fix_width(next(i, ''), w) for i, w in cols)
   -1    27 
   -1    28 
   -1    29 def box(s, rows, cols):
   -1    30 	lines = iter(textwrap.wrap(s, cols))
   -1    31 	c = cols - 2
   -1    32 	yield '+%s+' % ('-' * c)
   -1    33 	for _ in range(rows - 2):
   -1    34 		yield '|%s|' % fix_width(next(lines, ''), c)
   -1    35 	yield '+%s+' % ('-' * c)
   -1    36 
   -1    37 
   -1    38 def overlay(rows, bottom, top, y, x):
   -1    39 	bottom = iter(bottom)
   -1    40 	top = iter(top)
   -1    41 	for i in range(rows):
   -1    42 		if i < y:
   -1    43 			yield next(bottom, '')
   -1    44 		else:
   -1    45 			b = next(bottom, '')
   -1    46 			t = next(top, '')
   -1    47 			yield b[:x] + t + b[x + len(t):]
   -1    48 
   -1    49 
   -1    50 class Example(boon.App):
   -1    51 	def __init__(self):
   -1    52 		super().__init__()
   -1    53 		self.factor = 0.5
   -1    54 
   -1    55 	def render(self, rows, cols):
   -1    56 		sep = ' | '
   -1    57 		left = int((cols - len(sep)) * self.factor)
   -1    58 		right = cols - len(sep) - left
   -1    59 		bottom = columns(rows, [
   -1    60 			(textwrap.wrap(LOREM, left), left),
   -1    61 			(textwrap.wrap(LOREM, right), right),
   -1    62 		], sep=sep)
   -1    63 		top = box(LOREM, rows // 2, cols // 2)
   -1    64 		return overlay(rows, bottom, top, rows // 4, cols // 4)
   -1    65 
   -1    66 	def on_key(self, key):
   -1    67 		if key == 'q':
   -1    68 			self.running = False
   -1    69 		elif key == '>':
   -1    70 			self.factor += 0.1
   -1    71 		elif key == '<':
   -1    72 			self.factor -= 0.1
   -1    73 
   -1    74 
   -1    75 example = Example()
   -1    76 example.run()