Эх сурвалжийг харах

use colorma to make fancy colors

should work on windows and linux ...
oz123 11 жил өмнө
parent
commit
c3ac71b1dc
4 өөрчлөгдсөн 41 нэмэгдсэн , 34 устгасан
  1. 13 12
      pwman/ui/cli.py
  2. 14 14
      pwman/ui/tools.py
  3. 7 5
      pwman/ui/win.py
  4. 7 3
      scripts/pwman3

+ 13 - 12
pwman/ui/cli.py

@@ -43,6 +43,7 @@ from pwman.ui import tools
 from pwman.ui.tools import CliMenu
 from pwman.ui.tools import CliMenuItem
 from pwman.ui.tools import CLICallback
+from colorama import Fore
 
 try:
     import readline
@@ -172,15 +173,15 @@ class PwmanCli(cmd.Cmd):
     def print_node(self, node):
         width = str(tools._defaultwidth)
         print "Node %d." % (node.get_id())
-        print ("%"+width+"s %s") % (tools.typeset("Username:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Username:", Fore.RED),
                                     node.get_username())
-        print ("%"+width+"s %s") % (tools.typeset("Password:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Password:", Fore.RED),
                                     node.get_password())
-        print ("%"+width+"s %s") % (tools.typeset("Url:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Url:", Fore.RED),
                                     node.get_url())
-        print ("%"+width+"s %s") % (tools.typeset("Notes:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Notes:", Fore.RED),
                                     node.get_notes())
-        print tools.typeset("Tags: ", tools.ANSI.Red),
+        print tools.typeset("Tags: ", Fore.RED),
         for t in node.get_tags():
             print " %s \n" % t.get_name(),
 
@@ -453,7 +454,7 @@ class PwmanCli(cmd.Cmd):
 
                 fmt = "%%5d. %%-%ds %%-%ds" % (name_len, tagstring_len)
                 print tools.typeset(fmt % (n.get_id(), name, tagstring),
-                                    tools.ANSI.Yellow, False)
+                                    Fore.YELLOW, False)
                 i += 1
                 if i > rows-2:
                     i = 0
@@ -767,15 +768,15 @@ class PwmanCliNew(PwmanCli):
     def print_node(self, node):
         width = str(tools._defaultwidth)
         print "Node %d." % (node.get_id())
-        print ("%"+width+"s %s") % (tools.typeset("Username:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Username:", Fore.RED),
                                     node.get_username())
-        print ("%"+width+"s %s") % (tools.typeset("Password:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Password:", Fore.RED),
                                     node.get_password())
-        print ("%"+width+"s %s") % (tools.typeset("Url:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Url:", Fore.RED),
                                     node.get_url())
-        print ("%"+width+"s %s") % (tools.typeset("Notes:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Notes:", Fore.RED),
                                     node.get_notes())
-        print tools.typeset("Tags: ", tools.ANSI.Red),
+        print tools.typeset("Tags: ", Fore.RED),
         for t in node.get_tags():
             print " %s " % t
         print
@@ -886,7 +887,7 @@ class PwmanCliNew(PwmanCli):
                 fmt = "%%5d. %%-%ds %%-%ds" % (name_len, tagstring_len)
                 formatted_entry = tools.typeset(fmt % (n.get_id(),
                                                 name, tagstring),
-                                                tools.ANSI.Yellow, False)
+                                                Fore.YELLOW, False)
                 print formatted_entry
                 i += 1
                 if i > rows-2:

+ 14 - 14
pwman/ui/tools.py

@@ -27,6 +27,7 @@ import getpass
 import sys
 import struct
 import os
+import colorama
 # import traceback
 
 if sys.platform != 'win32':
@@ -48,6 +49,7 @@ else:
 
 _defaultwidth = 10
 
