easy_tui.window
1class Window: 2 """ 3Provides utilities for storing and printing one terminal-screen's worth of text. 4Intended to be an inherited class. 5 6When inheriting from `Window`, you may overload the `__init__` class. If you do, 7be sure to include the line `self.new_buffer()` at the end of the 8`__init__()` definition. You must also define self.window_height. 9 10Methods in this module also implement logging from the `logging` library. To enable 11logging, include the logging module in your project and run `self.do_logging()` in 12your __init__() definition. This module logs method calls and prints them at the 13debug level. 14 """ 15 16 window_height = 27 17 """Defines the height of the window. May be overridden.""" 18 19 logs = False 20 """Set to True (by calling self.do_logging()) to enable logging.""" 21 22 def __init__(self, window_height: int = 27): 23 """ 24Initializes a new Window. If deriving from this class, define `self.window_height` 25datamember and include the lines `self.new_buffer()` and (optionally) `self.do_logging()` 26in your __init__() function. 27 """ 28 29 self.window_height = window_height 30 self.new_buffer() 31 if self.logs: logging.debug(f"::easy_tui::Window - Initializing {self.__name__}") 32 33 def do_logging(self): 34 """Call to enable logging at the debug level.""" 35 self.logs = True 36 37 def __repr__(self): 38 """Prints the stored buffer to the console. Call after updating buffer.""" 39 return '\n'.join(self.buffer) 40 41 def new_buffer(self): 42 """ 43Creates a new buffer (`self.buffer`) with number of lines = `self.window_height`. 44Modify lines individually by accessing their index. 45 46e.g. 47``` 48>>> self.buffer[2] = "This is the third line in the screen" 49>>> self.print() 50· 51This is the third line in the screen 52· 53·...(23 lines) 54``` 55 56... Outputs a terminal screen's worth of lines. The third line contains the included 57text. 58 """ 59 if self.logs: logging.debug(f"::easy_tui::Window - Creating buffer of height {self.window_height} on {self.__name__}") 60 self.buffer = ["" for _ in range(self.window_height)] 61 62 def print(self): 63 """Alternative to printing the Window with print(...)""" 64 if self.logs: logging.debug(f"::easy_tui::Window - print() called on {self.__name__}") 65 for line in self.buffer: 66 print(line) 67 68 def wipe(self): 69 """Wipes each line of the buffer""" 70 if self.logs: logging.debug(f"::easy_tui::Window - wipe() called on {self.__name__}") 71 for i in range(self.window_height): 72 self.buffer[i] = "" 73 self.print()
2class Window: 3 """ 4Provides utilities for storing and printing one terminal-screen's worth of text. 5Intended to be an inherited class. 6 7When inheriting from `Window`, you may overload the `__init__` class. If you do, 8be sure to include the line `self.new_buffer()` at the end of the 9`__init__()` definition. You must also define self.window_height. 10 11Methods in this module also implement logging from the `logging` library. To enable 12logging, include the logging module in your project and run `self.do_logging()` in 13your __init__() definition. This module logs method calls and prints them at the 14debug level. 15 """ 16 17 window_height = 27 18 """Defines the height of the window. May be overridden.""" 19 20 logs = False 21 """Set to True (by calling self.do_logging()) to enable logging.""" 22 23 def __init__(self, window_height: int = 27): 24 """ 25Initializes a new Window. If deriving from this class, define `self.window_height` 26datamember and include the lines `self.new_buffer()` and (optionally) `self.do_logging()` 27in your __init__() function. 28 """ 29 30 self.window_height = window_height 31 self.new_buffer() 32 if self.logs: logging.debug(f"::easy_tui::Window - Initializing {self.__name__}") 33 34 def do_logging(self): 35 """Call to enable logging at the debug level.""" 36 self.logs = True 37 38 def __repr__(self): 39 """Prints the stored buffer to the console. Call after updating buffer.""" 40 return '\n'.join(self.buffer) 41 42 def new_buffer(self): 43 """ 44Creates a new buffer (`self.buffer`) with number of lines = `self.window_height`. 45Modify lines individually by accessing their index. 46 47e.g. 48``` 49>>> self.buffer[2] = "This is the third line in the screen" 50>>> self.print() 51· 52This is the third line in the screen 53· 54·...(23 lines) 55``` 56 57... Outputs a terminal screen's worth of lines. The third line contains the included 58text. 59 """ 60 if self.logs: logging.debug(f"::easy_tui::Window - Creating buffer of height {self.window_height} on {self.__name__}") 61 self.buffer = ["" for _ in range(self.window_height)] 62 63 def print(self): 64 """Alternative to printing the Window with print(...)""" 65 if self.logs: logging.debug(f"::easy_tui::Window - print() called on {self.__name__}") 66 for line in self.buffer: 67 print(line) 68 69 def wipe(self): 70 """Wipes each line of the buffer""" 71 if self.logs: logging.debug(f"::easy_tui::Window - wipe() called on {self.__name__}") 72 for i in range(self.window_height): 73 self.buffer[i] = "" 74 self.print()
Provides utilities for storing and printing one terminal-screen's worth of text. Intended to be an inherited class.
When inheriting from Window
, you may overload the __init__
class. If you do,
be sure to include the line self.new_buffer()
at the end of the
__init__()
definition. You must also define self.window_height.
Methods in this module also implement logging from the logging
library. To enable
logging, include the logging module in your project and run self.do_logging()
in
your __init__() definition. This module logs method calls and prints them at the
debug level.
23 def __init__(self, window_height: int = 27): 24 """ 25Initializes a new Window. If deriving from this class, define `self.window_height` 26datamember and include the lines `self.new_buffer()` and (optionally) `self.do_logging()` 27in your __init__() function. 28 """ 29 30 self.window_height = window_height 31 self.new_buffer() 32 if self.logs: logging.debug(f"::easy_tui::Window - Initializing {self.__name__}")
Initializes a new Window. If deriving from this class, define self.window_height
datamember and include the lines self.new_buffer()
and (optionally) self.do_logging()
in your __init__() function.
42 def new_buffer(self): 43 """ 44Creates a new buffer (`self.buffer`) with number of lines = `self.window_height`. 45Modify lines individually by accessing their index. 46 47e.g. 48``` 49>>> self.buffer[2] = "This is the third line in the screen" 50>>> self.print() 51· 52This is the third line in the screen 53· 54·...(23 lines) 55``` 56 57... Outputs a terminal screen's worth of lines. The third line contains the included 58text. 59 """ 60 if self.logs: logging.debug(f"::easy_tui::Window - Creating buffer of height {self.window_height} on {self.__name__}") 61 self.buffer = ["" for _ in range(self.window_height)]
Creates a new buffer (self.buffer
) with number of lines = self.window_height
.
Modify lines individually by accessing their index.
e.g.
>>> self.buffer[2] = "This is the third line in the screen"
>>> self.print()
·
This is the third line in the screen
·
·...(23 lines)
... Outputs a terminal screen's worth of lines. The third line contains the included text.