pyb-ts
Version:
PYB-CLI - Minimal AI Agent with multi-model support and CLI interface
164 lines (162 loc) • 5.59 kB
JavaScript
import { EOL, platform, homedir } from "os";
import { execFileNoThrow } from "@utils/execFileNoThrow";
import chalk from "chalk";
import { getTheme } from "@utils/theme";
import { env } from "@utils/env";
import { getGlobalConfig, saveGlobalConfig } from "@utils/config";
import { markProjectOnboardingComplete } from "@components/ProjectOnboarding";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { safeParseJSON } from "@utils/json";
import { logError } from "@utils/log";
const terminalSetup = {
type: "local",
name: "terminal-setup",
userFacingName() {
return "terminal-setup";
},
description: "Install Shift+Enter key binding for newlines (iTerm2 and VSCode only)",
isEnabled: platform() === "darwin" && env.terminal === "iTerm.app" || env.terminal === "vscode",
isHidden: false,
async call() {
let result = "";
switch (env.terminal) {
case "iTerm.app":
result = await installBindingsForITerm2();
break;
case "vscode":
result = installBindingsForVSCodeTerminal();
break;
}
const config = getGlobalConfig();
config.shiftEnterKeyBindingInstalled = true;
saveGlobalConfig(config);
markProjectOnboardingComplete();
return result;
}
};
function isShiftEnterKeyBindingInstalled() {
return getGlobalConfig().shiftEnterKeyBindingInstalled === true;
}
function handleHashCommand(interpreted) {
try {
const cwd = process.cwd();
const codeContextPath = join(cwd, "AGENTS.md");
const claudePath = join(cwd, "CLAUDE.md");
const filesToUpdate = [];
filesToUpdate.push({ path: codeContextPath, name: "AGENTS.md" });
try {
readFileSync(claudePath, "utf-8");
filesToUpdate.push({ path: claudePath, name: "CLAUDE.md" });
} catch {
}
const now = /* @__PURE__ */ new Date();
const timezoneMatch = now.toString().match(/\(([A-Z]+)\)/);
const timezone = timezoneMatch ? timezoneMatch[1] : now.toLocaleTimeString("en-us", { timeZoneName: "short" }).split(" ").pop();
const timestamp = interpreted.includes(now.getFullYear().toString()) ? "" : `
_Added on ${now.toLocaleString()} ${timezone}_`;
const updatedFiles = [];
for (const file of filesToUpdate) {
try {
let existingContent = "";
try {
existingContent = readFileSync(file.path, "utf-8").trim();
} catch (error) {
}
const separator = existingContent ? "\n\n" : "";
const newContent = `${existingContent}${separator}${interpreted}${timestamp}`;
writeFileSync(file.path, newContent, "utf-8");
updatedFiles.push(file.name);
} catch (error) {
logError(error);
console.error(
chalk.hex(getTheme().error)(
`Failed to update ${file.name}: ${error.message}`
)
);
}
}
if (updatedFiles.length > 0) {
console.log(
chalk.hex(getTheme().success)(
`Added note to ${updatedFiles.join(" and ")}`
)
);
}
} catch (e) {
logError(e);
console.error(
chalk.hex(getTheme().error)(`Failed to add note: ${e.message}`)
);
}
}
var terminalSetup_default = terminalSetup;
async function installBindingsForITerm2() {
const { code } = await execFileNoThrow("defaults", [
"write",
"com.googlecode.iterm2",
"GlobalKeyMap",
"-dict-add",
"0xd-0x20000-0x24",
`<dict>
<key>Text</key>
<string>\\n</string>
<key>Action</key>
<integer>12</integer>
<key>Version</key>
<integer>1</integer>
<key>Keycode</key>
<integer>13</integer>
<key>Modifiers</key>
<integer>131072</integer>
</dict>`
]);
if (code !== 0) {
throw new Error("Failed to install iTerm2 Shift+Enter key binding");
}
return `${chalk.hex(getTheme().success)(
"Installed iTerm2 Shift+Enter key binding"
)}${EOL}${chalk.dim("See iTerm2 \uFFFD\uFFFD?Preferences \uFFFD\uFFFD?Keys")}${EOL}`;
}
function installBindingsForVSCodeTerminal() {
const vscodeKeybindingsPath = join(
homedir(),
platform() === "win32" ? join("AppData", "Roaming", "Code", "User") : platform() === "darwin" ? join("Library", "Application Support", "Code", "User") : join(".config", "Code", "User"),
"keybindings.json"
);
try {
const content = readFileSync(vscodeKeybindingsPath, "utf-8");
const keybindings = safeParseJSON(content) ?? [];
const existingBinding = keybindings.find(
(binding) => binding.key === "shift+enter" && binding.command === "workbench.action.terminal.sendSequence" && binding.when === "terminalFocus"
);
if (existingBinding) {
return `${chalk.hex(getTheme().warning)(
"Found existing VSCode terminal Shift+Enter key binding. Remove it to continue."
)}${EOL}${chalk.dim(`See ${vscodeKeybindingsPath}`)}${EOL}`;
}
keybindings.push({
key: "shift+enter",
command: "workbench.action.terminal.sendSequence",
args: { text: "\\\r\n" },
when: "terminalFocus"
});
writeFileSync(
vscodeKeybindingsPath,
JSON.stringify(keybindings, null, 4),
"utf-8"
);
return `${chalk.hex(getTheme().success)(
"Installed VSCode terminal Shift+Enter key binding"
)}${EOL}${chalk.dim(`See ${vscodeKeybindingsPath}`)}${EOL}`;
} catch (e) {
logError(e);
throw new Error("Failed to install VSCode terminal Shift+Enter key binding");
}
}
export {
terminalSetup_default as default,
handleHashCommand,
isShiftEnterKeyBindingInstalled
};
//# sourceMappingURL=terminalSetup.js.map