def _GetNextLineAndRemainder(self, s, max_width, include_all_whitespace=False): """Helper function to get next line of wrappable text.""" # Get maximum split index where the next line will be wider than max. current_width = 0 split = 0 prefix = '' # Track any control sequence to use to start next line. while split < len(s): if self._csi and s[split:].startswith(self._csi): seq_length = self._console_attr.GetControlSequenceLen( s[split:]) prefix = s[split:split + seq_length] split += seq_length else: current_width += console_attr.GetCharacterDisplayWidth( s[split]) if current_width > max_width: break split += 1 if not include_all_whitespace: split += len(s[split:]) - len(s[split:].lstrip()) # Check if there is a newline character before the split. first_newline = re.search('\\n', s) if first_newline and first_newline.end() <= split: split = first_newline.end() # If not, split on the last whitespace character before the split # (if possible) else: max_whitespace = None for r in re.finditer(r'\s+', s): if r.end() > split: if include_all_whitespace and r.start() <= split: max_whitespace = split break max_whitespace = r.end() if max_whitespace: split = max_whitespace if not include_all_whitespace: next_line = s[:split].rstrip() else: next_line = s[:split] remaining_value = s[split:] # Reset font on this line if needed and add prefix to the remainder. if prefix and prefix != self._console_attr.GetFontCode(): next_line += self._console_attr.GetFontCode() remaining_value = prefix + remaining_value return next_line, remaining_value
def _GetNextLineAndRemainder(self, s, max_width, include_all_whitespace=False): """Helper function to get next line of wrappable text.""" # Get maximum split index where the next line will be wider than max. current_width = 0 split = 0 while split < len(s): current_width += console_attr.GetCharacterDisplayWidth(s[split]) if current_width > max_width: break split += 1 if not include_all_whitespace: split += len(s[split:]) - len(s[split:].lstrip()) # Check if there is a newline character before the split. first_newline = re.search('\\n', s) if first_newline and first_newline.end() <= split: split = first_newline.end() # If not, split on the last whitespace character before the split # (if possible) else: max_whitespace = None for r in re.finditer(r'\s+', s): if r.end() > split: if include_all_whitespace and r.start() <= split: max_whitespace = split break max_whitespace = r.end() if max_whitespace: split = max_whitespace if not include_all_whitespace: next_line = s[:split].rstrip() else: next_line = s[:split] remaining_value = s[split:] return next_line, remaining_value
def testGetCharacterDisplayWidth1(self): self.assertEqual(1, console_attr.GetCharacterDisplayWidth('U')) self.assertEqual(1, console_attr.GetCharacterDisplayWidth('U')) self.assertEqual(1, console_attr.GetCharacterDisplayWidth('Ü')) self.assertEqual(1, console_attr.GetCharacterDisplayWidth('Ⓤ'))
def testGetCharacterDisplayWidth2(self): self.assertEqual(2, console_attr.GetCharacterDisplayWidth('車'))
def testGetCharacterDisplayWidth0(self): self.assertEqual( 0, console_attr.GetCharacterDisplayWidth('\N{ZERO WIDTH SPACE}')) self.assertEqual( 0, console_attr.GetCharacterDisplayWidth('\N{SOFT HYPHEN}'))