[Urwid] Patches to the library: more attributes and colors

Derek Peschel dpeschel at eskimo.com
Sun Jul 2 21:18:56 EDT 2006


The urwid Web site recommends using the "xterm" terminal type with Apple's
Terminal program, but Terminal can do better than that (at least with OS
10.3.7).  It can display 16 foreground colors, 16 background colors,
and bold and underlined fonts, all at the same time.  These patches do
a few things:

	- Teach urwid about the 16 foreground and 16 background colors
	  and the fact that bold can be unrelated to color.

	- Add my terminal name "osxterm" to the list of terminals that
	  can display 16 fg/16 bg colors.  In a curses-centric world
	  where everyone had correct terminfo files, urwid would detect
	  the number of colors the terminal supports and whether bold is
	  related.  There are conventions for both, even if they're
	  not perfect.  These patches are not that good yet.  And how
	  does the "curses-centric world" idea fit in with everyone's
	  future plans for urwid?

	  The terminal name is defined by a terminfo file I've written
	  which I'd be happy to post.

	- Add some more monochrome attributes, even some that my
	  terminfo entry doesn't support, just because it was easy.

Have fun!

-- Derek
-------------- next part --------------
--- urwid-0.9.4/urwid/curses_display.py	Sun Apr 30 20:02:38 2006
+++ urwid-0.9.4-new/urwid/curses_display.py	Sat Jun 10 17:37:44 2006
@@ -44,15 +44,15 @@
 	'dark blue':		(curses.COLOR_BLUE,	0),
 	'dark magenta':		(curses.COLOR_MAGENTA,	0),
 	'dark cyan':		(curses.COLOR_CYAN,	0),
-	'light gray':		(curses.COLOR_WHITE,	0),
-	'dark gray':		(curses.COLOR_BLACK,    1),
-	'light red':		(curses.COLOR_RED,      1),
-	'light green':		(curses.COLOR_GREEN,    1),
-	'yellow':		(curses.COLOR_YELLOW,   1),
-	'light blue':		(curses.COLOR_BLUE,     1),
-	'light magenta':	(curses.COLOR_MAGENTA,  1),
-	'light cyan':		(curses.COLOR_CYAN,     1),
-	'white':		(curses.COLOR_WHITE,	1),
+	'light gray':		(7,			0),
+	'dark gray':		(8,			0),
+	'light red':		(9,			0),
+	'light green':		(10,			0),
+	'yellow':		(11,			0),
+	'light blue':		(12,			0),
+	'light magenta':	(13,			0),
+	'light cyan':		(14,			0),
+	'white':		(15,			0),
 }
 
 
@@ -110,16 +110,16 @@
 			'dark cyan', 'light gray', 'default' (light gray if
 			unable to use terminal's default)
 		mono -- monochrome terminal attribute, one of: None (default),
-			'bold',	'underline', 'standout', or a tuple containing
-			a combination eg. ('bold','underline')
-			
+			'blink', 'bold', 'dim', 'reverse', 'standout', 'underline',
+			or a tuple containing a combination eg. ('bold','underline')
 		"""
 		fg_a, fg_b = _curses_colours[foreground]
 		bg_a, bg_b = _curses_colours[background]
 		if bg_b: # can't do bold backgrounds
 			raise Exception("%s is not a supported background colour"%background )
 		assert (mono is None or 
-			mono in (None, 'bold', 'underline', 'standout') or
+			mono in (None, 'blink', 'bold', 'dim', 'reverse',
+			  'standout', 'underline') or
 			type(mono)==type(()))
 	
 		for i in range(len(self.curses_pairs)):
@@ -215,8 +215,20 @@
 				self.attrconv[name] = attr
 	
 	def _curses_attr(self, a):
+	        # no A_ALTCHARSET yet; probably those should go in encoding
+	        # no A_PROTECT yet; I don't know what it would do
+	        # no A_COLOR; color palette functions deal with that
+	        # no A_INVIS; I don't use it
+                # no A_HORIZONTAL or A_LEFT or A_LOW or A_RIGHT or A_TOP or
+                #  A_VERTICAL; I don't know of a terminal that supports them
+		if a == 'blink':
+			return curses.A_BLINK
 		if a == 'bold':
 			return curses.A_BOLD
+		if a == 'dim':
+			return curses.A_DIM
+		elif a == 'reverse':
+			return curses.A_REVERSE
 		elif a == 'standout':
 			return curses.A_STANDOUT
 		elif a == 'underline':
-------------- next part --------------
--- urwid-0.9.4/urwid/escape.py	Sun Apr 30 20:02:38 2006
+++ urwid-0.9.4-new/urwid/escape.py	Sat Jun 10 17:44:11 2006
@@ -331,13 +331,6 @@
 	'white':	"97",
 }
 
-###############################################
-# Detect xterm and use non-bold bright colours
-if os.environ.get('TERM',None) == 'xterm':
-	_fg_attr = _fg_attr_xterm
-###############################################
-
-
 _bg_attr = {
 	'default':	"49",
 	'black':	"40",
@@ -348,16 +341,35 @@
 	'dark magenta':	"45",
 	'dark cyan':	"46",
 	'light gray':	"47",
-#	'dark gray':	"100",
-#	'light red':	"101",
-#	'light green':	"102",
-#	'yellow':	"103",
-#	'light blue':	"104",
-#	'light magenta':"105",
-#	'light cyan':	"106",
-#	'white':	"107",
 }
 
+_bg_attr_xterm = {
+	'default':	"49",
+	'black':	"40",
+	'dark red':	"41",
+	'dark green':	"42",
+	'brown':	"43",
+	'dark blue':	"44",
+	'dark magenta':	"45",
+	'dark cyan':	"46",
+	'light gray':	"47",
+	'dark gray':	"100",
+	'light red':	"101",
+	'light green':	"102",
+	'yellow':	"103",
+	'light blue':	"104",
+	'light magenta':"105",
+	'light cyan':	"106",
+	'white':	"107",
+}
+
+###############################################
+# Detect xterm and use non-bold bright colours
+if os.environ.get('TERM',None) in ('xterm','osxterm'):
+	_fg_attr = _fg_attr_xterm
+	_bg_attr = _bg_attr_xterm
+###############################################
+
 def set_attributes( fg, bg ):
 	assert _fg_attr.has_key( fg )
 	assert _bg_attr.has_key( bg )
@@ -366,4 +378,3 @@
 		# xterm workaround
 	#return ESC+"[39m"+e
 	#return e
-


More information about the Urwid mailing list