diff --git a/.config/shell/bin/ted b/.config/shell/bin/ted index bb9314d..7235fc1 100755 --- a/.config/shell/bin/ted +++ b/.config/shell/bin/ted @@ -2,6 +2,8 @@ # "test edit" - makes a temporary file and edits it # requires: bash (uses arrays) +# shellcheck disable=SC2016 + set -eu pname="${0##*/}" @@ -10,26 +12,37 @@ ed() { ${EDITOR:-nvim} "$@" } +backup_git() { + cd "${1:-}" + if command -v git > /dev/null; then + # Commit all existing files + git commit -am "ted-backup: update $dirname" + # Add new files and commit them + git add "$1" + git commit -m "ted-backup: add $dirname" + fi +} + usage() { echo '"Test edit" - edit a temporary file for testing code fragments' echo "" echo "Usage: $pname [OPTIONS] [file] [ed-opts] [--] [cmd]..." - echo "" - echo "Arguments:" - echo " file A file to edit. Also acts as the name for the directory." - echo " ed-opts Any option to pass to the editor. (eg. another file name)" - echo " cmd... A list of commands to EVALuate after the editor exits. (example: bash)" + echo '' + echo 'Arguments:' + echo ' file A file to edit. Also acts as the name for the directory.' + echo ' ed-opts Any option to pass to the editor. (eg. another file name)' + echo ' cmd... A list of commands to EVALuate after the editor exits. Defaults to `bash`' echo ' Exposed variables:' echo ' $filename - the name of the file specified for `file`' echo ' $tempdir - the directory the editor was launched in.' echo ' $ed_args - array of arguments passed to the editor.' echo ' $cmd_args - the array of commands specified in the `cmd...` option' - echo "" - echo "Options:" - echo " -h, --help Print usage information and exit. This only gets read in the first argument position." + echo '' + echo 'Options:' + echo ' -h, --help Print usage information and exit. This only gets read in the first argument position.' echo "" echo "Environment:" - echo " EDITOR The editor you want to use." + echo " EDITOR The editor you want to use. (Currently $EDITOR)" echo "" echo "Examples:" echo " Edit hello.sh and then run bash (so you can run it)" @@ -53,7 +66,8 @@ dirname="${filename%%.*}" tests_dir="${XDG_DOCUMENTS_DIR:-"$HOME"/Documents}/code-tests" date="$(date +%F)" -tempdir="$tests_dir/$date-$dirname" +tempdir_arr=("$tests_dir/"*"$dirname") +tempdir="${tempdir_arr[0]:-"$tests_dir/$date-$dirname"}" mkdir -p "$tempdir" # build an argument list for the editor @@ -79,10 +93,15 @@ if [ "${#cmds}" != 0 ]; then for cmd in "${cmds[@]}"; do (eval "$cmd") done +else + # default to bash + "$SHELL" fi # check if directory is empty. If it is, remove it. filecount="$(find . -maxdepth 1 -print | wc -l)" if [ "$filecount" = 0 ]; then rmdir "$tempdir" +else + backup_git . fi