Skip to content

feat: add shell completions and improve switch UX#111

Open
behnambagheri wants to merge 3 commits into
Loongphy:mainfrom
behnambagheri:feat/fish-completion-cx-switch-ux
Open

feat: add shell completions and improve switch UX#111
behnambagheri wants to merge 3 commits into
Loongphy:mainfrom
behnambagheri:feat/fish-completion-cx-switch-ux

Conversation

@behnambagheri
Copy link
Copy Markdown

@behnambagheri behnambagheri commented May 20, 2026

Summary

  • add shell completions for Bash, Zsh, and Fish via codex-auth completion <shell>
  • improve switch completion suggestions across shells and keep the switch TUI input handling fixes
  • remove the cx alias from the package and docs

Details

  • add completion bash, completion zsh, and completion fish
  • keep Fish switch suggestions dynamic and sourced from stored accounts
  • add Bash switch suggestions for both display numbers and emails
  • add Zsh switch suggestions and tune the presentation for command completion and switch completion
  • remove the cx npm binary alias and related docs/examples
  • keep the earlier switch UX fixes for Esc exit handling and arrow-key navigation
  • add screenshots requested for review directly in this PR

Validation

  • HOME=/tmp/codex-auth-pr-update ZIG_GLOBAL_CACHE_DIR=/tmp/codex-auth-pr-update/zig-global-cache ZIG_LOCAL_CACHE_DIR=/tmp/codex-auth-pr-update/zig-local-cache zig build run -- list
  • manual completion verification in Fish, Bash, and Zsh

Screenshots

Fish command completion

Fish command completion

Fish switch suggestions

Fish switch suggestions

Bash switch suggestions

Bash switch suggestions

Zsh command completion

Zsh command completion

Zsh switch suggestions

Zsh switch suggestions

@Loongphy
Copy link
Copy Markdown
Owner

Thanks, this looks great overall!

A few requests before we move forward:

  1. Could we also support Bash and Zsh completions in addition to Fish? It would be nice to keep completion support consistent across the common shells.
  2. Please remove the cx alias from this PR. It feels a bit too opinionated for the project itself. If you prefer using cx, you can still define that alias locally in Fish.
  3. Could you add a few screenshots to the PR description to demonstrate the implementation? For example, screenshots showing command completion and the switch account suggestions would be very helpful for review.

@behnambagheri behnambagheri changed the title feat: add Fish completions and improve switch UX feat: add shell completions and improve switch UX May 20, 2026
@behnambagheri
Copy link
Copy Markdown
Author

Thanks for the review.

I addressed the requested follow-up changes in this PR:

  • added Bash and Zsh completion support alongside Fish via codex-auth completion <shell>
  • removed the cx alias from the package and docs
  • added screenshots to the PR description for Fish, Bash, and Zsh completion behavior, including switch suggestions

I also rechecked the completion flows after the shell-specific updates and pushed the follow-up changes to this same PR.

@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 22, 2026

Greptile Summary

This PR adds codex-auth completion <bash|zsh|fish> to emit shell completion scripts, introduces a dynamic completion query switch sub-command for live account suggestions, and refines the switch TUI's keyboard navigation (clearing the typed-number buffer on any navigation keypress, removing the kitty keyboard-enhancement protocol, fixing Esc to quit correctly).

  • Shell completions (src/cli/completion.zig): New Bash, Zsh, and Fish scripts are generated at runtime; Fish and Zsh use a completion query switch call to populate account suggestions dynamically.
  • TUI UX fixes (src/cli/tui.zig, live_switch.zig, picker_switch.zig): Standalone Esc now always quits; number_len is reset unconditionally on navigation, including at list boundaries.
  • Docs and JS wrapper: README and docs/commands/completion.md document the install flows; bin/codex-auth.js gains a resolveLocalBinary fallback for local dev builds.

Confidence Score: 5/5

The change is additive and well-tested; the TUI fixes are conservative and covered by new unit tests.

The completion feature is entirely new output code with no shared state; the TUI and switch-picker changes are small, clearly scoped, and exercised by the updated test suite. The only gaps are cosmetic completion omissions (missing app across all shells) that do not break existing behavior.

src/cli/completion.zig — the app omission in the Fish guard and the Zsh switch completions offering only emails are the items most worth a second look.

Important Files Changed

Filename Overview
src/cli/completion.zig New file generating Bash, Zsh, and Fish completion scripts; Fish __fish_codex_auth_needs_command omits app, causing spurious command completions after codex-auth app; Zsh switch completions only offer emails where Bash offers both display numbers and emails.
src/cli/commands/completion.zig New parser for the completion command and internal completion query sub-dispatch; handles help flags, shell names, and unknown-argument errors cleanly.
src/cli/tui.zig Removes kitty keyboard-enhancement protocol sequences and fixes Esc handling so a standalone \x1b byte followed by a timeout is treated as quit rather than ignore.
src/cli/live_switch.zig Moves number_len = 0 outside navigation success guards so the typed-number input buffer is cleared on every navigation keypress, including at list boundaries.
src/cli/picker_switch.zig Same number_len boundary-reset fix as live_switch.zig, applied to both selectInteractiveFromIndices and selectInteractive.
src/workflows/completion.zig Thin dispatch layer; safely gates codex_home.? unwrap behind the needs_codex_home check in root.zig, so the unwrap is always safe.
bin/codex-auth.js Adds resolveLocalBinary fallback that looks for a local zig-out/bin/codex-auth build, invoked inside the catch block when the platform package is missing.
tests/cli_behavior_test.zig Adds comprehensive tests for completion parsing (all shells, missing shell, unknown shell, query target) and completion script rendering for all three shells.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[codex-auth completion] --> B{shell arg?}
    B -->|bash| C[writeBashCompletion]
    B -->|zsh| D[writeZshCompletion]
    B -->|fish| E[writeFishCompletion]
    B -->|query| F[parseQuery]
    F --> G{target?}
    G -->|switch| H[writeSwitchQueryCompletion]
    H --> I[loadRegistry]
    I --> J[buildDisplayRows]
    J --> K[emit number TAB email per account]
    C --> L[stdout — static Bash script]
    D --> M[stdout — static Zsh script]
    E --> N[stdout — static Fish script]
    K --> O[stdout — TSV for shell completion]
