From d6cb52f8b3dbc79e6888054ed2ce150c7786ad26 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Wed, 26 Aug 2015 00:56:10 +0900 Subject: [PATCH 1/2] Fix wrong line width calculation in max_width for colored text --- clint/textui/formatters.py | 32 ++++++++++++++++++++++++-------- test_clint.py | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/clint/textui/formatters.py b/clint/textui/formatters.py index 4bc2f10..9962351 100644 --- a/clint/textui/formatters.py +++ b/clint/textui/formatters.py @@ -34,16 +34,27 @@ def min_width(string, cols, padding=' '): def max_width(string, cols, separator='\n'): - """Returns a freshly formatted """ + """Returns a freshly formatted + :param string: string to be formatted + :type string: basestring or clint.textui.colorred.ColoredString + :param cols: max width the text to be formatted + :type cols: int + :param separator: separator to break rows + :type separator: basestring + + >>> formatters.max_width('123 5678', 8) + '123 5678' + >>> formatters.max_width('123 5678', 7) + '123 \n5678' + + """ is_color = isinstance(string, ColoredString) if is_color: - offset = 10 string_copy = string._new('') - else: - offset = 0 - + string = string.s + stack = tsplit(string, NEWLINES) for i, substring in enumerate(stack): @@ -56,31 +67,36 @@ def max_width(string, cols, separator='\n'): _row_i = 0 for word in row: - if (len(_row[_row_i]) + len(word)) < (cols + offset): + if (len(_row[_row_i]) + len(word)) <= cols: _row[_row_i] += word _row[_row_i] += ' ' - elif len(word) > (cols - offset): + elif len(word) > cols: # ensure empty row if len(_row[_row_i]): + _row[_row_i] = _row[_row_i].rstrip() _row.append('') _row_i += 1 - chunks = schunk(word, (cols + offset)) + chunks = schunk(word, cols) for i, chunk in enumerate(chunks): if not (i + 1) == len(chunks): _row[_row_i] += chunk + _row[_row_i] = _row[_row_i].rstrip() _row.append('') _row_i += 1 else: _row[_row_i] += chunk _row[_row_i] += ' ' else: + _row[_row_i] = _row[_row_i].rstrip() _row.append('') _row_i += 1 _row[_row_i] += word _row[_row_i] += ' ' + else: + _row[_row_i] = _row[_row_i].rstrip() _row = map(str, _row) _stack.append(separator.join(_row)) diff --git a/test_clint.py b/test_clint.py index c0c7d58..0e22548 100755 --- a/test_clint.py +++ b/test_clint.py @@ -58,5 +58,30 @@ class ColoredStringTestCase(unittest.TestCase): assert new_str.always_color == True +class TextuiFormatterTestCase(unittest.TestCase): + + def test_max_width(self): + + def _test_n_rows_width(ins, rows, n_rows, max_width): + ins.assertEqual(len(rows), n_rows) + for row in rows: + ins.assertLessEqual(len(row), max_width) + + from clint.textui.formatters import max_width + from clint.textui import colored + # normal text + text = ' '.join(['XXX'] * 3) + rows = max_width(text, 6).split('\n') + _test_n_rows_width(self, rows, 3, 6) + rows = max_width(text, 7).split('\n') + _test_n_rows_width(self, rows, 2, 7) + # colored text + c_text = colored.yellow(text) + rows = max_width(c_text, 6).split('\n') + _test_n_rows_width(self, rows, 3, 6) + rows = max_width(c_text, 7).split('\n') + _test_n_rows_width(self, rows, 2, 7) + + if __name__ == '__main__': unittest.main() From d0a5237afe69e33aa8f3c52f14cefa2c1b5e6e93 Mon Sep 17 00:00:00 2001 From: Kentaro Wada Date: Wed, 26 Aug 2015 00:58:06 +0900 Subject: [PATCH 2/2] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index af8b8e7..b251a66 100644 --- a/AUTHORS +++ b/AUTHORS @@ -31,3 +31,4 @@ Patches and Suggestions - Eric Anderson - Joshua Richardson - Brandon Liu +- Kentaro Wada