Skip to content

Commit

Permalink
Introduce --color option to force colored output
Browse files Browse the repository at this point in the history
With introducing blessings module (from 0.4) which automatically
determines color for output, depending on whether stdout has tty
or not (i.e. piped), we should have a way to 'force' colored output.

For example, `watch --color gpustat` was always suppressing color.
Using the `--color` or `--force-color` option, we can always have
the colored output even if stdout is not a tty.
  • Loading branch information
wookayin committed Sep 17, 2017
1 parent 3551fae commit a38bc5f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ Usage

Options:

* `--no-color` : Suppress color (by default, color is enabled)
* `--color` : Force colored output (even when stdout is not a tty)
* `--no-color` : Suppress color (by default, color is automatically enabled)
* `-u`, `--show-user` : Display username of the process owner
* `-c`, `--show-cmd` : Display the process name
* `-p`, `--show-pid` : Display PID of the process
* `--json` : JSON Output (Experimental, [#10][gh-issue-10])

### Tips

- To periodically watch, try `watch --color -n1.0 gpustat` (built-in watch support will be added soon).
- To periodically watch, try `watch --color -n1.0 gpustat --color` (built-in watch support will be added soon).
- Running `nvidia-smi daemon` (root privilege required) will make the query much **faster**.
- The GPU ID (index) shown by `gpustat` (and `nvidia-smi`) is PCI BUS ID,
while CUDA differently assigns the fastest GPU with the lowest ID by default.
Expand Down
37 changes: 26 additions & 11 deletions gpustat.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
import pynvml as N
from blessings import Terminal

__version__ = '0.4.0.dev0'
__version__ = '0.4.0.dev1'


NOT_SUPPORTED = 'Not Supported'

term = Terminal()


class GPUStat(object):

Expand Down Expand Up @@ -124,11 +122,12 @@ def processes(self):


def print_to(self, fp,
with_colors=True,
with_colors=True, # deprecated arg
show_cmd=False,
show_user=False,
show_pid=False,
gpuname_width=16
gpuname_width=16,
term=Terminal(),
):
# color settings
colors = {}
Expand Down Expand Up @@ -322,17 +321,28 @@ def __repr__(self):

# --- Printing Functions ---

def print_formatted(self, fp=sys.stdout, no_color=False,
def print_formatted(self, fp=sys.stdout, force_color=False, no_color=False,
show_cmd=False, show_user=False, show_pid=False,
gpuname_width=16,
):
# ANSI color configuration
if force_color and no_color:
raise ValueError("--color and --no_color can't be used at the same time")

if force_color:
t_color = Terminal(kind='linux', force_styling=True)
elif no_color:
t_color = Terminal(force_styling=None)
else:
t_color = Terminal() # auto, depending on isatty

# header
time_format = locale.nl_langinfo(locale.D_T_FMT)

header_msg = '{t.bold_white}{hostname}{t.normal} {timestr}'.format(**{
'hostname' : self.hostname,
'timestr' : self.query_time.strftime(time_format),
't' : term if not no_color \
else Terminal(force_styling=None)
't' : t_color,
})

fp.write(header_msg)
Expand All @@ -342,11 +352,11 @@ def print_formatted(self, fp=sys.stdout, no_color=False,
gpuname_width = max([gpuname_width] + [len(g.entry['name']) for g in self])
for g in self:
g.print_to(fp,
with_colors=not no_color,
show_cmd=show_cmd,
show_user=show_user,
show_pid=show_pid,
gpuname_width=gpuname_width)
gpuname_width=gpuname_width,
term=t_color)
fp.write('\n')

fp.flush()
Expand Down Expand Up @@ -407,8 +417,13 @@ def main():
# arguments to gpustat
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--no-color', action='store_true',

parser_color = parser.add_mutually_exclusive_group()
parser_color.add_argument('--force-color', '--color', action='store_true',
help='Force to output with colors')
parser_color.add_argument('--no-color', action='store_true',
help='Suppress colored output')

parser.add_argument('-c', '--show-cmd', action='store_true',
help='Display cmd name of running process')
parser.add_argument('-u', '--show-user', action='store_true',
Expand Down

0 comments on commit a38bc5f

Please sign in to comment.