Skip to content

Commit

Permalink
[ui-] clipdraw to truncate correctly #2005
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 5, 2023
1 parent 0917f89 commit 530272f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
32 changes: 20 additions & 12 deletions visidata/cliptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,24 +117,31 @@ def iterchars(x):
def _clipstr(s, dispw, trunch='', oddspacech='', combch='', modch=''):
'''Return clipped string and width in terminal display characters.
Note: width may differ from len(s) if East Asian chars are 'fullwidth'.'''
if not s:
return '', 0

if dispw == 1:
return s[0], 1

w = 0
ret = ''

trunchlen = dispwidth(trunch)
for c in s:
newc, chlen = _dispch(c, oddspacech=oddspacech, combch=combch, modch=modch)
if newc:
ret += newc
w += chlen
else:
ret += c
w += dispwidth(c)

if dispw and w > dispw-trunchlen+1:
ret = ret[:-2] + trunch # replace final char with ellipsis
w += trunchlen
if not newc:
newc = c
chlen = dispwidth(c)

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

w += chlen
ret += newc

return ret, w


Expand Down Expand Up @@ -190,6 +197,9 @@ def clipdraw_chunks(scr, y, x, chunks, attr, w=None, clear=True, rtl=False, lite
clipped = ''
link = ''

if w and clear and not rtl:
scr.addstr(y, x, disp_column_fill*w, cattr.attr) # clear whole area before displaying

try:
for colorname, chunk in chunks:
if colorname.startswith('onclick'):
Expand Down Expand Up @@ -225,8 +235,6 @@ def clipdraw_chunks(scr, y, x, chunks, attr, w=None, clear=True, rtl=False, lite
# scr.addstr(y, x-dispw-1, disp_column_fill*dispw, attr)
scr.addstr(y, x-dispw-1, clipped, cattr.attr)
else:
if clear:
scr.addstr(y, x, disp_column_fill*chunkw, cattr.attr) # clear whole area before displaying
scr.addstr(y, x, clipped, cattr.attr)

if link:
Expand Down
2 changes: 1 addition & 1 deletion visidata/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
vd.menuRunning = False

def menudraw(*args):
return clipdraw(*args, truncator=' ')
return clipdraw(*args, truncator='')


def Menu(title, *args):
Expand Down
11 changes: 5 additions & 6 deletions visidata/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,18 +620,17 @@ def drawColHeader(self, scr, y, h, vcolidx):

hdrs = col.name.split('\n')
for i in range(h):
name = ' ' # save room at front for LeftMore or sorted arrow
name = ''
if colwidth > 2:
name = ' ' # save room at front for LeftMore or sorted arrow

if h-i-1 < len(hdrs):
name += hdrs[::-1][h-i-1]

if len(name) > colwidth-1:
name = name[:colwidth-len(self.options.disp_truncator)] + self.options.disp_truncator

if i == h-1:
hdrcattr = update_attr(hdrcattr, colors.color_bottom_hdr, 5)

clipdraw(scr, y+i, x, name, hdrcattr.attr, colwidth)
clipdraw(scr, y+i, x, name, hdrcattr.attr, w=colwidth)
vd.onMouse(scr, x, y+i, colwidth, 1, BUTTON3_RELEASED='rename-col')

if C and x+colwidth+len(C) < self.windowWidth and y+i < self.windowWidth:
Expand Down Expand Up @@ -787,7 +786,7 @@ def drawRow(self, scr, row, rowidx, ybase, rowcattr: ColorAttr, maxheight,
notewidth = 1 if note else 0
if note:
notecattr = update_attr(cattr, colors.get_color(cellval.notecolor), 10)
clipdraw(scr, ybase, x+colwidth-notewidth, note, notecattr.attr)
scr.addstr(ybase, x+colwidth-notewidth, note, notecattr.attr)

if voffset >= 0:
if len(lines)-voffset > height:
Expand Down
7 changes: 4 additions & 3 deletions visidata/sidebar.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ def drawSidebarText(sheet, scr, text:str, title:str='', overflowmsg:str='', bott
bottommsg = overflowmsg
break

x += clipdraw(sidebarscr, i+1, 2, line, cattr, w=w-2)
x += clipdraw(sidebarscr, i+1, 2, line, cattr, w=w-3)
i += 1

x = w-len(title)-6
clipdraw(sidebarscr, 0, x, f"|[:black on yellow] {title} [:]|", cattr)
titlew = dispwidth(title)
x = w-titlew-6
clipdraw(sidebarscr, 0, x, f"|[:black on yellow] {title} [:]|", cattr, w=titlew+4)
if bottommsg:
clipdraw(sidebarscr, h-1, winw-dispwidth(bottommsg)-4, '|'+bottommsg+'|[:]', cattr)

Expand Down

0 comments on commit 530272f

Please sign in to comment.