Add stty module and replace os.system

This commit is contained in:
Samuel Sloniker 2021-08-30 09:22:06 -07:00
parent de5850351b
commit 5f5f487689
2 changed files with 18 additions and 5 deletions

12
stty.py Normal file
View File

@ -0,0 +1,12 @@
import subprocess
def stty(raw=None, echo=None):
args = ['stty']
if raw is not None:
args.append('raw' if raw else '-raw')
if echo is not None:
args.append('echo' if echo else '-echo')
subprocess.run(args)

11
worm.py
View File

@ -10,6 +10,7 @@ import os
import argparse import argparse
import json import json
import info import info
import stty
from blessings import Terminal from blessings import Terminal
@ -211,15 +212,15 @@ except (FileNotFoundError, IsFullScreen):
try: try:
with term.fullscreen(): with term.fullscreen():
os.system('stty raw -echo') stty.stty(raw=True, echo=False)
draw_frame() draw_frame()
while True: while True:
if do_help: if do_help:
for info in (info_1, info_2): for info in (info_1, info_2):
os.system('stty -raw') stty.stty(raw=False)
print(info, end='') print(info, end='')
sys.stdout.flush() sys.stdout.flush()
os.system('stty raw') stty.stty(raw=True)
k = await_keys('cC') k = await_keys('cC')
if k in '': if k in '':
@ -241,7 +242,7 @@ except RanIntoSomethingError:
os.unlink(gamesave_path) os.unlink(gamesave_path)
except FileNotFoundError: except FileNotFoundError:
pass pass
os.system('stty -raw echo') stty.stty(raw=False, echo=True)
print('', end='\r\n') print('', end='\r\n')
if size + 1 >= (height-3) * (width-1): if size + 1 >= (height-3) * (width-1):
print('You won!', end='\r\n') print('You won!', end='\r\n')
@ -250,4 +251,4 @@ except RanIntoSomethingError:
print(f'Your final score was {score}', end='\r\n') print(f'Your final score was {score}', end='\r\n')
print('', end='\r\n') print('', end='\r\n')
finally: finally:
os.system('stty -raw echo') stty.stty(raw=False, echo=True)