Skip to content

Commit

Permalink
Merge pull request #3 from sdttttt/add-issue-pr-linked
Browse files Browse the repository at this point in the history
impl: commit specification.
  • Loading branch information
sdttttt authored Sep 25, 2020
2 parents c99dae7 + 45f5a42 commit 0b8730c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
33 changes: 31 additions & 2 deletions src/message.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use dialoguer::{theme::ColorfulTheme, Input, Select};

use crate::util::remove_pound_prefix;

const COMMIT_TYPES_DESCRIPTION: &[&str] = &[
"test: Adding missing tests.",
"feat: A new feature.",
Expand Down Expand Up @@ -29,8 +31,8 @@ impl Messager {
let typ = ask_type();
let scope = ask_scope();
let subject = ask_subject();
let body = ask_description();

let body = Self::build_body();
Self {
typ,
scope,
Expand All @@ -52,6 +54,23 @@ impl Messager {
format!("{} \n\n{}", header, self.body)
}
}

fn build_body() -> String {
let description = ask_description();
let closes = ask_close();

let mut body = String::new();

if description.len() != 0 {
body = description;
};

if closes.len() != 0 {
body = format!("{}\n\nClose #{}", body, closes);
};

body
}
}

fn ask_type() -> String {
Expand Down Expand Up @@ -111,4 +130,14 @@ fn ask_description() -> String {
.unwrap();

String::from(description.trim())
}

fn ask_close() -> String {
let closes = Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("GRC: PR & Issues this commit closes, e.g 123: (Optional)")
.allow_empty(true)
.interact()
.unwrap();

String::from(remove_pound_prefix(closes.trim()))
}
28 changes: 28 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,31 @@ pub fn is_all_workspace(statuses: &Statuses) -> bool {
}
tip
}


pub fn remove_pound_prefix(input: &str) -> &str {
match input.find("#") {
Some(index) => {
match input.get(index+1..input.len()) {
Some(s) => s,
_ => input,
}
}
_ => input,
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn test_string_start_with() {
let str_1 = "#123";
let result = "123";

let str_2 = remove_pound_prefix(str_1);
assert_eq!(str_2, result);
}
}

0 comments on commit 0b8730c

Please sign in to comment.