mirror of
https://github.com/kennethreitz/clint.git
synced 2026-06-05 23:00:18 +00:00
ColoredString support for width formatters
This commit is contained in:
+26
-10
@@ -10,28 +10,40 @@ Core TextUI functionality for text formatting.
|
||||
|
||||
from __future__ import absolute_import
|
||||
|
||||
from .colored import ColoredString, clean
|
||||
from ..utils import tsplit, schunk
|
||||
|
||||
|
||||
NEWLINES = ('\n', '\r', '\r\n')
|
||||
|
||||
|
||||
|
||||
|
||||
def min_width(string, cols, padding=' '):
|
||||
"""Returns given string with right padding."""
|
||||
|
||||
stack = tsplit(string, NEWLINES)
|
||||
is_color = isinstance(string, ColoredString)
|
||||
|
||||
stack = tsplit(str(string), NEWLINES)
|
||||
|
||||
for i, substring in enumerate(stack):
|
||||
stack[i] = substring.ljust(cols, padding)
|
||||
_sub = clean(substring).ljust((cols + 0), padding)
|
||||
if is_color:
|
||||
_sub = (_sub.replace(clean(substring), substring))
|
||||
stack[i] = _sub
|
||||
|
||||
return '\n'.join(stack)
|
||||
|
||||
|
||||
def max_width(string, cols, separator='\n'):
|
||||
"""Returns a freshly formatted """
|
||||
|
||||
|
||||
is_color = isinstance(string, ColoredString)
|
||||
|
||||
if is_color:
|
||||
offset = 10
|
||||
string_copy = string._new('')
|
||||
else:
|
||||
offset = 0
|
||||
|
||||
stack = tsplit(string, NEWLINES)
|
||||
|
||||
for i, substring in enumerate(stack):
|
||||
@@ -44,18 +56,18 @@ def max_width(string, cols, separator='\n'):
|
||||
_row_i = 0
|
||||
|
||||
for word in row:
|
||||
if (len(_row[_row_i]) + len(word)) < cols:
|
||||
if (len(_row[_row_i]) + len(word)) < (cols + offset):
|
||||
_row[_row_i] += word
|
||||
_row[_row_i] += ' '
|
||||
|
||||
elif len(word) > cols:
|
||||
elif len(word) > (cols - offset):
|
||||
|
||||
# ensure empty row
|
||||
if len(_row[_row_i]):
|
||||
_row.append('')
|
||||
_row_i += 1
|
||||
|
||||
chunks = schunk(word, cols)
|
||||
chunks = schunk(word, (cols + offset))
|
||||
for i, chunk in enumerate(chunks):
|
||||
if not (i + 1) == len(chunks):
|
||||
_row[_row_i] += chunk
|
||||
@@ -69,7 +81,11 @@ def max_width(string, cols, separator='\n'):
|
||||
_row_i += 1
|
||||
_row[_row_i] += word
|
||||
_row[_row_i] += ' '
|
||||
|
||||
_row = map(str, _row)
|
||||
_stack.append(separator.join(_row))
|
||||
|
||||
|
||||
return '\n'.join(_stack)
|
||||
_s = '\n'.join(_stack)
|
||||
if is_color:
|
||||
_s = string_copy._new(_s)
|
||||
return _s
|
||||
|
||||
Reference in New Issue
Block a user