-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sh): add lessfilter script for previewing projects
- Loading branch information
Showing
4 changed files
with
243 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters