fix(input): strip leading ! when entering bash mode
The PromptInput onChange handler had two branches for entering bash mode: a single-char path that just toggled the mode and a multi-char paste path that also stripped the leading `!` from the buffer. The single-char path returned without stripping, so typing a bare `!` into empty input switched modes but left the literal `!` visible. Consolidated both paths through a new pure helper `detectModeEntry` that returns the new mode plus the stripped buffer value, so there is no longer a branch where the mode character can leak into the buffer. Fixes #662
This commit is contained in:
@@ -111,7 +111,7 @@ import { BackgroundTasksDialog } from '../tasks/BackgroundTasksDialog.js';
|
|||||||
import { shouldHideTasksFooter } from '../tasks/taskStatusUtils.js';
|
import { shouldHideTasksFooter } from '../tasks/taskStatusUtils.js';
|
||||||
import { TeamsDialog } from '../teams/TeamsDialog.js';
|
import { TeamsDialog } from '../teams/TeamsDialog.js';
|
||||||
import VimTextInput from '../VimTextInput.js';
|
import VimTextInput from '../VimTextInput.js';
|
||||||
import { getModeFromInput, getValueFromInput } from './inputModes.js';
|
import { detectModeEntry, getModeFromInput, getValueFromInput } from './inputModes.js';
|
||||||
import { FOOTER_TEMPORARY_STATUS_TIMEOUT, Notifications } from './Notifications.js';
|
import { FOOTER_TEMPORARY_STATUS_TIMEOUT, Notifications } from './Notifications.js';
|
||||||
import PromptInputFooter from './PromptInputFooter.js';
|
import PromptInputFooter from './PromptInputFooter.js';
|
||||||
import type { SuggestionItem } from './PromptInputFooterSuggestions.js';
|
import type { SuggestionItem } from './PromptInputFooterSuggestions.js';
|
||||||
@@ -878,24 +878,22 @@ function PromptInput({
|
|||||||
abortPromptSuggestion();
|
abortPromptSuggestion();
|
||||||
abortSpeculation(setAppState);
|
abortSpeculation(setAppState);
|
||||||
|
|
||||||
// Check if this is a single character insertion at the start
|
// Strip the mode character from the buffer when entering bash mode — the
|
||||||
const isSingleCharInsertion = value.length === input.length + 1;
|
// mode itself is shown via the prompt prefix in the UI. Without this,
|
||||||
const insertedAtStart = cursorOffset === 0;
|
// typing `!` into empty input would enter bash mode but leave the literal
|
||||||
const mode = getModeFromInput(value);
|
// `!` in the buffer (issue #662).
|
||||||
if (insertedAtStart && mode !== 'prompt') {
|
const modeEntry = detectModeEntry({
|
||||||
if (isSingleCharInsertion) {
|
value,
|
||||||
onModeChange(mode);
|
prevInputLength: input.length,
|
||||||
return;
|
cursorOffset,
|
||||||
}
|
});
|
||||||
// Multi-char insertion into empty input (e.g. tab-accepting "! gcloud auth login")
|
if (modeEntry) {
|
||||||
if (input.length === 0) {
|
onModeChange(modeEntry.mode);
|
||||||
onModeChange(mode);
|
const cleaned = modeEntry.strippedValue.replaceAll('\t', ' ');
|
||||||
const valueWithoutMode = getValueFromInput(value).replaceAll('\t', ' ');
|
pushToBuffer(input, cursorOffset, pastedContents);
|
||||||
pushToBuffer(input, cursorOffset, pastedContents);
|
trackAndSetInput(cleaned);
|
||||||
trackAndSetInput(valueWithoutMode);
|
setCursorOffset(cleaned.length);
|
||||||
setCursorOffset(valueWithoutMode.length);
|
return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const processedValue = value.replaceAll('\t', ' ');
|
const processedValue = value.replaceAll('\t', ' ');
|
||||||
|
|
||||||
|
|||||||
104
src/components/PromptInput/inputModes.test.ts
Normal file
104
src/components/PromptInput/inputModes.test.ts
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
import { describe, expect, it } from 'bun:test'
|
||||||
|
import {
|
||||||
|
detectModeEntry,
|
||||||
|
getModeFromInput,
|
||||||
|
getValueFromInput,
|
||||||
|
isInputModeCharacter,
|
||||||
|
prependModeCharacterToInput,
|
||||||
|
} from './inputModes.js'
|
||||||
|
|
||||||
|
describe('inputModes', () => {
|
||||||
|
describe('getModeFromInput', () => {
|
||||||
|
it('returns bash mode for input starting with !', () => {
|
||||||
|
expect(getModeFromInput('!')).toBe('bash')
|
||||||
|
expect(getModeFromInput('!ls')).toBe('bash')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns prompt mode for non-bash input', () => {
|
||||||
|
expect(getModeFromInput('')).toBe('prompt')
|
||||||
|
expect(getModeFromInput('hello')).toBe('prompt')
|
||||||
|
expect(getModeFromInput(' !')).toBe('prompt')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('getValueFromInput', () => {
|
||||||
|
it('strips the leading ! when entering bash mode', () => {
|
||||||
|
expect(getValueFromInput('!')).toBe('')
|
||||||
|
expect(getValueFromInput('!ls -la')).toBe('ls -la')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns input unchanged in prompt mode', () => {
|
||||||
|
expect(getValueFromInput('')).toBe('')
|
||||||
|
expect(getValueFromInput('hello')).toBe('hello')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('isInputModeCharacter', () => {
|
||||||
|
it('returns true only for the bare ! character', () => {
|
||||||
|
expect(isInputModeCharacter('!')).toBe(true)
|
||||||
|
expect(isInputModeCharacter('!ls')).toBe(false)
|
||||||
|
expect(isInputModeCharacter('')).toBe(false)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('prependModeCharacterToInput', () => {
|
||||||
|
it('prepends ! when mode is bash', () => {
|
||||||
|
expect(prependModeCharacterToInput('ls', 'bash')).toBe('!ls')
|
||||||
|
expect(prependModeCharacterToInput('', 'bash')).toBe('!')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns input unchanged in prompt mode', () => {
|
||||||
|
expect(prependModeCharacterToInput('hello', 'prompt')).toBe('hello')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('detectModeEntry', () => {
|
||||||
|
// Regression for #662 — typing `!` into empty input must switch to bash
|
||||||
|
// mode AND yield an empty stripped buffer. Before the fix the single-char
|
||||||
|
// path returned without stripping, leaving `!` visible in the buffer.
|
||||||
|
it('strips the mode character when typing ! into empty input', () => {
|
||||||
|
expect(
|
||||||
|
detectModeEntry({ value: '!', prevInputLength: 0, cursorOffset: 0 }),
|
||||||
|
).toEqual({ mode: 'bash', strippedValue: '' })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('strips the mode character when pasting !cmd into empty input', () => {
|
||||||
|
expect(
|
||||||
|
detectModeEntry({ value: '!ls -la', prevInputLength: 0, cursorOffset: 0 }),
|
||||||
|
).toEqual({ mode: 'bash', strippedValue: 'ls -la' })
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns null when the cursor is not at the start', () => {
|
||||||
|
expect(
|
||||||
|
detectModeEntry({ value: '!', prevInputLength: 0, cursorOffset: 1 }),
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns null when the value does not start with !', () => {
|
||||||
|
expect(
|
||||||
|
detectModeEntry({ value: 'hello', prevInputLength: 0, cursorOffset: 0 }),
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns null when typing ! after existing text', () => {
|
||||||
|
// value="ab!" with prevInputLength=2 is a single-char insertion but does
|
||||||
|
// not start with ! — getModeFromInput returns 'prompt'.
|
||||||
|
expect(
|
||||||
|
detectModeEntry({ value: 'ab!', prevInputLength: 2, cursorOffset: 0 }),
|
||||||
|
).toBeNull()
|
||||||
|
})
|
||||||
|
|
||||||
|
it('returns null when prepending ! to non-empty existing text', () => {
|
||||||
|
// Single-char insertion at start that produces "!ab" from "ab" — value
|
||||||
|
// length is 3, prevInputLength is 2, so isSingleCharInsertion is true
|
||||||
|
// and isMultiCharIntoEmpty is false. We accept the mode change here so
|
||||||
|
// that typing ! at the start of existing text still toggles mode.
|
||||||
|
const result = detectModeEntry({
|
||||||
|
value: '!ab',
|
||||||
|
prevInputLength: 2,
|
||||||
|
cursorOffset: 0,
|
||||||
|
})
|
||||||
|
expect(result).toEqual({ mode: 'bash', strippedValue: 'ab' })
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -31,3 +31,30 @@ export function getValueFromInput(input: string): string {
|
|||||||
export function isInputModeCharacter(input: string): boolean {
|
export function isInputModeCharacter(input: string): boolean {
|
||||||
return input === '!'
|
return input === '!'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type ModeEntryDecision = {
|
||||||
|
mode: HistoryMode
|
||||||
|
strippedValue: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decide whether an onChange `value` should switch the input mode (e.g.
|
||||||
|
* `prompt` → `bash`) and what the stripped buffer value should be.
|
||||||
|
*
|
||||||
|
* Returns null when no mode change applies. Returns a decision otherwise so
|
||||||
|
* callers run a single update path — no separate single-char vs multi-char
|
||||||
|
* branches that can drift apart.
|
||||||
|
*/
|
||||||
|
export function detectModeEntry(args: {
|
||||||
|
value: string
|
||||||
|
prevInputLength: number
|
||||||
|
cursorOffset: number
|
||||||
|
}): ModeEntryDecision | null {
|
||||||
|
if (args.cursorOffset !== 0) return null
|
||||||
|
const mode = getModeFromInput(args.value)
|
||||||
|
if (mode === 'prompt') return null
|
||||||
|
const isSingleCharInsertion = args.value.length === args.prevInputLength + 1
|
||||||
|
const isMultiCharIntoEmpty = args.prevInputLength === 0
|
||||||
|
if (!isSingleCharInsertion && !isMultiCharIntoEmpty) return null
|
||||||
|
return { mode, strippedValue: getValueFromInput(args.value) }
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user