mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-24 17:32:18 +00:00
fix: address Codex review on PR #2975
This commit is contained in:
1 parent
a254acbc75
commit
193e88689a
5 files changed
+167
-40
No files matched your search
@@ -91,6 +91,7 @@ import {
|
||||
} from "./middleClickBehavior";
|
||||
import { handleSerialLineModeInput } from "./serialLineInput";
|
||||
import {
|
||||
doesKittyEncodingPreserveShiftEnter,
|
||||
getShiftEnterSubmittedInput,
|
||||
resolveShiftEnterPayload,
|
||||
shouldSendShiftEnterText,
|
||||
@@ -978,7 +979,11 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
|
||||
|
||||
const handleTerminalInputData = (
|
||||
data: string,
|
||||
options?: { source?: "terminal" | "shift-enter" | "kitty" },
|
||||
options?: {
|
||||
source?: "terminal" | "shift-enter" | "kitty";
|
||||
/** Skip string broadcast when peers will re-resolve from a key chord. */
|
||||
skipBroadcast?: boolean;
|
||||
},
|
||||
) => {
|
||||
// Clipboard paste / typed password while assist is open must dismiss the
|
||||
// hint first. Otherwise Enter is still hijacked for confirmFill and can
|
||||
@@ -1002,6 +1007,7 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
|
||||
if (suppressTerminalBroadcast) suppressNextTerminalDataBroadcast = false;
|
||||
const willBroadcastInput = !sensitive &&
|
||||
inputSource !== "kitty" &&
|
||||
options?.skipBroadcast !== true &&
|
||||
!handlingKittyBroadcast &&
|
||||
!suppressTerminalBroadcast &&
|
||||
!!id && shouldBroadcastTerminalUserInput(term, broadcastDataBeforeSudo, {
|
||||
@@ -1720,42 +1726,46 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
|
||||
return false;
|
||||
}
|
||||
|
||||
// Before baseline Kitty encoding: with protocol enabled but no enhancement
|
||||
// flags, encodeKittyKeyEvent(Shift+Enter) is "\r" while mode stays inactive.
|
||||
// Remap first so alternate-screen TUIs still receive CSI-u Shift+Enter.
|
||||
// Before Kitty encoding that collapses Shift+Enter to bare CR/LF (flags=0,
|
||||
// or non-preserving sets like alternate-key / associated-text alone). Remap
|
||||
// first so alternate-screen TUIs still receive CSI-u Shift+Enter.
|
||||
if (
|
||||
!isKittyKeyboardModeActive(kittyKeyboardMode) &&
|
||||
shouldSendShiftEnterText(e, ctx.terminalSettingsRef.current)
|
||||
shouldSendShiftEnterText(e, ctx.terminalSettingsRef.current) &&
|
||||
!doesKittyEncodingPreserveShiftEnter(kittySequenceForKeyDown)
|
||||
) {
|
||||
const id = ctx.sessionRef.current;
|
||||
if (id) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
const kittyEvent = toKittyKeyboardEvent(e);
|
||||
const shiftEnterPayload = resolveShiftEnterPayload(
|
||||
ctx.terminalSettingsRef.current,
|
||||
{ alternateScreen: term.buffer.active.type === "alternate" },
|
||||
);
|
||||
if (shiftEnterPayload.data) {
|
||||
if (shiftEnterPayload.kind === "key") {
|
||||
const kittyEvent = toKittyKeyboardEvent(e);
|
||||
// Local CSI-u as kitty-sourced input so raw bytes are not broadcast.
|
||||
// Peers re-encode the chord from their own keyboard mode.
|
||||
handleTerminalInputData(shiftEnterPayload.data, { source: "kitty" });
|
||||
const forwarded = broadcastKittyInput({
|
||||
kind: "key",
|
||||
event: kittyEvent,
|
||||
fallbackToLegacy: true,
|
||||
});
|
||||
if (forwarded) {
|
||||
upsertKittyKeyboardForwardedPress(
|
||||
broadcastForwardedKeys,
|
||||
kittyKeyIdentity(e),
|
||||
kittyEvent,
|
||||
forwarded.targetSessionIds,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
handleTerminalInputData(shiftEnterPayload.data, { source: "shift-enter" });
|
||||
// Skip string broadcast: peers resolve Shift+Enter from their own
|
||||
// buffer + keyboard mode via the key chord below.
|
||||
handleTerminalInputData(shiftEnterPayload.data, {
|
||||
source: "shift-enter",
|
||||
skipBroadcast: true,
|
||||
});
|
||||
}
|
||||
const forwarded = broadcastKittyInput({
|
||||
kind: "key",
|
||||
event: kittyEvent,
|
||||
fallbackToLegacy: true,
|
||||
});
|
||||
if (forwarded) {
|
||||
upsertKittyKeyboardForwardedPress(
|
||||
broadcastForwardedKeys,
|
||||
kittyKeyIdentity(e),
|
||||
kittyEvent,
|
||||
forwarded.targetSessionIds,
|
||||
);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
@@ -2010,6 +2020,8 @@ export const createXTermRuntime = (ctx: CreateXTermRuntimeContext): XTermRuntime
|
||||
applicationCursorMode: term.modes.applicationCursorKeysMode,
|
||||
encodedKeys: broadcastEncodedKeys,
|
||||
legacySuppressedKeys: broadcastLegacySuppressedKeys,
|
||||
alternateScreen: term.buffer.active.type === "alternate",
|
||||
shiftEnterSettings: ctx.terminalSettingsRef.current,
|
||||
}),
|
||||
getSessionId: () => ctx.sessionRef.current,
|
||||
isSensitiveInput: () => ctx.passwordPromptActiveRef?.current === true,
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
encodeKittyKeyEvent,
|
||||
setKittyKeyboardModeFlags,
|
||||
} from "./kittyKeyboardProtocol";
|
||||
import { SHIFT_ENTER_CSI_U_SEQUENCE } from "./shiftEnterText";
|
||||
|
||||
test("routes normalized Kitty input to the target session only", () => {
|
||||
const received: KittyKeyboardBroadcastInput[] = [];
|
||||
@@ -520,6 +521,59 @@ test("enhanced sources can fall back to reliable legacy data for plain targets",
|
||||
}
|
||||
});
|
||||
|
||||
test("Shift+Enter broadcast remaps from each target buffer when Kitty collapses the chord", () => {
|
||||
const alternateOnly = createKittyKeyboardModeState();
|
||||
setKittyKeyboardModeFlags(alternateOnly, 0b00100);
|
||||
const shiftEnter = {
|
||||
kind: "key" as const,
|
||||
fallbackToLegacy: true,
|
||||
event: {
|
||||
type: "keydown" as const,
|
||||
key: "Enter",
|
||||
code: "Enter",
|
||||
shiftKey: true,
|
||||
},
|
||||
};
|
||||
|
||||
assert.deepEqual(resolveKittyKeyboardBroadcastInput(shiftEnter, {
|
||||
kittyProtocolEnabled: true,
|
||||
kittyMode: alternateOnly,
|
||||
applicationCursorMode: false,
|
||||
encodedKeys: new Set<string>(),
|
||||
alternateScreen: true,
|
||||
}), {
|
||||
data: SHIFT_ENTER_CSI_U_SEQUENCE,
|
||||
kittyEncoded: true,
|
||||
urgentInterrupt: false,
|
||||
});
|
||||
|
||||
assert.deepEqual(resolveKittyKeyboardBroadcastInput(shiftEnter, {
|
||||
kittyProtocolEnabled: true,
|
||||
kittyMode: createKittyKeyboardModeState(),
|
||||
applicationCursorMode: false,
|
||||
encodedKeys: new Set<string>(),
|
||||
alternateScreen: false,
|
||||
}), {
|
||||
data: "\n",
|
||||
kittyEncoded: false,
|
||||
urgentInterrupt: false,
|
||||
});
|
||||
|
||||
const disambiguate = createKittyKeyboardModeState();
|
||||
setKittyKeyboardModeFlags(disambiguate, 0b00001);
|
||||
assert.deepEqual(resolveKittyKeyboardBroadcastInput(shiftEnter, {
|
||||
kittyProtocolEnabled: true,
|
||||
kittyMode: disambiguate,
|
||||
applicationCursorMode: false,
|
||||
encodedKeys: new Set<string>(),
|
||||
alternateScreen: true,
|
||||
}), {
|
||||
data: SHIFT_ENTER_CSI_U_SEQUENCE,
|
||||
kittyEncoded: true,
|
||||
urgentInterrupt: false,
|
||||
});
|
||||
});
|
||||
|
||||
test("plain Kitty targets preserve modified non-ASCII keys without a legacy fallback", () => {
|
||||
const plain = createKittyKeyboardModeState();
|
||||
assert.deepEqual(resolveKittyKeyboardBroadcastInput({
|
||||
|
||||
@@ -6,6 +6,12 @@ import {
|
||||
type KittyKeyboardEvent,
|
||||
type KittyKeyboardModeState,
|
||||
} from "./kittyKeyboardProtocol";
|
||||
import type { TerminalSettings } from "../../../domain/models";
|
||||
import {
|
||||
isBareShiftEnterLineEnding,
|
||||
resolveShiftEnterPayload,
|
||||
shouldSendShiftEnterText,
|
||||
} from "./shiftEnterText";
|
||||
|
||||
export type KittyKeyboardBroadcastInput =
|
||||
| {
|
||||
@@ -190,15 +196,46 @@ export const flushKittyKeyboardBroadcastReleases = (
|
||||
releases.clear();
|
||||
};
|
||||
|
||||
type ResolveKittyKeyboardBroadcastOptions = {
|
||||
kittyProtocolEnabled: boolean;
|
||||
kittyMode: KittyKeyboardModeState;
|
||||
applicationCursorMode: boolean;
|
||||
encodedKeys: Set<string>;
|
||||
legacySuppressedKeys?: Set<string>;
|
||||
/** Target buffer: remap Shift+Enter per peer, not from the broadcast source. */
|
||||
alternateScreen?: boolean;
|
||||
shiftEnterSettings?: Pick<
|
||||
TerminalSettings,
|
||||
"shiftEnterNewlineEnabled" | "shiftEnterNewlineText"
|
||||
>;
|
||||
};
|
||||
|
||||
const resolveShiftEnterBroadcastPayload = (
|
||||
input: Extract<KittyKeyboardBroadcastInput, { kind: "key" }>,
|
||||
options: ResolveKittyKeyboardBroadcastOptions,
|
||||
candidate: string | null,
|
||||
): ResolvedKittyKeyboardBroadcastInput | null => {
|
||||
if (
|
||||
!shouldSendShiftEnterText(input.event, options.shiftEnterSettings) ||
|
||||
candidate === null ||
|
||||
!isBareShiftEnterLineEnding(candidate)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
const payload = resolveShiftEnterPayload(options.shiftEnterSettings, {
|
||||
alternateScreen: options.alternateScreen === true,
|
||||
});
|
||||
if (!payload.data) return null;
|
||||
return {
|
||||
data: payload.data,
|
||||
kittyEncoded: payload.kind === "key",
|
||||
urgentInterrupt: false,
|
||||
};
|
||||
};
|
||||
|
||||
export const resolveKittyKeyboardBroadcastInput = (
|
||||
input: KittyKeyboardBroadcastInput,
|
||||
options: {
|
||||
kittyProtocolEnabled: boolean;
|
||||
kittyMode: KittyKeyboardModeState;
|
||||
applicationCursorMode: boolean;
|
||||
encodedKeys: Set<string>;
|
||||
legacySuppressedKeys?: Set<string>;
|
||||
},
|
||||
options: ResolveKittyKeyboardBroadcastOptions,
|
||||
): ResolvedKittyKeyboardBroadcastInput | null => {
|
||||
if (input.kind === "text") {
|
||||
if (options.kittyProtocolEnabled && shouldEncodeKittyCompositionText(options.kittyMode)) {
|
||||
@@ -237,6 +274,8 @@ export const resolveKittyKeyboardBroadcastInput = (
|
||||
options.encodedKeys.add(identity);
|
||||
legacySuppressedKeys.add(identity);
|
||||
}
|
||||
const remapped = resolveShiftEnterBroadcastPayload(input, options, encoded);
|
||||
if (remapped) return remapped;
|
||||
return { data: encoded, kittyEncoded: true, urgentInterrupt: false };
|
||||
}
|
||||
if (!input.fallbackToLegacy || input.event.type === "keyup") return null;
|
||||
@@ -244,6 +283,8 @@ export const resolveKittyKeyboardBroadcastInput = (
|
||||
if (!legacy) return null;
|
||||
options.encodedKeys.add(identity);
|
||||
legacySuppressedKeys.add(identity);
|
||||
const remapped = resolveShiftEnterBroadcastPayload(input, options, legacy);
|
||||
if (remapped) return remapped;
|
||||
return {
|
||||
data: legacy,
|
||||
kittyEncoded: false,
|
||||
@@ -252,13 +293,7 @@ export const resolveKittyKeyboardBroadcastInput = (
|
||||
};
|
||||
|
||||
export const createKittyKeyboardBroadcastHandler = (options: {
|
||||
resolveOptions: () => {
|
||||
kittyProtocolEnabled: boolean;
|
||||
kittyMode: KittyKeyboardModeState;
|
||||
applicationCursorMode: boolean;
|
||||
encodedKeys: Set<string>;
|
||||
legacySuppressedKeys?: Set<string>;
|
||||
};
|
||||
resolveOptions: () => ResolveKittyKeyboardBroadcastOptions;
|
||||
getSessionId: () => string | null;
|
||||
isSensitiveInput?: () => boolean;
|
||||
isConnected: () => boolean;
|
||||
|
||||
@@ -4,6 +4,7 @@ import { readFileSync } from "node:fs";
|
||||
|
||||
import {
|
||||
decodeTerminalTextEscapes,
|
||||
doesKittyEncodingPreserveShiftEnter,
|
||||
getShiftEnterSubmittedInput,
|
||||
isBareShiftEnterLineEnding,
|
||||
isShiftEnterLineContinuationText,
|
||||
@@ -91,6 +92,14 @@ test("bare line-ending Shift+Enter text is detected for TUI passthrough", () =>
|
||||
assert.equal(isBareShiftEnterLineEnding(""), false);
|
||||
});
|
||||
|
||||
test("Kitty encodings that collapse Shift+Enter to CR/LF do not preserve the chord", () => {
|
||||
assert.equal(doesKittyEncodingPreserveShiftEnter(null), false);
|
||||
assert.equal(doesKittyEncodingPreserveShiftEnter(""), false);
|
||||
assert.equal(doesKittyEncodingPreserveShiftEnter("\r"), false);
|
||||
assert.equal(doesKittyEncodingPreserveShiftEnter("\n"), false);
|
||||
assert.equal(doesKittyEncodingPreserveShiftEnter(SHIFT_ENTER_CSI_U_SEQUENCE), true);
|
||||
});
|
||||
|
||||
test("main-buffer Shift+Enter keeps configured send text", () => {
|
||||
assert.deepEqual(resolveShiftEnterPayload(), {
|
||||
kind: "text",
|
||||
@@ -137,16 +146,20 @@ test("runtime routes Shift+Enter text through the shared input handler", () => {
|
||||
|
||||
assert.match(
|
||||
source,
|
||||
/const handleTerminalInputData = \(\s+data: string,\s+options\?: \{ source\?: "terminal" \| "shift-enter" \| "kitty" \},\s+\) => \{/s,
|
||||
/const handleTerminalInputData = \(\s+data: string,\s+options\?: \{\s+source\?: "terminal" \| "shift-enter" \| "kitty";\s+[\s\S]*?skipBroadcast\?: boolean;\s+\},\s+\) => \{/s,
|
||||
);
|
||||
// Remap must run before baseline Kitty encoding consumes Shift+Enter as "\r".
|
||||
// Remap when Kitty encoding does not preserve Shift+Enter (not merely flags===0).
|
||||
assert.match(
|
||||
source,
|
||||
/if \(\s*!isKittyKeyboardModeActive\(kittyKeyboardMode\) &&\s*shouldSendShiftEnterText\([\s\S]*?\) \{\s*const id = ctx\.sessionRef\.current;[\s\S]*?const shiftEnterPayload = resolveShiftEnterPayload\([\s\S]*?\)[\s\S]*?\}\s*if \(kittySequenceForKeyDown\)/s,
|
||||
/if \(\s*shouldSendShiftEnterText\([\s\S]*?\) &&\s*!doesKittyEncodingPreserveShiftEnter\(kittySequenceForKeyDown\)\s*\) \{[\s\S]*?const shiftEnterPayload = resolveShiftEnterPayload\([\s\S]*?\)[\s\S]*?\}\s*if \(kittySequenceForKeyDown\)/s,
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/if \(shiftEnterPayload\.kind === "key"\) \{\s*const kittyEvent = toKittyKeyboardEvent\(e\);[\s\S]*?handleTerminalInputData\(shiftEnterPayload\.data, \{ source: "kitty" \}\);[\s\S]*?broadcastKittyInput\(\{\s*kind: "key",\s*event: kittyEvent,\s*fallbackToLegacy: true,\s*\}\);[\s\S]*?\} else \{\s*handleTerminalInputData\(shiftEnterPayload\.data, \{ source: "shift-enter" \}\);/s,
|
||||
/if \(shiftEnterPayload\.kind === "key"\) \{[\s\S]*?handleTerminalInputData\(shiftEnterPayload\.data, \{ source: "kitty" \}\);[\s\S]*?\} else \{[\s\S]*?handleTerminalInputData\(shiftEnterPayload\.data, \{\s*source: "shift-enter",\s*skipBroadcast: true,\s*\}\);[\s\S]*?\}\s*const forwarded = broadcastKittyInput\(\{\s*kind: "key",\s*event: kittyEvent,\s*fallbackToLegacy: true,\s*\}\);/s,
|
||||
);
|
||||
assert.match(
|
||||
source,
|
||||
/alternateScreen: term\.buffer\.active\.type === "alternate",\s*shiftEnterSettings: ctx\.terminalSettingsRef\.current,/s,
|
||||
);
|
||||
assert.match(source, /getShiftEnterSubmittedInput\(data\)/);
|
||||
assert.match(source, /inputSource !== "shift-enter"/);
|
||||
|
||||
@@ -78,6 +78,19 @@ export function isBareShiftEnterLineEnding(text: string): boolean {
|
||||
return text === "\n" || text === "\r" || text === "\r\n";
|
||||
}
|
||||
|
||||
/**
|
||||
* True when Kitty encoding already keeps Shift+Enter distinct from plain Enter.
|
||||
* Non-preserving flag sets (e.g. alternate-key or associated-text alone) still
|
||||
* encode Shift+Enter as a bare CR/LF, so the alternate-screen remap must run.
|
||||
*/
|
||||
export function doesKittyEncodingPreserveShiftEnter(
|
||||
encoded: string | null | undefined,
|
||||
): boolean {
|
||||
return typeof encoded === "string"
|
||||
&& encoded.length > 0
|
||||
&& !isBareShiftEnterLineEnding(encoded);
|
||||
}
|
||||
|
||||
export type ShiftEnterPayload =
|
||||
| { kind: "text"; data: string }
|
||||
| { kind: "key"; data: string };
|
||||
|
||||
Reference in new issue
Block a user