Skip to content

Commit

Permalink
[misc-] replace len() by dispwidth()
Browse files Browse the repository at this point in the history
In cliptext, ddwplay, cmdpalette, form, sheets, and statusbar.
  • Loading branch information
midichef committed Jan 6, 2025
1 parent 6b0a78a commit 472953e
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 8 deletions.
3 changes: 2 additions & 1 deletion visidata/cliptext.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ def wraptext(text, width=80, indent=''):
line = _markdown_to_internal(line)
chunks = re.split(internal_markup_re, line)
textchunks = [x for x in chunks if not is_vdcode(x)]
# textwrap.wrap does not handle variable-width characters #2416
for linenum, textline in enumerate(textwrap.wrap(''.join(textchunks), width=width, drop_whitespace=False)):
txt = textline
r = ''
Expand Down Expand Up @@ -345,7 +346,7 @@ def clipbox(scr, lines, attr, title=''):
for i, line in enumerate(lines):
clipdraw(scr, i+1, 2, line, attr)

clipdraw(scr, 0, w-len(title)-6, f"| {title} |", attr)
clipdraw(scr, 0, w-dispwidth(title)-6, f"| {title} |", attr)


vd.addGlobals(clipstr=clipstr,
Expand Down
2 changes: 1 addition & 1 deletion visidata/ddwplay.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from collections import defaultdict
import json
import time
from visidata import colors, vd, clipdraw, ColorAttr
from visidata import colors, vd, clipdraw, ColorAttr, dispwidth

__all__ = ['Animation', 'AnimationMgr']

Expand Down
4 changes: 2 additions & 2 deletions visidata/features/cmdpalette.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import collections
from functools import partial
from visidata import DrawablePane, BaseSheet, vd, VisiData, CompleteKey, clipdraw, HelpSheet, colors, AcceptInput, AttrDict, drawcache_property
from visidata import DrawablePane, BaseSheet, vd, VisiData, CompleteKey, clipdraw, HelpSheet, colors, AcceptInput, AttrDict, drawcache_property, dispwidth


vd.theme_option('color_cmdpalette', 'black on 72', 'base color of command palette')
Expand Down Expand Up @@ -180,7 +180,7 @@ def _fmt_cmdpal_summary(match, row, trigger_key):
formatted_name = f'[:bold][:onclick {row.longname}]{formatted_longname}[/][/]'
if vd.options.debug and match:
keystrokes = f'[{match.score}]'
r = f' [:keystrokes]{keystrokes.rjust(len(prompt)-5)}[/] '
r = f' [:keystrokes]{keystrokes.rjust(dispwidth(prompt)-5)}[/] '
if trigger_key:
r += f'[:keystrokes]{trigger_key}[/]'
else:
Expand Down
4 changes: 2 additions & 2 deletions visidata/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def draw(self, scr):
continue
x, y = r.x, r.y
if isinstance(y, float) and (0 < y < 1) or (-1 < y < 0): y = h*y
if isinstance(x, float) and (0 < x < 1) or (-1 < x < 0): x = w*x-(len(r.text)/2)
if isinstance(x, float) and (0 < x < 1) or (-1 < x < 0): x = w*x-(dispwidth(r.text)/2)
x = int(x)
y = int(y)
if y < 0: y += h
Expand All @@ -61,7 +61,7 @@ def run(self, scr):
vd.setWindows(vd.scrFull)
drawnrows = [r for r in self.source.rows if r.text]
inputs = [r for r in self.source.rows if r.input]
maxw = max(int(r.x)+len(r.text) for r in drawnrows)
maxw = max(int(r.x)+dispwidth(r.text) for r in drawnrows)
maxh = max(int(r.y) for r in drawnrows)
h, w = vd.scrFull.getmaxyx()
y, x = max(0, (h-maxh)//2-1), max(0, (w-maxw)//2-1)
Expand Down
2 changes: 1 addition & 1 deletion visidata/sheets.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ def drawColHeader(self, scr, y, h, vcolidx):
clipdraw(scr, y+i, x, name, hdrcattr, 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.windowHeight:
if C and x+colwidth+dispwidth(C) < self.windowWidth and y+i < self.windowHeight:
scr.addstr(y+i, x+colwidth, C, sepcattr.attr)

clipdraw(scr, y+h-1, min(x+colwidth, self.windowWidth-1)-dispwidth(T), T, hdrcattr)
Expand Down
2 changes: 1 addition & 1 deletion visidata/statusbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
BaseSheet.init('longname', lambda: '')

def fitWithin(s, n=10):
if len(s) > n:
if dispwidth(s) > n:
return s[:n//2-1] + '…' + s[-n//2+1:]
return s

Expand Down

0 comments on commit 472953e

Please sign in to comment.