diff --git a/README.md b/README.md index 13de711..30fb287 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,7 @@ An oh-my-zsh plugin for switching Temporal contexts (`~/.temporal/config`) simil - `temporalctx `: switch to named context - `temporalctx -`: switch to previous context - `temporalctx -c`: print current context +- `temporalctx edit` (`-e`/`--edit`): open config in `$VISUAL`, then `$EDITOR`, else `vi` - `temporal ...`: wrapped by plugin to automatically include current context flags ## Helper function diff --git a/temporalctx.plugin.zsh b/temporalctx.plugin.zsh index 2d3d177..c15e506 100644 --- a/temporalctx.plugin.zsh +++ b/temporalctx.plugin.zsh @@ -206,6 +206,13 @@ _temporalctx_pick_context() { _temporalctx_switch "$selected" } +_temporalctx_edit_config() { + local config_file editor_cmd + config_file="$(_temporalctx_config_file)" + editor_cmd="${VISUAL:-${EDITOR:-vi}}" + ${=editor_cmd} "$config_file" +} + typeset -ga _temporalctx_flags _temporalctx_build_flags() { @@ -270,6 +277,9 @@ temporalctx() { "") _temporalctx_pick_context ;; + edit|-e|--edit) + _temporalctx_edit_config + ;; -c) _temporalctx_current_context ;; diff --git a/tests/temporalctx_plugin_test.sh b/tests/temporalctx_plugin_test.sh index af83607..95cad43 100755 --- a/tests/temporalctx_plugin_test.sh +++ b/tests/temporalctx_plugin_test.sh @@ -76,6 +76,30 @@ assert_contains "$out" "pick=cloud-prod" "interactive picker should switch to fz assert_contains "$out" "curr=cloud-prod" "current context should match picker result" log "case passed: interactive picker" +# Test: edit subcommand opens config in VISUAL/EDITOR. +log "case: edit subcommand opens config with VISUAL/EDITOR" +cat > "$tmp_root/bin/editor-visual" <<'SH' +#!/usr/bin/env bash +printf '%s\n' "$*" > "${TMP_EDITOR_VISUAL_LOG:?}" +SH +cat > "$tmp_root/bin/editor-fallback" <<'SH' +#!/usr/bin/env bash +printf '%s\n' "$*" > "${TMP_EDITOR_FALLBACK_LOG:?}" +SH +chmod +x "$tmp_root/bin/editor-visual" "$tmp_root/bin/editor-fallback" + +visual_log="$tmp_root/visual.log" +fallback_log="$tmp_root/fallback.log" +PATH="$tmp_root/bin:$PATH" TEMPORAL_CONFIG="$cfg" VISUAL="$tmp_root/bin/editor-visual" EDITOR="$tmp_root/bin/editor-fallback" TMP_EDITOR_VISUAL_LOG="$visual_log" TMP_EDITOR_FALLBACK_LOG="$fallback_log" zsh -c ' + source "'$REPO_ROOT'/temporalctx.plugin.zsh" + temporalctx edit +' +[[ -f "$visual_log" ]] || fail "VISUAL editor should be invoked by edit subcommand" +visual_args="$(cat "$visual_log")" +assert_contains "$visual_args" "$cfg" "editor should receive config file path" +[[ ! -f "$fallback_log" ]] || fail "EDITOR fallback should not run when VISUAL is set" +log "case passed: edit subcommand" + # Test: temporal command wrapping + opt-out. log "case: temporal wrapper injects flags and supports opt-out" out="$(PATH="$tmp_root/bin:$PATH" TEMPORAL_CONFIG="$cfg" TEMPORAL_CLOUD_PROD_API_KEY=sekret zsh -c '