pub-solar-os/modules/terminal-life/nvim/preview-file.nix

37 lines
747 B
Nix
Raw Normal View History

2022-11-20 22:28:23 +00:00
self:
with self; ''
IFS=':' read -r -a INPUT <<< "$1"
FILE=''${INPUT[0]}
CENTER=''${INPUT[1]}
2022-11-20 22:28:23 +00:00
if [[ "$1" =~ ^[A-Za-z]:\\ ]]; then
FILE=$FILE:''${INPUT[1]}
CENTER=''${INPUT[2]}
fi
2022-11-20 22:28:23 +00:00
if [[ -n "$CENTER" && ! "$CENTER" =~ ^[0-9] ]]; then
exit 1
fi
CENTER=''${CENTER/[^0-9]*/}
2022-11-20 22:28:23 +00:00
FILE="''${FILE/#\~\//$HOME/}"
if [ ! -r "$FILE" ]; then
echo "File not found ''${FILE}"
exit 1
fi
2022-11-20 22:28:23 +00:00
if [ -z "$CENTER" ]; then
CENTER=0
fi
2022-11-20 22:28:23 +00:00
exec cat "$FILE" \
| sed -e '/[#|\/\/ ?]-- copyright/,/[#\/\/]++/c\\' \
| ${pkgs.coreutils}/bin/tr -s '\n' \
| ${pkgs.bat}/bin/bat \
--style="''${BAT_STYLE:-numbers}" \
--color=always \
--pager=never \
--file-name=''$FILE \
--highlight-line=$CENTER
''