Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[misc-] handle chars with screen width != 1, in unimportant cases #2666

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions visidata/aggregators.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from copy import copy

from visidata import Progress, Sheet, Column, ColumnsSheet, VisiData
from visidata import vd, anytype, vlen, asyncthread, wrapply, AttrDict, date, INPROGRESS
from visidata import vd, anytype, vlen, asyncthread, wrapply, AttrDict, date, INPROGRESS, dispwidth

vd.help_aggregators = '''# Choose Aggregators
Start typing an aggregator name or description.
Expand Down Expand Up @@ -264,7 +264,7 @@ def chooseAggregators(vd):
prompt = 'choose aggregators: '
def _fmt_aggr_summary(match, row, trigger_key):
formatted_aggrname = match.formatted.get('key', row.key) if match else row.key
r = ' '*(len(prompt)-3)
r = ' '*(dispwidth(prompt)-3)
r += f'[:keystrokes]{trigger_key}[/] '
r += formatted_aggrname
if row.desc:
Expand Down
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -77,7 +77,7 @@ def load_from(self, fp):
self.total_ms = sum(f.duration_ms or 0 for f in self.frames.values())
for f in self.frames.values():
for r, x, y, _ in self.iterdeep(f.rows):
self.width = max(self.width, x+len(r.text))
self.width = max(self.width, x+dispwidth(r.text))
self.height = max(self.height, y)

def draw(self, scr, *, t=0, x=0, y=0, loop=False, attr=ColorAttr(), **kwargs):
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
3 changes: 2 additions & 1 deletion visidata/features/go_col.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def nextColName(sheet, show_cells=True):

def _fmt_colname(match, row, trigger_key):
name = match.formatted.get('name', row.name) if match else row.name
r = ' '*(len(prompt)-3)
r = ' '*(dispwidth(prompt)-3)
r += f'[:keystrokes]{trigger_key}[/] '
if show_cells and len(sheet.rows) > 0:
# pad the right side with spaces
Expand All @@ -46,6 +46,7 @@ def _fmt_colname(match, row, trigger_key):
else:
r += name
return r

name = vd.activeSheet.inputPalette(prompt,
colnames,
value_key='name',
Expand Down
4 changes: 2 additions & 2 deletions visidata/features/join.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import functools
from copy import copy

from visidata import vd, VisiData, asyncthread, Sheet, Progress, IndexSheet, Column, CellColorizer, ColumnItem, SubColumnItem, TypedWrapper, ColumnsSheet, AttrDict
from visidata import vd, VisiData, asyncthread, Sheet, Progress, IndexSheet, Column, CellColorizer, ColumnItem, SubColumnItem, TypedWrapper, ColumnsSheet, AttrDict, dispwidth

vd.help_join = '# Join Help\nHELPTODO'

Expand Down Expand Up @@ -367,7 +367,7 @@ def chooseJointype(vd):
prompt = 'choose jointype: '
def _fmt_aggr_summary(match, row, trigger_key):
formatted_jointype = match.formatted.get('key', row.key) if match else row.key
r = ' '*(len(prompt)-3)
r = ' '*(dispwidth(prompt)-3)
r += f'[:keystrokes]{trigger_key}[/] '
r += formatted_jointype
if row.desc:
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
5 changes: 3 additions & 2 deletions visidata/man/parse_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import sys
import visidata
dispwidth = visidata.dispwidth


fncli, fnopts = sys.argv[1:]
Expand Down Expand Up @@ -31,7 +32,7 @@
optkeys = visidata.options.keys()
optvalues = [visidata.options._opts._get(optname) for optname in optkeys]

widestoptwidth, widestopt = sorted((len(opt.name)+len(str(opt.value)), opt.name) for opt in optvalues)[-1]
widestoptwidth, widestopt = sorted((dispwidth(opt.name)+dispwidth(str(opt.value)), opt.name) for opt in optvalues)[-1]
print('widest option+default is "%s", width %d' % (widestopt, widestoptwidth))
widestoptwidth = 35
menuOut.write('.Bl -tag -width %s -compact\n' % ('X'*(widestoptwidth+3)))
Expand All @@ -51,7 +52,7 @@
else:
cli_optname=opt.name.replace('_', '-')
cli_type=type(opt.value).__name__
optlen = len(cli_optname)+len(cli_type)+1
optlen = dispwidth(cli_optname)+dispwidth(cli_type)+1
if cli_type != 'bool' or visidata.options.getdefault(opt.name):
cliOut.write(options_cli_skel.format(cli_optname=cli_optname,
optname = opt.name,
Expand Down
16 changes: 8 additions & 8 deletions visidata/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ def drawSubmenu(vd, scr, sheet, y, x, menus, level, disp_menu_boxchars=''):

maxbinding = 0
if vd.options.disp_menu_keys:
maxbinding = max(len(item.binding or '') for item in menus)+1
maxbinding = max(dispwidth(item.binding or '') for item in menus)+1

w = max(len(item.title) for item in menus)+maxbinding+2
w = max(dispwidth(item.title) for item in menus)+maxbinding+2

# draw borders before/under submenus
if level > 1:
Expand Down Expand Up @@ -225,11 +225,11 @@ def drawSubmenu(vd, scr, sheet, y, x, menus, level, disp_menu_boxchars=''):
mainbinding = vd.prettykeys(revbinds[0])

# actually display the menu item
title += ' '*(w-len(pretitle)-len(item.title)+1) # padding
title += ' '*(w-dispwidth(pretitle)-dispwidth(item.title)+1) # padding

menudraw(scr, y+i, x+1, pretitle+title, attr)
if maxbinding and mainbinding:
menudraw(scr, y+i, x+1+w-len(mainbinding), mainbinding, attr.update(colors.keystrokes))
menudraw(scr, y+i, x+1+w-dispwidth(mainbinding), mainbinding, attr.update(colors.keystrokes))
menudraw(scr, y+i, x+2+w, titlenote, attr)
menudraw(scr, y+i, x+3+w, ls, colors.color_menu)

Expand Down Expand Up @@ -317,7 +317,7 @@ def drawMenu(vd, scr, sheet):
BUTTON1_RELEASED=vd.nop,
BUTTON2_RELEASED=vd.nop,
BUTTON3_RELEASED=vd.nop)
x += len(item.title)+2
x += dispwidth(item.title)+2

rightdisp = sheet.options.disp_menu_fmt.format(sheet=sheet, vd=vd)
menudraw(scr, 0, x+4, rightdisp, colors.color_menu)
Expand Down Expand Up @@ -355,13 +355,13 @@ def drawMenu(vd, scr, sheet):

# cmd.helpstr text
for i, line in enumerate(helplines):
menudraw(scr, y+i, helpx, ls+' '+line+' '*(helpw-len(line)-3)+rs, helpattr)
menudraw(scr, y+i, helpx, ls+' '+line+' '*(helpw-dispwidth(line)-3)+rs, helpattr)
y += len(helplines)

if sidelines:
menudraw(scr, y, helpx, ls+' '*(helpw-2)+rs, helpattr)
for i, line in enumerate(sidelines):
menudraw(scr, y+i+1, helpx, ls+' '+line+' '*(helpw-len(line)-6)+rs, helpattr)
menudraw(scr, y+i+1, helpx, ls+' '+line+' '*(helpw-dispwidth(line)-6)+rs, helpattr)
y += len(sidelines)+1

menudraw(scr, y, helpx, bl+bs*(helpw-2)+br, helpattr)
Expand All @@ -371,7 +371,7 @@ def drawMenu(vd, scr, sheet):
menudraw(scr, menuy, helpx+2, rsl, helpattr)
ks = vd.prettykeys(mainbinding or '(unbound)')
menudraw(scr, menuy, helpx+3, ' '+ks+' ', colors.color_menu_active)
menudraw(scr, menuy, helpx+2+len(ks)+3, lsr, helpattr)
menudraw(scr, menuy, helpx+2+dispwidth(ks)+3, lsr, helpattr)
menudraw(scr, menuy, helpx+19, ' '+cmd.longname+' ', helpattr)

vd.onMouse(scr, helpx, menuy, helpw, y-menuy+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:]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need to use the same kind of partition() function that was mentioned in #2662.

return s

Expand Down
Loading