This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
colorful nested (^^gdb) prompt
- From: Matt Rice <ratmice at gmail dot com>
- To: gdb-patches at sourceware dot org
- Date: Wed, 18 Jan 2012 10:23:28 -0800
- Subject: colorful nested (^^gdb) prompt
attached is the script mentioned here:
http://sourceware.org/ml/gdb-patches/2012-01/msg00619.html
mostly pulled out of
git://gitorious.org/misc-gdb-stuff/misc-gdb-stuff.git
into a single script since I haven't settled on a manner of easily and flexibly
doing explicit loading from .gdbinit (rather than relying on auto-loading).
doesn't work with tui (not sure why, haven't looked), and requires curses.
def go():
prompt_nest=""
if os.environ.has_key('GDB_PROMPT_NEST'):
prompt_nest = os.environ['GDB_PROMPT_NEST']
# note this is backwards from git's usage of ^
gdb.execute("set environment GDB_PROMPT_NEST " + prompt_nest + '^')
nesting_colors = ("blue", "green", "red", "yellow")
prompt_color = nesting_colors[len(prompt_nest) % len(nesting_colors)]
prompt_list = [('(', 'normal'),
(prompt_nest, 'normal'),
(first_prompt, prompt_color),
(') ', 'normal')]
# pick one of the following 2 lines...
gdb.prompt_hook = setup_gaudy_prompt(prompt_list, term_colors())
#gdb.prompt_hook = setup_gaudy_prompt(prompt_list, no_colors)
import types
import gdb
def ignore_escape_sequence(string):
return "\001" + string + "\002"
the_very_first_prompt = None
def first_prompt(current):
global the_very_first_prompt
if (the_very_first_prompt != None):
return the_very_first_prompt
x = current
y = x.find('(')
if (y != -1):
x = x[y + 1 : len(x)]
y = x.rfind(')')
if (y != -1):
x = x[0:y]
the_very_first_prompt = x
return the_very_first_prompt
# FIXME fgcolor,color,term_colors are platform specific.
def fgcolor(COLOR):
return color("setaf", COLOR)
def color(string, COLOR):
return ignore_escape_sequence(tparm(tigetstr(string), COLOR))
from curses import *
# accepts an option term, term = None is getenv("TERM").
def term_colors(term = None):
# under gdb sys.stdout doesn't implement fileno()
# use normal stdout fileno?
setupterm(term, 1);
return {
'black' : fgcolor(COLOR_BLACK),
'red' : fgcolor(COLOR_RED),
'green' : fgcolor(COLOR_GREEN),
'yellow' : fgcolor(COLOR_YELLOW),
'blue' : fgcolor(COLOR_BLUE),
'magenta' : fgcolor(COLOR_MAGENTA),
'cyan' : fgcolor(COLOR_CYAN),
'white' : fgcolor(COLOR_WHITE),
'normal' : ignore_escape_sequence(tigetstr("sgr0")),
}
no_colors = {
'black' : "",
'red' : "",
'green' : "",
'yellow' : "",
'blue' : "",
'magenta' : "",
'cyan' : "",
'white' : "",
'normal' : "",
}
def string_for_prompt_element(current, e):
if e[0].__class__ == types.FunctionType:
return e[0](current)
else:
return e[0]
def prompt_recursive(current, alist, colors_dict):
ret = ""
for e in alist:
if e.__class__ == list:
ret += prompt_recursive(current, e, colors_dict)
else:
x = string_for_prompt_element(current, e)
if (x == None):
return ""
ret += colors_dict[e[1]] + x + colors_dict['normal']
return ret
def setup_gaudy_prompt(prompt_stuff, colors_stuff):
return lambda current: prompt_recursive(current, prompt_stuff, colors_stuff)
go()