License New BSD license
Lines 33
Keywords
color (8) highlight (1) pretty (3) print (6) stdout (1) style (1) stylize (1) unix (2)
Permissions
Owner: impiaaa
Viewable by Everyone
Editable by All Siafoo Users
Hide
Bored? Check out the Recent Activity on Siafoo Join Siafoo Now or Learn More

String Stylizer Atom Feed 1

In Brief Stylizes a string (changes its color, makes it bold, etc.) for pretty output to a *nix terminal. Example:... more
# 's
 1styles = {"normal":	0,
2 "bold": 1,
3 "underline": 4,
4 "blink": 5,
5 "inverse": 7,
6 "black_fg": 30,
7 "red_fg": 31,
8 "green_fg": 32,
9 "yellow_fg": 33,
10 "blue_fg": 34,
11 "magenta_fg": 35,
12 "cyan_fg": 36,
13 "gray_fg": 37,
14 "default_fg": 39,
15 "black_bg": 40,
16 "red_bg": 41,
17 "green_bg": 42,
18 "yellow_bg": 43,
19 "blue_bg": 44,
20 "magenta_bg": 45,
21 "cyan_bg": 46,
22 "gray_bg": 47,
23 "default_bg": 49}
24for key, val in styles.items(): exec "%s = %i" % (key.upper(), val)
25RESET = "\033[m"
26def style(s='', *args):
27 formatted = "\033["
28 for arg in args:
29 if type(arg) == str: arg = styles[arg]
30 formatted += "%i;" % arg
31 if formatted.endswith(';'): formatted = formatted[:-1]
32 formatted += "m"+s
33 return formatted

Stylizes a string (changes its color, makes it bold, etc.) for pretty output to a *nix terminal. Example:

# 's
1print style("Hello, Color!", BOLD, RED_FG, CYAN_BG)+RESET

Note that the RESET constant is the equivalent of style(). It resets the text to the default for that terminal.

Comments

over 8 years ago (12 Aug 2008 at 12:25 PM) by David Isaacson
Heh... I've been wanting to write something like this, glad someone beat me to it : )
over 8 years ago (12 Aug 2008 at 12:48 PM) by Stou S.
Woah seriously... that beats the hell out of my 'python colored output' snippet.
over 8 years ago (12 Aug 2008 at 12:49 PM) by Stou S.
I'd give it a 5 if it was BSD licensed, GPL is way too restrictive for something that useful
over 8 years ago (12 Aug 2008 at 02:13 PM) by impiaaa
Sorry, I don't know my licenses… I'll change it if you can come up with a good enough reason.
over 8 years ago (12 Aug 2008 at 02:46 PM) by Stou S.
I am not well versed in licenses either but basically BSD lets [pretty much] anyone use the code inside their code (commercial or otherwise) but they have to give you credit in the source when distributing the software. The great thing about the BSD license is that it's compatible with a lot more licenses than GPL making your code useful to a lot more people/projects.

There's nothing wrong with GPL, it is an excellent license but I usually consider it only 'good' for large projects.

Also look into LGPL it's like the GPL but allows your code to be linked against other code that is not under GPL. For example most GNU libraries are under LGPL.