Skip to content

Commit 86e3ea7

Browse files
fix(zsh): clean up terminal after opencode TUI exit
opencode's TUI exits without fully restoring the terminal (anomalyco/opencode#32336, #28358): it leaves mouse tracking, bracketed paste, and the alternate screen enabled and never drains its cursor-position-report reply (ESC[8;1R). Under zellij that reply races into zsh, which self-inserts "8;1R" onto the next prompt. Wrap opencode in a zsh function that re-disables the leaked modes and drains pending query replies once it exits. Interim fix until the upstream teardown bug is resolved and opencode-pinned is bumped past it.
1 parent 1068fc1 commit 86e3ea7

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

‎home-manager/common/zsh.nix‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,34 @@ in
206206
source ${cachedInits.atuin}
207207
fi
208208
'')
209+
(lib.mkOrder 700 ''
210+
# opencode TUI teardown workaround (anomalyco/opencode#32336, #28358).
211+
# opencode's TUI exits without fully restoring the terminal: it leaves
212+
# mouse tracking, bracketed paste, and the alternate screen enabled, and
213+
# never drains its terminal-query replies — notably the cursor-position
214+
# report (ESC[8;1R, the answer to opencode's own ESC[6n). Inside zellij
215+
# that reply races past opencode's read loop into zsh, whose ZLE consumes
216+
# the ESC[ CSI introducer and self-inserts the trailing "8;1R" onto the
217+
# next prompt (the stray characters seen after quitting). This wrapper
218+
# runs after opencode exits to (1) re-disable the modes opencode forgot
219+
# and (2) drain any pending query replies so they never reach the prompt.
220+
#
221+
# `command opencode` keeps the foreground process named "opencode" so the
222+
# zellij autolock trigger (home-manager/common/zellij.nix) still fires.
223+
# Guarded on a tty so a non-interactive `opencode run` in a pipe is left
224+
# untouched. Remove once the upstream teardown bug is fixed and
225+
# opencode-pinned (overlays/misc.nix) is bumped past it.
226+
function opencode() {
227+
command opencode "$@"
228+
local _oc_status=$?
229+
if [[ -t 0 && -t 1 ]]; then
230+
printf '\e[?1049l\e[?1000l\e[?1002l\e[?1003l\e[?1006l\e[?2004l\e[?1004l\e[?25h'
231+
local _oc_flush
232+
while read -t 0.1 -k 1 -s _oc_flush 2>/dev/null; do : ; done
233+
fi
234+
return $_oc_status
235+
}
236+
'')
209237
(lib.mkOrder 9999 ''
210238
if [[ -n "$ZSH_XTRACE" ]]; then
211239
unsetopt xtrace

0 commit comments

Comments
 (0)