Skip to content

feat(diff): per-diff terminal width, auto_resize_terminal opt-out, and lifecycle events#270

Merged
ThomasK33 merged 2 commits into
mainfrom
feat/diff-terminal-width-and-events
Jun 9, 2026
Merged

feat(diff): per-diff terminal width, auto_resize_terminal opt-out, and lifecycle events#270
ThomasK33 merged 2 commits into
mainfrom
feat/diff-terminal-width-and-events

Conversation

@ThomasK33

Copy link
Copy Markdown
Member

Summary

Closes #242.

Currently terminal.split_width_percentage controls the Claude terminal width both when idle and while a diff is open, coupling two preferences that often want different values (wide for chatting, narrow while reviewing a diff). This PR adds three layered, fully backward-compatible controls:

  1. terminal.diff_split_width_percentage (default nil) — optional terminal width while a diff is open. Falls back to split_width_percentage when unset, so behavior is unchanged by default. This is the zero-Lua path for the original request.
  2. diff_opts.auto_resize_terminal (default true) — when false, the plugin leaves the terminal width untouched across the diff lifecycle so you can own it yourself.
  3. User autocmds ClaudeCodeDiffOpened / ClaudeCodeDiffClosed (always emitted, pcall-guarded) carrying a data payload — so configs can react to diffs opening/closing (resize, relayout, statusline, etc.) without monkey-patching private functions. This mirrors the existing ClaudeCodeSendComplete event convention added in [BUG] focus_after_send has no effect with provider = "none" #228.

The five existing terminal-resize sites are consolidated behind a single gated helper (resize_terminal_for_diff); the events fire exactly once per open/close at the single setup/cleanup points, so they also cover the :q-reject (#266) and disconnect-auto-close (#248) paths.

Design notes

This follows the triage discussion on #242: events are the durable, idiomatic mechanism (same shape as diffview.nvim's DiffviewViewOpened/Closed and this plugin's own ClaudeCodeSendComplete), with the width key as convenient sugar and the opt-out flag as the clean "I own the layout" escape hatch. Nothing is removed and the default behavior is byte-identical.

event.data for ClaudeCodeDiffOpened: tab_name, file_path, new_file_path, is_new_file, diff_window, target_window, terminal_window, tab_number. For ClaudeCodeDiffClosed: tab_name, file_path, reason (a best-effort human-readable label, not a stable enum).

Opt-out + event example

require("claudecode").setup({
  diff_opts = { auto_resize_terminal = false },
})

vim.api.nvim_create_autocmd("User", {
  pattern = "ClaudeCodeDiffOpened",
  callback = function(ev)
    local term = ev.data.terminal_window
    if term and vim.api.nvim_win_is_valid(term) then
      vim.api.nvim_win_set_width(term, math.floor(vim.o.columns * 0.20))
    end
  end,
})

Test plan

  • mise run all — full suite green, lint + format clean.
  • New unit tests: width resolution/fallback (diff_split_width_spec), auto_resize_terminal gate + resize_terminal_for_diff gating (enabled/disabled/floating/invalid/nil) + lifecycle-event payloads via the recorder convention (diff_auto_resize_events_spec), plus config/terminal validation for the new options.
  • Headless Neovim integration (real diff open/close): both events fire with correct payloads; auto_resize_terminal=true shrinks the terminal to the diff width and restores the idle width on close; =false leaves width untouched while events still fire.
  • Live interactive verification (real TUI): diff open shrank the terminal to 20% and restored to 50% on close; with auto_resize_terminal=false a user ClaudeCodeDiffOpened handler owned the width instead.

Docs

README gains a "Diff Lifecycle Events" section and the new config options; CLAUDE.md, dev-config.lua, and type annotations updated.

🤖 Generated with Claude Code

@ThomasK33

Copy link
Copy Markdown
Member Author

@claude review

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 70383e938b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread tests/unit/diff_auto_resize_events_spec.lua Outdated
…le events

Implements issue #242. Adds three layered, backward-compatible controls for
the Claude terminal during diff review:

- terminal.diff_split_width_percentage: optional terminal width while a diff is
  open (falls back to split_width_percentage; default nil keeps current behavior).
- diff_opts.auto_resize_terminal (default true): when false, the plugin leaves
  the terminal width untouched so users own it themselves.
- User autocmds ClaudeCodeDiffOpened / ClaudeCodeDiffClosed (always emitted,
  pcall-guarded) carrying a data payload, so configs can react to the diff
  lifecycle without monkey-patching private functions.

The five existing terminal-resize sites are consolidated behind one gated
helper; events fire exactly once per open/close.

Change-Id: I4a712a576cd1a3bd203e3f3659784bb8122f31c0
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Thomas Kosiewski <tk@coder.com>
@ThomasK33 ThomasK33 force-pushed the feat/diff-terminal-width-and-events branch from 70383e9 to d4bfab3 Compare June 9, 2026 09:07
@ThomasK33

Copy link
Copy Markdown
Member Author

Rebased onto latest main and CI is green (unit + integration tests pass). Please review the current commit (d4bfab3).

@claude review

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d4bfab3402

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread lua/claudecode/diff.lua
…eeze-width

Per review feedback (#270): when auto_resize_terminal=false the plugin applies
no width policy, but the diff layout still runs `wincmd =` (equalizing splits),
so opt-out means "own the width via the ClaudeCodeDiffOpened/Closed events"
(which fire after layout, so a handler's resize wins) rather than "freeze the
width". Tighten the helper comment and README wording to match.

Change-Id: I2ab94caf160ae7e1fe57c3917ba00ce628aaf52b
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Signed-off-by: Thomas Kosiewski <tk@coder.com>
@ThomasK33

Copy link
Copy Markdown
Member Author

@codex review

Re-requesting on c88184e — the earlier P1 (stale-commit test) and P2 (opt-out docs wording) are both addressed and CI is green (unit + integration).

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ThomasK33 ThomasK33 merged commit 7b8b709 into main Jun 9, 2026
2 checks passed
@ThomasK33 ThomasK33 deleted the feat/diff-terminal-width-and-events branch June 9, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Allow separate terminal width during diff vs idle

1 participant