Skip to content

Commit

Permalink
[DirSheet] integrate plugins/vmailcap, add sysopen-mailcap
Browse files Browse the repository at this point in the history
  • Loading branch information
saulpw committed Oct 26, 2023
1 parent 3dcf66f commit 6e55619
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 43 deletions.
42 changes: 0 additions & 42 deletions plugins/vmailcap.py

This file was deleted.

2 changes: 1 addition & 1 deletion visidata/_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,6 @@ def loadInternalSheet(vd, cls, p, **kwargs):
BaseSheet.addCommand('gU', 'undo-last-quit', 'push(allSheets[-1])', 'reopen most recently closed sheet')

vd.addMenuItems('''
File > Open file/url > open-file
File > Open > input file/url > open-file
File > Reopen last closed > undo-last-quit
''')
46 changes: 46 additions & 0 deletions visidata/features/sysopen_mailcap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
''' Plugin for viewing files with appropriate mailcap-specified application.
Add mailcap-view and mailcap-view-selected commands to DirSheet.
mimetype can be given explicitly with `mimetype` option; will be guessed by filename otherwise.
Usage:
- add `import experimental.mailcap_view` to .visidatarc
- on the DirSheet, `Ctrl+V` or `gCtrl+V` to view file(s) using mailcap entry for the guessed (or given via options) mimetype
'''

import os
from visidata import vd, DirSheet, SuspendCurses

vd.option('mailcap_mimetype', '', 'force mimetype for sysopen-mailcap')
vd.optalias('mimetype', 'mailcap_mimetype')


@DirSheet.api
def run_mailcap(sheet, p, key='view'):
import mailcap
import mimetypes

mimetype = sheet.options.mailcap_mimetype
if not mimetype:
mimetype, encoding = mimetypes.guess_type(str(p))

if not mimetype:
vd.fail('no mimetype given and no guess')

caps = mailcap.getcaps()

plist = [f'{k}={v}' for k, v in sheet.options.getall('mailcap_').items() if k != 'mailcap_mimetype']
cmdline, mcap_entry = mailcap.findmatch(caps, mimetype, key=key, filename=str(p), plist=plist)

with SuspendCurses():
os.system(cmdline)


DirSheet.addCommand('', 'sysopen-mailcap', 'run_mailcap(cursorRow)', 'open using mailcap entry for current row, guessing mimetype')
DirSheet.addCommand('', 'sysopen-mailcap-selected', 'for r in selectedRows: run_mailcap(r)', 'open selected files in succession, using mailcap')


vd.addMenuItems('''
File > Open > using mailcap > file at cursor > sysopen-mailcap
File > Open > using mailcap > selected files > sysopen-mailcap-selected
''')

0 comments on commit 6e55619

Please sign in to comment.