From f655fed5e9dc862cd8e788b9947c61503cc94018 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Tue, 23 Oct 2018 15:32:42 -0700 Subject: [PATCH] Added fless script --- fless.sh | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 fless.sh diff --git a/fless.sh b/fless.sh new file mode 100755 index 0000000..53b7cfc --- /dev/null +++ b/fless.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# fless - fuzzy find a file and run less on it +# Dependencies +# - fd +# - fzf + +printHelp() { +cat << EOF +Usage: fless [-h|--help] [-b|--boot] [-d|--dir directory] [-e|--etc] +Options: + -b, --boot edit a file in /boot/loader + -d, --dir edit a file in a given directory + -e, --etc edit a file in /etc + -h, --help print this help page +EOF +} + +# Error messages +readonly directory_error="Error, enter a directory" + +# Pre-run correctness checks +unset fd_opts +ans= +file= +dir= + +declare -a fd_opts +fd_opts+=('--hidden') +fd_opts+=('--type' 'f') +fd_opts+=('--print0') +fd_opts+=('--no-ignore-vcs') + +while true; do + case "${1}" in + '-b'|'--boot') + dir="/boot/loader" + shift + continue + ;; + '-d'|'--dir') + case "${2}" in + "") + printf '%s\n' "${directory_error}" >&2 + exit 1 + ;; + *) + dir="${2}" + [[ ! -d "${dir}" ]] && printf '%s\n' "Not a directory: ${dir}" >&2 && exit 1 + ;; + esac + shift 2 + continue + ;; + --dir=*) + dir="${1#*=}" + [[ -z "${dir}" ]] && printf '%s\n' "${directory_error}" >&2 && exit 1 + [[ ! -d "${dir}" ]] && printf '%s\n' "Not a directory: ${dir}" >&2 && exit 1 + shift + continue + ;; + '-e'|'--etc') + dir='/etc' + shift + continue + ;; + '-h'|'--help') + printHelp + exit + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Unknown option: ${1}" + exit 1 + ;; + *) + break + ;; + esac +done + +if [[ "${dir}" ]]; then + file="$(fd "${fd_opts[@]}" . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +else + file="$(fd "${fd_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +fi + +[[ ! "${file}" ]] && exit 1 + +"${PAGER:-/usr/bin/less}" -- "${file}"