2021-02-22 12:23:32 +01:00
|
|
|
#!/bin/env bash
|
|
|
|
|
2021-03-28 14:33:07 +02:00
|
|
|
# shellcheck disable=SC2034
|
2021-02-22 12:23:32 +01:00
|
|
|
VSCODE_PATH=$(command -v code)
|
2021-02-23 05:20:45 +01:00
|
|
|
NANO_PATH=$(command -v nano)
|
|
|
|
VI_PATH=$(command -v vi)
|
2021-03-14 12:23:03 +01:00
|
|
|
OPEN_EDITOR_LOCKFILE=$HOME/.dotfiles/config/open-editor
|
2021-02-22 12:23:32 +01:00
|
|
|
|
2021-03-14 12:23:03 +01:00
|
|
|
if [[ $1 == "" ]]; then
|
2021-03-28 14:33:07 +02:00
|
|
|
echo "open-editor: Want to learn more how to use me? Use the '--help' flag to see the docs."
|
2021-03-14 12:23:03 +01:00
|
|
|
exit
|
2021-02-23 05:20:45 +01:00
|
|
|
fi
|
2021-03-14 12:23:03 +01:00
|
|
|
|
2021-03-28 14:33:07 +02:00
|
|
|
# Stack Overflow: https://stackoverflow.com/questions/7069682/how-to-get-arguments-with-flags-in-bash#21128172
|
2021-03-14 12:23:03 +01:00
|
|
|
while test $# -gt 0; do
|
|
|
|
case "$1" in
|
|
|
|
-h|--help)
|
|
|
|
echo "$0 - shortcut to open editors within and from command line"
|
|
|
|
echo "By default, the script will attempt to guess what text editor to use as much"
|
2021-03-28 14:33:07 +02:00
|
|
|
echo "as possible. You can lock with the ~/.dotfiles/config/open-editor file."
|
2021-03-14 12:23:03 +01:00
|
|
|
echo
|
|
|
|
echo "$0 [options] filename"
|
|
|
|
echo " "
|
|
|
|
echo "options:"
|
|
|
|
echo "-h, --help show brief help"
|
|
|
|
echo "-c, --use-code use Visual Studio Code, with the 'wait' flag"
|
|
|
|
echo "-n, --use-nano use GNU Nano"
|
|
|
|
echo "--lockfile Generate a lockfile within your home directory"
|
|
|
|
echo " or edit if found."
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
-c|--use-code)
|
|
|
|
shift
|
2021-03-28 14:33:07 +02:00
|
|
|
echo "open-editor: Firing up your editor, please wait..."
|
|
|
|
sleep 3
|
2021-03-14 12:23:03 +01:00
|
|
|
if test $# -gt 0; then
|
2021-03-28 14:33:07 +02:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
code --wait $1
|
2021-03-14 12:23:03 +01:00
|
|
|
else
|
|
|
|
echo "error: no file specified, aborting..."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
-n|--use-nano)
|
|
|
|
shift
|
2021-03-28 14:33:07 +02:00
|
|
|
echo "open-editor: Firing up your editor, please wait..."
|
|
|
|
sleep 3
|
2021-03-14 12:23:03 +01:00
|
|
|
if test $# -gt 0; then
|
2021-03-28 14:33:07 +02:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
nano $1
|
|
|
|
exit
|
2021-03-14 12:23:03 +01:00
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
|
|
|
--lockfile)
|
|
|
|
shift
|
|
|
|
if test $# -gt 0; then
|
|
|
|
export OUTPUT=$1
|
|
|
|
else
|
|
|
|
echo "no output dir specified"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
shift
|
|
|
|
;;
|
2021-03-28 14:33:07 +02:00
|
|
|
-*)
|
|
|
|
echo "open-editor: Unsupported flag, edit the script file to customize."
|
|
|
|
exit 1
|
|
|
|
;;
|
2021-03-14 12:23:03 +01:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2021-03-28 14:33:07 +02:00
|
|
|
if [[ $VSCODE_PATH != "" ]]; then
|
|
|
|
echo "open-editor: Firing up your editor, please wait..."
|
|
|
|
sleep 3
|
|
|
|
code --wait "$1"
|
|
|
|
exit
|
|
|
|
elif [[ $NANO_PATH != "" ]]; then
|
|
|
|
echo "open-editor: Firing up your editor, please wait..."
|
|
|
|
sleep 3
|
|
|
|
nano "$1"
|
|
|
|
exit
|
|
|
|
else
|
|
|
|
echo "open-editor: Firing up your editor, please wait..."
|
|
|
|
sleep 3
|
|
|
|
vi "$1"
|
|
|
|
exit
|
|
|
|
fi
|