From b90198d38be39ecd7bd1be8fe9ac59c8d3e481b3 Mon Sep 17 00:00:00 2001 From: Kenichi Kamiya Date: Tue, 17 Sep 2024 04:31:52 +0900 Subject: [PATCH] Make better preview displays in terminals (#758) * Implement markdown renderer for terminal * Implement preview command for file|mime types * Introduce preview command into walk command * Drop to support KDL for now * Replace renmark with mdcat in preview command to support darwin * I might add poppler in another PR for yazi PDF preview --- flake.nix | 2 ++ home-manager/packages.nix | 4 ++++ pkgs/default.nix | 2 ++ pkgs/preview/default.nix | 25 +++++++++++++++++++++++++ pkgs/preview/preview.bash | 33 +++++++++++++++++++++++++++++++++ pkgs/renmark/default.nix | 14 ++++++++++++++ pkgs/renmark/renmark.bash | 1 + pkgs/walk/default.nix | 11 ++++++----- pkgs/walk/walk.bash | 2 +- 9 files changed, 88 insertions(+), 6 deletions(-) create mode 100644 pkgs/preview/default.nix create mode 100644 pkgs/preview/preview.bash create mode 100644 pkgs/renmark/default.nix create mode 100644 pkgs/renmark/renmark.bash diff --git a/flake.nix b/flake.nix index 1c3b0ff8..e48e764e 100644 --- a/flake.nix +++ b/flake.nix @@ -132,6 +132,8 @@ "nix-hash-url" "trim-github-user-prefix-for-reponame" "gredit" + "renmark" + "preview" ] ) ++ [ diff --git a/home-manager/packages.nix b/home-manager/packages.nix index 4f9b0273..af3a6f3e 100644 --- a/home-manager/packages.nix +++ b/home-manager/packages.nix @@ -61,6 +61,7 @@ with pkgs; edge-pkgs.jnv # interactive jq - Use unstable because it is a fresh tool ripgrep # `rg` bat # alt cat + mdcat # pipe friendly markdown viewer rather than glow hexyl # hex viewer dysk # alt df fd # alt find @@ -129,6 +130,7 @@ with pkgs; archive-home-files prs gredit + preview ]) ++ (lib.optionals stdenv.isLinux [ # Fix missing locales as `locale: Cannot set LC_CTYPE to default locale` @@ -146,6 +148,8 @@ with pkgs; # Do not install in dawin yet: https://github.com/NixOS/nixpkgs/blob/b4b293ec6c61e846d69224ea0637411283e2ad39/pkgs/by-name/ch/chawan/package.nix#L82 # Keybindigs: https://git.sr.ht/~bptato/chawan/tree/master/item/res/config.toml chawan # `cha` + + homemade-pkgs.renmark # Depend on chawan ]) ++ (lib.optionals stdenv.isDarwin [ # https://github.com/NixOS/nixpkgs/issues/240819 diff --git a/pkgs/default.nix b/pkgs/default.nix index da2516e8..89aea780 100644 --- a/pkgs/default.nix +++ b/pkgs/default.nix @@ -70,4 +70,6 @@ gredit = pkgs.callPackage ./gredit { }; git-resolve-conflict = pkgs.callPackage ./git-resolve-conflict { }; + renmark = pkgs.callPackage ./renmark { }; + preview = pkgs.callPackage ./preview { }; } diff --git a/pkgs/preview/default.nix b/pkgs/preview/default.nix new file mode 100644 index 00000000..64548723 --- /dev/null +++ b/pkgs/preview/default.nix @@ -0,0 +1,25 @@ +{ pkgs, ... }: +pkgs.writeShellApplication rec { + name = "preview"; + text = builtins.readFile ./${name}.bash; + # TODO: Support KDL highlight + # - bat does not support KDL, however TUI editors with pipe is not to be easy handled + # - Helix does not have readonly mode and the KDL highlighting is not correct + # https://github.com/helix-editor/helix/discussions/9245 + # - micro https://github.com/kachick/micro-kdl + # - vim https://github.com/imsnif/kdl.vim + runtimeInputs = + (with pkgs; [ + file # Detect file/mime type + coreutils # For `basename` + bat # code + hexyl # binary + # libsixel + mdcat # markdown - Avoid renmark to keep compatibility in darwin + ]) + ++ [ + (import ../la { inherit pkgs; }) # directory + ]; + # Especially provided for fzf: https://github.com/junegunn/fzf/issues/2855#issuecomment-1164015794 + meta.description = "Run preview commands that are suitable for the file type"; +} diff --git a/pkgs/preview/preview.bash b/pkgs/preview/preview.bash new file mode 100644 index 00000000..79477ff4 --- /dev/null +++ b/pkgs/preview/preview.bash @@ -0,0 +1,33 @@ +path="$1" +filename=$(basename -- "$path") +extension="${filename##*.}" + +case "$(file --dereference --brief --mime-type "$path")" in +text/html) + cha "$path" + ;; +text/*) + case "$extension" in + md | markdown) + mdcat "$path" + ;; + *) + bat --color=always "$path" + ;; + esac + ;; +inode/directory) + la "$path" + ;; +application/x-executable) + hexyl "$path" + ;; +# image/*) +# # TODO: Support images/PDF after using sixel supported terminals for main. Alacritty isn't +# # https://github.com/alacritty/alacritty/issues/910 +# img2sixel "$path" +# ;; +*) + bat --color=always "$path" + ;; +esac diff --git a/pkgs/renmark/default.nix b/pkgs/renmark/default.nix new file mode 100644 index 00000000..3ec29b71 --- /dev/null +++ b/pkgs/renmark/default.nix @@ -0,0 +1,14 @@ +{ pkgs, ... }: +pkgs.writeShellApplication rec { + name = "renmark"; + text = builtins.readFile ./${name}.bash; + # Old candidates + # - glow, pandoc, inlyne, chawan built-in markdown renderer, mdcat, gh-markdown-preview, Lynx, w3m + # + # After several candidates, I think this combination is the best for now. + runtimeInputs = with pkgs; [ + chawan + comrak + ]; + meta.description = "RENder MARkdown in terminal. See GH-740"; +} diff --git a/pkgs/renmark/renmark.bash b/pkgs/renmark/renmark.bash new file mode 100644 index 00000000..5c47e0da --- /dev/null +++ b/pkgs/renmark/renmark.bash @@ -0,0 +1 @@ +comrak "$@" | cha --type 'text/html' diff --git a/pkgs/walk/default.nix b/pkgs/walk/default.nix index 8b71ade8..0a25bbdf 100644 --- a/pkgs/walk/default.nix +++ b/pkgs/walk/default.nix @@ -2,9 +2,10 @@ pkgs.writeShellApplication rec { name = "walk"; text = builtins.readFile ./${name}.bash; - runtimeInputs = with pkgs; [ - fzf - fd - bat - ]; + runtimeInputs = + (with pkgs; [ + fzf + fd + ]) + ++ [ (import ../preview { inherit pkgs; }) ]; } diff --git a/pkgs/walk/walk.bash b/pkgs/walk/walk.bash index aed9be91..21d87e17 100644 --- a/pkgs/walk/walk.bash +++ b/pkgs/walk/walk.bash @@ -10,4 +10,4 @@ fi # shellcheck disable=SC2016 fd --type f --hidden --follow --exclude .git . "$@" | - fzf --query "$query" --preview 'bat --color=always {}' --preview-window '~3' --bind 'enter:become(command "$EDITOR" {})' + fzf --query "$query" --preview 'preview {}' --preview-window '~3' --bind 'enter:become(command "$EDITOR" {})'