+
 class ANSI(object):
     """
     ANSI Colors
@@ -67,19 +69,16 @@ class ANSI(object):
 
 
 def typeset(text, color, bold=False, underline=False):
-    """print colored strings"""
+    """
+    print colored strings using colorama
+    """
     if not config.get_value("Global", "colors") == 'yes':
         return text
-    if (bold):
-        bold = "%d ;" % (ANSI.Bold)
-    else:
-        bold = ""
-    if (underline):
-        underline = "%d;" % (ANSI.Underscore)
-    else:
-        underline = ""
-    return "\033[%s%s%sm%s\033[%sm" % (bold, underline, color,
-                                       text, ANSI.Reset)
+    if bold:
+        text = colorama.Style.BRIGHT + text
+    if underline and not 'win32' in sys.platform:
+        text = ANSI.Underscore + text
+    return color+text+colorama.Style.RESET_ALL
 
 
 def select(question, possible):
@@ -180,6 +179,7 @@ def getinput(question, default="", completer=None, width=_defaultwidth):
         readline.set_startup_hook()
         return x
 
+
 def getyesno(question, defaultyes=False, width=_defaultwidth):
     if (defaultyes):
         default = "[Y/n]"
@@ -222,9 +222,9 @@ class CliMenu(object):
                 else:
                     currentstr = current
 
-                print ("%d - %-"+str(_defaultwidth)+\
-                      "s %s") % (i, x.name+":",
-                                 currentstr)
+                print ("%d - %-"+str(_defaultwidth)
+                       + "s %s") % (i, x.name+":",
+                                    currentstr)
             print "%c - Finish editing" % ('X')
             option = getonechar("Enter your choice:")
             try:

+ 7 - 5
pwman/ui/win.py

@@ -30,6 +30,8 @@ import msvcrt
 import pwman.util.config as config
 import ast
 from pwman.util.crypto import zerome
+from colorama import Fore
+
 
 class PwmanCliWinNew(PwmanCliNew):
 
@@ -80,15 +82,15 @@ class PwmanCliWinNew(PwmanCliNew):
     def print_node(self, node):
         width = str(tools._defaultwidth)
         print "Node %d." % (node.get_id())
-        print ("%"+width+"s %s") % (tools.typeset("Username:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Username:", Fore.RED),
                                     node.get_username())
-        print ("%"+width+"s %s") % (tools.typeset("Password:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Password:", Fore.RED),
                                     node.get_password())
-        print ("%"+width+"s %s") % (tools.typeset("Url:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Url:", Fore.RED),
                                     node.get_url())
-        print ("%"+width+"s %s") % (tools.typeset("Notes:", tools.ANSI.Red),
+        print ("%"+width+"s %s") % (tools.typeset("Notes:", Fore.RED),
                                     node.get_notes())
-        print tools.typeset("Tags: ", tools.ANSI.Red),
+        print tools.typeset("Tags: ", Fore.RED),
         for t in node.get_tags():
             print " %s \n" % t.get_name(),
 

+ 7 - 3
scripts/pwman3

@@ -58,7 +58,7 @@ if 'darwin' in sys.platform:
     from pwman.ui.mac import PwmanCliMacNew as PwmanCliNew
     OSX = True
 elif 'win' in sys.platform:
-    from pwman.ui.cli import PwmanCli 
+    from pwman.ui.cli import PwmanCli
     from pwman.ui.win import PwmanCliWinNew as PwmanCliNew
     OSX = False
 else:
@@ -104,7 +104,11 @@ try:
 
     config.set_defaults(default_config)
     if 'win' in sys.platform:
-		config.set_value("Global", "colors", 'no')
+        try:
+            import colorama
+            colorama.init()
+        except ImportError:
+            config.set_value("Global", "colors", 'no')
     if os.path.exists(config_file):
         config.load(config_file)
         xselpath = config.get_value("Global", "xselpath")
@@ -169,7 +173,7 @@ try:
 finally:
     try:
         if _saveconfig:
-            config.save(config_file) 
+            config.save(config_file)
     except Exception, e:
         print "Error: %s" % (e)
         sys.exit(-1)