Skip to content

Commit 36cf84e

Browse files
committed
[cliptext-] clipstr(): truncate too-wide char into width 1
1 parent 6b0a78a commit 36cf84e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

visidata/cliptext.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,16 @@ def _clipstr(s, dispw, trunch='', oddspacech='', combch='', modch=''):
148148
return '', 0
149149

150150
if dispw == 1:
151-
return s[0], 1
151+
w_s = dispwidth(s)
152+
if w_s > 1:
153+
ret = ''
154+
for c in s:
155+
if dispwidth(ret + c) > 1:
156+
break
157+
ret += c
158+
return ret, dispwidth(ret)
159+
else:
160+
return s, w_s
152161

153162
w = 0
154163
ret = ''

visidata/tests/test_cliptext.py

+22
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,34 @@ def test_dispwidth(self, s, dispw):
1919
(' jsonl', 5, ' jso…', 5),
2020
('abcdで', 6, 'abcdで', 6),
2121
('abcdで', 5, 'abcd…', 5),
22+
('a', 1, 'a', 1),
23+
('ab', 1, 'a', 1),
24+
('で', 1, '', 0),
25+
('でで', 1, '', 0),
26+
('', 1, '', 0),
2227
])
2328
def test_clipstr(self, s, w, clippeds, clippedw):
2429
clips, clipw = visidata.clipstr(s, w)
2530
assert clips == clippeds
2631
assert clipw == clippedw
2732

33+
@pytest.mark.parametrize('s, w, clippeds, clippedw', [
34+
('b to', 4, 'b to', 4),
35+
('abcde', 8, 'abcde', 5),
36+
(' jsonl', 5, ' json', 5),
37+
('abcdで', 6, 'abcdで', 6),
38+
('abcdで', 5, 'abcd', 4),
39+
('a', 1, 'a', 1),
40+
('ab', 1, 'a', 1),
41+
('で', 1, '', 0),
42+
('でで', 1, '', 0),
43+
('', 1, '', 0),
44+
])
45+
def test_clipstr_no_truncator(self, s, w, clippeds, clippedw):
46+
clips, clipw = visidata.clipstr(s, w, truncator='')
47+
assert clips == clippeds
48+
assert clipw == clippedw
49+
2850
def test_clipdraw_chunks(self):
2951
prechunks = [
3052
('', 'x'),

0 commit comments

Comments
 (0)