def __init__( self, menuItems, submitCallback, invalidSelectionCallback = "DEFAULT", alphabeticOptions = False ): """ creates a form menu widget, used to contain other menu widgets """ assert isList( menuItems ) assert isFunc( submitCallback ) assert isBool( alphabeticOptions ) # invalidSelectionCallback may be: # "DEFAULT" - an internal hack to bind to self.menu # None - meaning an invalid selection defers to a later cmdHandler # Func - a client-supplied invalidSelectionCallback if invalidSelectionCallback == "DEFAULT": invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback( clientId, self.menu) if isDefined( invalidSelectionCallback ): assert isFunc( invalidSelectionCallback ) assert len(menuItems) > 0 self.menuItems = menuItems self.prompt = "{!{FB<options: " self.cmdMap = CmdMap( invalidSelectionCallback ) self.alphabeticOptions = alphabeticOptions menuIndex = 1 self.cmdMap.addCmd( "f", lambda clientId, remaining: submitCallback( clientId ) ) self.cmdMap.addCmd( "a", lambda clientId, remaining: submitCallback( clientId, abort=True ) ) for item in self.menuItems: if isString( item ): continue assert isTuple( item ) # item wasn't tuple or string assert len( item ) == 3 ( itemDesc, itemSelectedFunc, itemValueDescFunc ) = item assert isString( itemDesc ) assert isFunc( itemSelectedFunc) assert isFunc( itemValueDescFunc) itemLabel = menuIndex if self.alphabeticOptions: itemLabel = chr(96 + menuIndex ) self.cmdMap.addCmd( "%s" % itemLabel, lambda clientId, remaining: itemSelectedFunc( clientId ) ) self.prompt += "{FC%s{FB, " % itemLabel menuIndex += 1 self.prompt += "{FCa{FB, {FCf{FB> "
class Mode: def __init__(self, modeName, modeCmd, modeDefaultCallback=None, modeAddDefaultCmds=True): assert isString(modeCmd) assert isString(modeName) if not modeDefaultCallback: modeDefaultCallback = lambda clientId, remaining: sendToClient( clientId, "Invalid %s mode command. ({!{FC!{@ to exit, {!{FCcommands{@ for help)\r\n" % self.modeName) self.modeCmd = modeCmd self.modeName = modeName self.modeMap = CmdMap(modeDefaultCallback) def enterMode(clientId, remaining): pushCmdHandler(clientId, self.modeMap) sendToClient( clientId, "\r\n{!{FG[{FYEntering %s mode{FG] {@({!{FC!{@ to exit)\r\n" % self.modeName) if modeAddDefaultCmds: sendToClient(clientId, self.modeMap.commands()) sendToClient(clientId, "\r\n") def exitMode(clientId, remaining): popCmdHandler(clientId) sendToClient(clientId, "{!{FG[{FYExiting %s Mode{FG]\r\n" % self.modeName) self.enterModeCallback = enterMode self.modeMap.addCmd("!", exitMode) if modeAddDefaultCmds: self.modeMap.addCmd("exit", exitMode) self.modeMap.addCmd( "commands", lambda clientId, remaining: sendToClient( clientId, self.modeMap.commands())) def addCmd(self, cmd, callback, allowAbbrev=True): self.modeMap.addCmd(cmd, callback, allowAbbrev) def register(self, parentMap=rootCmdMap): parentMap.addCmd(self.modeCmd, self.enterModeCallback)
def __init__( self, modeName, modeCmd, modeDefaultCallback = None, modeAddDefaultCmds = True ): assert isString( modeCmd ) assert isString( modeName ) if not modeDefaultCallback: modeDefaultCallback = lambda clientId, remaining: sendToClient( clientId, "Invalid %s mode command. ({!{FC!{@ to exit, {!{FCcommands{@ for help)\r\n" % self.modeName ) self.modeCmd = modeCmd self.modeName = modeName self.modeMap = CmdMap( modeDefaultCallback ) def enterMode( clientId, remaining ): pushCmdHandler( clientId, self.modeMap ) sendToClient( clientId, "\r\n{!{FG[{FYEntering %s mode{FG] {@({!{FC!{@ to exit)\r\n" % self.modeName) if modeAddDefaultCmds: sendToClient( clientId, self.modeMap.commands() ) sendToClient( clientId, "\r\n" ) def exitMode( clientId, remaining ): popCmdHandler( clientId ) sendToClient( clientId, "{!{FG[{FYExiting %s Mode{FG]\r\n" % self.modeName) self.enterModeCallback = enterMode self.modeMap.addCmd("!", exitMode ) if modeAddDefaultCmds: self.modeMap.addCmd("exit", exitMode ) self.modeMap.addCmd("commands", lambda clientId, remaining: sendToClient( clientId, self.modeMap.commands() ) )
def getOneLine( clientId, submitCallback ): """ activates a text input widget for clientId, which returns one line of text submitCallback: func( clientId, text ) """ assert isInt( clientId ) assert isFunc( submitCallback) pushCmdHandler( clientId, CmdMap( lambda x, remaining: _submitText( x, submitCallback, remaining ) ) )
def __init__(self, clientId, nameOfEdit, initialText, submitCallback, invalidSelectionCallback="DEFAULT"): """ creates a text editor widget and activates it for clientId, used to input arbitrarily formatted text submitCallback: func( clientId, text ) """ assert isString(nameOfEdit) assert isString(initialText) assert isFunc(submitCallback) # invalidSelectionCallback may be: # "DEFAULT" - an internal hack to bind to self.menu # None - meaning an invalid selection defers to a later cmdHandler # Func - a client-supplied invalidSelectionCallback if invalidSelectionCallback == "DEFAULT": invalidSelectionCallback = lambda clientId, remaining: _defaultInvalidSelectionCallback( clientId, self.menu) if isDefined(invalidSelectionCallback): assert isFunc(invalidSelectionCallback) self.text = initialText self.submitCallback = submitCallback self.menu = endl + "{!{FYEditing %s" % nameOfEdit + endl self.cmdMap = CmdMap(invalidSelectionCallback) _addDisplay(self) _addFinish(self) self.menu += endl _activate(clientId, self)
def __init__(self, menuItems, selectionCallback, invalidSelectionCallback="DEFAULT", alphabeticOptions=False): """ creates a menu widget, used to prompt the user to select a value from a set menuItems: [item] - the list of items to display in the menu. item: string - a line of text (e.g. header) to display in the menu or ( string, value ) - a description, value pair that the user can select selectionCallback: func - the function called with args ( clientId, selectedValue ) when the user selects a value invalidSelectionCallback: func - the function called with args ( clientId, clientInputRemaining ) when the user's input doesn't map to a valid choice alphabeticOptions: bool - if true, use alphabetic, i.e. a..z, instead of numeric indices for the menu """ assert isList(menuItems) assert isFunc(selectionCallback) assert isBool(alphabeticOptions) assert len(menuItems) > 0 self.menu = "{!" # invalidSelectionCallback may be: # "DEFAULT" - an internal hack to bind to self.menu # None - meaning an invalid selection defers to a later cmdHandler # Func - a client-supplied invalidSelectionCallback if invalidSelectionCallback == "DEFAULT": invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback(clientId, self.menu) if isDefined(invalidSelectionCallback): assert isFunc(invalidSelectionCallback) self.prompt = "" self.cmdMap = CmdMap(invalidSelectionCallback) menuIndex = 1 for item in menuItems: if isString(item): self.menu += item + endl continue assert isTuple(item) # item wasn't string assert len(item) == 2 (itemDesc, itemValue) = item assert isString(itemDesc) itemLabel = menuIndex if alphabeticOptions: itemLabel = chr(96 + menuIndex) self.menu += " {FC%s{FG) - {FU%s" % (itemLabel, itemDesc) + endl def getItemSelectedFunction(selectedValue): return lambda clientId, remaining: selectionCallback(clientId, selectedValue) self.cmdMap.addCmd("%s" % itemLabel, getItemSelectedFunction(itemValue)) menuIndex += 1 self.menu += "{@"
from pydispatch import dispatcher from mud.parser.cmdMap import CmdMap from cmds import pushCmdHandler from send import sendToClient import signals def invalidCmd( clientId, remaining ): sendToClient( clientId, "Invalid command. ({!{FCcommands{@ for help)\r\n") rootCmdMap = CmdMap( invalidCmd ) rootCmdMap.addCmd("commands", lambda clientId, remaining: sendToClient( clientId, rootCmdMap.commands() ) ) def addRootMap( clientId ): pushCmdHandler( clientId, rootCmdMap ) signals.connect( addRootMap, signals.CONNECTED )
class Form: def __init__( self, menuItems, submitCallback, invalidSelectionCallback = "DEFAULT", alphabeticOptions = False ): """ creates a form menu widget, used to contain other menu widgets """ assert isList( menuItems ) assert isFunc( submitCallback ) assert isBool( alphabeticOptions ) # invalidSelectionCallback may be: # "DEFAULT" - an internal hack to bind to self.menu # None - meaning an invalid selection defers to a later cmdHandler # Func - a client-supplied invalidSelectionCallback if invalidSelectionCallback == "DEFAULT": invalidSelectionCallback = lambda clientId, remaining: defaultInvalidSelectionCallback( clientId, self.menu) if isDefined( invalidSelectionCallback ): assert isFunc( invalidSelectionCallback ) assert len(menuItems) > 0 self.menuItems = menuItems self.prompt = "{!{FB<options: " self.cmdMap = CmdMap( invalidSelectionCallback ) self.alphabeticOptions = alphabeticOptions menuIndex = 1 self.cmdMap.addCmd( "f", lambda clientId, remaining: submitCallback( clientId ) ) self.cmdMap.addCmd( "a", lambda clientId, remaining: submitCallback( clientId, abort=True ) ) for item in self.menuItems: if isString( item ): continue assert isTuple( item ) # item wasn't tuple or string assert len( item ) == 3 ( itemDesc, itemSelectedFunc, itemValueDescFunc ) = item assert isString( itemDesc ) assert isFunc( itemSelectedFunc) assert isFunc( itemValueDescFunc) itemLabel = menuIndex if self.alphabeticOptions: itemLabel = chr(96 + menuIndex ) self.cmdMap.addCmd( "%s" % itemLabel, lambda clientId, remaining: itemSelectedFunc( clientId ) ) self.prompt += "{FC%s{FB, " % itemLabel menuIndex += 1 self.prompt += "{FCa{FB, {FCf{FB> " def menu( self, clientId ): menu = "{!" menuIndex = 1 for item in self.menuItems: if isString( item ): menu += item + endl continue assert isTuple( item ) # item wasn't tuple or string assert len( item ) == 3 ( itemDesc, itemSelectedFunc, itemValueDescFunc ) = item assert isString( itemDesc ) assert isFunc( itemSelectedFunc) assert isFunc( itemValueDescFunc) itemLabel = menuIndex if self.alphabeticOptions: itemLabel = chr(96 + menuIndex ) menu += " {FC%s{FG) {FU%s{FG: {FY%s" % ( itemLabel, itemDesc, itemValueDescFunc( clientId ) ) + endl menuIndex += 1 menu += " {FCa{FG) abort " + endl menu += " {FCf{FG) finish " + endl return menu def activate( self, clientId ): """ convenience function. pushes menu cmdMap and prompt, and displays menu """ pushCmdHandler( clientId, self.cmdMap ) pushPrompt( clientId, lambda x: self.prompt ) sendToClient( clientId, self.menu( clientId ) )