mirror of
https://github.com/m-housh/dotfiles.git
synced 2026-02-15 06:32:40 +00:00
Updated newx and alias, now handles creating functions and local executables
This commit is contained in:
@@ -1,26 +1,74 @@
|
|||||||
#!/bin/sh
|
#!/bin/zsh
|
||||||
|
|
||||||
# Creates a new executable file in the $SCRIPTS directory.
|
# Creates a new executable file.
|
||||||
|
|
||||||
# check if the scripts path / environment variable is set
|
declare -a localOpt
|
||||||
test ! -n "${SCRIPTS}" && echo "SCRIPTS not set" && exit 1;
|
declare -a functionOpt
|
||||||
|
declare -a shellOpt=("zsh")
|
||||||
|
DEBUG="$(env | grep DEBUG)"
|
||||||
|
|
||||||
# fallback to check if the name arg ${1} exists, if not show usage text.
|
zparseopts -D -K -- \
|
||||||
test ! -n "${1}" && \
|
{l,-local}+:=localOpt \
|
||||||
echo "usage: newx <name> <optional: shell>" && \
|
{f,-function}+:=functionOpt \
|
||||||
echo "shell defaults to sh if not supplied" && \
|
{s,-shell}+=shellOpt
|
||||||
exit 1;
|
|
||||||
|
|
||||||
path="${SCRIPTS}/${1}"
|
shell=${shellOpt[-1]}
|
||||||
shell="${2:-sh}"
|
localPath=${localOpt[-1]}
|
||||||
|
|
||||||
if [ -e "${path}" ]; then
|
#-------------------- Helpers --------------------
|
||||||
echo "Already exists try:"
|
|
||||||
echo "$EDITOR $path"
|
function debug_print() {
|
||||||
exit 1
|
[ -n "$DEBUG" ] \
|
||||||
|
&& echo "DEBUG: $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
function create_script() {
|
||||||
|
|
||||||
|
local script_path="$1"
|
||||||
|
|
||||||
|
debug_print "shell: $shell"
|
||||||
|
debug_print "script-path: $script_path"
|
||||||
|
|
||||||
|
printf "#!/bin/%s\n\n" "${shell}" > "${script_path}"
|
||||||
|
chmod +x "${script_path}"
|
||||||
|
exec "${EDITOR}" +2 "${script_path}"
|
||||||
|
}
|
||||||
|
|
||||||
|
function parse_local_path() {
|
||||||
|
if [ -z "$localPath" ]; then
|
||||||
|
local prefix="${1[1]}${1[2]}"
|
||||||
|
debug_print "prefix: $prefix"
|
||||||
|
[ "$prefix" = "./" ] && localPath="$1"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
#-------------------- Main --------------------
|
||||||
|
|
||||||
|
debug_print "first-arg: $1"
|
||||||
|
|
||||||
|
if [ -n "${functionOpt[-1]}" ]; then
|
||||||
|
debug_print "function-opt: $functionOpt"
|
||||||
|
|
||||||
|
[ ! -d "$ZFUNCDIR" ] \
|
||||||
|
&& echo "Function directory not found" \
|
||||||
|
&& return 1
|
||||||
|
|
||||||
|
create_script "$ZFUNCDIR/${functionOpt[-1]}"
|
||||||
|
return 0
|
||||||
fi
|
fi
|
||||||
|
|
||||||
printf "#!/bin/%s\n\n" "${shell}" > "${path}"
|
parse_local_path "$1"
|
||||||
chmod +x "${path}"
|
|
||||||
exec "${EDITOR}" +2 "${path}"
|
if [ -n "$localPath" ]; then
|
||||||
|
debug_print "local: $localPath"
|
||||||
|
create_script "$localPath"
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
debug_print "scripts: $1"
|
||||||
|
[ ! -d "$SCRIPTS" ] \
|
||||||
|
&& echo "Scripts directory not found. Set the SCRIPTS environment variable." \
|
||||||
|
&& return 1
|
||||||
|
|
||||||
|
create_script "${SCRIPTS}/$1"
|
||||||
|
|
||||||
|
|||||||
@@ -149,6 +149,7 @@ alias tss='~/.local/scripts/tmux-sessionator --choose'
|
|||||||
alias tls='tmux list-sessions'
|
alias tls='tmux list-sessions'
|
||||||
alias temp='cd $(mktemp -d)'
|
alias temp='cd $(mktemp -d)'
|
||||||
alias vi='nvim'
|
alias vi='nvim'
|
||||||
|
alias newxf='~/.local/scripts/newx --function'
|
||||||
alias nvim='unset VIMINIT && unset MYVIMRC && nvim'
|
alias nvim='unset VIMINIT && unset MYVIMRC && nvim'
|
||||||
alias nvim-mhoush='NVIM_APPNAME=m-housh && nvim'
|
alias nvim-mhoush='NVIM_APPNAME=m-housh && nvim'
|
||||||
alias nvim-kickstart='NVIM_APPNAME=kickstart nvim'
|
alias nvim-kickstart='NVIM_APPNAME=kickstart nvim'
|
||||||
|
|||||||
Reference in New Issue
Block a user