Newbie: raw_input and other input methods

Carel Fellinger cfelling at
Sat Nov 27 16:56:06 EST 1999

M.-A. Lemburg <mal at> wrote:

> It even does the Right Thing for the Python prompt. Try this:
> The above adds a little color to your average Python prompt.

Well, I was to save this for a rainy day, but now that you generalized it
I can't help. Besides it isn't nice to use terminal specific codes.

import readline, os

class PromptFunc:
    '''bucket for function to be evaluated during prompting'''
    def __init__(self, func, *args, **keys):
	self.func, self.args, self.keys  = func, args, keys

    def __str__(self):
	return apply(self.func, self.args, self.keys)

class Tput:
    '''terminfo capability string to be evaluated during prompting
    embedded with readline specials so it won\'t take up any space'''
    def __init__(self, attribute):
	self.attribute = attribute

    def __str__(self):
	return '\001' + os.popen('tput ' + self.attribute).read() + '\002'

class Var:
    '''(environment) variable to be evaluated during prompting'''
    def __init__(self, var, dict=os.environ):
	self.var, self.dict = var, dict

    def __str__(self):
	return self.dict[self.var]

class Prompt:
    def __init__(self, prompt='>>>', **state):
	self.prompt = prompt
	#colors; unfortunately xterm doesn't seem to obey terminfo names, = Tput('setf 0'), Tput('setf 1'), self.yellow = Tput('setf 2'), Tput('setf 3'), self.magenta = Tput('setf 4'), Tput('setf 5')
	self.cyan, self.white = Tput('setf 6'), Tput('setf 7')
	#background colors
	self.Back, self.Red = Tput('setb 0'), Tput('setb 1')
	self.Green, self.Yellow = Tput('setb 2'), Tput('setb 3')
	self.Blue, self.Magenta = Tput('setb 4'), Tput('setb 5')
	self.Cyan, self.White = Tput('setb 6'), Tput('setb 7')
	#video attributes
	self.bold, self.blink = Tput('bold'), Tput('blink')
	self.dim, self.reverse = Tput('dim'), Tput('rev')
	self.flash, self.normal = Tput('flash'), Tput('sgr0')
	self.begin_standout, self.end_standout = Tput('smso'), Tput('rmso')
	self.begin_underline, self.end_underline = Tput('smul'), Tput('rmul')
	#alternate charset
	self.begin_alternate, self.end_alternate = Tput('smacs'), Tput('rmacs')
	#override whatever

    def __str__(self):
	return self.prompt % self.__dict__

if __name__ == '__main__':
    prompt = Prompt('%(red)s%(cwd)s(%(nr)d) %(green)shallo daar:%(normal)s ')
    prompt.cwd = PromptFunc(os.getcwd) = 0
    while 1: = + 1
	print raw_input(prompt)

groetjes, carel

More information about the Python-list mailing list