boon

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

commit
c71d656276d9dd9ecc01c2ee7a5da985e17bd726
parent
60d5ddaaf6bd3aed4d3c2bbe79333ead1d56f175
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2023-03-30 05:28
make contextmanagers reusable

Diffstat

M boon.py 27 +++++++++++++++++++++++++--

1 files changed, 25 insertions, 2 deletions


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

@@ -7,6 +7,7 @@ import sys
    7     7 import termios
    8     8 import tty
    9     9 from contextlib import contextmanager
   -1    10 from functools import wraps
   10    11 
   11    12 curses.setupterm()
   12    13 
@@ -46,7 +47,29 @@ def move(y, x):
   46    47 	sys.stdout.write(get_cap('cup', y, x))
   47    48 
   48    49 
   49    -1 @contextmanager
   -1    50 class ReusableContextManager:
   -1    51 	def __init__(self, factory, *args, **kwargs):
   -1    52 		self.factory = factory
   -1    53 		self.args = args
   -1    54 		self.kwargs = kwargs
   -1    55 
   -1    56 	def __enter__(self):
   -1    57 		self.mgr = self.factory(*self.args, **self.kwargs)
   -1    58 		return self.mgr.__enter__()
   -1    59 
   -1    60 	def __exit__(self, *args, **kwargs):
   -1    61 		return self.mgr.__exit__(*args, **kwargs)
   -1    62 
   -1    63 
   -1    64 def reusable_contextmanager(func):
   -1    65 	factory = contextmanager(func)
   -1    66 	@wraps(func)
   -1    67 	def wrapper(*args, **kwargs):
   -1    68 		return ReusableContextManager(factory, *args, **kwargs)
   -1    69 	return wrapper
   -1    70 
   -1    71 
   -1    72 @reusable_contextmanager
   50    73 def tty_restore(fd):
   51    74 	old = termios.tcgetattr(fd)
   52    75 	try:
@@ -55,7 +78,7 @@ def tty_restore(fd):
   55    78 		termios.tcsetattr(fd, termios.TCSADRAIN, old)
   56    79 
   57    80 
   58    -1 @contextmanager
   -1    81 @reusable_contextmanager
   59    82 def fullscreen():
   60    83 	sys.stdout.write(get_cap('civis'))
   61    84 	sys.stdout.write(get_cap('smcup'))