refactor(ui): group menu by category and unify file selection
This commit is contained in:
+35
-20
@@ -7,6 +7,7 @@ import {
|
||||
getStagedDiff,
|
||||
getRecentCommits,
|
||||
stageFiles,
|
||||
applyFileSelection,
|
||||
commit,
|
||||
} from "../git";
|
||||
import { selectFiles } from "../selector";
|
||||
@@ -50,6 +51,19 @@ 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`);
|
||||
@@ -173,16 +187,22 @@ export async function handleCommit(args: ParsedArgs): Promise<number> {
|
||||
const stagedFiles = await getStagedFiles();
|
||||
const unstagedFiles = await getUnstagedFiles();
|
||||
|
||||
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) {
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,18 +235,13 @@ export async function handleCommit(args: ParsedArgs): Promise<number> {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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 SKIP_WAIT as unknown as number;
|
||||
if (selected.length > 0) {
|
||||
await stageFiles(selected);
|
||||
console.log(` ${GREEN()}Staged ${selected.length} file(s).${RESET()}`);
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
const diff = await getStagedDiff();
|
||||
|
||||
Reference in New Issue
Block a user