aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/sh/.local/bin
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2022-10-18 18:40:04 -0500
committerToby Vincent <tobyv13@gmail.com>2022-10-18 18:40:04 -0500
commita69bbedfd1f3e5c0eb53aed980de65e4ec6b4d70 (patch)
tree65b93424c4053f0defc2f26e3cd2806d97e29dbf /sh/.local/bin
parent5bdf3c640f390305fef7f771ea7f05db85482ff6 (diff)
feat(sh): add lessfilter script for previewing projects
Diffstat (limited to 'sh/.local/bin')
-rwxr-xr-xsh/.local/bin/lessfilter237
1 files changed, 237 insertions, 0 deletions
diff --git a/sh/.local/bin/lessfilter b/sh/.local/bin/lessfilter
new file mode 100755
index 0000000..dcd4d01
--- /dev/null
+++ b/sh/.local/bin/lessfilter
@@ -0,0 +1,237 @@
+#!/bin/sh
+
+SCRIPT="$(basename "$0")"
+
+long='width,verbose,help'
+short='wvh'
+
+if ! opts="$(getopt -o $short -l $long -n "$SCRIPT" -- "$@")"; then
+ exit 1
+fi
+
+eval set -- "$opts"
+
+help() {
+ cat <<-EOF
+ $SCRIPT
+ Toby Vincent <tobyv13@gmail.com>
+
+ $SCRIPT
+ Filter script used for lesspipe.sh filtering
+
+ USAGE:
+ $SCRIPT [OPTION ...] <PATH> [PATH ...]
+
+ OPTIONS:
+ -w, --width Specify width. By default uses FZF_PREVIEW_COLUMNS if
+ set, and falls back to COLUMNS
+ -v, --verbose Increase verbosity
+ -h, --help Show this help
+ EOF
+}
+
+say() {
+ printf "%s: %s\n" "$SCRIPT" "$@"
+}
+
+say_verbose() {
+ if [ "$verbose" -gt "0" ]; then
+ say "$@"
+ fi
+}
+
+say_err() {
+ say "$@" >&2
+}
+
+err() {
+ err_dir="$1"
+ shift
+ say_err "cannot preview '$err_dir': $*"
+ exit 1
+}
+
+err_help() {
+ help
+ err "$*"
+}
+
+has() {
+ command -v "$1" >/dev/null
+}
+
+is_git_repo() {
+ [ -d "$1/.git" ] || git -C "$1" rev-parse --is-inside-work-tree >/dev/null 2>&1
+}
+
+mime() {
+ if [ -e "$1" ]; then
+ mtype=$(file -L -s -b --mime "$1" 2>/dev/null)
+ fcat="${mtype%/*}"
+ mtype="${mtype#*/}"
+ mtype="${mtype%;*}"
+ mtype="${mtype#x-}"
+ mtype="${mtype#vnd\.}"
+
+ if [ "$fcat" = "application" ] &&
+ [ "$ftype" = "octet-stream" ] ||
+ [ "$fcat" = "text" ] &&
+ [ "$ftype" = "plain" ]; then
+ mtype=$(file -L -s -b "$1" 2>/dev/null)
+ fi
+ else
+ case "$1" in
+ http://*)
+ mtype='x-scheme-handler/http'
+ ;;
+ https://*)
+ mtype='x-scheme-handler/https'
+ ;;
+ *)
+ mtype='repo'
+ ;;
+ esac
+ fi
+
+ printf %s\\n "$mtype"
+}
+
+fileext() {
+ case "$1" in
+ .*.*) extension=${1##*.} ;;
+ .*) extension= ;;
+ *.*) extension=${1##*.} ;;
+ esac
+
+ printf %s\\n "$extension"
+}
+
+filetype() {
+ fname="$1"
+ fext=$(fileext "$fname")
+ mtype=$(mime "$fname")
+
+ ft="${mtype#*/}"
+ ft="${ft%;*}"
+ ft="${ft#x-}"
+ ftype="${ft#vnd\.}"
+
+ case "$fext" in
+ epub) [ "$ftype" = "zip" ] && ftype='epub' ;;
+ ipynb) [ "$ftype" = "json" ] && ftype='ipynb' ;;
+ mp3) [ "$ftype" = "mpeg" ] && ftype='mp3' ;;
+ crt | pem) ftype='x509' ;;
+ crl) ftype='crl' ;;
+ csr) ftype='csr' ;;
+ pod) ftype='pod' ;;
+ pm) ftype='perl' ;;
+ md | MD | mkd | markdown | rst) ftype='markdown' ;;
+ log) ftype='log' ;;
+ ebuild | eclass) ftype='sh' ;;
+ esac
+
+ case "$ftype" in
+ openxmlformats-officedocument.wordprocessingml.document) ftype='docx' ;;
+ openxmlformats-officedocument.presentationml.presentation) ftype='pptx' ;;
+ openxmlformats-officedocument.spreadsheetml.sheet) ftype='xlsx' ;;
+ oasis.opendocument.text*) ftype='odt' ;;
+ oasis.opendocument.spreadsheet) ftype='ods' ;;
+ oasis.opendocument.presentation) ftype='odp' ;;
+ sun.xml.writer) ftype='ooffice1' ;;
+ shellscript) ftype='sh' ;;
+ makefile) ftype='make' ;;
+ epub+zip) ftype='epub' ;;
+ matlab-data) ftype='matlab' ;;
+ troff) case "${fname##*/}" in
+ [Mm]akefile | [Mm]akefile.* | BSDMakefile) ftype='make' ;;
+ esac ;;
+ *mat-file*) ftype='matlab' ;;
+ *POD\ document*) ftype='pod' ;;
+ *PEM\ certificate\ request) ftype='csr' ;;
+ *PEM\ certificate) ftype='csr' ;;
+ *Microsoft\ OOXML) ftype='docx' ;;
+ Apple\ binary\ property\ list) ftype='plist' ;;
+ PGP\ *ncrypted* | GPG\ encrypted*) ftype='pgp' ;;
+ Audio\ file\ with\ ID3\ *) ftype='mp3' ;;
+ 'OpenOffice.org 1.x Writer document') ftype='ooffice1' ;;
+ # if still unspecific, determine file type by extension
+ data)
+ ### binary only file formats, type not guessed by 'file'
+ case "$fext" in
+ mat) ftype='matlab' ;;
+ br | bro | tbr) ftype='brotli' ;;
+ lz4 | lt4 | tz4 | tlz4) ftype='lz4' ;;
+ esac
+ ;;
+ esac
+
+ printf %s\\n "$ftype"
+}
+
+verbose=0
+width=${FZF_PREVIEW_COLUMNS:-$COLUMNS}
+while true; do
+ case "$1" in
+ -h | --help)
+ help
+ exit 0
+ ;;
+ -v | --verbose)
+ verbose=$((verbose + 1))
+ shift
+ ;;
+ -w | --width)
+ width=$2
+ shift 2
+ ;;
+ --)
+ shift
+ break
+ ;;
+ *)
+ err_help "Invalid argument: $1"
+ ;;
+ esac
+done
+
+if [ "$#" -eq 0 ]; then
+ IFS='
+'
+ set -o noglob
+ # shellcheck disable=2046
+ set -- $(cat)
+fi
+
+if [ -z "$width" ]; then
+ width=$(tput cols)
+fi
+
+ft=$(filetype "$1")
+case "$ft" in
+directory)
+ if has onefetch && is_git_repo "$1" 2>/dev/null; then
+ onefetch --hidden --show-logo="$([ "$width" -lt "80" ] && printf 'never' || printf 'always')" "$1" 2>/dev/null
+ elif [ -f "$1/README.md" ]; then
+ $SCRIPT "$1/README.md"
+ else
+ exa --tree --git-ignore --level=3 --icons "$1" 2>/dev/null
+ fi
+ ;;
+markdown)
+ bat --color always "$1" 2>/dev/null
+ ;;
+http | https)
+ if printf %s\\n "$1" | grep -q "git.sr.ht"; then
+ has hut && hut git show --repo "$1" 2>/dev/null
+ elif printf %s\\n "$1" | grep -q "github.com"; then
+ has gh && gh repo view "$1" 2>/dev/null
+ fi
+ ;;
+repo)
+ hut git show --repo "https://git.sr.ht/$1" 2>/dev/null ||
+ gh repo view "https://github.com/$1" 2>/dev/null
+ ;;
+*)
+ exit 1
+ ;;
+esac