summaryrefslogtreecommitdiffstats
path: root/planetlab/pssh/psshlib/color.py
diff options
context:
space:
mode:
authorthibauth <thibauth@30fcff6e-8de6-41c7-acce-77ff6d1dd07b>2011-08-01 16:05:49 +0000
committerthibauth <thibauth@30fcff6e-8de6-41c7-acce-77ff6d1dd07b>2011-08-01 16:05:49 +0000
commit3bb7785b59ef92278b24f2636b5250b07ce788ee (patch)
tree44679a6548631ff2f278bcf941cdb6c56349cdf5 /planetlab/pssh/psshlib/color.py
parenta33698d5d7d4bb49fadb4e29daef0d6d58c7c2fc (diff)
downloadpacemaker-3bb7785b59ef92278b24f2636b5250b07ce788ee.tar.gz
Planetlab utilities
git-svn-id: https://scm.gforge.inria.fr/svn/pacemaker@50 30fcff6e-8de6-41c7-acce-77ff6d1dd07b
Diffstat (limited to 'planetlab/pssh/psshlib/color.py')
-rw-r--r--planetlab/pssh/psshlib/color.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/planetlab/pssh/psshlib/color.py b/planetlab/pssh/psshlib/color.py
new file mode 100644
index 0000000..eb9f001
--- /dev/null
+++ b/planetlab/pssh/psshlib/color.py
@@ -0,0 +1,39 @@
+# Copyright (c) 2009, Andrew McNabb
+# Copyright (c) 2003-2008, Brent N. Chun
+
+def with_color(string, fg, bg=49):
+ '''Given foreground/background ANSI color codes, return a string that,
+ when printed, will format the supplied string using the supplied colors.
+ '''
+ return "\x1b[%dm\x1b[%dm%s\x1b[39m\x1b[49m" % (fg, bg, string)
+
+def B(string):
+ '''Returns a string that, when printed, will display the supplied string
+ in ANSI bold.
+ '''
+ return "\x1b[1m%s\x1b[22m" % string
+
+def r(string): return with_color(string, 31) # Red
+def g(string): return with_color(string, 32) # Green
+def y(string): return with_color(string, 33) # Yellow
+def b(string): return with_color(string, 34) # Blue
+def m(string): return with_color(string, 35) # Magenta
+def c(string): return with_color(string, 36) # Cyan
+def w(string): return with_color(string, 37) # White
+
+#following from Python cookbook, #475186
+def has_colors(stream):
+ '''Returns boolean indicating whether or not the supplied stream supports
+ ANSI color.
+ '''
+ if not hasattr(stream, "isatty"):
+ return False
+ if not stream.isatty():
+ return False # auto color only on TTYs
+ try:
+ import curses
+ curses.setupterm()
+ return curses.tigetnum("colors") > 2
+ except:
+ # guess false in case of error
+ return False