def _split_lines(self, text, width): lines = list() main_indent = TERMINAL.length(re.match(r'( *)', text).group(1)) # Wrap each line individually to allow for partial formatting for line in text.splitlines(): # Get this line's indent and figure out what indent to use # if the line wraps. Account for lists of small variety. indent = TERMINAL.length(re.match(r'( *)', line).group(1)) list_match = re.match(r'( *)(([*-+>]+|\w+\)|\w+\.) +)', line) if(list_match): sub_indent = indent + TERMINAL.length(list_match.group(2)) else: sub_indent = indent # Textwrap will do all the hard work for us line = self._whitespace_matcher.sub(' ', line).strip() new_lines = textwrap.wrap( text=line, width=width, initial_indent=' ' * (indent - main_indent), subsequent_indent=' ' * (sub_indent - main_indent), ) # Blank lines get eaten by textwrap, put it back with [' '] lines.extend(new_lines or [' ']) return lines
def _split_lines(self, text, width): lines = list() main_indent = TERMINAL.length(re.match(r'( *)', text).group(1)) # Wrap each line individually to allow for partial formatting for line in text.splitlines(): # Get this line's indent and figure out what indent to use # if the line wraps. Account for lists of small variety. indent = TERMINAL.length(re.match(r'( *)', line).group(1)) list_match = re.match(r'( *)(([*-+>]+|\w+\)|\w+\.) +)', line) if (list_match): sub_indent = indent + TERMINAL.length(list_match.group(2)) else: sub_indent = indent # Textwrap will do all the hard work for us line = self._whitespace_matcher.sub(' ', line).strip() new_lines = textwrap.wrap( text=line, width=width, initial_indent=' ' * (indent - main_indent), subsequent_indent=' ' * (sub_indent - main_indent), ) # Blank lines get eaten by textwrap, put it back with [' '] lines.extend(new_lines or [' ']) return lines
def format_proposed_upgrades(response): tabledata = [] for upgrade in response.json(): tabledata.append([ upgrade_id_with_flags(upgrade, omit_flags=('proposed', )), TERMINAL.bold(upgrade.get('title')), ]) print TERMINAL.bold('Proposed upgrades:') print_table(tabledata, ['ID:', 'Title:'])
def format_proposed_upgrades(response): tabledata = [] for upgrade in response.json(): tabledata.append( [upgrade_id_with_flags(upgrade, omit_flags=('proposed',)), TERMINAL.bold(upgrade.get('title')), ]) print TERMINAL.bold('Proposed upgrades:') print_table(tabledata, ['ID:', 'Title:'])
def format_profiles(response): tabledata = [] for profile in response.json(): tabledata.append( [colorize_profile_id(profile['id']), colorized_profile_flags(profile), TERMINAL.bold(profile['title']), colorized_profile_versions(profile)]) print TERMINAL.bold('Installed profiles:') print_table(tabledata, ['ID:', '', 'Title:', 'Versions (DB/FS):'])
def format_profiles(response): tabledata = [] for profile in response.json(): tabledata.append([ colorize_profile_id(profile['id']), colorized_profile_flags(profile), TERMINAL.bold(profile['title']), colorized_profile_versions(profile) ]) print(TERMINAL.bold('Installed profiles:')) print_table(tabledata, ['ID:', '', 'Title:', 'Versions (DB/FS):'])
def format_proposed_upgrades(response): proposed = [] for upgrade in response.json(): is_deferrable = upgrade.get('deferrable', False) omit_flags = ('proposed', 'orphan') if is_deferrable else ('proposed',) table_row = [upgrade_id_with_flags(upgrade, omit_flags=omit_flags), TERMINAL.bold(upgrade.get('title')), ] proposed.append(table_row) print TERMINAL.bold('Proposed upgrades:') print_table(proposed, ['ID:', 'Title:'])
def format_proposed_upgrades(response): proposed = [] for upgrade in response.json(): is_deferrable = upgrade.get('deferrable', False) omit_flags = ('proposed', 'orphan') if is_deferrable else ('proposed', ) table_row = [ upgrade_id_with_flags(upgrade, omit_flags=omit_flags), TERMINAL.bold(upgrade.get('title')), ] proposed.append(table_row) print(TERMINAL.bold('Proposed upgrades:')) print_table(proposed, ['ID:', 'Title:'])
def _fill_text(self, text, width, indent): text = (re.compile('\[quote\].*?\[\/quote\]', re.DOTALL).sub( lambda match: (match.group(0).replace('\n', '[QUOTE:NEWLINE]')), text)) text = '\n'.join( [indent + line for line in self._split_lines(text, width)]) text = (re.compile('\[quote\](.*?)\[\/quote\]', re.DOTALL).sub( lambda match: TERMINAL.green( match.group(1).replace('\n', ' ').replace( '[QUOTE:NEWLINE]', TERMINAL.normal + '\n ' + TERMINAL. green).rstrip(' ').strip('\n')), text)) return text
def _fill_text(self, text, width, indent): text = (re.compile('\[quote\].*?\[\/quote\]', re.DOTALL) .sub(lambda match: ( match.group(0) .replace('\n', '[QUOTE:NEWLINE]')), text)) text = '\n'.join([indent + line for line in self._split_lines(text, width)]) text = (re.compile('\[quote\](.*?)\[\/quote\]', re.DOTALL) .sub(lambda match: TERMINAL.green( match.group(1) .replace('\n', ' ') .replace('[QUOTE:NEWLINE]', TERMINAL.normal + '\n ' + TERMINAL.green) .rstrip(' ').strip('\n')), text)) return text
def test_lstrip(self): self.assertEquals(TERMINAL.lstrip(' foo '), FakeTerminal().lstrip(' foo '))
def test_center(self): self.assertEquals(TERMINAL.center('foo', 10), FakeTerminal().center('foo', 10))
def test_rjust(self): self.assertEquals(TERMINAL.rjust('foo', 10), FakeTerminal().rjust('foo', 10))
def test_ljust(self): self.assertEquals(TERMINAL.ljust('foo', 10), FakeTerminal().ljust('foo', 10)) self.assertEquals(TERMINAL.ljust(u'foo', 10), FakeTerminal().ljust(u'foo', 10))
def translate(text): if text in ('positional arguments', 'optional arguments'): return TERMINAL.bold(_(text).upper()) else: return _(text)
def test_rstrip(self): self.assertEqual(TERMINAL.rstrip(' foo '), FakeTerminal().rstrip(' foo '))