From 3d3ed5fe974089cd18f8ba3a561613044f5d8863 Mon Sep 17 00:00:00 2001 From: Jongwook Choi Date: Sat, 1 Feb 2020 22:08:25 -0800 Subject: [PATCH] Code cleanup after #78 and tweak on blessed usage - To prevent blinking cursors, do not re-store cursor locations - Workaround sgr0 character issues (#32) again - Misc: various style cleanups --- gpustat/cli.py | 10 +++++++--- gpustat/core.py | 23 +++++++++++------------ 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/gpustat/cli.py b/gpustat/cli.py index 6dc6458..5248c4e 100644 --- a/gpustat/cli.py +++ b/gpustat/cli.py @@ -2,6 +2,7 @@ from __future__ import division from __future__ import print_function +import os import sys import time @@ -44,9 +45,12 @@ def loop_gpustat(interval=1.0, **kwargs): while 1: try: query_start = time.time() - with term.location(0, 0): - print_gpustat(eol_char=term.clear_eol + '\n', **kwargs) - print(term.clear_eos, end='') + + # Move cursor to (0, 0) but do not restore original cursor loc + print(term.move(0, 0), end='') + print_gpustat(eol_char=term.clear_eol + os.linesep, **kwargs) + print(term.clear_eos, end='') + query_duration = time.time() - query_start sleep_duration = interval - query_duration if sleep_duration > 0: diff --git a/gpustat/core.py b/gpustat/core.py index bf748a7..1365010 100644 --- a/gpustat/core.py +++ b/gpustat/core.py @@ -32,6 +32,8 @@ NOT_SUPPORTED = 'Not Supported' MB = 1024 * 1024 +IS_WINDOWS = 'windows' in platform.platform().lower() + class GPUStat(object): @@ -164,8 +166,11 @@ def print_to(self, fp, show_power=None, show_fan_speed=None, gpuname_width=16, - term=Terminal(), + term=None, ): + if term is None: + term = Terminal(stream=sys.stdout) + # color settings colors = {} @@ -345,7 +350,7 @@ def get_process_info(nv_process): process['command'] = os.path.basename(_cmdline[0]) process['full_command'] = _cmdline # Bytes to MBytes - # if drivers are not TTC this will be None. + # if drivers are not TTC this will be None. usedmem = nv_process.usedGpuMemory // MB if \ nv_process.usedGpuMemory else None process['gpu_memory_usage'] = usedmem @@ -476,9 +481,6 @@ def __repr__(self): s += '\n'.join(' ' + str(g) for g in self.gpus) s += '\n])' return s - - def is_windows(self): - return 'windows' in platform.platform().lower() # --- Printing Functions --- @@ -494,11 +496,10 @@ def print_formatted(self, fp=sys.stdout, force_color=False, no_color=False, " be used at the same time") if force_color: - t_color = Terminal(kind='linux', force_styling=True) + t_color = Terminal(force_styling=True) # workaround of issue #32 (watch doesn't recognize sgr0 characters) - try: t_color.normal = u'\x1b[0;10m' - except: pass + t_color._normal = u'\x1b[0;10m' elif no_color: t_color = Terminal(force_styling=None) else: @@ -510,10 +511,8 @@ def print_formatted(self, fp=sys.stdout, force_color=False, no_color=False, # header if show_header: - # no localization is available that easily - # however,everybody should be able understand the - # standard datetime string format %Y-%m-%d %H:%M:%S - if self.is_windows(): + if IS_WINDOWS: + # no localization is available; just use a reasonable default # same as str(timestr) but without ms timestr = self.query_time.strftime('%Y-%m-%d %H:%M:%S') else: