Explorar o código

tools/qvm-run: factor out print_no_color function

Will be useful for other messages that should not be in red.
Marek Marczykowski-Górecki %!s(int64=5) %!d(string=hai) anos
pai
achega
d34b1bfc4f
Modificáronse 1 ficheiros con 13 adicións e 7 borrados
  1. 13 7
      qubesadmin/tools/qvm_run.py

+ 13 - 7
qubesadmin/tools/qvm_run.py

@@ -128,6 +128,16 @@ def copy_stdin(stream):
             break
     stream.close()
 
+def print_no_color(msg, file, color):
+    '''Print a *msg* to *file* without coloring it.
+    Namely reset to base color first, print a message, then restore color.
+    '''
+    if color:
+        print('\033[0m{}\033[0;{}m'.format(msg, color), file=file)
+    else:
+        print(msg, file=file)
+
+
 def main(args=None, app=None):
     '''Main function of qvm-run tool'''
     args = parser.parse_args(args, app=app)
@@ -200,13 +210,9 @@ def main(args=None, app=None):
                 continue
             try:
                 if verbose > 0:
-                    if args.color_output:
-                        print('\033[0mRunning \'{}\' on {}\033[0;{}m'.format(
-                            args.cmd, vm.name, args.color_stderr),
-                            file=sys.stderr)
-                    else:
-                        print('Running \'{}\' on {}'.format(args.cmd, vm.name),
-                            file=sys.stderr)
+                    print_no_color(
+                        'Running \'{}\' on {}'.format(args.cmd, vm.name),
+                        file=sys.stderr, color=args.color_stderr)
                 if args.gui and not args.dispvm:
                     wait_session = vm.run_service('qubes.WaitForSession',
                         stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)