Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[cliptext-] clipstr() to width 1: truncate chars having width > 1 #2667

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion visidata/cliptext.py
Original file line number Diff line number Diff line change
@@ -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 = ''
22 changes: 22 additions & 0 deletions visidata/tests/test_cliptext.py
Original file line number Diff line number Diff line change
@@ -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'),
Loading