Add several updates
This commit is contained in:
parent
815089d968
commit
6b13675289
146
cmd
146
cmd
|
@ -226,7 +226,14 @@ x.some.config.setting() {
|
||||||
|
|
||||||
# ------------------------------- usage ------------------------------
|
# ------------------------------- usage ------------------------------
|
||||||
|
|
||||||
HELP[usage]='Displays a summary of usage.'
|
HELP[usage]='
|
||||||
|
|
||||||
|
```
|
||||||
|
'"$EXE"' usage
|
||||||
|
```
|
||||||
|
|
||||||
|
Display all possible commands. Note that this is usually easier by
|
||||||
|
simply using tab completion instead.'
|
||||||
|
|
||||||
x.usage() {
|
x.usage() {
|
||||||
local -a cmds
|
local -a cmds
|
||||||
|
@ -258,17 +265,19 @@ Also see `readme` and `usage` commands.
|
||||||
'
|
'
|
||||||
|
|
||||||
x.help() {
|
x.help() {
|
||||||
local name="${1:-main}" title body
|
local name="${1:-main}" title body file
|
||||||
title=$(_help_title "$name") || true
|
title=$(_help_title "$name") || true
|
||||||
if [[ -z "$title" ]]; then
|
if [[ -z "$title" ]]; then
|
||||||
body="${HELP[$name]}"
|
|
||||||
title="$EXE $name"
|
title="$EXE $name"
|
||||||
[[ $name = main ]] && title="$EXE"
|
[[ $name == main ]] && title="$EXE"
|
||||||
|
fi
|
||||||
|
if [[ $name == main ]]; then
|
||||||
|
body=$(x.readme)
|
||||||
|
body=${body#*$title}
|
||||||
else
|
else
|
||||||
body="${HELP[$name]}"
|
body="${HELP[$name]}"
|
||||||
body=${body#*$title}
|
|
||||||
fi
|
fi
|
||||||
local file="/tmp/help-$EXE-$name.html"
|
file="/tmp/help-$EXE-$name.html"
|
||||||
if _have pandoc ; then
|
if _have pandoc ; then
|
||||||
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
if _have "$HELP_BROWSER" && [[ -t 1 ]] ;then
|
||||||
pandoc -f gfm -s --metadata title="$title" \
|
pandoc -f gfm -s --metadata title="$title" \
|
||||||
|
@ -318,9 +327,6 @@ x.readme() {
|
||||||
echo -e "----\n\n*Autogenerated $(date)*\n"
|
echo -e "----\n\n*Autogenerated $(date)*\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
# x.json() { _jsonstr "$@"; }
|
|
||||||
# x.urlencode() { _urlencode "$@"; }
|
|
||||||
|
|
||||||
# ------------------------------ config ------------------------------
|
# ------------------------------ config ------------------------------
|
||||||
|
|
||||||
HELP[config]='
|
HELP[config]='
|
||||||
|
@ -328,34 +334,39 @@ HELP[config]='
|
||||||
```
|
```
|
||||||
'"$EXE"' config
|
'"$EXE"' config
|
||||||
'"$EXE"' config KEY
|
'"$EXE"' config KEY
|
||||||
'"$EXE"' config KEY VALUE
|
'"$EXE"' config.set KEY VALUE
|
||||||
'"$EXE"' config KEY ""
|
'"$EXE"' config.set KEY ""
|
||||||
'"$EXE"' config keys
|
'"$EXE"' config.keys
|
||||||
'"$EXE"' config val[ues]
|
'"$EXE"' config.values
|
||||||
'"$EXE"' config dir[ectory]
|
'"$EXE"' config.directory
|
||||||
'"$EXE"' config path [file]
|
'"$EXE"' config.path [file]
|
||||||
'"$EXE"' config edit [file]
|
'"$EXE"' config.edit [file]
|
||||||
'"$EXE"' config del[ete]
|
'"$EXE"' config.delete
|
||||||
|
'"$EXE"' config.read
|
||||||
|
'"$EXE"' config.write
|
||||||
|
'"$EXE"' config.dump
|
||||||
```
|
```
|
||||||
|
|
||||||
The `config` command is for reading, writing, and displaying standard
|
The `config` command is for reading, writing, and displaying standard
|
||||||
open desktop configuration properties. Pass an empty string to delete
|
open desktop configuration properties.
|
||||||
a property.
|
|
||||||
|
|
||||||
### Arguments
|
### Arguments
|
||||||
|
|
||||||
With no arguments outputs all the currently cached configuration
|
With no arguments calls `dump` and outputs all the currently cached
|
||||||
settings.
|
configuration settings.
|
||||||
|
|
||||||
With a single KEY argument fetches the value for that key and outputs
|
With a single KEY argument fetches the value for that key and outputs
|
||||||
it unless it is one of the following special (reserved) key names:
|
it unless it is one of the following special (reserved) key names:
|
||||||
|
|
||||||
* `dir*` full path to config directory
|
* `directory` full path to config directory
|
||||||
* `path` full path to specific config file (default: `values`)
|
* `path` full path to specific config file (default: `values`)
|
||||||
* `edit` opens config file in editor (default: `editor` or `$EDITOR)
|
* `edit` opens config file in editor (default: `editor` or `$EDITOR)
|
||||||
* `keys` output the configuration keys, one per line
|
* `keys` output the configuration keys, one per line
|
||||||
* `val*` output the configuration values, one per line
|
* `values` output the configuration values, one per line
|
||||||
* `del*` if key argument then delete a specific key, otherwise prompt
|
* `delete` if key argument then delete a specific key, otherwise prompt
|
||||||
|
* `read` reads the configuration file into CONF associative array
|
||||||
|
* `write` write the CONF associative array to the configuration file
|
||||||
|
* `dump` write the flattened CONF associative array to standard output
|
||||||
|
|
||||||
With more than one argument the remaining arguments after the KEY will
|
With more than one argument the remaining arguments after the KEY will
|
||||||
be combined into the VALUE and written to a `values` file in the
|
be combined into the VALUE and written to a `values` file in the
|
||||||
|
@ -386,46 +397,38 @@ existing tools (and no `jq` dependency).
|
||||||
* KEYs may be anything but the equal sign (`=`)
|
* KEYs may be anything but the equal sign (`=`)
|
||||||
* VALUEs may be anything but line returns must be escaped
|
* VALUEs may be anything but line returns must be escaped
|
||||||
|
|
||||||
Note that this is *not* the same as Java properties and other similar
|
Note that, although similar, this is *not* the same as Java properties
|
||||||
format. It is designed for ultimate simplicity, efficiency, and
|
and other similar format. It is designed for ultimate simplicity,
|
||||||
portability.'
|
efficiency, and portability.'
|
||||||
|
|
||||||
x.config() {
|
x.config() {
|
||||||
case $1 in
|
|
||||||
dir*) shift; _config_dir "$@"; return $? ;;
|
|
||||||
path) shift; _config_path "$@"; return $? ;;
|
|
||||||
edit) shift; _config_edit "$@"; return $? ;;
|
|
||||||
del*) shift; _config_del "$@"; return $? ;;
|
|
||||||
keys) shift; _config_keys "$@"; return $? ;;
|
|
||||||
val*) shift; _config_vals "$@"; return $? ;;
|
|
||||||
esac
|
|
||||||
case $# in
|
case $# in
|
||||||
0) _config_dump ;;
|
0) x.config.dump ;;
|
||||||
1) _config_get "$@" ;;
|
1) x.config.get "$@" ;;
|
||||||
*) _config_set "$@" ;;
|
*) x.config.set "$@" ;;
|
||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_edit() {
|
x.config.edit() {
|
||||||
: "${CONF[editor]:="${EDITOR:=vi}"}"
|
: "${CONF[editor]:="${EDITOR:=vi}"}"
|
||||||
exec "${CONF[editor]}" "$(_config_path "${1:-values}")"
|
exec "${CONF[editor]}" "$(x.config.path "${1:-values}")"
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_del() {
|
x.config.delete() {
|
||||||
if [[ -z "$1" ]];then
|
if [[ -z "$1" ]];then
|
||||||
select key in "${!CONF[@]}"; do
|
select key in "${!CONF[@]}"; do
|
||||||
_config_del "$key"
|
x.config.delete "$key"
|
||||||
return $?
|
return $?
|
||||||
done
|
done
|
||||||
fi
|
fi
|
||||||
_config_set "$1" ''
|
x.config.set "$1" ''
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_keys() { printf "%s\n" "${!CONF[@]}"; }
|
x.config.keys() { printf "%s\n" "${!CONF[@]}"; }
|
||||||
|
|
||||||
_config_vals() { printf "%s\n" "${CONF[@]}"; }
|
x.config.values() { printf "%s\n" "${CONF[@]}"; }
|
||||||
|
|
||||||
_config_dir() {
|
x.config.dir() {
|
||||||
local dir="$HOME/.config/$EXE"
|
local dir="$HOME/.config/$EXE"
|
||||||
[[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE"
|
[[ -n "$XDG_CONFIG_HOME" ]] && dir="$XDG_CONFIG_HOME/$EXE"
|
||||||
[[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR"
|
[[ -n "$CONFIG_DIR" ]] && dir="$CONFIG_DIR"
|
||||||
|
@ -435,28 +438,28 @@ _config_dir() {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_path() {
|
x.config.path() {
|
||||||
local file=${1:-values}
|
local file=${1:-values}
|
||||||
printf "%s/%s" "$(_config_dir)" "$file"
|
printf "%s/%s" "$(x.config.dir)" "$file"
|
||||||
[[ -t 1 ]] && echo
|
[[ -t 1 ]] && echo
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_set() {
|
x.config.set() {
|
||||||
local key="$1"; shift; local val="$*"
|
local key="$1"; shift; local val="$*"
|
||||||
val="${val//$'\n'/\\n}"
|
val="${val//$'\n'/\\n}"
|
||||||
CONF["$key"]="$val"
|
CONF["$key"]="$val"
|
||||||
_config_write
|
x.config.write
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_get() {
|
x.config.get() {
|
||||||
printf "%s" "${CONF[$1]}"
|
printf "%s" "${CONF[$1]}"
|
||||||
[[ -t 1 ]] && echo
|
[[ -t 1 ]] && echo
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_read() {
|
x.config.read() {
|
||||||
local values="$(_config_path)"
|
local values="$(x.config.path)"
|
||||||
[[ -r "$values" ]] || return 0
|
[[ -r "$values" ]] || return 0
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
|
[[ $line =~ ^([^=]+)=(.+)$ ]] || continue
|
||||||
|
@ -464,13 +467,13 @@ _config_read() {
|
||||||
done < "$values"
|
done < "$values"
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_write() {
|
x.config.write() {
|
||||||
local dir="$(_config_dir)"
|
local dir="$(x.config.dir)"
|
||||||
mkdir -p "$dir"
|
mkdir -p "$dir"
|
||||||
_config_dump > "$dir/values"
|
x.config.dump > "$dir/values"
|
||||||
}
|
}
|
||||||
|
|
||||||
_config_dump() {
|
x.config.dump() {
|
||||||
(( ${#CONF[@]} == 0 )) && return 0
|
(( ${#CONF[@]} == 0 )) && return 0
|
||||||
paste -d=\
|
paste -d=\
|
||||||
<(printf "%s\n" "${!CONF[@]}") \
|
<(printf "%s\n" "${!CONF[@]}") \
|
||||||
|
@ -512,7 +515,7 @@ _reduce() {
|
||||||
|
|
||||||
_newest() {
|
_newest() {
|
||||||
IFS=$'\n'
|
IFS=$'\n'
|
||||||
local -a f=($(ls -1 --color=never -trd ${1:-.}/* 2>/dev/null))
|
mapfile -t f < <(ls -1 --color=never -trd "${1:-.}"/* 2>/dev/null)
|
||||||
[[ ${#f} > 0 ]] && echo "${f[-1]}"
|
[[ ${#f} > 0 ]] && echo "${f[-1]}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -521,6 +524,8 @@ _trim() {
|
||||||
echo -e "${it%"${it##*[![:space:]]}"}"
|
echo -e "${it%"${it##*[![:space:]]}"}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_join() { local IFS="$1"; shift; echo "$*"; }
|
||||||
|
|
||||||
_have(){ type "$1" &>/dev/null; }
|
_have(){ type "$1" &>/dev/null; }
|
||||||
|
|
||||||
_checkdep() {
|
_checkdep() {
|
||||||
|
@ -541,10 +546,27 @@ _buffer() {
|
||||||
"${FUNCNAME[1]}" "$(</dev/stdin)"
|
"${FUNCNAME[1]}" "$(</dev/stdin)"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_prompt() {
|
||||||
|
local key="$1" def="$2" regx="$3" value first=yes
|
||||||
|
shift 3
|
||||||
|
local text="${*:-Enter value for %s [%s]: }"
|
||||||
|
[[ -z "$key" ]] && echo "Missing prompt key" >&2 && return 1
|
||||||
|
[[ -z "$regx" ]] && echo "Missing valid regx" >&2 && return 1
|
||||||
|
while [[ ! $value =~ $regx ]];do
|
||||||
|
printf "$text" "$key" "$def" >&2
|
||||||
|
IFS= read -r value
|
||||||
|
[[ -z "$value" ]] && value="$def"
|
||||||
|
[[ $value =~ ^\ +$ ]] && value=""
|
||||||
|
[[ -n "$first" ]] && unset first && continue
|
||||||
|
echo "Must match /$regx/" >&2
|
||||||
|
done
|
||||||
|
_trim "$value"
|
||||||
|
}
|
||||||
|
|
||||||
# --------------------- completion and delegation --------------------
|
# --------------------- completion and delegation --------------------
|
||||||
# `complete -C foo foo` > `source <(foo bloated_completion)`
|
# `complete -C foo foo` > `source <(foo bloated_completion)`
|
||||||
|
|
||||||
_config_read
|
x.config.read
|
||||||
_have _initialize && _initialize "$@"
|
_have _initialize && _initialize "$@"
|
||||||
|
|
||||||
while IFS= read -r line; do
|
while IFS= read -r line; do
|
||||||
|
|
Loading…
Reference in New Issue