dsWidths = namedtuple('dsWidths', ('name', 'size', 'time')) def getFieldWidths(totalwidth : int) -> dsWidths: return dsWidths(totalwidth-32, 9, 23) TERMINAL_COLUMNS = getSystemTerm().getSize().cols class Formatter: def __init__(self, s:str=''): self.format = lambda: s def add(self, field, width): return Formatter(self.format() + "{0:{1}}".format(field, width)) ellipsizeName = (lambda f, prefix, width: ellipsize(('' if prefix==None else prefix)+f.name, width) ) def formatFile (f, widths:dsWidths, prefix:str=None) -> str: return (Formatter().add(ellipsizeName(f, prefix, widths.name-1), widths.name) .add(f.size(), widths.size) .add(f.mtime(), widths.time) ).format() fill = lambda s, ch: __fill(s, ch, TERMINAL_COLUMNS) fancy = lambda f, prefix=None: formatFile(f, getFieldWidths(TERMINAL_COLUMNS), prefix)
def test_ellipsize_string_to_smaller_length_should_return_string_with_ellipsis(self): self.assertEqual('12345...', ellipsize('123456789', 8))
def test_ellipsize_string_to_its_own_length_should_return_string_untouched(self): self.assertEqual('123456789', ellipsize('123456789', 9))
def test_ellipsize_short_string_to_long_length_should_return_string_untouched(self): self.assertEqual('asdf', ellipsize('asdf', 10))
def test_ellipsize_empty_string_to_positive_length_should_return_empty_string(self): self.assertEqual('', ellipsize('', 10))
def test_ellipsize_empty_string_to_zero_length_should_return_empty_string(self): self.assertEqual('', ellipsize('', 0))