diff --git a/Cargo.toml b/Cargo.toml index a4a2380..796e6c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rain" -version = "0.2.0" +version = "0.2.1" license = "MIT" readme = "README.md" keywords = ["log", "logger", "vertical", "terminal", "graph"] diff --git a/src/lib.rs b/src/lib.rs index 76b7979..a80a5a5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ use termion::color::{self, LightBlack, Reset, Fg}; pub struct Graph { lines_to_be_removed: Vec, columns: Vec>, + prefix_len: usize, } impl Graph @@ -69,12 +70,26 @@ impl Graph /// let _ :Graph = Graph::new(); /// ``` pub fn new() -> Self { + Self::with_prefix_length(8) + } + + /// Create a new `Graph` for drawing with a custom length of the identifier (prefix) + /// + /// # Example + /// ``` + /// use rain::Graph; + /// + /// let _ :Graph = Graph::with_prefix_length(25); + /// ``` + pub fn with_prefix_length(length: usize) -> Self { Graph { lines_to_be_removed: vec![], columns: vec![], + prefix_len: length + 3, } } + /// Set the global log level for reporting pub fn set_log_level(self, level: LogLevel) -> Self { // Setup the logger if not already set @@ -183,8 +198,7 @@ impl Graph let (width, _) = termion::terminal_size()?; - let prefix_len = 10; - let mut cursor = prefix_len; + let mut cursor = self.prefix_len as u16; let end_cursor = if width % 2 == 0 { width - 2 } else { width - 1 }; // A string representation for a row to be printed @@ -226,9 +240,9 @@ impl Graph Column::Used(ref mut line) => { // Get a row prefix format and keep three characters left let mut row_prefix = format!("{:>w$.*}", - prefix_len as usize - 3, + self.prefix_len - 3, line.name, - w = prefix_len as usize - 3); + w = self.prefix_len - 3); // Get the character to be printed let (c, free_column) = if line.started { @@ -280,7 +294,7 @@ impl Graph // Print the row including the prefix if set let prefix_string = match row.prefix { Some(prefix) => prefix, - _ => iter::repeat(' ').take(prefix_len as usize).collect::(), + _ => iter::repeat(' ').take(self.prefix_len).collect::(), }; println!("{}{}", prefix_string, row.content); diff --git a/tests/lib.rs b/tests/lib.rs index 9687621..a776206 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -83,6 +83,15 @@ fn add_remove_success_4() { assert!(graph.print().is_ok()); } +#[test] +fn print_long_prefix_success() { + let mut graph = Graph::with_prefix_length(18); + assert!(graph.add("Line 1", 0).is_ok()); + assert!(graph.print().is_ok()); + assert!(graph.add("A very long prefix", 0).is_ok()); + assert!(graph.print().is_ok()); +} + #[test] fn print_if_new_data_success() { let mut graph = Graph::new();