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