diff options
author | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-12 09:54:32 +0200 |
---|---|---|
committer | Aineopintojen-harjoitustyo-Algoritmit-j <github-hy-tiralabra@v.hix.fi> | 2024-01-12 09:54:32 +0200 |
commit | 1ac23423308e9dcb662cdaf9709b8e28787b5a2b (patch) | |
tree | 1a97b0a55019312156e740f20454a9366186c2a4 /tui | |
parent | f4b13f9b739a2b70c457b51d3117492527cbd015 (diff) |
Cleaning up duplicate terminal modification code inside Tui class.
Diffstat (limited to 'tui')
-rw-r--r-- | tui/tui.py | 24 |
1 files changed, 11 insertions, 13 deletions
@@ -1,27 +1,25 @@ import termios, fcntl, sys, os -fd = sys.stdin.fileno() - -oldterm = termios.tcgetattr(fd) -newattr = termios.tcgetattr(fd) -newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO -termios.tcsetattr(fd, termios.TCSANOW, newattr) - -oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) -fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK) - class Tui(): def __init__(self): + # Vaatii hieman terminaaliasetusten muokkaamista jotta yksittäiset + # napin painallukset voidaan lukea # https://stackoverflow.com/questions/983354/how-do-i-wait-for-a-pressed-key fd = sys.stdin.fileno() - oldterm = termios.tcgetattr(fd) + self.oldterm = termios.tcgetattr(fd) + newattr = termios.tcgetattr(fd) newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO termios.tcsetattr(fd, termios.TCSANOW, newattr) + self.oldflags = fcntl.fcntl(fd, fcntl.F_GETFL) + fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags | os.O_NONBLOCK) + def __del__(self): - termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm) - fcntl.fcntl(fd, fcntl.F_SETFL, oldflags) + # palautetaan terminaali takaisin alkupetäiseen uskoon + fd = sys.stdin.fileno() + termios.tcsetattr(fd, termios.TCSAFLUSH, self.oldterm) + fcntl.fcntl(fd, fcntl.F_SETFL, self.oldflags) def set_color(self, color): if color>=0 and color<16: |