Beispiel #1
0
 def format_config_vals(self, config_vals):
     """Format an iterable of config values from a config object."""
     text_buffer = StringIO()
     for i, k, v in config_vals:
         val = "" if v is None else str(v)
         text_buffer.write(("    " * i) + self.colorize(
             pad_line(str(k) + ":", 20, "left"), color=Color.lightgrey) +
                           pad_line(val, 20, "left") + "\n")
     return text_buffer.getvalue()
Beispiel #2
0
 def cli_table_row(
     self,
     fields: List[Tuple[str, str]],
     col_width,
     max_label_width=10,
     sep_char=": ",
     divider_char=" ",
     label_color=Color.lightgrey,
     val_align="right",
 ) -> str:
     """Make a row of a CLI table, using wrapped values."""
     # Do some intel first
     cols = len(fields)
     last_col_idx = cols - 1
     wrapped_fields = [
         wrap_field(
             field[0],
             field[1],
             width=col_width,
             max_label_width=max_label_width,
             sep_char=sep_char,
         ) for field in fields
     ]
     max_lines = max(fld["lines"] for fld in wrapped_fields)
     last_line_idx = max_lines - 1
     # Make some text
     buff = StringIO()
     for line_idx in range(max_lines):
         for col_idx in range(cols):
             # Assume we pad labels left and values right
             fld = wrapped_fields[col_idx]
             ll = fld["label_list"]
             vl = fld["val_list"]
             buff.write(
                 self.colorize(
                     pad_line(
                         ll[line_idx] if line_idx < len(ll) else "",
                         width=fld["label_width"],
                     ),
                     color=label_color,
                 ))
             if line_idx == 0:
                 buff.write(sep_char)
             else:
                 buff.write(" " * len(sep_char))
             buff.write(
                 pad_line(
                     vl[line_idx] if line_idx < len(vl) else "",
                     width=fld["val_width"],
                     align=val_align,
                 ))
             if col_idx != last_col_idx:
                 buff.write(divider_char)
             elif line_idx != last_line_idx:
                 buff.write("\n")
     return buff.getvalue()
Beispiel #3
0
def test__cli__helpers__pad_line():
    """Test line padding."""
    assert pad_line("abc", 5) == "abc  "
    assert pad_line("abcdef", 10, align="right") == "    abcdef"
Beispiel #4
0
def test__cli__helpers__pad_line():
    assert pad_line("abc", 5) == 'abc  '
    assert pad_line("abcdef", 10, align='right') == '    abcdef'
Beispiel #5
0
def test__cli__helpers__pad_line():
    """Test line padding."""
    assert pad_line("abc", 5) == 'abc  '
    assert pad_line("abcdef", 10, align='right') == '    abcdef'