From d8914cb4d2212fb1e3018edb7df55eff132906d2 Mon Sep 17 00:00:00 2001 From: Donovan Glover Date: Sat, 27 May 2023 15:37:07 -0400 Subject: [PATCH] meta: Switch from ranger to joshuto Joshuto is *significantly* faster than ranger and is written in Rust instead of Python. Although both ranger and joshuto have not seen a new release in a while, the future of joshuto seems more promising. Joshuto is additionally faster than lf and, similar to lf, does not hang when previewing images with kitty. --- terminal/default.nix | 2 +- terminal/fish-starship/default.nix | 2 +- terminal/joshuto/default.nix | 28 +++++++++ terminal/joshuto/kitty-remove.sh | 5 ++ terminal/joshuto/kitty-show.sh | 32 ++++++++++ terminal/joshuto/package/default.nix | 22 +++++++ terminal/joshuto/preview.sh | 89 ++++++++++++++++++++++++++++ terminal/ranger/default.nix | 28 --------- 8 files changed, 178 insertions(+), 30 deletions(-) create mode 100644 terminal/joshuto/default.nix create mode 100755 terminal/joshuto/kitty-remove.sh create mode 100755 terminal/joshuto/kitty-show.sh create mode 100644 terminal/joshuto/package/default.nix create mode 100755 terminal/joshuto/preview.sh delete mode 100644 terminal/ranger/default.nix diff --git a/terminal/default.nix b/terminal/default.nix index d9630be6..acb9eaa1 100644 --- a/terminal/default.nix +++ b/terminal/default.nix @@ -7,7 +7,7 @@ ./gpg ./ncmpcpp ./neovim - ./ranger + ./joshuto ]; environment.systemPackages = with pkgs; [ diff --git a/terminal/fish-starship/default.nix b/terminal/fish-starship/default.nix index 54dea28e..d18800f1 100644 --- a/terminal/fish-starship/default.nix +++ b/terminal/fish-starship/default.nix @@ -68,7 +68,7 @@ e = "exit"; k = "kitty @ set-colors -c -a ~/.cache/wal/kitty"; l = "ls -l"; - r = "ranger"; + j = "joshuto"; w = "wal -o ~/.config/wal/done.sh"; T = "tree"; }; diff --git a/terminal/joshuto/default.nix b/terminal/joshuto/default.nix new file mode 100644 index 00000000..5e6e461d --- /dev/null +++ b/terminal/joshuto/default.nix @@ -0,0 +1,28 @@ +{ pkgs, ... }: + +{ + environment.systemPackages = [ + (pkgs.callPackage ./package {}) + ]; + + home-manager.sharedModules = [{ + xdg.configFile."joshuto/joshuto.toml".text = '' + [display] + automatically_count_files = true + show_borders = false + show_hidden = true + line_number_style = "absolute" + collapse_preview = false + + [preview] + max_preview_size = 104857600 + preview_script = "~/.config/joshuto/preview.sh" + preview_shown_hook_script = "~/.config/joshuto/kitty-show.sh" + preview_removed_hook_script = "~/.config/joshuto/kitty-remove.sh" + ''; + + xdg.configFile."joshuto/preview.sh".source = ./preview.sh; + xdg.configFile."joshuto/kitty-show.sh".source = ./kitty-show.sh; + xdg.configFile."joshuto/kitty-remove.sh".source = ./kitty-remove.sh; + }]; +} diff --git a/terminal/joshuto/kitty-remove.sh b/terminal/joshuto/kitty-remove.sh new file mode 100755 index 00000000..bd66564d --- /dev/null +++ b/terminal/joshuto/kitty-remove.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +kitty +kitten icat \ + --transfer-mode=file \ + --clear 2>/dev/null diff --git a/terminal/joshuto/kitty-show.sh b/terminal/joshuto/kitty-show.sh new file mode 100755 index 00000000..c1f6a9d1 --- /dev/null +++ b/terminal/joshuto/kitty-show.sh @@ -0,0 +1,32 @@ +#!/usr/bin/env bash + +FILE_PATH="$1" # Full path of the previewed file +PREVIEW_X_COORD="$2" # x coordinate of upper left cell of preview area +PREVIEW_Y_COORD="$3" # y coordinate of upper left cell of preview area +PREVIEW_WIDTH="$4" # Width of the preview pane (number of fitting characters) +PREVIEW_HEIGHT="$5" # Height of the preview pane (number of fitting characters) + +TMP_FILE="$HOME/.cache/joshuto/thumbcache.png" + +mimetype=$(file --mime-type -Lb "$FILE_PATH") + +function image { + kitty +kitten icat \ + --transfer-mode=file \ + --clear 2>/dev/null + kitty +kitten icat \ + --transfer-mode=file \ + --place "${PREVIEW_WIDTH}x${PREVIEW_HEIGHT}@${PREVIEW_X_COORD}x${PREVIEW_Y_COORD}" \ + "$1" 2>/dev/null +} + +case "$mimetype" in + image/*) + image "${FILE_PATH}" + ;; + *) + kitty +kitten icat \ + --transfer-mode=file \ + --clear 2>/dev/null + ;; +esac diff --git a/terminal/joshuto/package/default.nix b/terminal/joshuto/package/default.nix new file mode 100644 index 00000000..6ef0e34b --- /dev/null +++ b/terminal/joshuto/package/default.nix @@ -0,0 +1,22 @@ +{ lib, rustPlatform, fetchFromGitHub }: + +rustPlatform.buildRustPackage rec { + pname = "joshuto"; + version = "493af3185092036cbbae81ae620b101f66cf4e9a"; + + src = fetchFromGitHub { + owner = "kamiyaa"; + repo = pname; + rev = version; + sha256 = "sha256-jLlDMV03eFWDB1D6pFEq2MFAfoVwFTy8ZpweS9syDB0="; + }; + + cargoSha256 = "sha256-dffKMgXhm4VpDSEzFW5d4oGCBKY/ppj1gx29Iw3Msc8="; + + meta = with lib; { + description = "Ranger-like terminal file manager written in Rust"; + homepage = "https://github.com/kamiyaa/joshuto"; + license = licenses.lgpl3Only; + maintainers = with maintainers; [ figsoda totoroot ]; + }; +} diff --git a/terminal/joshuto/preview.sh b/terminal/joshuto/preview.sh new file mode 100755 index 00000000..0c1d308b --- /dev/null +++ b/terminal/joshuto/preview.sh @@ -0,0 +1,89 @@ +#!/usr/bin/env bash + +IFS=$'\n' +set -o noclobber -o noglob -o nounset -o pipefail + +FILE_PATH="" +PREVIEW_WIDTH=10 +PREVIEW_HEIGHT=10 + +while [ "$#" -gt 0 ]; do + case "$1" in + "--path") + shift + FILE_PATH="$1" + ;; + "--preview-width") + shift + PREVIEW_WIDTH="$1" + ;; + "--preview-height") + shift + PREVIEW_HEIGHT="$1" + ;; + esac + shift +done + +realpath=$(realpath "$FILE_PATH") + +handle_extension() { + case "${FILE_EXTENSION_LOWER}" in + rar) + unrar lt -p- -- "${FILE_PATH}" && exit 0 + exit 1;; + 7z) + 7z l -p -- "${FILE_PATH}" && exit 0 + exit 1;; + + pdf) + pdftotext -l 10 -nopgbrk -q -- "${FILE_PATH}" - | \ + fmt -w "${PREVIEW_WIDTH}" && exit 0 + mutool draw -F txt -i -- "${FILE_PATH}" 1-10 | \ + fmt -w "${PREVIEW_WIDTH}" && exit 0 + exiftool "${FILE_PATH}" && exit 0 + exit 1;; + + torrent) + transmission-show -- "${FILE_PATH}" && exit 0 + exit 1;; + + json) + jq --color-output . "${FILE_PATH}" && exit 0 + ;; + esac +} + +handle_mime() { + local mimetype="${1}" + + case "${mimetype}" in + ## Text + text/* | */xml) + bat --color=always --paging=never \ + --style=plain \ + --terminal-width="${PREVIEW_WIDTH}" \ + "${FILE_PATH}" && exit 0 + cat "${FILE_PATH}" && exit 0 + exit 1;; + + ## Image + image/*) + exit 5;; + + ## Video and audio + video/* | audio/*) + echo "$realpath" + mediainfo "${FILE_PATH}" && exit 0 + exiftool "${FILE_PATH}" && exit 0 + exit 1;; + esac +} + +FILE_EXTENSION="${FILE_PATH##*.}" +FILE_EXTENSION_LOWER="$(printf "%s" "${FILE_EXTENSION}" | tr '[:upper:]' '[:lower:]')" +handle_extension +MIMETYPE="$( file --dereference --brief --mime-type -- "${FILE_PATH}" )" +handle_mime "${MIMETYPE}" + +exit 1 diff --git a/terminal/ranger/default.nix b/terminal/ranger/default.nix deleted file mode 100644 index d46393d0..00000000 --- a/terminal/ranger/default.nix +++ /dev/null @@ -1,28 +0,0 @@ -{ pkgs, ... }: - -{ - environment.systemPackages = [ pkgs.ranger ]; - - home-manager.sharedModules = [{ - xdg.configFile."ranger/rc.conf".text = '' - set line_numbers absolute - set padding_right false - set vcs_aware true - set show_hidden true - set confirm_on_delete always - set save_console_history false - set mouse_enabled false - set tilde_in_titlebar true - - alias r rename - alias d delete - - map DD shell trash %s - - set use_preview_script true - set preview_files true - set preview_images true - set preview_images_method kitty - ''; - }]; -}