diff options
author | Tom Ryder <tom@sanctum.geek.nz> | 2015-11-13 13:19:28 +1300 |
---|---|---|
committer | Tom Ryder <tom@sanctum.geek.nz> | 2015-11-13 13:19:28 +1300 |
commit | 385f5e0c9a4a1c8f3bea49669118d599229bbf15 (patch) | |
tree | 350336390957c5a2369c243878c33ca0a6fe4edb /bin | |
parent | Update plugin (diff) | |
download | dotfiles-385f5e0c9a4a1c8f3bea49669118d599229bbf15.tar.gz dotfiles-385f5e0c9a4a1c8f3bea49669118d599229bbf15.zip |
Add vis(1) script editor
Diffstat (limited to 'bin')
-rwxr-xr-x | bin/vis | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/bin/vis b/bin/vis new file mode 100755 index 00000000..8b0ddd57 --- /dev/null +++ b/bin/vis @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +# +# vis(1) -- Create and edit executable scripts in a directory VISPATH (defaults +# to ~/.local/bin). +# +# Author: Tom Ryder +# Copyright: 2015 +# License: Public domain +# + +# Name self +self=vis + +# Define a function to show usage +usage() { + printf '%s: usage: %s [EDITOR_OPTS] [--] FILE1 [FILE2...]\n' \ + "$self" "$self" +} + +# Process options, including detecting requests for help +declare -a opts +for arg ; do + case $arg in + --help|-h|-\?) + usage + exit 0 + ;; + --) + shift + break + ;; + -*) + shift + opts=("${opts[@]}" "$arg") + ;; + *) + break + ;; + esac +done + +# If no arguments left, spit usage as an error and bail +if ! (($#)) ; then + usage >&2 + exit 1 +fi + +# Create the script directory if it doesn't exist yet +vispath=${VISPATH:-$HOME/.local/bin} +if [[ ! -d $vispath ]] ; then + mkdir -p -- "$vispath" || exit +fi + +# Create a new array with the script directory prepended to the given names +declare -a files +files=("${@/#/$vispath/}") + +# Collect the names of any scripts that don't exist yet so we can make them +# executable after we're done editing +declare -a creations +for file in "${files[@]}" ; do + [[ -e $file ]] && continue + creations=("${creations[@]}" "$file") +done + +# Run the editor; if EDITOR isn't set, use vi(1) +"${EDITOR:-vi}" "${opts[@]}" -- "${files[@]}" + +# Make any created scripts executable if they now appear to be files +for creation in "${creations[@]}" ; do + [[ -f $creation ]] || continue + chmod +x -- "$creation" +done + |