zsh: Cleanup conf() a bit

Add more editor fallback values.
Cleanup comments and fix indentation.
Do not check for existence of specified config file. (Since the config
file can now be created that way)
"Downgrade" all file checks for readability to checks for existence,
since this should be handled on the editor layer.
This commit is contained in:
2020-09-23 02:36:11 +02:00
parent 0a2d70412e
commit 04ea8e9a89

View File

@@ -92,10 +92,18 @@ function qr() {
## Edit config file ## Edit config file
function conf() { function conf() {
# default to vim if no editor is set local CONF_EDITOR
local CONF_EDITOR=${EDITOR:-vim} if [ -n "$EDITOR" ]; then
CONF_EDITOR="$EDITOR"
elif command -v vim &>/dev/null; then
CONF_EDITOR=vim
elif command -v nano &>/dev/null; then
CONF_EDITOR=nano
else
CONF_EDITOR=cat
fi
# parse otions # Parse otions
while getopts "e:" opt 2>/dev/null; do while getopts "e:" opt 2>/dev/null; do
case $opt in case $opt in
e) CONF_EDITOR="$OPTARG";; e) CONF_EDITOR="$OPTARG";;
@@ -105,8 +113,6 @@ function conf() {
done done
shift $(( $OPTIND - 1 )) shift $(( $OPTIND - 1 ))
# CONF_EDITOR=( $(resolve -s $CONF_EDITOR) )
# conf needs an argument # conf needs an argument
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
printf "\033[1;31mPlease specify a config.\n\033[0m" >&2 printf "\033[1;31mPlease specify a config.\n\033[0m" >&2
@@ -114,23 +120,18 @@ function conf() {
fi fi
# search for program name in XDG_CONFIG_HOME and $HOME # search for program name in XDG_CONFIG_HOME and $HOME
local CONF_DIR="$(_get_config_dir "$1")" local CONF_DIR="$(_get_config_dir "$1")"
if [ $? -ne 0 ]; then if (( $? )); then
printf "\033[1;31mFalling back to $HOME.\n\033[0m" >&2 printf "\033[1;31mFalling back to $HOME.\n\033[0m" >&2
CONF_DIR="$HOME" CONF_DIR="$HOME"
fi fi
# open file with specified name if # If specific name is given, open file.
if [ $# -gt 1 ]; then if [ $# -gt 1 ]; then
if [ -r "$CONF_DIR/$2" ]; then "$CONF_EDITOR" "$CONF_DIR/$2"
$CONF_EDITOR "$CONF_DIR/$2" return
return 0
else
printf "\033[1;31mCould not find config file with that name.\n\033[0m" >&2
return 1
fi
fi fi
# possible config-file names + same in hidden # possible config-file names + same but hidden
local -a CONF_PATTERNS local -a CONF_PATTERNS
CONF_PATTERNS=( CONF_PATTERNS=(
"$1.conf" "$1.conf"
@@ -146,10 +147,10 @@ function conf() {
# check if config file exists # check if config file exists
for config in $CONF_PATTERNS; do for config in $CONF_PATTERNS; do
if [ -r "$CONF_DIR/$config" ]; then if [ -e "$CONF_DIR/$config" ]; then
$CONF_EDITOR "$CONF_DIR/$config" $CONF_EDITOR "$CONF_DIR/$config"
return 0 return 0
elif [ -r "$CONF_DIR/.$config" ]; then elif [ -e "$CONF_DIR/.$config" ]; then
$CONF_EDITOR "$CONF_DIR/.$config" $CONF_EDITOR "$CONF_DIR/.$config"
return 0 return 0
fi fi
@@ -160,7 +161,7 @@ function conf() {
if [ "$CONF_DIR" != "$HOME" ];then if [ "$CONF_DIR" != "$HOME" ];then
for config in $CONF_PATTERNS; do for config in $CONF_PATTERNS; do
# Only look for hidden files # Only look for hidden files
if [ -r "$HOME/.$config" ]; then if [ -e "$HOME/.$config" ]; then
$CONF_EDITOR "$HOME/.$config" $CONF_EDITOR "$HOME/.$config"
return 0 return 0
fi fi