def __init__(self, key, text="", links=None, linktexts=None, keywords=None, cols=1, helptext=None, selectcmds=None, callback=None, nodefaultcmds=False, separator=""): """ Initialize the node. Args: key (str): The unique identifier of this node. text (str, optional): The text that will be displayed at top when viewing this node. Kwargs: links (list): A list of keys for unique menunodes this is connected to. The actual keys will not printed - keywords will be used (or a number) linktexts (list)- A list of texts to describe the links. Must match order of `links` list if defined. Entries can be None to not generate any extra text for a particular link. keywords (list): A list of unique keys for choosing links. Must match links list. If not given, index numbers will be used. Also individual list entries can be None and will be replaed by indices. If CMD_NOMATCH or CMD_NOENTRY, no text will be generated to indicate the option exists. cols (int): How many columns to use for displaying options. helptext (str): If defined, this is shown when using the help command instead of the normal help index. selectcmds (list): A list of custom cmdclasses for handling each option. Must match links list, but some entries may be set to None to use default menu cmds. The given command's key will be used for the menu list entry unless it's CMD_NOMATCH or CMD_NOENTRY, in which case no text will be generated. These commands have access to self.menutree and so can be used to select nodes. callback (function): Function callback. This will be called as callback(currentnode) just before this node is loaded (i.e. as soon as possible as it's been selected from another node). currentnode.caller is available. nodefaultcmds (bool): If `True`, don't offer the default help and look commands in the node separator (str): This string will be put on the line between menu nodes. """ self.key = key self.cmdset = None self.links = links self.linktexts = linktexts self.keywords = keywords self.cols = cols self.selectcmds = selectcmds self.callback = MethodType(callback, self, MenuNode) if callback else None self.nodefaultcmds = nodefaultcmds self.separator = separator Nlinks = len(self.links) # validate the input if not self.links: self.links = [] if not self.linktexts or (len(self.linktexts) != Nlinks): self.linktexts = [None for i in range(Nlinks)] if not self.keywords or (len(self.keywords) != Nlinks): self.keywords = [None for i in range(Nlinks)] if not selectcmds or (len(self.selectcmds) != Nlinks): self.selectcmds = [None for i in range(Nlinks)] # Format default text for the menu-help command if not helptext: helptext = "Select one of the valid options (" for i in range(Nlinks): if self.keywords[i]: if self.keywords[i] not in (CMD_NOMATCH, CMD_NOINPUT): helptext += "%s, " % self.keywords[i] else: helptext += "%s, " % (i + 1) helptext = helptext.rstrip(", ") + ")" self.helptext = helptext # Format text display string = "" if text: string += "%s\n" % text # format the choices into as many columns as specified choices = [] for ilink, link in enumerate(self.links): choice = "" if self.keywords[ilink]: if self.keywords[ilink] not in (CMD_NOMATCH, CMD_NOINPUT): choice += "{g{lc%s{lt%s{le{n" % (self.keywords[ilink], self.keywords[ilink]) else: choice += "{g {lc%i{lt%i{le{n" % ((ilink + 1), (ilink + 1)) if self.linktexts[ilink]: choice += " - %s" % self.linktexts[ilink] choices.append(choice) cols = [[] for i in range(min(len(choices), cols))] while True: for i in range(len(cols)): if not choices: cols[i].append("") else: cols[i].append(choices.pop(0)) if not choices: break ftable = utils.format_table(cols) for row in ftable: string += "\n" + "".join(row) # store text self.text = self.separator + "\n" + string.rstrip()
def __init__(self, key, text="", links=None, linktexts=None, keywords=None, cols=1, helptext=None, selectcmds=None, callback=None, nodefaultcmds=False, separator=""): """ Initialize the node. Args: key (str): The unique identifier of this node. text (str, optional): The text that will be displayed at top when viewing this node. Kwargs: links (list): A liist of keys for unique menunodes this is connected to. The actual keys will not printed - keywords will be used (or a number) linktexts (list)- A list of texts to describe the links. Must match order of `links` list if defined. Entries can be None to not generate any extra text for a particular link. keywords (list): A list of unique keys for choosing links. Must match links list. If not given, index numbers will be used. Also individual list entries can be None and will be replaed by indices. If CMD_NOMATCH or CMD_NOENTRY, no text will be generated to indicate the option exists. cols (int): How many columns to use for displaying options. helptext (str): If defined, this is shown when using the help command instead of the normal help index. selectcmds (list): A list of custom cmdclasses for handling each option. Must match links list, but some entries may be set to None to use default menu cmds. The given command's key will be used for the menu list entry unless it's CMD_NOMATCH or CMD_NOENTRY, in which case no text will be generated. These commands have access to self.menutree and so can be used to select nodes. callback (function): Function callback. This will be called as callback(currentnode) just before this node is loaded (i.e. as soon as possible as it's been selected from another node). currentnode.caller is available. nodefaultcmds (bool): If `True`, don't offer the default help and look commands in the node separator (str): This string will be put on the line between menu nodes. """ self.key = key self.cmdset = None self.links = links self.linktexts = linktexts self.keywords = keywords self.cols = cols self.selectcmds = selectcmds self.callback = MethodType(callback, self, MenuNode) if callback else None self.nodefaultcmds = nodefaultcmds self.separator = separator Nlinks = len(self.links) # validate the input if not self.links: self.links = [] if not self.linktexts or (len(self.linktexts) != Nlinks): self.linktexts = [None for i in range(Nlinks)] if not self.keywords or (len(self.keywords) != Nlinks): self.keywords = [None for i in range(Nlinks)] if not selectcmds or (len(self.selectcmds) != Nlinks): self.selectcmds = [None for i in range(Nlinks)] # Format default text for the menu-help command if not helptext: helptext = "Select one of the valid options (" for i in range(Nlinks): if self.keywords[i]: if self.keywords[i] not in (CMD_NOMATCH, CMD_NOINPUT): helptext += "%s, " % self.keywords[i] else: helptext += "%s, " % (i + 1) helptext = helptext.rstrip(", ") + ")" self.helptext = helptext # Format text display string = "" if text: string += "%s\n" % text # format the choices into as many columns as specified choices = [] for ilink, link in enumerate(self.links): choice = "" if self.keywords[ilink]: if self.keywords[ilink] not in (CMD_NOMATCH, CMD_NOINPUT): choice += "{g{lc%s{lt%s{le{n" % (self.keywords[ilink], self.keywords[ilink]) else: choice += "{g {lc%i{lt%i{le{n" % ((ilink + 1), (ilink + 1)) if self.linktexts[ilink]: choice += " - %s" % self.linktexts[ilink] choices.append(choice) cols = [[] for i in range(min(len(choices), cols))] while True: for i in range(len(cols)): if not choices: cols[i].append("") else: cols[i].append(choices.pop(0)) if not choices: break ftable = utils.format_table(cols) for row in ftable: string += "\n" + "".join(row) # store text self.text = self.separator + "\n" + string.rstrip()