Skip to content

Commit

Permalink
Added "category_delimiters" configuration option
Browse files Browse the repository at this point in the history
which is per default `["[", "]"]`

- Changed summary parsing to remove dot at the end
  of the line if existing
  • Loading branch information
Sascha Grunert authored and Sascha Grunert committed Oct 10, 2016
1 parent 5c79184 commit 8b33c1e
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitjournal.toml
@@ -1,4 +1,5 @@
categories = ["Added", "Changed", "Fixed", "Improved", "Removed"]
category_delimiters = ["[", "]"]
colored_output = true
enable_debug = true
enable_footers = false
Expand Down
6 changes: 5 additions & 1 deletion src/config.rs
Expand Up @@ -44,11 +44,14 @@ impl fmt::Display for Error {
}

/// The configuration structure for git-journal.
#[derive(Default, Debug, PartialEq, RustcEncodable, RustcDecodable)]
#[derive(Default, Debug, Clone, PartialEq, RustcEncodable, RustcDecodable)]
pub struct Config {
/// Specifies the available categories for the commit message
pub categories: Vec<String>,

/// Set the characters where the categories are wrapped in
pub category_delimiters: (String, String),

/// Set to false if the output should not be colored
pub colored_output: bool,

Expand Down Expand Up @@ -90,6 +93,7 @@ impl Config {
pub fn new() -> Self {
Config {
categories: Self::get_default_categories(),
category_delimiters: ("[".to_owned(), "]".to_owned()),
colored_output: true,
default_template: None,
enable_debug: true,
Expand Down
9 changes: 6 additions & 3 deletions src/lib.rs
Expand Up @@ -186,7 +186,7 @@ impl GitJournal {

// Create a new parser with empty results
let new_parser = Parser {
categories: new_config.categories.clone(),
config: new_config.clone(),
result: vec![],
};

Expand Down Expand Up @@ -217,6 +217,9 @@ impl GitJournal {
/// # Specifies the available categories for the commit message, allowd regular expressions.
/// categories = ["Added", "Changed", "Fixed", "Improved", "Removed"]
///
/// Set the characters where the categories are wrapped in
/// category_delimiters: ["[", "]"],
///
/// # Set to false if the output should not be colored
/// colored_output = true
///
Expand Down Expand Up @@ -696,7 +699,7 @@ impl GitJournal {
};

// Print the log
let output_vec = try!(self.parser.print(&self.config, &compact, used_template));
let output_vec = try!(self.parser.print(&compact, used_template));

// Print the log to the file if necessary
if let Some(output) = output {
Expand Down Expand Up @@ -826,7 +829,7 @@ mod tests {
assert_eq!(journal.config.excluded_commit_tags.len(), 0);
assert!(journal.parse_log("HEAD", "rc", &0, &true, &false).is_ok());
assert_eq!(journal.parser.result.len(), journal.tags.len() + 1);
assert_eq!(journal.parser.result[0].commits.len(), 14);
assert_eq!(journal.parser.result[0].commits.len(), 15);
assert_eq!(journal.parser.result[1].commits.len(), 1);
assert_eq!(journal.parser.result[2].commits.len(), 2);
assert!(journal.print_log(false, None, Some("CHANGELOG.md")).is_ok());
Expand Down
29 changes: 18 additions & 11 deletions src/parser.rs
Expand Up @@ -529,7 +529,9 @@ impl Print for SummaryElement {
if config.colored_output {
try!(c1(t));
}
tryw!(t, "[{}] ", self.category);
tryw!(t, "{}", config.category_delimiters.0);
tryw!(t, "{}", self.category);
tryw!(t, "{} ", config.category_delimiters.1);
if config.colored_output {
try!(c2(t));
}
Expand Down Expand Up @@ -665,7 +667,9 @@ impl Print for ListElement {
if config.colored_output {
try!(c1(t));
}
tryw!(t, "[{}] ", self.category);
tryw!(t, "{}", config.category_delimiters.0);
tryw!(t, "{}", self.category);
tryw!(t, "{} ", config.category_delimiters.1);
if config.colored_output {
try!(c2(t));
}
Expand Down Expand Up @@ -776,19 +780,19 @@ lazy_static! {

#[derive(Clone)]
pub struct Parser {
pub categories: Vec<String>,
pub config: Config,
pub result: Vec<ParsedTag>,
}

impl Parser {
method!(parse_category<Self, &[u8], &str>, self,
chain!(
tag!("[")? ~
tag!(self.config.category_delimiters.0.as_str())? ~
p_category: map_res!(
re_bytes_find!(&self.categories.join("|")),
re_bytes_find!(&self.config.categories.join("|")),
str::from_utf8
) ~
tag!("]")? ,
tag!(self.config.category_delimiters.1.as_str())? ,
|| p_category
));

Expand Down Expand Up @@ -844,7 +848,11 @@ impl Parser {
})
.collect::<Vec<String>>());
}
(tags, RE_TAGS.replace_all(string, ""))
let mut text = RE_TAGS.replace_all(string, "");
if let Some('.') = text.chars().rev().nth(0) {
text.pop();
}
(tags, text)
}

/// Parses a single commit message and returns a changelog ready form
Expand Down Expand Up @@ -909,13 +917,13 @@ impl Parser {
}

/// Prints the commits without any template
pub fn print(&self, config: &Config, compact: &bool, template: Option<&str>) -> Result<Vec<u8>, Error> {
pub fn print(&self, compact: &bool, template: Option<&str>) -> Result<Vec<u8>, Error> {
let mut term = try!(term::stdout().ok_or(Error::Terminal));
let mut vec = vec![];

// Print every tag
for (index, tag) in self.result.iter().enumerate() {
try!(tag.print_to_term_and_write_to_vector(&mut term, &mut vec, compact, config,
try!(tag.print_to_term_and_write_to_vector(&mut term, &mut vec, compact, &self.config,
template, (index, self.result.len())));
}

Expand Down Expand Up @@ -952,9 +960,8 @@ mod tests {
use config::Config;

fn get_parser() -> Parser {
let config = Config::new();
Parser {
categories: config.categories,
config: Config::new(),
result: vec![],
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_repo
Submodule test_repo updated 1 files
+1 −0 .gitjournal.toml
2 changes: 1 addition & 1 deletion tests/test_repo2
Submodule test_repo2 updated 1 files
+1 −0 .gitjournal.toml

0 comments on commit 8b33c1e

Please sign in to comment.