From ed3584f909a3549252be117fd1db2f15cf266f62 Mon Sep 17 00:00:00 2001 From: Benjamin Lupton Date: Wed, 23 Aug 2023 16:56:32 +0800 Subject: [PATCH] fix [dorothy edit] with vscode by adding support for [edit --only-editor] also rename beta command [get-nonflags] to [echo-nonflags] but it was not suitable, so still beta --- commands.beta/echo-nonflags | 52 +++++++++++++++++++++++++++++++++ commands.beta/get-nonflags | 58 ------------------------------------- commands/dorothy | 2 +- commands/edit | 17 +++++++++-- 4 files changed, 68 insertions(+), 61 deletions(-) create mode 100755 commands.beta/echo-nonflags delete mode 100755 commands.beta/get-nonflags diff --git a/commands.beta/echo-nonflags b/commands.beta/echo-nonflags new file mode 100755 index 000000000..ca16c9644 --- /dev/null +++ b/commands.beta/echo-nonflags @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +function echo_nonflags() ( + source "$DOROTHY/sources/bash.bash" + + function help { + cat <<-EOF >/dev/stderr + ABOUT: + Output all arguments that aren't flags. + + USAGE: + echo-nonflags <...input> + echo-lines <...input> | echo-nonflags + + EXAMPLE: + + echo-nonflags code -a --b c + + code + c + # exit status: 0 + + echo-lines code -a --b c | echo-nonflags + + code + c + # exit status: 0 + + QUIRKS: + + [edit --dry | echo-nonflags] will output [code -w -n] because of the following: + + echo 'code -a --b c' | echo-nonflags + + code -a --b c + # exit status: 0 + EOF + return 22 # EINVAL 22 Invalid argument + } + + function on_input { + if [[ $1 != -* ]]; then + print_line "$1" + fi + } + source "$DOROTHY/sources/stdinargs.bash" +) + +# fire if invoked standalone +if test "$0" = "${BASH_SOURCE[0]}"; then + echo_nonflags "$@" +fi diff --git a/commands.beta/get-nonflags b/commands.beta/get-nonflags deleted file mode 100755 index 952bb77a8..000000000 --- a/commands.beta/get-nonflags +++ /dev/null @@ -1,58 +0,0 @@ -#!/usr/bin/env bash - -function get_nonflags() ( - source "$DOROTHY/sources/bash.bash" - - # ===================================== - # Arguments - - function help { - cat <<-EOF >/dev/stderr - ABOUT: - Output arguments that are not flags. - - USAGE: - get-nonflags ... - EOF - if test "$#" -ne 0; then - echo-error "$@" - fi - return 22 # EINVAL 22 Invalid argument - } - - # process - local item args=() - while test "$#" -ne 0; do - item="$1" - shift - case "$item" in - '--help' | '-h') help ;; - '--') - args+=("$@") - shift "$#" - break - ;; - '--'*) help "An unrecognised flag was provided: $item" ;; - *) - args+=("$item" "$@") - shift "$#" - break - ;; - esac - done - - # ===================================== - # Action - - local arg - for arg in "${args[@]}"; do - if [[ $arg != -* ]]; then - print_line "$arg" - fi - done -) - -# fire if invoked standalone -if test "$0" = "${BASH_SOURCE[0]}"; then - get_nonflags "$@" -fi diff --git a/commands/dorothy b/commands/dorothy index 2d0db62e5..39e4cf86a 100755 --- a/commands/dorothy +++ b/commands/dorothy @@ -1212,7 +1212,7 @@ function dorothy() ( echo-segment --h1='Edit Dorothy' ensure_permissions_configured - if test "$(edit --dry)" == 'code'; then + if test "$(edit --dry --only-editor)" == 'code'; then code -- "$DOROTHY/.vscode/workspace.code-workspace" else edit -- "$DOROTHY" diff --git a/commands/edit b/commands/edit index 15974b3a7..b6665109f 100755 --- a/commands/edit +++ b/commands/edit @@ -22,6 +22,10 @@ function edit() ( --[no-]prompt If enabled (the default), support editors that require additional arguments to edit the file. + --only-editor + If enabled, only output the editor command, and not any arguments. + Enforces --dry. + --sudo If enabled, the editor will be opened in sudo mode. @@ -38,7 +42,7 @@ function edit() ( } # process - local item dry='no' prompt='yes' sudo='no' terminal='' gui='' args=() + local item dry='no' prompt='yes' only_editor='no' sudo='no' terminal='' gui='' args=() while test "$#" -ne 0; do item="$1" shift @@ -50,6 +54,9 @@ function edit() ( '--no-prompt'* | '--prompt'*) prompt="$(get-flag-value prompt --missing="$prompt" -- "$item" | echo-affirmative)" ;; + '--no-only-editor'* | '--only-editor'*) + only_editor="$(get-flag-value only-editor --missing="$only_editor" -- "$item" | echo-affirmative)" + ;; '--no-sudo'* | '--sudo'*) sudo="$(get-flag-value sudo --missing="$sudo" -- "$item" | echo-affirmative)" ;; @@ -165,7 +172,13 @@ function edit() ( return 42 # ENOPROTOOPT 42 Protocol not available fi - # generate command + # only output the editor command + if test "$only_editor" != 'no'; then + print_line "${editor[0]}" + return + fi + + # generate the entire command local cmd=() if test "$sudo" = 'yes'; then cmd+=(