From 68000272ea2af39944872ff5393bde48b600f32f Mon Sep 17 00:00:00 2001 From: 2Shirt <2xShirt@gmail.com> Date: Fri, 13 Sep 2019 16:14:36 -0700 Subject: [PATCH] Improved clear_screen() * Now uses subprocess.run() instead of os.system() * Avoids weird clear -> print issues * i.e. Missing newlines, etc --- scripts/wk/std.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/scripts/wk/std.py b/scripts/wk/std.py index 53e2fadd..09fadb28 100644 --- a/scripts/wk/std.py +++ b/scripts/wk/std.py @@ -8,12 +8,12 @@ import os import pathlib import platform import re +import subprocess import sys import time import traceback from collections import OrderedDict -from subprocess import CalledProcessError, CompletedProcess try: from termios import tcflush, TCIOFLUSH except ImportError: @@ -337,10 +337,8 @@ def choice(choices, prompt='答えろ!'): def clear_screen(): """Simple wrapper for clear/cls.""" - if os.name == 'nt': - os.system('cls') - else: - os.system('clear') + cmd = 'cls' if os.name == 'nt' else 'clear' + subprocess.run(cmd, check=False, stderr=subprocess.PIPE) 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 try: - if isinstance(_exception, CalledProcessError): + if isinstance(_exception, subprocess.CalledProcessError): message = _exception.stderr if not isinstance(message, str): message = message.decode('utf-8') @@ -403,7 +401,7 @@ def format_function_output(output, indent=INDENT, width=WIDTH): raise GenericWarning('No output') # Ensure we're working with a list - if isinstance(output, CompletedProcess): + if isinstance(output, subprocess.CompletedProcess): stdout = output.stdout if not isinstance(stdout, str): stdout = stdout.decode('utf8')