diff --git a/home/joshuto/default.nix b/home/joshuto/default.nix index e070cc55..8de429b8 100644 --- a/home/joshuto/default.nix +++ b/home/joshuto/default.nix @@ -434,9 +434,153 @@ }; }; - 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; + xdg.configFile = { + "joshuto/preview.sh" = { + executable = true; + text = /* bash */ '' + #!/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 + lock) + cat "''${FILE_PATH}" && exit 0 + exit 1;; + + 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 + ''; + }; + + "joshuto/kitty-show.sh" = { + executable = true; + text = /* bash */ '' + #!/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 + ''; + }; + + "joshuto/kitty-remove.sh" = { + executable = true; + text = /* bash */ '' + #!/usr/bin/env bash + + kitty +kitten icat --transfer-mode=file --clear 2>/dev/null + ''; + }; + }; xdg.configFile."joshuto/icons.toml".text = /* toml */ '' # Default fallback icons diff --git a/home/joshuto/kitty-remove.sh b/home/joshuto/kitty-remove.sh deleted file mode 100755 index bd66564d..00000000 --- a/home/joshuto/kitty-remove.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/usr/bin/env bash - -kitty +kitten icat \ - --transfer-mode=file \ - --clear 2>/dev/null diff --git a/home/joshuto/kitty-show.sh b/home/joshuto/kitty-show.sh deleted file mode 100755 index c1f6a9d1..00000000 --- a/home/joshuto/kitty-show.sh +++ /dev/null @@ -1,32 +0,0 @@ -#!/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/home/joshuto/preview.sh b/home/joshuto/preview.sh deleted file mode 100755 index f85e064e..00000000 --- a/home/joshuto/preview.sh +++ /dev/null @@ -1,93 +0,0 @@ -#!/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 - lock) - cat "${FILE_PATH}" && exit 0 - exit 1;; - - 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