Improved clear_screen()

* Now uses subprocess.run() instead of os.system()
* Avoids weird clear -> print issues
  * i.e. Missing newlines, etc
This commit is contained in:
2Shirt 2019-09-13 16:14:36 -07:00
parent e52e90454d
commit 68000272ea
Signed by: 2Shirt
GPG key ID: 152FAC923B0E132C

View file

@ -8,12 +8,12 @@ import os
import pathlib import pathlib
import platform import platform
import re import re
import subprocess
import sys import sys
import time import time
import traceback import traceback
from collections import OrderedDict from collections import OrderedDict
from subprocess import CalledProcessError, CompletedProcess
try: try:
from termios import tcflush, TCIOFLUSH from termios import tcflush, TCIOFLUSH
except ImportError: except ImportError:
@ -337,10 +337,8 @@ def choice(choices, prompt='答えろ!'):
def clear_screen(): def clear_screen():
"""Simple wrapper for clear/cls.""" """Simple wrapper for clear/cls."""
if os.name == 'nt': cmd = 'cls' if os.name == 'nt' else 'clear'
os.system('cls') subprocess.run(cmd, check=False, stderr=subprocess.PIPE)
else:
os.system('clear')
def format_exception_message(_exception, indent=INDENT, width=WIDTH): def format_exception_message(_exception, indent=INDENT, width=WIDTH):
@ -354,7 +352,7 @@ def format_exception_message(_exception, indent=INDENT, width=WIDTH):
# Use known argument index or first string found # Use known argument index or first string found
try: try:
if isinstance(_exception, CalledProcessError): if isinstance(_exception, subprocess.CalledProcessError):
message = _exception.stderr message = _exception.stderr
if not isinstance(message, str): if not isinstance(message, str):
message = message.decode('utf-8') message = message.decode('utf-8')
@ -403,7 +401,7 @@ def format_function_output(output, indent=INDENT, width=WIDTH):
raise GenericWarning('No output') raise GenericWarning('No output')
# Ensure we're working with a list # Ensure we're working with a list
if isinstance(output, CompletedProcess): if isinstance(output, subprocess.CompletedProcess):
stdout = output.stdout stdout = output.stdout
if not isinstance(stdout, str): if not isinstance(stdout, str):
stdout = stdout.decode('utf8') stdout = stdout.decode('utf8')