Skip to content

Commit

Permalink
feat(sh): add lessfilter script for previewing projects
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyvin committed Oct 18, 2022
1 parent 5bdf3c6 commit a69bbed
Show file tree
Hide file tree
Showing 4 changed files with 243 additions and 118 deletions.
237 changes: 237 additions & 0 deletions sh/.local/bin/lessfilter
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
$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
1 change: 1 addition & 0 deletions sh/.profile
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export PASSWORD_STORE_ENABLE_EXTENSIONS=true
export ZK_NOTEBOOK_DIR="$HOME/notebook"
export STARSHIP_LOG="error"
export FZF_DEFAULT_COMMAND="fd --type f || git ls-tree -r --name-only HEAD || rg --files || find ."
export FZF_TMUX_OPTS="-p"
export FZF_DEFAULT_OPTS='--bind ctrl-q:abort
--bind ctrl-y:preview-up
--bind ctrl-e:preview-down
Expand Down
114 changes: 0 additions & 114 deletions tmux/.local/bin/previewer.sh

This file was deleted.

9 changes: 5 additions & 4 deletions tmux/.local/bin/tmux-sessionizer
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@
# shellcheck disable=2016,2089

PROJECT_DIR="$HOME/src"
DOCKER_DIR="$HOME/dkr"
SESSION_DIR="$XDG_DATA_HOME/nvim/sessions"
PREVIEW_CMD="previewer.sh {} 2>/dev/null || previewer.sh {q} --remote"
ATTACHED="$(tmux list-sessions -F '#{?session_attached,#{session_path},}' 2>/dev/null | sed '/^$/d')"
PREVIEW_CMD="([ -n {} ] && less {}) || ([ ! -e {q} ] && less {q})"
ATTACHED="$(tmux display -p '#{session_path}')"

if [ "$#" -eq 0 ]; then
# shellcheck disable=2046
set -- $({
printf %s\\n "$HOME/.dotfiles"
fd . "$PROJECT_DIR" --type=d --max-depth=1
fd . "$PROJECT_DIR" "$DOCKER_DIR" --type=d --max-depth=1
fd . "$SESSION_DIR" --type=f --max-depth=1 | sed "s|^$SESSION_DIR/||" | sed 's#%%\|__#/#g'
} | sed 's|/$||' | sed "\|^$HOME\$|d" | sed "\|^$ATTACHED\$|d" | sort -u |
timestamp.sh --git --sessions="$SESSION_DIR" --format="{}:{1}" | sort -r | cut -d':' -f2 |
fzf-tmux -p -- --multi --print-query -d/ --with-nth -1 --preview-window=right,75% --preview="$PREVIEW_CMD" |
fzf-tmux -p -- --multi --print-query -d/ --with-nth -1 --preview-window='right,75%,<30(up,30%,border-bottom)' --preview="$PREVIEW_CMD" |
tr -s '\n' ' ')
fi

Expand Down

0 comments on commit a69bbed

Please sign in to comment.