Skip to content

Commit 1aa5b20

Browse files
committed
refactor: streamline editor adapter scripts with terminal definition
Consolidate the logic for checking and opening editors (Emacs, Neovim, Vim) into a single terminal editor adapter function. This refactor reduces redundancy in the editor scripts and enhances maintainability by utilizing shared global variables for command execution and error messaging.
1 parent 8ab2d56 commit 1aa5b20

5 files changed

Lines changed: 58 additions & 61 deletions

File tree

adapters/editor/emacs.sh

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
#!/usr/bin/env bash
22
# Emacs editor adapter
33

4-
# Check if Emacs is available
5-
editor_can_open() {
6-
command -v emacs >/dev/null 2>&1
7-
}
8-
9-
# Open a directory in Emacs
10-
# Usage: editor_open path
11-
editor_open() {
12-
local path="$1"
13-
14-
if ! editor_can_open; then
15-
log_error "Emacs not found. Install from https://www.gnu.org/software/emacs/"
16-
return 1
17-
fi
18-
19-
# Open emacs with the directory
20-
emacs "$path" &
21-
}
4+
_EDITOR_CMD="emacs"
5+
_EDITOR_ERR_MSG="Emacs not found. Install from https://www.gnu.org/software/emacs/"
6+
_EDITOR_BACKGROUND=1
7+
_editor_define_terminal

adapters/editor/nvim.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
#!/usr/bin/env bash
22
# Neovim editor adapter
33

4-
# Check if Neovim is available
5-
editor_can_open() {
6-
command -v nvim >/dev/null 2>&1
7-
}
8-
9-
# Open a directory in Neovim
10-
# Usage: editor_open path
11-
editor_open() {
12-
local path="$1"
13-
14-
if ! editor_can_open; then
15-
log_error "Neovim not found. Install from https://neovim.io"
16-
return 1
17-
fi
18-
19-
# Open neovim in the directory
20-
(cd "$path" && nvim .)
21-
}
4+
_EDITOR_CMD="nvim"
5+
_EDITOR_ERR_MSG="Neovim not found. Install from https://neovim.io"
6+
_editor_define_terminal

adapters/editor/vim.sh

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
#!/usr/bin/env bash
22
# Vim editor adapter
33

4-
# Check if Vim is available
5-
editor_can_open() {
6-
command -v vim >/dev/null 2>&1
7-
}
8-
9-
# Open a directory in Vim
10-
# Usage: editor_open path
11-
editor_open() {
12-
local path="$1"
13-
14-
if ! editor_can_open; then
15-
log_error "Vim not found. Install via your package manager."
16-
return 1
17-
fi
18-
19-
# Open vim in the directory
20-
(cd "$path" && vim .)
21-
}
4+
_EDITOR_CMD="vim"
5+
_EDITOR_ERR_MSG="Vim not found. Install via your package manager."
6+
_editor_define_terminal

bin/gtr

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,27 @@ _editor_define_standard() {
111111
}
112112
}
113113

