Skip to content

Commit

Permalink
[guide] rework API to use a GuideSheet
Browse files Browse the repository at this point in the history
  • Loading branch information
anjakefala committed Nov 25, 2023
1 parent ab0d438 commit cba7c7f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 16 deletions.
30 changes: 29 additions & 1 deletion visidata/experimental/guide.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import re

from visidata import vd, BaseSheet, Sheet, ItemColumn, Column, VisiData, ENTER
from visidata import wraptext

vd.guides = {} # guidename -> guideobj

Expand Down Expand Up @@ -91,15 +92,42 @@ def iterload(self):
yield [i] + list(m.groups())
i += 1

def openRow(self, row):
guidename = row[1]
return vd.getGuide(guidename)

class GuideSheet(Sheet):
rowtype = 'lines'
filetype = 'guide'
columns = [
ItemColumn('linenum', 0, type=int, width=0),
ItemColumn('guide', 1, width=80, displayer='full'),
]
precious = False
guide = ''

def iterload(self):
winWidth = 78
for startingLine, text in enumerate(self.guide.splitlines()):
text = text.strip()
if text:
for i, (L, _) in enumerate(wraptext(str(text), width=winWidth)):
yield [startingLine+i+1, L]
else:
yield [startingLine+1, text]



@VisiData.api
def getGuide(vd, guidename):
if guidename in vd.guides:
return vd.guides[guidename]
vd.warning(f'there is no guide: {guidename}')

BaseSheet.addCommand('', 'open-guide', 'vd.push(GuideGuide("VisiData_Guide"))', 'opens guide to features in VisiData')
GuideGuide.addCommand(ENTER, 'open-guide-row', 'vd.push(vd.getGuide(cursorRow[1]))')

vd.addMenuItems('''
Help > Guide > open-guide
''')

vd.addGlobals({'GuideSheet':GuideSheet})
29 changes: 14 additions & 15 deletions visidata/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from visidata.cmdlog import CommandLog, CommandLogJsonl
from visidata import vd, UNLOADED, asyncthread
from visidata import IndexSheet, VisiData, Sheet, Path, VisiDataMetaSheet, Column, ItemColumn, BaseSheet, TextSheet
from visidata import IndexSheet, VisiData, Sheet, Path, VisiDataMetaSheet, Column, ItemColumn, BaseSheet, GuideSheet

vd.macroMode = None
vd.macrobindings = {}
Expand Down Expand Up @@ -129,18 +129,8 @@ def startMacro(cmdlog):
def run(vd, *args, **kwargs):
vd.macrosheet

class MacrosGuide(TextSheet):
pass


Sheet.addCommand('m', 'macro-record', 'vd.cmdlog.startMacro()', 'record macro')
Sheet.addCommand('gm', 'macro-sheet', 'vd.push(vd.macrosheet)', 'open macros sheet')

vd.addMenuItems('''
System > Macros sheet > macro-sheet
''')

vd.addGuide('MacrosSheet', MacrosGuide(source = '''# Macros
class MacrosGuide(GuideSheet):
guide = '''# Macros
Macros allow you to bind a series of commands to a key and then replay those commands within a session by using that keystroke.
The basic usage is:
Expand All @@ -158,5 +148,14 @@ class MacrosGuide(TextSheet):
Macros can be marked for deletion (with `d`). Changes can then be committed with `z Ctrl+S`.
`Enter` will open the macro in the current row, and you can view the series of commands composing it.'''.splitlines()
) )
`Enter` will open the macro in the current row, and you can view the series of commands composing it.'''


Sheet.addCommand('m', 'macro-record', 'vd.cmdlog.startMacro()', 'record macro')
Sheet.addCommand('gm', 'macro-sheet', 'vd.push(vd.macrosheet)', 'open macros sheet')

vd.addMenuItems('''
System > Macros sheet > macro-sheet
''')

vd.addGuide('MacrosSheet', MacrosGuide())

0 comments on commit cba7c7f

Please # to comment.