From 6e55619efa8da84a8c18ea8ae07cf9159982cfaa Mon Sep 17 00:00:00 2001 From: Saul Pwanson Date: Wed, 25 Oct 2023 21:14:18 -0700 Subject: [PATCH] [DirSheet] integrate plugins/vmailcap, add sysopen-mailcap --- plugins/vmailcap.py | 42 ------------------------- visidata/_open.py | 2 +- visidata/features/sysopen_mailcap.py | 46 ++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 43 deletions(-) delete mode 100644 plugins/vmailcap.py create mode 100644 visidata/features/sysopen_mailcap.py diff --git a/plugins/vmailcap.py b/plugins/vmailcap.py deleted file mode 100644 index 910f28ee1..000000000 --- a/plugins/vmailcap.py +++ /dev/null @@ -1,42 +0,0 @@ -''' -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: - 1. copy to plugins dir - 2. add `import plugins.vmailcap` to .visidatarc or .vlsrc - 3. press Ctrl+V or g-Ctrl+V to view file(s) as desired. -''' - -__name__ = 'vmailcap' -__author__ = 'Saul Pwanson ' -__version__ = '0.9' - -import os -from visidata import vd, DirSheet, SuspendCurses - -vd.option('mimetype', '', 'mimetype to be used with mailcap') - -@DirSheet.api -def run_mailcap(sheet, p, key): - import mailcap - import mimetypes - - mimetype = sheet.options.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() - - cmdline, mcap_entry = mailcap.findmatch(caps, mimetype, key=key, filename=str(p)) - with SuspendCurses(): - os.system(cmdline) - - -DirSheet.addCommand('^V', 'mailcap-view', 'run_mailcap(cursorRow, "view")') -DirSheet.addCommand('g^V', 'mailcap-view-selected', 'for r in selectedRows: run_mailcap(r, "view")') diff --git a/visidata/_open.py b/visidata/_open.py index 86508485a..6a11d3475 100644 --- a/visidata/_open.py +++ b/visidata/_open.py @@ -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 ''') diff --git a/visidata/features/sysopen_mailcap.py b/visidata/features/sysopen_mailcap.py new file mode 100644 index 000000000..fa1821c18 --- /dev/null +++ b/visidata/features/sysopen_mailcap.py @@ -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 +''')