From 18e685171fa3e8ce1b00e6b3b1b78d45a1755de4 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Fri, 18 Aug 2023 16:46:04 +0800 Subject: [PATCH] fix `dorothy theme` for nushell - config-helper: include test for $ - echo-escape-regex-replacement: escape $ - setup-util-nushell: fix bad hashbang - dorothy: theme editing for nushell - config.bash: support `nu` extension --- commands/config-helper | 9 ++++++++ commands/dorothy | 7 ++++++- commands/echo-escape-regex-replacement | 5 +++-- commands/setup-util-nushell | 4 ++-- sources/config.bash | 29 +++++++++++++++++--------- 5 files changed, 39 insertions(+), 15 deletions(-) diff --git a/commands/config-helper b/commands/config-helper index ef452a9e8..814638614 100755 --- a/commands/config-helper +++ b/commands/config-helper @@ -103,6 +103,15 @@ function config_helper_test() ( eval_tester --name='tcv2' --stdout=$'a=\'double "quote" inside\'\n\tb=(\n\t\t\'double "quote" inside\'\t"single \'quote\' inside"\n\t\t\'3\'\t\'4\'\n\t)\naa="single \'quote\' inside"\nbb=(\n\t\'more double "quote" inside\'\t"more single \'quote\' inside"\n\t\'33\'\t\'44\'\n)' \ -- cat "$file" + # test values with dollar signs and backslashes + printf '%s\n' '$env.VALUE="hello\world"' >"$file" + # trunk-ignore-all(shellcheck/SC2016) + eval_tester --name='tcv1' \ + -- config-helper --file="$file" -- \ + --string-find='$env.VALUE' --string-replace='$env.VALUE="HELLO\WORLD"' + eval_tester --name='tcv2' --stdout='$env.VALUE="HELLO\WORLD"' \ + -- cat "$file" + # done echo-segment --g1="TEST: $0" ) diff --git a/commands/dorothy b/commands/dorothy index 8c638cb2b..f00d13c5c 100755 --- a/commands/dorothy +++ b/commands/dorothy @@ -1238,7 +1238,12 @@ function dorothy() ( source "$DOROTHY/sources/config.bash" update_dorothy_user_config 'interactive.sh' -- \ --find='export DOROTHY_THEME=(.*)' --replace="export DOROTHY_THEME=$(echo-quote "$theme")" - # config-edit --file="$DOROTHY/user/config/interactive.nu" --needle="\$env.DOROTHY_THEME" --line="\$env.DOROTHY_THEME = $(echo-quote "$theme")" --add + if test -f "$DOROTHY/user/config/interactive.nu"; then + # update_dorothy_user_config 'interactive.nu' -- --string-find="\$env.DOROTHY_THEME" --string-replace="\$env.DOROTHY_THEME = $(echo-quote "$theme")" + # ^ don't use that, as theming will always be in the config not config.local + # ^ at some point, update_dorothy_user_config should use [config-edit] + config-edit --file="$DOROTHY/user/config/interactive.nu" --needle="\$env.DOROTHY_THEME" --line="\$env.DOROTHY_THEME = $(echo-quote "$theme")" --add + fi # log echo-style --success='Configuration change applied, ' --notice='restart your terminal for the change to take effect.' diff --git a/commands/echo-escape-regex-replacement b/commands/echo-escape-regex-replacement index 3d7626c24..9b9761e4e 100755 --- a/commands/echo-escape-regex-replacement +++ b/commands/echo-escape-regex-replacement @@ -16,12 +16,12 @@ function echo_escape_backslash() ( echo-escape-regex-replacement 'Special: \040b' - Special: \\\\040b + Hello \$\$world. Special: \\\\040b # exit status: 0 echo-escape-regex-replacement 'Special: \040b' | echo-escape-regex-replacement - Special: \\\\040b + Hello \$\$world. Special: \\\\040b # exit status: 0 EOF @@ -31,6 +31,7 @@ function echo_escape_backslash() ( function on_input { local value="$1" value="${value//\\/\\\\}" + value="${value//$/\$$}" # https://github.com/chmln/sd/issues/129 printf '%s\n' "$value" } diff --git a/commands/setup-util-nushell b/commands/setup-util-nushell index 8780ff25a..53c1e3211 100755 --- a/commands/setup-util-nushell +++ b/commands/setup-util-nushell @@ -33,7 +33,7 @@ function setup_util_nushell() ( # ensure [sources/interactive.nu] works if test ! -f "$DOROTHY/user/config.local/interactive.nu"; then cat <<-EOF >"$DOROTHY/user/config.local/interactive.nu" - #!/usr/bin/env env + #!/usr/bin/env nu # load the dorothy defaults source ~/.local/share/dorothy/config/interactive.nu @@ -41,7 +41,7 @@ function setup_util_nushell() ( fi if test ! -f "$DOROTHY/user/config/interactive.nu"; then cat <<-EOF >"$DOROTHY/user/config/interactive.nu" - #!/usr/bin/env env + #!/usr/bin/env nu # load my public configuration source ../config/interactive.nu diff --git a/sources/config.bash b/sources/config.bash index fea36fd30..12e8b770e 100755 --- a/sources/config.bash +++ b/sources/config.bash @@ -1,6 +1,9 @@ #!/usr/bin/env bash source "$DOROTHY/sources/config.sh" +# @todo this should be moved into [commands/dorothy] or at last be its own command +# as [config-edit], [config-helper], are all doubling up on this + # todo # if test \"\$(get-hostname)\" = '$(get-hostname)'; then @@ -112,7 +115,7 @@ function update_dorothy_user_config { # check extension local dorothy_config_extension # this is used later too dorothy_config_extension="$(fs-extension -- "$dorothy_config_filename")" - if ! [[ $dorothy_config_extension =~ bash|zsh|sh|fish ]]; then + if ! [[ $dorothy_config_extension =~ bash|zsh|sh|fish|nu ]]; then help "The file extension of [$dorothy_config_filename] is not yet supported." fi @@ -139,30 +142,36 @@ function update_dorothy_user_config { local dorothy_config_default="$DOROTHY/config/$dorothy_config_filename" if test -f "$dorothy_config_default"; then if test "$dorothy_config_template" = 'default'; then - # copy the entire template + # copy the entire template cp "$dorothy_config_default" "$dorothy_config_filepath" else # copy only the header echo-before-blank --append=$'\n' "$dorothy_config_default" >"$dorothy_config_filepath" fi else - # default missing, make it with the typical header + # even the dorothy default is missing cat <<-EOF >"$dorothy_config_filepath" #!/usr/bin/env $dorothy_config_extension - # do not use \`export\` keyword in this file: - # shellcheck disable=SC2034 EOF fi # add the source of the default file if test "$dorothy_config_source" = 'default'; then - # use `.` over `source` as must be posix, in case we are saving a .sh file - cat <<-EOF >>"$dorothy_config_filepath" - # Load the default configuration file - . "\$DOROTHY/config/${dorothy_config_filename}" + if test "$dorothy_config_extension" = 'nu'; then + cat <<-EOF >>"$dorothy_config_filepath" + # load the dorothy defaults + source ~/.local/share/dorothy/config/${dorothy_config_filename} - EOF + EOF + else + # use `.` over `source` as must be posix, in case we are saving a .sh file + cat <<-EOF >>"$dorothy_config_filepath" + # load the dorothy defaults + . "\$DOROTHY/config/${dorothy_config_filename}" + + EOF + fi fi # add the new file to the paths