container: lib_edit: create if not found, refactor

This commit is contained in:
2024-07-05 23:32:58 -07:00
parent adebadbaa9
commit f7d1542b32

View File

@@ -201,22 +201,45 @@ function lib_edit::__parse_args()
function lib_edit::__edit()
{
[ -z "$EDITOR" ] && lib_utils::die_fatal
[ -z "$global_child_profile" ] && lib_utils::die_fatal
[ -z "$global_child_profile_flow" ] && lib_utils::die_fatal
[ -z "$global_conf_fetch" ] && lib_utils::die_fatal
[ -z "$global_conf_hledger" ] && lib_utils::die_fatal
[ -z "$global_conf_meta" ] && lib_utils::die_fatal
[ -z "$global_conf_shell" ] && lib_utils::die_fatal
for _type in "${global_arg_type[@]}"; do
case "$_type" in
fetch)
$EDITOR "$global_conf_fetch"
[ -z "$global_conf_fetch" ] && lib_utils::die_fatal
local _dir
_dir="$(dirname $global_conf_fetch)"
[ ! -d "$_dir" ] && mkdir -p "$_dir"
local _file
_file="$(basename $global_conf_fetch)"
local _path
_path="${_dir}/${_file}"
[ ! -f "$_path" ] && touch "$_path"
$EDITOR "$_path" || lib_utils::die_fatal
;;
hledger)
$EDITOR "$global_conf_hledger"
[ -z "$global_conf_hledger" ] && lib_utils::die_fatal
local _dir
_dir="$(dirname $global_conf_hledger)"
[ ! -d "$_dir" ] && mkdir -p "$_dir"
local _file
_file="$(basename $global_conf_hledger)"
local _path
_path="${_dir}/${_file}"
[ ! -f "$_path" ] && touch "$_path"
$EDITOR "$_path" || lib_utils::die_fatal
;;
iadd | manual)
[ -z "$global_child_profile" ] && lib_utils::die_fatal
[ -z "$global_child_profile_flow" ] && lib_utils::die_fatal
local _path="${global_child_profile_flow}/import/${global_child_profile}/_manual_/${global_arg_year}"
[ ! -d "$_path" ] && mkdir -p "$_path"
@@ -230,16 +253,29 @@ function lib_edit::__edit()
[[ "$_type" == "manual" ]] && $EDITOR "$_path"
;;
meta)
[ -z "$global_conf_meta" ] && lib_utils::die_fatal
local _dir
_dir="$(dirname $global_conf_meta)"
[ ! -d "$_dir" ] && mkdir -p "$_dir"
local _file
_file="$(basename $global_conf_meta)"
local _path
_path="${_dir}/${_file}"
[ ! -f "$_path" ] && touch "$_path"
# NOTE:
# - Expects comments to begin with format: //!
# - If saved to original, opening with `--skip` will clobber the
# original file's comments.
local -r _skip="$(grep -E "^//!" $global_conf_meta | wc -l)"
visidata --quitguard --motd-url file:///dev/null --filetype csv --skip "$_skip" "$global_conf_meta"
local -r _skip="$(grep -E "^//!" $_path | wc -l)"
visidata --quitguard --motd-url file:///dev/null --filetype csv --skip "$_skip" "$_path"
# TODO: HACK: visidata saves w/ DOS-style carriage...
# ...but there seems to be no option out of this.
sed -i 's:\r::g' "$global_conf_meta"
sed -i 's:\r::g' "$_path"
;;
preprocess | rules)
# Run all paths through one editor instance
@@ -278,10 +314,23 @@ function lib_edit::__edit()
done
# Execute
$EDITOR "${_paths[@]}"
$EDITOR "${_paths[@]}" || lib_utils::die_fatal
;;
shell)
$EDITOR "$global_conf_shell"
[ -z "$global_conf_shell" ] && lib_utils::die_fatal
local _dir
_dir="$(dirname $global_conf_shell)"
[ ! -d "$_dir" ] && mkdir -p "$_dir"
local _file
_file="$(basename $global_conf_shell)"
local _path
_path="${_dir}/${_file}"
[ ! -f "$_path" ] && touch "$_path"
$EDITOR "$_path" || lib_utils::die_fatal
;;
esac
done