feat: edit body for commit messages
This commit is contained in:
@@ -9,7 +9,7 @@ use inquire::{Confirm, Text};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
use crate::{
|
||||
commit::types::{BreakingChange, CommitType, Description, Scope},
|
||||
commit::types::{Body, BreakingChange, CommitType, Description, Scope},
|
||||
error::Error,
|
||||
};
|
||||
|
||||
@@ -30,6 +30,9 @@ pub trait Prompter: Send + Sync {
|
||||
/// Prompt the user for breaking change
|
||||
fn input_breaking_change(&self) -> Result<BreakingChange, Error>;
|
||||
|
||||
/// Prompt the user to optionally add a free-form body via an external editor
|
||||
fn input_body(&self) -> Result<Body, Error>;
|
||||
|
||||
/// Prompt the user to confirm applying the commit message
|
||||
fn confirm_apply(&self, message: &str) -> Result<bool, Error>;
|
||||
|
||||
@@ -176,6 +179,38 @@ impl Prompter for RealPrompts {
|
||||
Ok(trimmed.into())
|
||||
}
|
||||
|
||||
fn input_body(&self) -> Result<Body, Error> {
|
||||
use inquire::Editor;
|
||||
|
||||
let wants_body = Confirm::new("Add a body?")
|
||||
.with_default(false)
|
||||
.prompt()
|
||||
.map_err(|_| Error::Cancelled)?;
|
||||
|
||||
if !wants_body {
|
||||
return Ok(Body::default());
|
||||
}
|
||||
|
||||
let template = "\
|
||||
JJ: Body (optional). Markdown is supported.\n\
|
||||
JJ: Wrap prose lines at 72 characters where possible.\n\
|
||||
JJ: Lines starting with \"JJ:\" will be removed.\n";
|
||||
|
||||
let raw = Editor::new("Body:")
|
||||
.with_predefined_text(template)
|
||||
.with_file_extension(".md")
|
||||
.prompt()
|
||||
.map_err(|_| Error::Cancelled)?;
|
||||
|
||||
let stripped: String = raw
|
||||
.lines()
|
||||
.filter(|line| !line.starts_with("JJ:"))
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
|
||||
Ok(Body::from(stripped))
|
||||
}
|
||||
|
||||
fn confirm_apply(&self, message: &str) -> Result<bool, Error> {
|
||||
use inquire::Confirm;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user