Skip to content

Commit 4921212

Browse files
authored
Merge pull request #12 from spc-28/feat/yes-no-with-arrow-keys
📦 new: add yes/no as select input
2 parents e5926ac + 5ecf3a9 commit 4921212

File tree

5 files changed

+7063
-4806
lines changed

5 files changed

+7063
-4806
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,10 @@ dist
1414
## env
1515
.env
1616
.env.*
17+
18+
# yarn v2
19+
.yarn/cache
20+
.yarn/unplugged
21+
.yarn/build-state.yml
22+
.yarn/install-state.gz
23+
.pnp.*

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"ink": "^4.1.0",
3939
"ink-big-text": "^2.0.0",
4040
"ink-gradient": "^3.0.0",
41+
"ink-select-input": "^6.0.0",
4142
"is-git-repository": "^2.0.0",
4243
"meow": "^11.0.0",
4344
"openai": "^4.28.4",

source/cli.js

100644100755
File mode changed.

source/utils/commit.js

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
1-
import generatePrompt from "./openai.js";
1+
import generatePrompt from './openai.js';
22
import {execa} from 'execa';
3-
import readline from "readline";
3+
import readline from 'readline';
4+
import React from 'react';
5+
import {Box, render, Text, useApp} from 'ink';
6+
import SelectInput from 'ink-select-input';
47

58
async function askForCommitMessage() {
6-
const prompt = await generatePrompt();
9+
const prompt = await generatePrompt();
710

8-
const rl = readline.createInterface({
9-
input: process.stdin,
10-
output: process.stdout,
11-
});
11+
const rl = readline.createInterface({
12+
input: process.stdin,
13+
output: process.stdout,
14+
});
1215

13-
if (prompt) {
14-
rl.question(`Suggested commit message: ${prompt}\nDo you want to proceed? (y/N) `, (answer) => {
15-
if (answer.toLowerCase() === "y") {
16-
execa("git", ["commit", "-m", prompt])
17-
.then(() => {
18-
console.log("Changes committed successfully!");
19-
})
20-
.catch((error) => {
21-
console.error("Failed to commit changes:", error);
22-
});
23-
} else {
24-
console.log("Changes not committed.");
25-
}
16+
const SelectSuggestedCommit = () => {
17+
const {exit} = useApp();
18+
const handleSelect = item => {
19+
if (item.value) {
20+
execa('git', ['commit', '-m', prompt])
21+
.then(() => {
22+
console.log('Changes committed successfully!');
23+
})
24+
.catch(error => {
25+
console.error('Failed to commit changes:', error);
26+
});
27+
}
28+
else {
29+
console.log('Changes not committed.');
30+
}
31+
exit();
32+
};
2633

27-
rl.close();
28-
});
29-
} else {
30-
console.log("No changes to commit...");
31-
rl.close();
32-
}
34+
const items = [
35+
{
36+
label: 'No',
37+
value: false,
38+
},
39+
{
40+
label: 'Yes',
41+
value: true,
42+
},
43+
];
44+
45+
return (
46+
<Box flexDirection="column">
47+
<Text>{`Suggested commit message: ${prompt}\nDo you want to proceed?`}</Text>
48+
<SelectInput items={items} onSelect={handleSelect} />
49+
</Box>
50+
);
51+
};
52+
53+
if (prompt) {
54+
render(<SelectSuggestedCommit />);
55+
} else {
56+
console.log('No changes to commit...');
57+
rl.close();
58+
}
3359
}
3460

35-
export default askForCommitMessage;
61+
export default askForCommitMessage;

0 commit comments

Comments
 (0)