mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 23:00:18 +00:00
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2bd5aef5bc | |||
| a47ed4f711 | |||
| d0a5237afe | |||
| d6cb52f8b3 |
@@ -31,3 +31,4 @@ Patches and Suggestions
|
||||
- Eric Anderson
|
||||
- Joshua Richardson
|
||||
- Brandon Liu <thenovices>
|
||||
- Kentaro Wada <www.kentaro.wada@gmail.com>
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
History
|
||||
-------
|
||||
|
||||
0.5.1
|
||||
+++++
|
||||
* Fix line width calculation in max_width when using coloured text (thanks to @wkentaro)
|
||||
|
||||
0.5.0
|
||||
+++++
|
||||
* Added option prompt
|
||||
|
||||
+2
-2
@@ -26,8 +26,8 @@ from .pipes import piped_in
|
||||
|
||||
|
||||
__title__ = 'clint'
|
||||
__version__ = '0.5.0'
|
||||
__build__ = 0x000500
|
||||
__version__ = '0.5.1'
|
||||
__build__ = 0x000501
|
||||
__author__ = 'Kenneth Reitz'
|
||||
__license__ = 'ISC'
|
||||
__copyright__ = 'Copyright 2012 Kenneth Reitz'
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user