Skip to content

Commit

Permalink
Code cleanup after #78 and tweak on blessed usage
Browse files Browse the repository at this point in the history
- To prevent blinking cursors, do not re-store cursor locations
- Workaround sgr0 character issues (#32) again
- Misc: various style cleanups
  • Loading branch information
wookayin committed Feb 2, 2020
1 parent ebd7f46 commit 3d3ed5f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
10 changes: 7 additions & 3 deletions gpustat/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import division
from __future__ import print_function

import os
import sys
import time

Expand Down Expand Up @@ -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:
Expand Down
23 changes: 11 additions & 12 deletions gpustat/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
NOT_SUPPORTED = 'Not Supported'
MB = 1024 * 1024

IS_WINDOWS = 'windows' in platform.platform().lower()


class GPUStat(object):

Expand Down Expand Up @@ -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 = {}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 ---

Expand All @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 3d3ed5f

Please sign in to comment.