from dialog import * class GLIDialog(Dialog): def __init__(self, dialog="dialog", DIALOGRC=None, compat="dialog", use_stdout=None): Dialog.__init__(self,dialog="dialog", DIALOGRC=None, compat="dialog", use_stdout=None) def form(self, formtitle, fields, **kwargs): """Display a set of form fields. formtitle -- Title of the form fields -- ('Field Label', 'Max length,', 'Default Value') Field Label -- The label displayed before the input box Max Length -- The maximum size the input box should be Default Value -- A default value for the field (Optional) The form dialog allows one to build multiple inputs into one dialog box. Just like a web based form you use TAB to move between fields and ENTER to submit the dialog. It returns a tuple containing the form data. Typical usage: data = d.form('title', (('Interface',5,'eth0'),('IP Address', 15), ('Netmask', 15))) """ #Find the maximum size of the labels and boxes maxoptlen = 0 maxinputlen = 0 for field in fields: if len(field[0]) > maxoptlen: maxoptlen = len(field[0]) if int(field[1]) > maxinputlen: maxinputlen = int(field[1]) #Set the starting args formargs = ["--form", formtitle, len(fields)+5, maxinputlen + maxoptlen + 12, len(fields)] #Make the args for the fields counter = 1 for field in fields: formargs.append(field[0]) formargs.append(counter) if len(field) > 2: formargs.append(field[2]) formargs.append(counter) formargs.append(maxoptlen+5) formargs.append(int(field[1])+1) formargs.append(field[1]) counter = counter + 1 #Run the dialog program (code, output) = self._perform( *([formargs]),**kwargs) output = self._strip_xdialog_newline(output) return (code, output) if __name__ == '__main__': d = GLIDialog() d.form('title', (('Interface',5,'eth0'),('IP Address', 15), ('Netmask', 15)))