Skip to content

Commit

Permalink
highlight matched letters on a set basis
Browse files Browse the repository at this point in the history
  • Loading branch information
zormit committed Oct 18, 2023
1 parent 14e7d40 commit e060f45
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions visidata/features/cmdpalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ def levenshtein(a, b):
d[i+1][j+1] = min(d[i][j+1]+1, d[i+1][j]+1, d[i][j]+cost)
return d[m][n]

def format_name(s, letters):
out = ""
for l in s:
if l in letters:
out += f"[:bold][:onclick {s}]{l}[:][:]"
else:
out += f"[:onclick {s}]{l}[:]"
return out


@BaseSheet.api
def inputLongname(sheet):
Expand All @@ -34,7 +43,7 @@ def inputLongname(sheet):
longnames = set(k for (k, obj), v in vd.commands.iter(sheet))
this_sheets_help = HelpSheet("", source=sheet)
this_sheets_help.ensureLoaded()
Match = collections.namedtuple('Match', 'name keystrokes description distance')
Match = collections.namedtuple('Match', 'name formatted_name keystrokes description distance')
bindings = dict()

def longname_executor(name):
Expand All @@ -52,7 +61,8 @@ def myupdater(value):
if letters.issubset(set(row.longname)):
description = this_sheets_help.cmddict[(row.sheet, row.longname)].helpstr
keystrokes = this_sheets_help.revbinds.get(row.longname, [None])[0]
matches.append(Match(row.longname, keystrokes, description, distance))
formatted_name = format_name(row.longname, letters)
matches.append(Match(row.longname, formatted_name, keystrokes, description, distance))
matches.sort(key=lambda m: m[3])

# do the drawing
Expand All @@ -66,7 +76,7 @@ def myupdater(value):
else:
trigger_key = " "
buffer = " "*(len(label)-2)
match_summary = f"{buffer}{trigger_key} [:onclick {m.name}]{m.name}[:] ({m.keystrokes}) - {m.description}"
match_summary = f"{buffer}{trigger_key} {m.formatted_name} ({m.keystrokes}) - {m.description}"
if vd.options.debug:
debug_info = f"[{m.distance}]"
match_summary = debug_info + match_summary[len(debug_info):]
Expand Down

0 comments on commit e060f45

Please sign in to comment.