114+
# Terminal editor adapter builder — for editors that run in the current terminal
115+
# Sets globals then call this: _EDITOR_CMD, _EDITOR_ERR_MSG, _EDITOR_BACKGROUND (optional, 0 or 1)
116+
_editor_define_terminal() {
117+
editor_can_open() {
118+
command -v "$_EDITOR_CMD" >/dev/null 2>&1
119+
}
120+
121+
editor_open() {
122+
local path="$1"
123+
if ! editor_can_open; then
124+
log_error "$_EDITOR_ERR_MSG"
125+
return 1
126+
fi
127+
if [ "${_EDITOR_BACKGROUND:-0}" = "1" ]; then
128+
"$_EDITOR_CMD" "$path" &
129+
else
130+
(cd "$path" && "$_EDITOR_CMD" .)
131+
fi
132+
}
133+
}
134+
114135
# Main dispatcher
115136
main() {
116137
local cmd="${1:-help}"
@@ -276,7 +297,7 @@ _auto_launch_editor() {
276297
local editor
277298
editor=$(cfg_default gtr.editor.default GTR_EDITOR_DEFAULT "none" defaults.editor)
278299
if [ "$editor" != "none" ]; then
279-
load_editor_adapter "$editor"
300+
load_editor_adapter "$editor" || return 1
280301
local workspace_file
281302
workspace_file=$(resolve_workspace_file "$worktree_path")
282303
log_step "Opening in $editor..."
@@ -295,7 +316,7 @@ _auto_launch_ai() {
295316
if [ "$ai_tool" = "none" ]; then
296317
log_warn "No AI tool configured. Set with: git gtr config set gtr.ai.default claude"
297318
else
298-
load_ai_adapter "$ai_tool"
319+
load_ai_adapter "$ai_tool" || return 1
299320
log_step "Starting $ai_tool..."
300321
ai_start "$worktree_path"
301322
fi
@@ -455,8 +476,8 @@ cmd_create() {
455476
log_info "Worktree created: $worktree_path"
456477

457478
# Auto-launch editor/AI or show next steps
458-
[ "$open_editor" -eq 1 ] && _auto_launch_editor "$worktree_path"
459-
[ "$start_ai" -eq 1 ] && _auto_launch_ai "$worktree_path"
479+
[ "$open_editor" -eq 1 ] && { _auto_launch_editor "$worktree_path" || true; }
480+
[ "$start_ai" -eq 1 ] && { _auto_launch_ai "$worktree_path" || true; }
460481
if [ "$open_editor" -eq 0 ] && [ "$start_ai" -eq 0 ]; then
461482
_post_create_next_steps "$branch_name" "$folder_name" "$folder_override" "$repo_root" "$base_dir" "$prefix"
462483
fi
@@ -926,7 +947,7 @@ cmd_editor() {
926947
log_info "Opened in file browser"
927948
else
928949
# Load editor adapter and open
929-
load_editor_adapter "$editor"
950+
load_editor_adapter "$editor" || exit 1
930951
local workspace_file
931952
workspace_file=$(resolve_workspace_file "$worktree_path")
932953
log_step "Opening in $editor..."
@@ -983,7 +1004,7 @@ cmd_ai() {
9831004
fi
9841005

9851006
# Load AI adapter
986-
load_ai_adapter "$ai_tool"
1007+
load_ai_adapter "$ai_tool" || exit 1
9871008

9881009
resolve_repo_context || exit 1
9891010
local repo_root="$_ctx_repo_root" base_dir="$_ctx_base_dir" prefix="$_ctx_prefix"
@@ -1525,6 +1546,9 @@ cmd_config() {
15251546
if [ -n "$extra_args" ]; then
15261547
log_warn "set action: ignoring extra arguments: $extra_args"
15271548
fi
1549+
if ! _cfg_is_known_key "$key"; then
1550+
log_warn "Unknown config key: $key (not a recognized gtr.* key)"
1551+
fi
15281552
cfg_set "$key" "$value" "$resolved_scope"
15291553
log_info "Config set: $key = $value ($resolved_scope)"
15301554
;;
@@ -1537,6 +1561,9 @@ cmd_config() {
15371561
if [ -n "$extra_args" ]; then
15381562
log_warn "add action: ignoring extra arguments: $extra_args"
15391563
fi
1564+
if ! _cfg_is_known_key "$key"; then
1565+
log_warn "Unknown config key: $key (not a recognized gtr.* key)"
1566+
fi
15401567
cfg_add "$key" "$value" "$resolved_scope"
15411568
log_info "Config added: $key = $value ($resolved_scope)"
15421569
;;
@@ -1549,6 +1576,9 @@ cmd_config() {
15491576
if [ -n "$value" ] || [ -n "$extra_args" ]; then
15501577
log_warn "unset action: ignoring extra arguments: ${value}${value:+ }${extra_args}"
15511578
fi
1579+
if ! _cfg_is_known_key "$key"; then
1580+
log_warn "Unknown config key: $key (not a recognized gtr.* key)"
1581+
fi
15521582
cfg_unset "$key" "$resolved_scope"
15531583
log_info "Config unset: $key ($resolved_scope)"
15541584
;;
@@ -1878,7 +1908,7 @@ _load_adapter() {
18781908
log_error "$label '$name' not found"
18791909
log_info "Built-in adapters: $builtin_list"
18801910
log_info "Or use any $label command available in your PATH (e.g., $path_hint)"
1881-
exit 1
1911+
return 1
18821912
fi
18831913

18841914
# Set globals for generic adapter functions

lib/config.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,17 @@ cfg_map_from_file_key() {
125125
case "$1" in gtr.*) echo "$1" ;; esac
126126
}
127127

128+
# Check if a key is a recognized gtr.* config key
129+
# Usage: _cfg_is_known_key <key>
130+
# Returns: 0 if known, 1 if not
131+
_cfg_is_known_key() {
132+
local pair
133+
for pair in "${_CFG_KEY_MAP[@]}"; do
134+
[ "${pair%%|*}" = "$1" ] && return 0
135+
done
136+
return 1
137+
}
138+
128139
# Get all values for a multi-valued config key
129140
# Usage: cfg_get_all key [file_key] [scope]
130141
# file_key: optional key name in .gtrconfig (e.g., "copy.include" for gtr.copy.include)

0 commit comments

Comments
 (0)