Skip to content

feat: support function for external_terminal_cmd configuration #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jdurand
Copy link
Contributor

@jdurand jdurand commented Aug 14, 2025

Summary

  • Enhanced external terminal provider to support both string and function for external_terminal_cmd
  • Added comprehensive test coverage for the new functionality
  • Updated documentation with examples of both usage patterns

Changes

1. Enhanced External Terminal Provider

The external_terminal_cmd option can now be:

  • String template (existing): "alacritty -e %s" where %s is replaced with the Claude command
  • Function (new): function(cmd, env) that returns either:
    • A string command to execute
    • A table of command arguments

2. Use Cases for Function Support

  • Dynamic terminal selection based on OS or environment
  • Complex command construction with proper escaping
  • Conditional terminal behavior based on environment variables
  • Integration with terminal multiplexers or custom launchers

3. Example Configurations

-- Function returning a different command based on window dimensions
external_terminal_cmd = function(cmd)
  -- Get current Neovim window dimensions
  local width = vim.api.nvim_win_get_width(0)
  local height = vim.api.nvim_win_get_height(0)

  -- Choose split direction based on window aspect ratio
  -- Typical terminal is ~2:1 aspect ratio (80 cols × 40 rows)
  if aspect_ratio > 2.4 then
    return "tmux-run --width 30 " .. cmd -- Horizontal split for wider windows
  else
    return "tmux-run --height 40 " .. cmd -- Vertical split for taller windows
  end
end

-- Function returning different commands per OS
external_terminal_cmd = function(cmd, env)
  if vim.fn.has("mac") == 1 then
    return { "osascript", "-e", string.format('tell app "Terminal" to do script "%s"', cmd) }
  else
    return "alacritty -e " .. cmd
  end
end

-- Function with environment-based logic
external_terminal_cmd = function(cmd, env)
  if env.CLAUDE_CODE_SSE_PORT then
    return string.format("kitty --title 'Claude Code (Port %s)' %s", env.CLAUDE_CODE_SSE_PORT, cmd)
  else
    return "kitty " .. cmd
  end
end

Test Coverage

Added comprehensive test suite in tests/unit/terminal/external_spec.lua covering:

  • String template with %s placeholder
  • Function returning string
  • Function returning table
  • Error handling for invalid configurations
  • Function receiving correct parameters (cmd, env)

Checklist

  • Code follows project conventions
  • Tests added and passing
  • Documentation updated
  • No breaking changes to existing API

@jdurand jdurand force-pushed the feat/external-terminal-cmd-function branch 4 times, most recently from 5b21405 to cc53b36 Compare August 14, 2025 13:01
@jdurand jdurand marked this pull request as ready for review August 14, 2025 13:05
@jdurand jdurand force-pushed the feat/external-terminal-cmd-function branch 7 times, most recently from 97f1a17 to 78433b0 Compare August 14, 2025 19:34
Allow external_terminal_cmd to be either a string template with %s
placeholder
or a function that receives (cmd, env) and returns the command to
execute.
This enables more dynamic terminal command generation based on
environment
or runtime conditions.

Examples:
- String: "alacritty -e %s"
- Function: function(cmd, env) return { "osascript", "-e", ... } end
@jdurand jdurand force-pushed the feat/external-terminal-cmd-function branch from 78433b0 to 8a78ad3 Compare August 14, 2025 19:40
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.

1 participant