Compare commits
19 Commits
main
..
ab9a41ab83
| Author | SHA1 | Date | |
|---|---|---|---|
| ab9a41ab83 | |||
| 724d4d3b6b | |||
| 8ff481f630 | |||
| b9de1267e3 | |||
| 4572605f33 | |||
| 4b384a7581 | |||
| 8b2babfa5d | |||
| e69b08ac01 | |||
| c0c3dfce7d | |||
| 8b21ab8d4a | |||
| 42e0fafaab | |||
| d0506381f5 | |||
| 14df49b110 | |||
| 962b76d20f | |||
| 12e71a0af7 | |||
| e1354e8651 | |||
| 7e662b25cc | |||
| 5bb2dc8e8a | |||
| 1dbfac7985 |
@@ -118,33 +118,23 @@ git diff | gai suggest branch
|
||||
|
||||
### Interactive Menu
|
||||
|
||||
Run `gai` without arguments to open the mole-style interactive menu:
|
||||
|
||||
```
|
||||
gai v0.1.3
|
||||
AI-powered git helper for commits, PRs, reviews, and changelogs
|
||||
────────────────────────────────────────────────────────────────────────
|
||||
$ gai
|
||||
|
||||
CREATE
|
||||
│ › 1 Commit Generate AI commit message
|
||||
2 PR Create a PR with AI-generated title
|
||||
3 Amend Amend last commit with AI message
|
||||
gai
|
||||
AI-powered git helper — choose a workflow
|
||||
↑/↓ navigate · enter/space select · ctrl+c cancel
|
||||
|
||||
INSPECT
|
||||
4 Explain Explain staged changes in plain language
|
||||
5 Review AI code review of staged changes
|
||||
6 Changelog Generate changelog from commits
|
||||
7 Suggest Suggest branch name or commit type
|
||||
|
||||
PROJECT
|
||||
8 Config Configure API settings
|
||||
|
||||
────────────────────────────────────────────────────────────────────────
|
||||
↑/↓ navigate enter run 1-8 jump h help v version q quit
|
||||
❯ ● commit Generate AI commit message
|
||||
○ pr Create a PR with AI-generated title
|
||||
○ explain Explain staged changes in plain language
|
||||
○ review AI code review of staged changes
|
||||
○ changelog Generate changelog from commits
|
||||
○ suggest Suggest branch name or commit type
|
||||
○ amend Amend last commit with AI message
|
||||
○ config Configure API settings
|
||||
```
|
||||
|
||||
Number keys `1`–`8` jump directly to the corresponding action. After a command finishes, press Enter to return to the menu.
|
||||
|
||||
### Command Examples
|
||||
|
||||
#### Commit
|
||||
|
||||
@@ -14,8 +14,7 @@ import { handleSuggest } from "./src/commands/suggest";
|
||||
import { setColorEnabled } from "./src/terminal";
|
||||
import { BOLD, GREEN, CYAN, DIM, RESET } from "./src/terminal";
|
||||
import { isStdinTTY, initTTY } from "./src/tty";
|
||||
import { VERSION } from "./src/brand";
|
||||
import { SKIP_WAIT } from "./src/menu";
|
||||
import { showBanner } from "./src/brand";
|
||||
|
||||
// ── Interactive Menu (mole-style) ─────────────────────────────────────
|
||||
|
||||
@@ -23,18 +22,17 @@ interface MenuItem {
|
||||
key: string;
|
||||
label: string;
|
||||
description: string;
|
||||
group: "Create" | "Inspect" | "Project";
|
||||
}
|
||||
|
||||
const MENU_ITEMS: MenuItem[] = [
|
||||
{ key: "commit", label: "Commit", description: "Generate AI commit message", group: "Create" },
|
||||
{ key: "pr", label: "PR", description: "Create a PR with AI-generated title", group: "Create" },
|
||||
{ key: "amend", label: "Amend", description: "Amend last commit with AI message", group: "Create" },
|
||||
{ key: "explain", label: "Explain", description: "Explain staged changes in plain language", group: "Inspect" },
|
||||
{ key: "review", label: "Review", description: "AI code review of staged changes", group: "Inspect" },
|
||||
{ key: "changelog", label: "Changelog", description: "Generate changelog from commits", group: "Inspect" },
|
||||
{ key: "suggest", label: "Suggest", description: "Suggest branch name or commit type", group: "Inspect" },
|
||||
{ key: "config", label: "Config", description: "Configure API settings", group: "Project" },
|
||||
{ key: "commit", label: "Commit", description: "Generate AI commit message" },
|
||||
{ key: "pr", label: "PR", description: "Create a PR with AI-generated title" },
|
||||
{ key: "explain", label: "Explain", description: "Explain staged changes in plain language" },
|
||||
{ key: "review", label: "Review", description: "AI code review of staged changes" },
|
||||
{ key: "changelog", label: "Changelog", description: "Generate changelog from commits" },
|
||||
{ key: "suggest", label: "Suggest", description: "Suggest branch name or commit type" },
|
||||
{ key: "amend", label: "Amend", description: "Amend last commit with AI message" },
|
||||
{ key: "config", label: "Config", description: "Configure API settings" },
|
||||
];
|
||||
|
||||
function hideCursor() { process.stdout.write("\x1b[?25l"); }
|
||||
@@ -56,70 +54,51 @@ function visibleLen(s: string): number {
|
||||
return s.replace(/\x1b\[[0-9;]*m/g, "").length;
|
||||
}
|
||||
|
||||
function padRight(value: string, width: number): string {
|
||||
return value + " ".repeat(Math.max(0, width - visibleLen(value)));
|
||||
}
|
||||
|
||||
function renderMenu(cursor: number): number {
|
||||
function renderMenu(banner: string, cursor: number): number {
|
||||
process.stdout.write("\x1b[H"); // cursor home
|
||||
|
||||
let lineCount = 0;
|
||||
for (const line of banner.split("\n")) {
|
||||
clearLine();
|
||||
process.stdout.write(line + "\n");
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
const G = GREEN();
|
||||
const C = CYAN();
|
||||
const D = DIM();
|
||||
const G = GREEN();
|
||||
const R = RESET();
|
||||
const width = 72;
|
||||
const separator = `${D}${"─".repeat(width)}${R}`;
|
||||
const ARROW = "➤";
|
||||
|
||||
const write = (line = "") => {
|
||||
clearLine();
|
||||
process.stdout.write(`${line}\n`);
|
||||
lineCount++;
|
||||
};
|
||||
|
||||
write("");
|
||||
write(` ${BOLD()}gai${R} ${D}v${VERSION}${R}`);
|
||||
write(` ${D}AI-powered git helper for commits, PRs, reviews, and changelogs${R}`);
|
||||
write(` ${separator}`);
|
||||
write("");
|
||||
|
||||
const keyWidth = 3;
|
||||
// Calculate padding
|
||||
const labelWidth = Math.max(...MENU_ITEMS.map((m) => visibleLen(m.label))) + 2;
|
||||
let currentGroup: MenuItem["group"] | null = null;
|
||||
|
||||
for (let i = 0; i < MENU_ITEMS.length; i++) {
|
||||
const item = MENU_ITEMS[i]!;
|
||||
const num = String(i + 1);
|
||||
const active = i === cursor;
|
||||
|
||||
if (item.group !== currentGroup) {
|
||||
if (currentGroup !== null) write("");
|
||||
write(` ${D}${item.group.toUpperCase()}${R}`);
|
||||
currentGroup = item.group;
|
||||
}
|
||||
|
||||
const pointer = active ? `${C}›${R}` : " ";
|
||||
const key = active ? `${G}${num}${R}` : `${D}${num}${R}`;
|
||||
const label = active ? `${BOLD()}${item.label}${R}` : item.label;
|
||||
const description = active ? item.description : `${D}${item.description}${R}`;
|
||||
const row = [
|
||||
pointer,
|
||||
padRight(key, keyWidth),
|
||||
padRight(label, labelWidth),
|
||||
description,
|
||||
].join(" ");
|
||||
|
||||
clearLine();
|
||||
if (active) {
|
||||
write(` ${C}│${R} ${row}`);
|
||||
const padding = " ".repeat(Math.max(1, labelWidth - visibleLen(item.label)));
|
||||
process.stdout.write(` ${C}${ARROW} ${num}. ${item.label}${R}${padding}${D}${item.description}${R}\n`);
|
||||
} else {
|
||||
write(` ${row}`);
|
||||
const padding = " ".repeat(labelWidth - visibleLen(item.label) + 3);
|
||||
process.stdout.write(` ${num}. ${item.label}${padding}${D}${item.description}${R}\n`);
|
||||
}
|
||||
lineCount++;
|
||||
}
|
||||
|
||||
write("");
|
||||
write(` ${separator}`);
|
||||
write(` ${D}↑/↓ navigate enter run ${G}1-8${D} jump ${G}h${D} help ${G}v${D} version ${G}q${D} quit${R}`);
|
||||
write("");
|
||||
// Footer
|
||||
clearLine();
|
||||
process.stdout.write("\n");
|
||||
lineCount++;
|
||||
clearLine();
|
||||
process.stdout.write(` ${D}↑↓ | Enter | ${G}H${D} Help | ${G}V${D} Version | ${G}Q${D} Quit${R}\n`);
|
||||
lineCount++;
|
||||
clearLine();
|
||||
process.stdout.write("\n");
|
||||
lineCount++;
|
||||
|
||||
// Clear rest of screen
|
||||
process.stdout.write("\x1b[J");
|
||||
@@ -174,9 +153,6 @@ async function dispatchAndWait(item: MenuItem, wasRaw: boolean): Promise<number>
|
||||
process.stdin.pause();
|
||||
process.stdout.write("\x1b[2J\x1b[H"); // clear screen
|
||||
const result = await dispatchMenuAction(item.key);
|
||||
if (result === (SKIP_WAIT as unknown as number)) {
|
||||
return 0; // user explicitly backed out — skip "Press Enter" and return directly
|
||||
}
|
||||
await waitForEnter();
|
||||
return result;
|
||||
}
|
||||
@@ -187,6 +163,7 @@ async function showMenu(): Promise<number> {
|
||||
return 1;
|
||||
}
|
||||
|
||||
const banner = showBanner();
|
||||
let cursor = 0;
|
||||
const wasRaw = process.stdin.isRaw;
|
||||
|
||||
@@ -195,7 +172,7 @@ async function showMenu(): Promise<number> {
|
||||
hideCursor();
|
||||
|
||||
// Initial render
|
||||
renderMenu(cursor);
|
||||
renderMenu(banner, cursor);
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
@@ -203,11 +180,11 @@ async function showMenu(): Promise<number> {
|
||||
|
||||
// Escape sequences (arrows)
|
||||
if (raw === "\x1b[A" || raw === "\x1bOA") {
|
||||
if (cursor > 0) { cursor--; renderMenu(cursor); }
|
||||
if (cursor > 0) { cursor--; renderMenu(banner, cursor); }
|
||||
continue;
|
||||
}
|
||||
if (raw === "\x1b[B" || raw === "\x1bOB") {
|
||||
if (cursor < MENU_ITEMS.length - 1) { cursor++; renderMenu(cursor); }
|
||||
if (cursor < MENU_ITEMS.length - 1) { cursor++; renderMenu(banner, cursor); }
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -218,7 +195,7 @@ async function showMenu(): Promise<number> {
|
||||
hideCursor();
|
||||
if (wasRaw !== true) process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
renderMenu(cursor);
|
||||
renderMenu(banner, cursor);
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -236,13 +213,13 @@ async function showMenu(): Promise<number> {
|
||||
const idx = parseInt(raw) - 1;
|
||||
if (idx < MENU_ITEMS.length) {
|
||||
cursor = idx;
|
||||
renderMenu(cursor);
|
||||
renderMenu(banner, cursor);
|
||||
const result = await dispatchAndWait(MENU_ITEMS[idx]!, wasRaw);
|
||||
if (result !== 0) return result;
|
||||
hideCursor();
|
||||
if (wasRaw !== true) process.stdin.setRawMode(true);
|
||||
process.stdin.resume();
|
||||
renderMenu(cursor);
|
||||
renderMenu(banner, cursor);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
@@ -262,7 +239,7 @@ async function showMenu(): Promise<number> {
|
||||
process.stdin.setRawMode(wasRaw === true);
|
||||
process.stdin.pause();
|
||||
process.stdout.write("\x1b[2J\x1b[H");
|
||||
console.log(`gai v${VERSION}`);
|
||||
console.log("gai v0.1.3");
|
||||
return 0;
|
||||
}
|
||||
if (lower === "q") {
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
import { GREEN, CYAN, RESET } from "./terminal";
|
||||
|
||||
export const VERSION = "0.1.3";
|
||||
const VERSION = "0.1.3";
|
||||
|
||||
export function showBanner(): string {
|
||||
const G = GREEN();
|
||||
|
||||
@@ -62,7 +62,7 @@ export async function handleChangelog(args: ParsedArgs): Promise<number> {
|
||||
}
|
||||
|
||||
try {
|
||||
const callbacks: StreamCallbacks | undefined = tty ? {
|
||||
const callbacks: StreamCallbacks = tty ? {
|
||||
onToken: (token) => process.stdout.write(token),
|
||||
} : undefined;
|
||||
|
||||
|
||||
+21
-36
@@ -7,11 +7,10 @@ import {
|
||||
getStagedDiff,
|
||||
getRecentCommits,
|
||||
stageFiles,
|
||||
applyFileSelection,
|
||||
commit,
|
||||
} from "../git";
|
||||
import { selectFiles } from "../selector";
|
||||
import { BACK, SKIP_WAIT } from "../menu";
|
||||
import { BACK } from "../menu";
|
||||
import { collectProjectContext } from "../context";
|
||||
import { buildPrompt, SYSTEM_PROMPT } from "../prompt";
|
||||
import { generateCommitMessage } from "../ai";
|
||||
@@ -51,19 +50,6 @@ function printCommitResult(result: CommitResult, msg: string) {
|
||||
if (parts.length > 0) console.log(` ${parts.join(", ")}`);
|
||||
}
|
||||
|
||||
function printSelectionResult(result: { staged: string[]; unstaged: string[] }) {
|
||||
const parts: string[] = [];
|
||||
if (result.staged.length > 0) {
|
||||
parts.push(`${GREEN()}staged ${result.staged.length}${RESET()}`);
|
||||
}
|
||||
if (result.unstaged.length > 0) {
|
||||
parts.push(`${YELLOW()}unstaged ${result.unstaged.length}${RESET()}`);
|
||||
}
|
||||
if (parts.length > 0) {
|
||||
console.log(` Updated staging area: ${parts.join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
async function confirmCommit(message: string): Promise<"y" | "n" | "e"> {
|
||||
console.log(`\n ${BOLD()}Generated commit message:${RESET()}`);
|
||||
console.log(` ${GREEN()}${message}${RESET()}\n`);
|
||||
@@ -187,22 +173,16 @@ export async function handleCommit(args: ParsedArgs): Promise<number> {
|
||||
const stagedFiles = await getStagedFiles();
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
|
||||
if (!amend && stagedFiles.length === 0 && unstagedFiles.length === 0) {
|
||||
console.error(`\n ${RED()}Error: Nothing to commit.${RESET()}\n`);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!amend && autoMode && unstagedFiles.length > 0) {
|
||||
await stageFiles(unstagedFiles.map((f) => f.path));
|
||||
console.log(` ${GREEN()}Auto-staged ${unstagedFiles.length} file(s).${RESET()}`);
|
||||
} else if (!amend) {
|
||||
if (isStdinTTY()) {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return SKIP_WAIT as unknown as number;
|
||||
printSelectionResult(await applyFileSelection(stagedFiles, unstagedFiles, selected));
|
||||
} else if (stagedFiles.length === 0 && unstagedFiles.length > 0) {
|
||||
if (stagedFiles.length === 0 && !amend) {
|
||||
if (autoMode && unstagedFiles.length > 0) {
|
||||
await stageFiles(unstagedFiles.map((f) => f.path));
|
||||
console.log(` ${GREEN()}Auto-staged ${unstagedFiles.length} file(s).${RESET()}`);
|
||||
} else if (unstagedFiles.length > 0) {
|
||||
console.error(`\n ${RED()}Error: No staged changes. Use -a to auto-stage, or stage files manually.${RESET()}\n`);
|
||||
return 1;
|
||||
} else {
|
||||
console.error(`\n ${RED()}Error: Nothing to commit.${RESET()}\n`);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,13 +215,18 @@ export async function handleCommit(args: ParsedArgs): Promise<number> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (autoMode && unstagedFiles.length > 0) {
|
||||
await stageFiles(unstagedFiles.map((f) => f.path));
|
||||
console.log(` ${GREEN()}Auto-staged ${unstagedFiles.length} file(s).${RESET()}`);
|
||||
} else {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return SKIP_WAIT as unknown as number;
|
||||
printSelectionResult(await applyFileSelection(stagedFiles, unstagedFiles, selected));
|
||||
if (unstagedFiles.length > 0) {
|
||||
if (autoMode) {
|
||||
await stageFiles(unstagedFiles.map((f) => f.path));
|
||||
console.log(` ${GREEN()}Auto-staged ${unstagedFiles.length} file(s).${RESET()}`);
|
||||
} else {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return 0;
|
||||
if (selected.length > 0) {
|
||||
await stageFiles(selected);
|
||||
console.log(` ${GREEN()}Staged ${selected.length} file(s).${RESET()}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const diff = await getStagedDiff();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { loadConfig, saveConfig } from "../config";
|
||||
import { BOLD, GREEN, YELLOW, CYAN, RED, DIM, RESET } from "../terminal";
|
||||
import { isStdinTTY } from "../tty";
|
||||
import { SKIP_WAIT } from "../menu";
|
||||
import type { Config } from "../types";
|
||||
import type { ParsedArgs } from "../cli";
|
||||
|
||||
@@ -324,7 +323,7 @@ export async function handleConfig(args: ParsedArgs): Promise<number> {
|
||||
// gai config (no args) → interactive
|
||||
if (positional.length === 0) {
|
||||
const result = await interactiveConfig();
|
||||
return result === "back" ? (SKIP_WAIT as unknown as number) : 0;
|
||||
return result === "back" ? 0 : 0;
|
||||
}
|
||||
|
||||
console.error(`\n ${RED()}Error: Unknown config subcommand: ${positional[0]}${RESET()}`);
|
||||
|
||||
+19
-19
@@ -1,14 +1,7 @@
|
||||
import { loadConfig } from "../config";
|
||||
import {
|
||||
isGitRepo,
|
||||
getStagedFiles,
|
||||
getStagedDiff,
|
||||
getUnstagedFiles,
|
||||
getRepoRoot,
|
||||
applyFileSelection,
|
||||
} from "../git";
|
||||
import { isGitRepo, getStagedDiff, getUnstagedFiles, getRepoRoot, stageFiles } from "../git";
|
||||
import { selectFiles } from "../selector";
|
||||
import { BACK, SKIP_WAIT } from "../menu";
|
||||
import { BACK } from "../menu";
|
||||
import { collectProjectContext } from "../context";
|
||||
import { EXPLAIN_SYSTEM_PROMPT, buildExplainPrompt } from "../prompt";
|
||||
import { callAI } from "../ai";
|
||||
@@ -26,6 +19,7 @@ export async function handleExplain(args: ParsedArgs): Promise<number> {
|
||||
}
|
||||
|
||||
const unstaged = args.flags["unstaged"] as boolean;
|
||||
const staged = args.flags["staged"] as boolean;
|
||||
const verbose = args.flags["verbose"] as boolean;
|
||||
|
||||
// Determine which diff to explain
|
||||
@@ -50,16 +44,22 @@ export async function handleExplain(args: ParsedArgs): Promise<number> {
|
||||
console.error(`\n ${RED()}Error: Not a git repository.${RESET()}\n`);
|
||||
return 1;
|
||||
}
|
||||
const stagedFiles = await getStagedFiles();
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
sourceLabel = "selected changes";
|
||||
|
||||
if (stagedFiles.length > 0 || unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return SKIP_WAIT as unknown as number;
|
||||
await applyFileSelection(stagedFiles, unstagedFiles, selected);
|
||||
}
|
||||
diff = await getStagedDiff();
|
||||
sourceLabel = "staged changes";
|
||||
|
||||
// If no staged changes, offer to stage unstaged files
|
||||
if (!diff) {
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
if (unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles([], unstagedFiles);
|
||||
if (selected === BACK) return 0;
|
||||
if (selected.length > 0) {
|
||||
await stageFiles(selected);
|
||||
console.log(` ${GREEN()}Staged ${selected.length} file(s).${RESET()}`);
|
||||
diff = await getStagedDiff();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Read from pipe
|
||||
const chunks: Buffer[] = [];
|
||||
@@ -109,7 +109,7 @@ export async function handleExplain(args: ParsedArgs): Promise<number> {
|
||||
}
|
||||
|
||||
try {
|
||||
const callbacks: StreamCallbacks | undefined = tty ? {
|
||||
const callbacks: StreamCallbacks = tty ? {
|
||||
onToken: (token) => process.stdout.write(token),
|
||||
} : undefined;
|
||||
|
||||
|
||||
+3
-3
@@ -4,7 +4,7 @@ import { isGitRepo, getRepoRoot } from "../git";
|
||||
import { collectProjectContext } from "../context";
|
||||
import { PR_SYSTEM_PROMPT, buildPRPrompt } from "../prompt";
|
||||
import { generatePRMessage } from "../ai";
|
||||
import { BACK, SKIP_WAIT, selectOne } from "../menu";
|
||||
import { BACK, selectOne } from "../menu";
|
||||
import {
|
||||
getDefaultBranch,
|
||||
getBranchName,
|
||||
@@ -68,7 +68,7 @@ export async function handlePR(args: ParsedArgs): Promise<number> {
|
||||
if (!platform) {
|
||||
const hostname = (await getRemoteHostname()) || "unknown";
|
||||
const chosen = await selectPlatform(hostname);
|
||||
if (chosen === BACK) return SKIP_WAIT as unknown as number;
|
||||
if (chosen === BACK) return 0;
|
||||
if (!chosen) {
|
||||
console.log(" Aborted.");
|
||||
return 0;
|
||||
@@ -98,7 +98,7 @@ export async function handlePR(args: ParsedArgs): Promise<number> {
|
||||
items: [{ label: "Back", value: "back" as const }],
|
||||
});
|
||||
if (choice === null) process.exit(0);
|
||||
return SKIP_WAIT as unknown as number;
|
||||
return 0;
|
||||
}
|
||||
|
||||
console.log(` ${commits.length} commit${commits.length > 1 ? "s" : ""} on this branch`);
|
||||
|
||||
+18
-19
@@ -1,14 +1,7 @@
|
||||
import { loadConfig } from "../config";
|
||||
import {
|
||||
isGitRepo,
|
||||
getStagedFiles,
|
||||
getStagedDiff,
|
||||
getUnstagedFiles,
|
||||
getRepoRoot,
|
||||
applyFileSelection,
|
||||
} from "../git";
|
||||
import { isGitRepo, getStagedDiff, getUnstagedFiles, getRepoRoot, stageFiles } from "../git";
|
||||
import { selectFiles } from "../selector";
|
||||
import { BACK, SKIP_WAIT } from "../menu";
|
||||
import { BACK } from "../menu";
|
||||
import { collectProjectContext } from "../context";
|
||||
import { REVIEW_SYSTEM_PROMPT, buildReviewPrompt } from "../prompt";
|
||||
import { callAI } from "../ai";
|
||||
@@ -60,16 +53,22 @@ export async function handleReview(args: ParsedArgs): Promise<number> {
|
||||
console.error(`\n ${RED()}Error: Not a git repository.${RESET()}\n`);
|
||||
return 1;
|
||||
}
|
||||
const stagedFiles = await getStagedFiles();
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
sourceLabel = "selected changes";
|
||||
|
||||
if (stagedFiles.length > 0 || unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return SKIP_WAIT as unknown as number;
|
||||
await applyFileSelection(stagedFiles, unstagedFiles, selected);
|
||||
}
|
||||
diff = await getStagedDiff();
|
||||
sourceLabel = "staged changes";
|
||||
|
||||
// If no staged changes, offer to stage unstaged files
|
||||
if (!diff) {
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
if (unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles([], unstagedFiles);
|
||||
if (selected === BACK) return 0;
|
||||
if (selected.length > 0) {
|
||||
await stageFiles(selected);
|
||||
console.log(` ${GREEN()}Staged ${selected.length} file(s).${RESET()}`);
|
||||
diff = await getStagedDiff();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!diff) {
|
||||
@@ -111,7 +110,7 @@ export async function handleReview(args: ParsedArgs): Promise<number> {
|
||||
}
|
||||
|
||||
try {
|
||||
const callbacks: StreamCallbacks | undefined = tty ? {
|
||||
const callbacks: StreamCallbacks = tty ? {
|
||||
onToken: (token) => process.stdout.write(token),
|
||||
} : undefined;
|
||||
|
||||
|
||||
+16
-16
@@ -1,13 +1,7 @@
|
||||
import { loadConfig } from "../config";
|
||||
import {
|
||||
isGitRepo,
|
||||
getStagedFiles,
|
||||
getStagedDiff,
|
||||
getUnstagedFiles,
|
||||
applyFileSelection,
|
||||
} from "../git";
|
||||
import { isGitRepo, getStagedDiff, getUnstagedFiles, stageFiles } from "../git";
|
||||
import { selectFiles } from "../selector";
|
||||
import { BACK, SKIP_WAIT } from "../menu";
|
||||
import { BACK } from "../menu";
|
||||
import {
|
||||
SUGGEST_SYSTEM_PROMPT,
|
||||
buildSuggestBranchPrompt,
|
||||
@@ -57,15 +51,21 @@ export async function handleSuggest(args: ParsedArgs): Promise<number> {
|
||||
diff = "";
|
||||
}
|
||||
} else {
|
||||
const stagedFiles = await getStagedFiles();
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
|
||||
if (stagedFiles.length > 0 || unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles(stagedFiles, unstagedFiles);
|
||||
if (selected === BACK) return SKIP_WAIT as unknown as number;
|
||||
await applyFileSelection(stagedFiles, unstagedFiles, selected);
|
||||
}
|
||||
diff = await getStagedDiff();
|
||||
|
||||
// If no staged changes, offer to stage unstaged files
|
||||
if (!diff) {
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
if (unstagedFiles.length > 0) {
|
||||
const selected = await selectFiles([], unstagedFiles);
|
||||
if (selected === BACK) return 0;
|
||||
if (selected.length > 0) {
|
||||
await stageFiles(selected);
|
||||
console.log(` ${GREEN()}Staged ${selected.length} file(s).${RESET()}`);
|
||||
diff = await getStagedDiff();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
-27
@@ -98,33 +98,6 @@ export async function stageFiles(paths: string[]): Promise<void> {
|
||||
await Bun.$`git add -- ${paths}`;
|
||||
}
|
||||
|
||||
export async function unstageFiles(paths: string[]): Promise<void> {
|
||||
if (paths.length === 0) return;
|
||||
try {
|
||||
await Bun.$`git restore --staged -- ${paths}`.quiet();
|
||||
} catch {
|
||||
await Bun.$`git rm --cached -r -- ${paths}`.quiet();
|
||||
}
|
||||
}
|
||||
|
||||
export async function applyFileSelection(
|
||||
stagedFiles: FileEntry[],
|
||||
unstagedFiles: FileEntry[],
|
||||
selectedPaths: string[],
|
||||
): Promise<{ staged: string[]; unstaged: string[] }> {
|
||||
const selected = new Set(selectedPaths);
|
||||
const stagedPaths = new Set(stagedFiles.map((file) => file.path));
|
||||
const unstagedPaths = new Set(unstagedFiles.map((file) => file.path));
|
||||
|
||||
const toUnstage = [...stagedPaths].filter((path) => !selected.has(path));
|
||||
const toStage = [...selected].filter((path) => unstagedPaths.has(path));
|
||||
|
||||
await unstageFiles(toUnstage);
|
||||
await stageFiles(toStage);
|
||||
|
||||
return { staged: toStage, unstaged: toUnstage };
|
||||
}
|
||||
|
||||
export async function commit(
|
||||
message: string,
|
||||
): Promise<{ branch: string; hash: string; files: number; insertions: number; deletions: number }> {
|
||||
|
||||
@@ -15,10 +15,6 @@ const BACKSPACE = "\x7f";
|
||||
export const BACK = Symbol("prompt-back");
|
||||
export type PromptBack = typeof BACK;
|
||||
|
||||
// Sent by command handlers to skip the "Press Enter to return" wait in the
|
||||
// interactive menu when the user explicitly backed out of a sub-menu.
|
||||
export const SKIP_WAIT = Symbol("skip-wait");
|
||||
|
||||
export interface Choice<T> {
|
||||
label: string;
|
||||
value: T;
|
||||
@@ -225,7 +221,6 @@ export async function selectMany<T>(
|
||||
if (!options.selectAllLabel) return;
|
||||
items[0]!.selected = items.slice(1).every((item) => item.selected);
|
||||
};
|
||||
syncSelectAll();
|
||||
|
||||
const toggle = (index: number) => {
|
||||
const item = items[index]!;
|
||||
|
||||
+19
-29
@@ -1,54 +1,44 @@
|
||||
import type { FileEntry } from "./types";
|
||||
import { BOLD, GREEN, YELLOW, RESET } from "./terminal";
|
||||
import { isStdinTTY } from "./tty";
|
||||
import { BACK, selectMany } from "./menu";
|
||||
import type { PromptBack } from "./menu";
|
||||
|
||||
function mergeFiles(stagedFiles: FileEntry[], unstagedFiles: FileEntry[]) {
|
||||
const files = new Map<string, FileEntry & { staged: boolean; unstaged: boolean }>();
|
||||
|
||||
for (const file of stagedFiles) {
|
||||
files.set(file.path, { ...file, staged: true, unstaged: false });
|
||||
}
|
||||
|
||||
for (const file of unstagedFiles) {
|
||||
const existing = files.get(file.path);
|
||||
if (existing) {
|
||||
existing.unstaged = true;
|
||||
existing.label = existing.label === file.label
|
||||
? existing.label
|
||||
: `${existing.label}, ${file.label}`;
|
||||
} else {
|
||||
files.set(file.path, { ...file, staged: false, unstaged: true });
|
||||
}
|
||||
}
|
||||
|
||||
return [...files.values()];
|
||||
}
|
||||
|
||||
export async function selectFiles(
|
||||
stagedFiles: FileEntry[],
|
||||
unstagedFiles: FileEntry[],
|
||||
): Promise<string[] | PromptBack> {
|
||||
const files = mergeFiles(stagedFiles, unstagedFiles);
|
||||
if (files.length === 0) return [];
|
||||
if (unstagedFiles.length === 0) return [];
|
||||
|
||||
if (!isStdinTTY()) return stagedFiles.map((file) => file.path);
|
||||
if (stagedFiles.length > 0) {
|
||||
process.stdout.write(`\n ${BOLD()}Staged files (will be included):${RESET()}\n`);
|
||||
for (const f of stagedFiles) {
|
||||
process.stdout.write(` ${GREEN()}✓${RESET()} ${f.path} (${YELLOW()}${f.label}${RESET()})\n`);
|
||||
}
|
||||
}
|
||||
|
||||
if (!isStdinTTY()) return [];
|
||||
|
||||
const selected = await selectMany({
|
||||
title: "Select files for this action",
|
||||
subtitle: `${stagedFiles.length} staged, ${unstagedFiles.length} unstaged`,
|
||||
title: "Select files to stage",
|
||||
subtitle: `${unstagedFiles.length} unstaged file${unstagedFiles.length > 1 ? "s" : ""} available`,
|
||||
selectAllLabel: "Select all",
|
||||
cancelMessage: "Aborted.",
|
||||
items: files.map((f) => ({
|
||||
items: unstagedFiles.map((f) => ({
|
||||
label: f.path,
|
||||
value: f.path,
|
||||
description: f.label,
|
||||
selected: f.staged,
|
||||
})),
|
||||
});
|
||||
|
||||
if (selected === null) process.exit(1);
|
||||
if (selected === BACK) return BACK;
|
||||
|
||||
if (selected.length > 0) {
|
||||
process.stdout.write(
|
||||
` ${GREEN()}Staged ${selected.length} file(s):${RESET()} ${selected.join(", ")}\n`,
|
||||
);
|
||||
}
|
||||
|
||||
return selected;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user