Skip to content

Commit

Permalink
Make better preview displays in terminals (#758)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kachick authored Sep 16, 2024
1 parent 31727e1 commit b90198d
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 6 deletions.
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@
"nix-hash-url"
"trim-github-user-prefix-for-reponame"
"gredit"
"renmark"
"preview"
]
)
++ [
Expand Down
4 changes: 4 additions & 0 deletions home-manager/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkgs/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,6 @@

gredit = pkgs.callPackage ./gredit { };
git-resolve-conflict = pkgs.callPackage ./git-resolve-conflict { };
renmark = pkgs.callPackage ./renmark { };
preview = pkgs.callPackage ./preview { };
}
25 changes: 25 additions & 0 deletions pkgs/preview/default.nix
Original file line number Diff line number Diff line change
@@ -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";
}
33 changes: 33 additions & 0 deletions pkgs/preview/preview.bash
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions pkgs/renmark/default.nix
Original file line number Diff line number Diff line change
@@ -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";
}
1 change: 1 addition & 0 deletions pkgs/renmark/renmark.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
comrak "$@" | cha --type 'text/html'
11 changes: 6 additions & 5 deletions pkgs/walk/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; }) ];
}
2 changes: 1 addition & 1 deletion pkgs/walk/walk.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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" {})'

0 comments on commit b90198d

Please sign in to comment.