def pad(*args, **kwargs): """ Inlinefunc. Pads text to given width. Args: text (str, optional): Text to pad. width (str, optional): Will be converted to integer. Width of padding. align (str, optional): Alignment of padding; one of 'c', 'l' or 'r'. fillchar (str, optional): Character used for padding. Defaults to a space. Kwargs: session (Session): Session performing the pad. Example: `$pad(text, width, align, fillchar)` """ text, width, align, fillchar = "", 78, "c", " " nargs = len(args) if nargs > 0: text = args[0] if nargs > 1: width = int(args[1]) if args[1].strip().isdigit() else 78 if nargs > 2: align = args[2] if args[2] in ("c", "l", "r") else "c" if nargs > 3: fillchar = args[3] return utils.pad(text, width=width, align=align, fillchar=fillchar)
def pad(*args, **kwargs): """ Inlinefunc. Pads text to given width. Args: text (str, optional): Text to pad. width (str, optional): Will be converted to integer. Width of padding. align (str, optional): Alignment of padding; one of 'c', 'l' or 'r'. fillchar (str, optional): Character used for padding. Defaults to a space. Kwargs: session (Session): Session performing the pad. Example: `$pad(text, width, align, fillchar)` """ text, width, align, fillchar = "", 78, 'c', ' ' nargs = len(args) if nargs > 0: text = args[0] if nargs > 1: width = int(args[1]) if args[1].strip().isdigit() else 78 if nargs > 2: align = args[2] if args[2] in ('c', 'l', 'r') else 'c' if nargs > 3: fillchar = args[3] return utils.pad(text, width=width, align=align, fillchar=fillchar)
def evtable_options_formatter(optionlist, caller=None): """ Formats the option list display. """ if not optionlist: return "" # column separation distance colsep = 4 nlist = len(optionlist) # get the widest option line in the table. table_width_max = -1 table = [] for key, desc in optionlist: if not (key or desc): continue table_width_max = max(table_width_max, max(m_len(p) for p in key.split("\n")) + max(m_len(p) for p in desc.split("\n")) + colsep) raw_key = strip_ansi(key) if raw_key != key: # already decorations in key definition table.append(ANSIString(" |lc%s|lt%s|le: %s" % (raw_key, key, desc))) else: # add a default white color to key table.append(ANSIString(" |lc%s|lt|w%s|n|le: %s" % (raw_key, raw_key, desc))) ncols = (_MAX_TEXT_WIDTH // table_width_max) + 1 # number of ncols nlastcol = nlist % ncols # number of elements left in last row # get the amount of rows needed (start with 4 rows) nrows = 4 while nrows * ncols < nlist: nrows += 1 ncols = nlist // nrows # number of full columns nlastcol = nlist % nrows # number of elements in last column # get the final column count ncols = ncols + 1 if nlastcol > 0 else ncols if ncols > 1: # only extend if longer than one column table.extend([" " for i in range(nrows - nlastcol)]) # build the actual table grid table = [table[icol * nrows : (icol * nrows) + nrows] for icol in range(0, ncols)] # adjust the width of each column for icol in range(len(table)): col_width = max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]] # format the table into columns return unicode(EvTable(table=table, border="none"))
def pad(text, *args, **kwargs): "Pad to width. pad(text, width=78, align='c', fillchar=' ')" width = _DEFAULT_WIDTH align = 'c' fillchar = ' ' for iarg, arg in enumerate(args): if iarg == 0: width = int(arg) if arg.isdigit() else width elif iarg == 1: align = arg if arg in ('c', 'l', 'r') else align elif iarg == 2: fillchar = arg[0] else: break return utils.pad(text, width=width, align=align, fillchar=fillchar)
def _format_node(self, nodetext, optionlist): """ Format the node text + option section Args: nodetext (str): The node text optionlist (list): List of (key, desc) pairs. Returns: string (str): The options section, including all needed spaces. Notes: This will adjust the columns of the options, first to use a maxiumum of 4 rows (expanding in columns), then gradually growing to make use of the screen space. """ # # handle the node text # nodetext = dedent(nodetext).strip() nodetext_width_max = max(m_len(line) for line in nodetext.split("\n")) if not optionlist: # return the node text "naked". separator1 = "_" * nodetext_width_max + "\n\n" if nodetext_width_max else "" separator2 = "\n" if nodetext_width_max else "" + "_" * nodetext_width_max return separator1 + nodetext + separator2 # # handle the options # # column separation distance colsep = 4 nlist = len(optionlist) # get the widest option line in the table. table_width_max = -1 table = [] for key, desc in optionlist: table_width_max = max(table_width_max, max(m_len(p) for p in key.split("\n")) + max(m_len(p) for p in desc.split("\n")) + colsep) raw_key = strip_ansi(key) if raw_key != key: # already decorations in key definition table.append(ANSIString(" {lc%s{lt%s{le: %s" % (raw_key, key, desc))) else: # add a default white color to key table.append(ANSIString(" {lc%s{lt{w%s{n{le: %s" % (raw_key, raw_key, desc))) ncols = (_MAX_TEXT_WIDTH // table_width_max) + 1 # number of ncols nlastcol = nlist % ncols # number of elements left in last row # get the amount of rows needed (start with 4 rows) nrows = 4 while nrows * ncols < nlist: nrows += 1 ncols = nlist // nrows # number of full columns nlastcol = nlist % nrows # number of elements in last column # get the final column count ncols = ncols + 1 if nlastcol > 0 else ncols if ncols > 1: # only extend if longer than one column table.extend([" " for i in xrange(nrows-nlastcol)]) # build the actual table grid table = [table[icol*nrows:(icol*nrows) + nrows] for icol in xrange(0, ncols)] # adjust the width of each column total_width = 0 for icol in xrange(len(table)): col_width = max(max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep table[icol] = [pad(part, width=col_width + colsep, align="l") for part in table[icol]] total_width += col_width # format the table into columns table = EvTable(table=table, border="none") # build the page total_width = max(total_width, nodetext_width_max) separator1 = "_" * total_width + "\n\n" if nodetext_width_max else "" separator2 = "\n" + "_" * total_width + "\n\n" if total_width else "" return separator1 + nodetext + separator2 + unicode(table)
def options_formatter(self, optionlist, options_format): """ Formats the option block. Args: optionlist (list): List of (key, description) tuples for every option related to this node. caller (Object, Account or None, optional): The caller of the node. Returns: options (str): The formatted option display. """ if not optionlist: return "" # column separation distance colsep = 4 # parse out options for Quit, Back, Proceed, None, and Finish option_list_1 = [] option_list_2 = [] for item in optionlist: if ('move_keys' in options_format and item[0] in options_format['move_keys']): option_list_2.append(item) elif ('hide_keys' in options_format and item[0] in options_format['hide_keys']): pass else: option_list_1.append(item) nlist = len(option_list_1) # get the widest option line in the table. table_width_max = -1 table = [] for key, desc in option_list_1: if key or desc: desc_string = ": %s" % desc if desc else "" table_width_max = max( table_width_max, max(m_len(p) for p in key.split("\n")) + max(m_len(p) for p in desc_string.split("\n")) + colsep, ) raw_key = strip_ansi(key) if raw_key != key: # already decorations in key definition table.append(" |lc%s|lt%s|le%s" % (raw_key, key, desc_string)) else: # add a default white color to key table.append(" |lc%s|lt|w%s|n|le%s" % (raw_key, raw_key, desc_string)) ncols = _MAX_TEXT_WIDTH // table_width_max # number of ncols if ncols < 0: # no visible option at all return "" ncols = ncols + 1 if ncols == 0 else ncols # get the amount of rows needed (start with 4 rows) if 'rows' in options_format: nrows = options_format['rows'] else: nrows = 4 while nrows * ncols < nlist: nrows += 1 ncols = nlist // nrows # number of full columns nlastcol = nlist % nrows # number of elements in last column # get the final column count ncols = ncols + 1 if nlastcol > 0 else ncols if ncols > 1: # only extend if longer than one column table.extend([" " for i in range(nrows - nlastcol)]) # build the actual table grid table = [ table[icol * nrows:(icol * nrows) + nrows] for icol in range(0, ncols) ] # adjust the width of each column for icol in range(len(table)): col_width = (max( max(m_len(p) for p in part.split("\n")) for part in table[icol]) + colsep) table[icol] = [ pad(part, width=col_width + colsep, align="l") for part in table[icol] ] result = EvTable(table=table, border='none') if len(option_list_2) > 0: result.add_row(' ') for key, desc in option_list_2: if key or desc: desc_string = ": %s" % desc if desc else "" table_width_max = max( table_width_max, max(m_len(p) for p in key.split("\n")) + max(m_len(p) for p in desc_string.split("\n")) + colsep, ) raw_key = strip_ansi(key) if raw_key != key: # already decorations in key definition result.add_row(" |lc%s|lt%s|le%s" % (raw_key, key, desc_string)) else: # add a default white color to key result.add_row(" |lc%s|lt|w%s|n|le%s" % (raw_key, raw_key, desc_string)) # format the table into columns return str(result)