Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use clap_verbosity_flag::{InfoLevel, Verbosity};
use log::{error, info};
use question::{Answer, Question};
use std::{
fs,
fs::File,
io::Write,
process::{Command, Stdio},
str,
Expand Down Expand Up @@ -142,7 +144,7 @@ async fn main() -> Result<(), ()> {
sp.unwrap().stop_with_message("Finished Analyzing!".into());
}

let commit_msg = completion.choices[0].text.to_owned();
let mut commit_msg = completion.choices[0].text.to_owned();

if cli.dry_run {
info!("{}", commit_msg);
Expand All @@ -154,17 +156,60 @@ async fn main() -> Result<(), ()> {
);

if !cli.force {
let answer = Question::new("Do you want to continue? (Y/n)")
.yes_no()
let answer = Question::new("Do you want to continue? [Y]es [e]dit [n]o")
.accept("yes")
.accept("y")
.accept("edit")
.accept("e")
.accept("no")
.accept("n")
.default(Answer::RESPONSE(String::from("yes")))
.until_acceptable()
.default(Answer::YES)
.ask()
.expect("Couldn't ask question.");

if answer == Answer::NO {
error!("Commit aborted by user.");
std::process::exit(1);
if let Answer::RESPONSE(input) = answer {
let input_lower = input.to_lowercase();
match input_lower.as_str() {
"no" | "n" => {
error!("Commit aborted by user.");
std::process::exit(1);
}

"edit" | "e" => {
let editor = match std::env::var("EDITOR") {
Ok(val) => val,
Err(_) => "vi".to_string(),
};
let mut commit_msg_file =
File::create(".auto_commit_msg").expect("Couldn't create temp file");
commit_msg_file
.write_all(commit_msg.as_bytes())
.expect("Couldn't write commit msg into temp file");

Command::new("/usr/bin/sh")
.arg("-c")
.arg(format!("{} .auto_commit_msg", editor))
.spawn()
.expect("Error: Failed to run editor")
.wait()
.expect("Error: Editor returned a non-zero status");

commit_msg = fs::read_to_string(".auto_commit_msg")
.expect("Cloudn't read commit message from file");
fs::remove_file(".auto_commit_msg").expect("Failed to delete temp file");

if commit_msg.len() == 0 {
error!("Commit aborted by user.");
std::process::exit(1);
}

info!("Using new commit message:\n{}", commit_msg);
}
_ => {}
}
}

info!("Committing Message...");
}
}
Expand Down