From fb16f8aa70f871eceb194fa3f93fe8cfaac1e11f Mon Sep 17 00:00:00 2001 From: agryphus Date: Sun, 9 Feb 2025 21:36:34 -0500 Subject: [PATCH] Added keybinds for options for Yazi opener --- misc/.config/yazi/keymap.toml | 13 +++- .../yazi/plugins/smart-enter.yazi/init.lua | 19 +++++- misc/.config/yazi/theme.toml | 42 +++++-------- misc/.config/yazi/yazi.toml | 11 +++- misc/.local/bin/opener | 63 ++++++++++++++++--- 5 files changed, 106 insertions(+), 42 deletions(-) diff --git a/misc/.config/yazi/keymap.toml b/misc/.config/yazi/keymap.toml index 1774179..753e62e 100644 --- a/misc/.config/yazi/keymap.toml +++ b/misc/.config/yazi/keymap.toml @@ -5,10 +5,19 @@ [manager] prepend_keymap = [ + # Smart enter + { on = [ "o" ], run = [ "plugin smart-enter --sync --args='list'", "escape --visual --select" ], desc = "List openers for file." }, + { on = [ "l" ], run = [ "plugin smart-enter --sync", "escape --visual --select" ], desc = "Enter the child directory, or open file." }, + { on = [ "L" ], run = [ "plugin smart-enter --sync --args='detatch'", "escape --visual --select" ], desc = "Open in new window." }, + { on = [ "1" ], run = [ "shell 'opener -n 1 $0' --block --confirm"], desc = "Open with program 1." }, + { on = [ "2" ], run = [ "shell 'opener -n 2 $0' --block --confirm"], desc = "Open with program 2." }, + { on = [ "3" ], run = [ "shell 'opener -n 3 $0' --block --confirm"], desc = "Open with program 3." }, + { on = [ "!" ], run = [ "shell 'opener -n 1 detatch $0' --block --confirm"], desc = "Open in new window with program 1." }, + { on = [ "@" ], run = [ "shell 'opener -n 2 detatch $0' --block --confirm"], desc = "Open in new window with program 2." }, + { on = [ "#" ], run = [ "shell 'opener -n 3 detatch $0' --block --confirm"], desc = "Open in new window with program 3." }, + { on = [ "j" ], run = [ "plugin --sync arrow --args=1"], desc = "Move selection downwards, or wrap." }, { on = [ "k" ], run = [ "plugin --sync arrow --args=-1"], desc = "Move selection upwards, or wrap." }, - { on = [ "l" ], run = [ "plugin smart-enter --sync", "escape --visual --select" ], desc = "Enter the child directory, or open file." }, - { on = [ "L" ], run = "plugin smart-enter --sync --args='detatch'", desc = "Open in new window." }, { on = [ "T" ], run = "plugin --sync max-preview", desc = "Maximize preview pane"}, { on = [ "n" ], run = "create", desc = "Create a file or directory (ends with / for directories)" }, { on = [ ""], run = ''' diff --git a/misc/.config/yazi/plugins/smart-enter.yazi/init.lua b/misc/.config/yazi/plugins/smart-enter.yazi/init.lua index 80da2a9..048360c 100644 --- a/misc/.config/yazi/plugins/smart-enter.yazi/init.lua +++ b/misc/.config/yazi/plugins/smart-enter.yazi/init.lua @@ -8,11 +8,24 @@ return { return end - if #args > 0 and args[1] == "detatch" then - os.execute(string.format("opener detatch \"%s\"", h.url)) - else + if #args == 0 then ya.manager_emit("open", {}) end + + if args[1] == "detatch" then + os.execute(string.format("opener detatch \"%s\"", h.url)) + elseif args[1] == "list" then + local f = assert(io.popen(string.format( + "opener list \"%s\"", h.url), 'r')) + local out = assert(f:read('*a')) + f:close() + ya.notify { + title = string.format("Openers for %s:", h.name), + content = out, + timeout = 6.5, + level = "info", + } + end end, } diff --git a/misc/.config/yazi/theme.toml b/misc/.config/yazi/theme.toml index 86dcccb..10a75f4 100644 --- a/misc/.config/yazi/theme.toml +++ b/misc/.config/yazi/theme.toml @@ -169,35 +169,31 @@ icon_error = "" [filetype] rules = [ - # Images - { mime = "image/*", fg = "yellow" }, + # Sections correspond to different colors and their meaning - # Media + # Can see and not hear + { mime = "image/*", fg = "yellow" }, + { mime = "application/{epub*,pdf,doc,rtf,vnd.*}", fg = "yellow" }, + + # Can hear { mime = "{audio,video}/*", fg = "magenta" }, - # Archives - { mime = "application/{,g}zip", fg = "red" }, - { mime = "application/x-{tar,bzip*,7z-compressed,xz,rar}", fg = "red" }, + # Is link + { name = "*", is = "link", fg = "cyan" }, + { name = "*/", is = "link", fg = "cyan", bold = true }, - # Documents - { mime = "application/{pdf,doc,rtf,vnd.*}", fg = "cyan" }, + # Can open + { mime = "application/{,g}zip", fg = "blue" }, + { mime = "application/x-{tar,bzip*,7z-compressed,xz,rar}", fg = "blue" }, + { name = "*/", fg = "blue", bold = true }, - # Empty files - # { mime = "inode/x-empty", fg = "red" }, + # Can run + { name = "*", is = "exec", fg = "green" }, - # Special files + # Broken { name = "*", is = "orphan", bg = "red" }, - { name = "*", is = "exec" , fg = "green" }, - - # Dummy files { name = "*", is = "dummy", bg = "red" }, { name = "*/", is = "dummy", bg = "red" }, - - # Fallback - # { name = "*", fg = "white" }, - { name = "*/", is = "link", fg = "cyan", bold = true }, - { name = "*", is = "link", fg = "cyan", bold = true }, - { name = "*/", fg = "blue", bold = true }, ] [icon] @@ -757,10 +753,6 @@ exts = [ { name = "zip", text = "", fg_dark = "#eca517", fg_light = "#76520c" }, { name = "zsh", text = "", fg_dark = "#89e051", fg_light = "#447028" }, { name = "zst", text = "", fg_dark = "#eca517", fg_light = "#76520c" }, - - { name = "*", text = "", is = "exec" }, - { name = "*" , text = "" }, - { name = "*/", text = "󰝰", fg = "yellow" }, ] conds = [ # Special files @@ -774,7 +766,7 @@ conds = [ # Fallback { if = "dir", text = "󰝰", fg_dark = "yellow" }, - { if = "exec", text = "" }, + { if = "exec", text = "", fg_dark = "yellow" }, { if = "!dir", text = "" }, ] diff --git a/misc/.config/yazi/yazi.toml b/misc/.config/yazi/yazi.toml index 9097723..4b022c3 100644 --- a/misc/.config/yazi/yazi.toml +++ b/misc/.config/yazi/yazi.toml @@ -31,12 +31,17 @@ ueberzug_offset = [ 0, 0, 0, 0 ] open = [ { run = 'opener "$@"', block = true, desc = "Open" }, ] +alt = [ + { run = 'opener "$@"', block = true, desc = "Alt" }, +] +nothing = [ + { run = 'echo -n', desc = "" }, +] [open] rules = [ - { name = "*/", use = [ "edit", "open", "reveal" ] }, - - { mime = "*", use = [ "open" ] }, + { name = "*/", use = [ "nothing" ] }, + { mime = "*", use = [ "open", "alt" ] }, ] [tasks] diff --git a/misc/.local/bin/opener b/misc/.local/bin/opener index d34360c..1421aa5 100755 --- a/misc/.local/bin/opener +++ b/misc/.local/bin/opener @@ -1,18 +1,46 @@ #!/usr/bin/env sh # Script for opening a file, intended for use by a terminal file manager. +function help_exit() { + echo "Utility for opening different filetypes. + +Usage: + $ opener [-n {num}] {file} # Open the file in the current shell + $ opener [-n {num}] detatch {file} # Detatch the program into a new shell + +[num]: which opener to use, in order of precedence. defaults to 1 +" + exit 0 +} + +num=1 +[ "$1" = "-n" ] && num=$2 && shift 2 + # Whether to open in a new window detatch=0 [ "$1" = "detatch" ] && detatch=1 && shift 1 +list=0 +[ "$1" = "list" ] && list=1 && shift 1 + # First arg must be a file file="$1" -[ ! -f "$file" ] && echo "Not a file: '$file'" && exit 1 +[ ! -f "$file" ] && echo "Not a file: '$file'" && help_exit while [ -L "$file" ]; do # Resolve symlinks file=$(readlink "$file") done +function list_exit() { + [ "$list" -ne 1 ] && return + i=1 + for arg in "$@"; do + echo "$i: $arg" + i=$((i + 1)) + done + exit 0 +} + function launch_gui() { if [ "$detatch" -eq 0 ]; then [ -n "$WAYLAND_DISPLAY" ] \ @@ -52,20 +80,29 @@ case $(file --mime-type "$file" -b) in application/x-subrip|\ inode/x-empty|\ text/*) - case "${1##*.}" in - org|typ) - # Any "document" like file ought to be in emacs - launch_gui emacsclient -c "$file" - exit - ;; - esac + list_exit \ + "Neovim" \ + "Emacs GUI" \ + "Emacs TUI" - launch_term nvim "$file" + if [ "$num" -eq 2 ]; then + launch_gui emacsclient -c -a emacs "$file" + elif [ "$num" -eq 3 ]; then + launch_term emacsclient -nw -a 'emacs -nw' "$file" + else + launch_term nvim "$file" + fi ;; image/*) + list_exit \ + "nsxiv" launch_gui nsxiv "$file" ;; video/*) + list_exit \ + "Neovim" \ + "Emacs GUI" \ + "Emacs TUI" launch_gui mpv -quiet "$file" ;; application/epub*|\ @@ -74,10 +111,15 @@ case $(file --mime-type "$file" -b) in application/postscript|\ application/vnd.djvu|\ image/vnd.djvu) + list_exit \ + "Zathura" launch_gui zathura "$file" ;; audio/*|\ video/x-ms-asf) + list_exit \ + "MPV" + # If it has an album cover, launch graphical mpv [ -z "$(mediainfo "$1" | grep "Cover\s*: Yes")" ] \ && (launch_term mpv --audio-display=no "$file") \ || (launch_gui mpv "$file") @@ -97,9 +139,12 @@ case $(file --mime-type "$file" -b) in application/vnd.openxmlformats-officedocument.presentationml.presentation|\ application/vnd.openxmlformats-officedocument.spreadsheetml.sheet|\ application/vnd.openxmlformats-officedocument.wordprocessingml.document) + list_exit \ + "LibreOffice" launch_gui libreoffice "$file" ;; *) + echo "No program defined for this filetype." ;; esac