aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.config/shell/env2
-rw-r--r--.gitignore4
-rwxr-xr-x.local/bin/new74
-rw-r--r--.local/templates/sh46
4 files changed, 125 insertions, 1 deletions
diff --git a/.config/shell/env b/.config/shell/env
index 99eb1ff..61b91b9 100644
--- a/.config/shell/env
+++ b/.config/shell/env
@@ -30,7 +30,7 @@ export XDG_CONFIG_HOME="${HOME}/.config"
export XDG_DATA_DIRS="/usr/local/share/:/usr/share/"
export XDG_CONFIG_DIRS="/etc/xdg/"
export XDG_CACHE_HOME="${HOME}/.cache"
-export XDG_TEMPLATES_DIR="${HOME}/templates"
+export XDG_TEMPLATES_DIR="${HOME}/.local/templates"
#export XDG_RUNTIME_DIR=""
# export gpg-agent
diff --git a/.gitignore b/.gitignore
index f54d0fc..fe7c3e1 100644
--- a/.gitignore
+++ b/.gitignore
@@ -26,6 +26,9 @@
!/.local/bin
/.local/bin/*
+# template files
+!/.local/templates
+
# gittab
!/.local/bin/gittab
!/.local/etc
@@ -40,6 +43,7 @@
!/.local/bin/lpass
!/.local/bin/mkbak
!/.local/bin/mkpasswd
+!/.local/bin/new
!/.local/bin/ta
!/.local/bin/uncolor
!/.local/bin/up
diff --git a/.local/bin/new b/.local/bin/new
new file mode 100755
index 0000000..2de7ecd
--- /dev/null
+++ b/.local/bin/new
@@ -0,0 +1,74 @@
+#! /usr/bin/env sh
+
+main()
+{
+ # Handle opts
+ opts "$@"
+ shift "$OPTS"
+ unset OPTS
+
+ # Show help
+ [ "$OPT_HELP_ONLY" ] && usage && exit 0
+
+ if [ -d "$1" ]
+ then
+ printf "A directory already exists at %s\n" "$1" >&2
+ exit 1
+ fi
+
+ if [ -f "$1" ]
+ then
+ printf "A file already exists at %s\n" "$1" >&2
+ exit 1
+ fi
+
+ if [ -z "$TYPE" ]
+ then
+ # TODO: Calculate type from extension
+ TYPE=
+ fi
+
+ if [ ! -f "$XDG_TEMPLATES_DIR/$TYPE" ]
+ then
+ printf "No template for %s\n" "$TYPE" >&2
+ touch "$1"
+ return 0
+ fi
+
+ cp -- "$XDG_TEMPLATES_DIR/$TYPE" "$1"
+}
+
+opts()
+{
+ OPTS=0
+
+ while getopts ":ht:" opt
+ do
+ case "$opt" in
+ h) OPT_HELP_ONLY=1 OPTS=$((OPTS + 1)) ;;
+ t) TYPE=$OPTARG OPTS=$((OPTS + 2)) ;;
+ *)
+ printf "Invalid option passed: %s\n" "$OPTARG" >&2
+ ;;
+ esac
+ done
+
+ unset opt
+}
+
+usage()
+{
+ cat <<EOF
+Usage:
+ $(basename "$0") -h
+ $(basename "$0") [-t TYPE] <LOCATION>
+
+Create a new file from a template, found in the \$XDG_TEMPLATES_DIR directory.
+
+Options:
+ -h Show this help text and exit.
+ -t Specify the template type to use.
+EOF
+}
+
+main "$@"
diff --git a/.local/templates/sh b/.local/templates/sh
new file mode 100644
index 0000000..ff7bc6d
--- /dev/null
+++ b/.local/templates/sh
@@ -0,0 +1,46 @@
+#! /usr/bin/env sh
+
+main()
+{
+ # Handle opts
+ opts "$@"
+ shift "$OPTS"
+ unset "$OPTS"
+
+ # Show help
+ [ "$OPT_HELP_ONLY" ] && usage && exit 0
+
+ # TODO: Write actual program
+}
+
+opts()
+{
+ OPTS=0
+
+ while getopts ":h" opt
+ do
+ case "$opt" in
+ h) OPT_HELP_ONLY=1
+ *)
+ printf "Invalid option passed: %s\n" "$OPTARG" >&2
+ ;;
+ esac
+ done
+
+ unset opt
+}
+
+usage()
+{
+ cat <<EOF
+Usage:
+ $(basename "$0") -h
+
+Nondescript
+
+Options:
+ -h Show this help text and exit.
+EOF
+}
+
+main "$@"