Skip to content

Commit

Permalink
[cliptext-] clipstr(): truncate too-wide char into width 1
Browse files Browse the repository at this point in the history
  • Loading branch information
midichef committed Jan 7, 2025
1 parent 6b0a78a commit 36cf84e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 10 additions & 1 deletion visidata/cliptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,16 @@ def _clipstr(s, dispw, trunch='', oddspacech='', combch='', modch=''):
return '', 0

if dispw == 1:
return s[0], 1
w_s = dispwidth(s)
if w_s > 1:
ret = ''
for c in s:
if dispwidth(ret + c) > 1:
break
ret += c
return ret, dispwidth(ret)
else:
return s, w_s

w = 0
ret = ''
Expand Down
22 changes: 22 additions & 0 deletions visidata/tests/test_cliptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,34 @@ def test_dispwidth(self, s, dispw):
(' jsonl', 5, ' jso…', 5),
('abcdで', 6, 'abcdで', 6),
('abcdで', 5, 'abcd…', 5),
('a', 1, 'a', 1),
('ab', 1, 'a', 1),
('で', 1, '', 0),
('でで', 1, '', 0),
('', 1, '', 0),
])
def test_clipstr(self, s, w, clippeds, clippedw):
clips, clipw = visidata.clipstr(s, w)
assert clips == clippeds
assert clipw == clippedw

@pytest.mark.parametrize('s, w, clippeds, clippedw', [
('b to', 4, 'b to', 4),
('abcde', 8, 'abcde', 5),
(' jsonl', 5, ' json', 5),
('abcdで', 6, 'abcdで', 6),
('abcdで', 5, 'abcd', 4),
('a', 1, 'a', 1),
('ab', 1, 'a', 1),
('で', 1, '', 0),
('でで', 1, '', 0),
('', 1, '', 0),
])
def test_clipstr_no_truncator(self, s, w, clippeds, clippedw):
clips, clipw = visidata.clipstr(s, w, truncator='')
assert clips == clippeds
assert clipw == clippedw

def test_clipdraw_chunks(self):
prechunks = [
('', 'x'),
Expand Down

0 comments on commit 36cf84e

Please sign in to comment.