Loading

Fix All in Codex

Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
src/cli/completion.zig:115-124
**Zsh switch completions only offer emails, not display numbers**

`_codex_auth_switch_queries` in Zsh stores only the `$description` column (email) and discards `$value` (the display number). Bash includes both columns via `cut -f1` and `cut -f2`, so a Bash user can type `01<TAB>` while a Zsh user cannot. Consider collecting both `nums` and `emails` arrays and passing them both to `compadd`: `compadd -Q -l -- "${nums[@]}" "${emails[@]}"`.

### Issue 2 of 2
src/cli/completion.zig:215
**`app` missing from Fish `__fish_codex_auth_needs_command` guard**

`app` is absent from the sentinel list in `__fish_codex_auth_needs_command`. Because the condition is `not __fish_seen_subcommand_from …`, after a user types `codex-auth app ` and presses Tab, the guard still evaluates to true and Fish presents every top-level command (help, list, login, …) as completions. Additionally, there is no `-a app` entry in the generated script, so `app` cannot be reached via Tab at all. Adding `app` to the `__fish_seen_subcommand_from` list and inserting the corresponding `complete … -n '__fish_codex_auth_needs_command' -a app` entry would fix both gaps. The same omission is present in the Bash `commands` string (line 53) and Zsh `_values 'command'` block (lines 145–156).

Reviews (2): Last reviewed commit: "Merge branch 'main' into feat/fish-compl..." | Re-trigger Greptile

Comment thread src/cli/completion.zig
Comment on lines +80 to +82
\\ switch)
\\ COMPREPLY=( $(compgen -W "--live --api --skip-api $( _codex_auth_switch_queries | cut -f1 ) $( _codex_auth_switch_queries | cut -f2 )" -- "$cur") )
\\ ;;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Bash switch completion spawns the binary twice on every Tab press. The switch case calls _codex_auth_switch_queries once to pipe through cut -f1 and a second time for cut -f2, so codex-auth completion query switch is executed twice per completion attempt. Capturing the output once avoids the double spawn.

Suggested change
\\ switch)
\\ COMPREPLY=( $(compgen -W "--live --api --skip-api $( _codex_auth_switch_queries | cut -f1 ) $( _codex_auth_switch_queries | cut -f2 )" -- "$cur") )
\\ ;;
\\ switch)
\\ local _sw_targets
\\ _sw_targets="$(_codex_auth_switch_queries)"
\\ COMPREPLY=( $(compgen -W "--live --api --skip-api $(echo "$_sw_targets" | cut -f1) $(echo "$_sw_targets" | cut -f2)" -- "$cur") )
\\ ;;
Prompt To Fix With AI
This is a comment left during a code review.
Path: src/cli/completion.zig
Line: 80-82

Comment:
Bash switch completion spawns the binary twice on every Tab press. The `switch` case calls `_codex_auth_switch_queries` once to pipe through `cut -f1` and a second time for `cut -f2`, so `codex-auth completion query switch` is executed twice per completion attempt. Capturing the output once avoids the double spawn.

```suggestion
        \\        switch)
        \\            local _sw_targets
        \\            _sw_targets="$(_codex_auth_switch_queries)"
        \\            COMPREPLY=( $(compgen -W "--live --api --skip-api $(echo "$_sw_targets" | cut -f1) $(echo "$_sw_targets" | cut -f2)" -- "$cur") )
        \\            ;;
```

How can I resolve this? If you propose a fix, please make it concise.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 22, 2026

Open in StackBlitz

@loongphy/codex-auth-darwin-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-arm64@2ae55f1

@loongphy/codex-auth-darwin-x64

npx https://pkg.pr.new/@loongphy/codex-auth-darwin-x64@2ae55f1

@loongphy/codex-auth-linux-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-linux-arm64@2ae55f1

@loongphy/codex-auth-linux-x64

npx https://pkg.pr.new/@loongphy/codex-auth-linux-x64@2ae55f1

@loongphy/codex-auth-win32-arm64

npx https://pkg.pr.new/@loongphy/codex-auth-win32-arm64@2ae55f1

@loongphy/codex-auth-win32-x64

npx https://pkg.pr.new/@loongphy/codex-auth-win32-x64@2ae55f1

@loongphy/codex-auth

npx https://pkg.pr.new/@loongphy/codex-auth@2ae55f1

commit: 2ae55f1

@Loongphy
Copy link
Copy Markdown
Owner

@greptileai

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.

2 participants