boon

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

commit
d2c215ddae1d89a449eeb074b05292e4c3d9d5ad
parent
1a9361c86eb1cb8c0d7980d8a3280483b67ae0e1
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-07-13 07:06
self-pipe to avoid concurrency issues with signal

https://ldpreload.com/blog/signalfd-is-useless

Diffstat

M boon.py 14 +++++++++++---

1 files changed, 11 insertions, 3 deletions


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

@@ -87,6 +87,9 @@ class App:
   87    87 		self.old_lines = []
   88    88 		self.running = False
   89    89 		self.timeout = 0.5
   -1    90 
   -1    91 		# self-pipe to avoid concurrency issues with signal
   -1    92 		self.resize_in, self.resize_out = os.pipe2(os.O_NONBLOCK)
   90    93 		signal.signal(signal.SIGWINCH, self.on_resize)
   91    94 
   92    95 	def update(self, force=False):
@@ -107,8 +110,7 @@ class App:
  107   110 		self.old_lines = lines
  108   111 
  109   112 	def on_resize(self, *args):
  110    -1 		self.cols, self.rows = shutil.get_terminal_size()
  111    -1 		self.update(force=True)
   -1   113 		os.write(self.resize_out, b'.')
  112   114 
  113   115 	def run(self):
  114   116 		self.running = True
@@ -116,9 +118,15 @@ class App:
  116   118 			self.on_resize()
  117   119 			while self.running:
  118   120 				try:
  119    -1 					r, _w, _e = select.select([sys.stdin], [], [], self.timeout)
   -1   121 					r, _w, _e = select.select(
   -1   122 						[sys.stdin, self.resize_in], [], [], self.timeout
   -1   123 					)
  120   124 				except select.error:
  121   125 					continue
   -1   126 				if self.resize_in in r:
   -1   127 					os.read(self.resize_in, 8)
   -1   128 					self.cols, self.rows = shutil.get_terminal_size()
   -1   129 					self.update(force=True)
  122   130 				if sys.stdin in r:
  123   131 					self.on_key(getch())
  124   132 					self.update()