Esempio n. 1
0
    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> " 
Esempio n. 2
0
    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 += "{@"