diff --git a/visidata/cliptext.py b/visidata/cliptext.py index a505b0325..d5de0e279 100644 --- a/visidata/cliptext.py +++ b/visidata/cliptext.py @@ -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 = '' diff --git a/visidata/tests/test_cliptext.py b/visidata/tests/test_cliptext.py index 868d27249..ca6d3e13a 100644 --- a/visidata/tests/test_cliptext.py +++ b/visidata/tests/test_cliptext.py @@ -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'),