Skip to content

Commit

Permalink
[ui-] fix clipdraw, add unit tests #2005
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 6, 2023
1 parent 5bbd5cd commit a50e7ee
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
8 changes: 4 additions & 4 deletions visidata/cliptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def dispwidth(ss, maxwidth=None, literal=False):
if cc:
w += wcwidth(cc, disp_ambig_width)
if maxwidth and w > maxwidth:
break
return maxwidth
return w


Expand Down Expand Up @@ -133,10 +133,10 @@ def _clipstr(s, dispw, trunch='', oddspacech='', combch='', modch=''):
newc = c
chlen = dispwidth(c)

if dispw and w+chlen > dispw-trunchlen:
if dispw and w+chlen > dispw:
if trunchlen and dispw > trunchlen:
ret = ret[:-1] + trunch # replace final char with ellipsis
w += trunchlen
w += trunchlen-1
break

w += chlen
Expand Down Expand Up @@ -222,7 +222,7 @@ def clipdraw_chunks(scr, y, x, chunks, attr, w=None, clear=True, rtl=False, lite
if origw is None:
chunkw = dispwidth(chunk, maxwidth=windowWidth-totaldispw)
else:
chunkw = origw
chunkw = origw-totaldispw

chunkw = min(chunkw, (x-1) if rtl else (windowWidth-x-1))
if chunkw <= 0: # no room anyway
Expand Down
37 changes: 37 additions & 0 deletions visidata/tests/test_cliptext.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from unittest.mock import Mock, call

import visidata


class TestClipText:
@pytest.mark.parametrize('s, dispw', [
('abcdef', 6),
('桜 高橋', 7),
('[:onclick sidebar-toggle][:reverse] b to toggle sidebar [:]', 21),
])
def test_dispwidth(self, s, dispw):
assert visidata.dispwidth(s) == dispw

@pytest.mark.parametrize('s, w, clippeds, clippedw', [
('b to', 4, 'b to', 4),
('abcde', 8, 'abcde', 5),
(' jsonl', 5, ' jso…', 5),
])
def test_clipstr(self, s, w, clippeds, clippedw):
clips, clipw = visidata.clipstr(s, w)
assert clips == clippeds
assert clipw == clippedw

def test_clipdraw_chunks(self):
prechunks = [
('', 'x'),
('', 'jsonl'),
]
scr = Mock()
scr.getmaxyx.return_value = (80,25)
visidata.clipdraw_chunks(scr, 0, 0, prechunks, 0, w=5)
scr.addstr.assert_has_calls([
call(0, 0, 'x', 0),
call(0, 1, 'jso…', 0),
], any_order=True)

0 comments on commit a50e7ee

Please sign in to comment.