From 16dec73a9d5c36138354e15e5fac12509905fdb2 Mon Sep 17 00:00:00 2001 From: Eric Torres Date: Thu, 3 Jan 2019 13:29:47 -0800 Subject: [PATCH] Add fless and fedit scripts along with zsh plugins --- fedit.sh | 132 +++++++++++++++++++++++++++++++++++++++++++++++++ fless.sh | 100 +++++++++++++++++++++++++++++++++++++ zsh/_fedit.zsh | 11 +++++ zsh/_fless.zsh | 0 4 files changed, 243 insertions(+) create mode 100755 fedit.sh create mode 100755 fless.sh create mode 100644 zsh/_fedit.zsh create mode 100644 zsh/_fless.zsh diff --git a/fedit.sh b/fedit.sh new file mode 100755 index 0000000..fb0bc03 --- /dev/null +++ b/fedit.sh @@ -0,0 +1,132 @@ +#!/usr/bin/bash +# fedit - fuzzy find a file and edit it +# Dependencies +# - fd +# - fzf + +help() { +cat << EOF +Usage: fedit [-h|--help] [-b|--boot] [-d|--dir directory] [-e|--etc] [-E|--editor editor] +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 + -E, --editor use a given editor (default: ${EDITOR:-none}) + -h, --help print this help page +EOF +} + +[[ ! -f /usr/bin/fzf ]] && exit 1 + +# Error messages +readonly directory_error="Error, enter a directory" +readonly noeditor_error="Error, no editor entered" + +# Pre-run correctness checks +unset find_opts +file= +dir= +editor= + +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 + ;; + '-E'|'--editor') + editor="${2}" + case "${2}" in + "") + printf '%s\n' "${noeditor_error}" >&2 + exit 1 + ;; + -*) + printf '%s\n' "Not an editor: ${editor}" >&2 + exit 1 + ;; + esac + shift 2 + continue + ;; + --editor=*) + editor="${1#*=}" + [[ -z "${editor}" ]] && printf '%s\n' "${noeditor_error}" >&2 && exit 1 + shift + continue + ;; + '-h'|'--help') + help + exit + ;; + --) + shift + break + ;; + -*) + printf '%s\n' "Unknown option: ${1}" + exit 1 + ;; + *) + break + ;; + esac +done + +declare -a find_opts +if [[ -x '/usr/bin/fd' ]]; then + find_bin='/usr/bin/fd' + find_opts+=('--hidden') + find_opts+=('--print0') + find_opts+=('--type' 'f') + find_opts+=('--no-ignore-vcs') + [[ -n "${dir}" ]] && find_opts+=('.' -- "${dir}") +else + find_bin='/usr/bin/find' + [[ -n "${dir}" ]] && find_opts+=("${dir}") || find_opts+=('.') + find_opts+=('-mindepth' '0') + find_opts+=('-type' 'f') + find_opts+=('-print0') +fi + +if [[ -z "${editor:-${EDITOR}}" ]]; then + printf '%s\n' "No editor found" >&2 + exit 1 +fi + +file="$("${find_bin}" "${find_opts[@]}" 2> /dev/null | fzf --read0 --select-1 --exit-0)" + +[[ ! "${file}" ]] && exit 1 + +if [[ -w "${file}" ]]; then + "${editor:-${EDITOR}}" -- "${file}" +else + sudo --edit -- "${file}" +fi diff --git a/fless.sh b/fless.sh new file mode 100755 index 0000000..d26b9c1 --- /dev/null +++ b/fless.sh @@ -0,0 +1,100 @@ +#!/usr/bin/bash +# fless - fuzzy find a file and run less on it +# Dependencies +# - fd (soft) +# - fzf + +_help() { +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 find_opts +ans= +file= +dir= + +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 + +declare -a find_opts +template_dir="${HOME}/Templates" + +if [[ -x '/usr/bin/fd' ]]; then + find_bin='/usr/bin/fd' + find_opts+=('--print0') + find_opts+=('--type' 'f') +else + find_bin='/usr/bin/find' + find_opts+=('-mindepth' '0') + find_opts+=('-type' 'f') + find_opts+=('-print0') +fi + +if [[ "${dir}" ]]; then + file="$("${find_bin}" "${find_opts[@]}" . -- "${dir}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +else + file="$("${find_bin}" "${find_opts[@]}" | fzf --read0 --select-1 --exit-0 --no-mouse)" +fi + +[[ ! "${file}" ]] && exit 1 + +"${PAGER:-/usr/bin/less}" -- "${file}" diff --git a/zsh/_fedit.zsh b/zsh/_fedit.zsh new file mode 100644 index 0000000..ce954cf --- /dev/null +++ b/zsh/_fedit.zsh @@ -0,0 +1,11 @@ +#!/usr/bin/bash + +_etcedit() { + fedit -e +} + +zle -N fedit +bindkey -M viins '^o' fedit + +zle -N _etcedit +bindkey -M viins '^e' _etcedit diff --git a/zsh/_fless.zsh b/zsh/_fless.zsh new file mode 100644 index 0000000..e69de29