Skip to content

Commit

Permalink
Added possibility to define the prefix length by your own
Browse files Browse the repository at this point in the history
  • Loading branch information
Sascha Grunert authored and Sascha Grunert committed Jan 5, 2017
1 parent a0c258e commit 68c4cb6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"]
Expand Down
24 changes: 19 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ use termion::color::{self, LightBlack, Reset, Fg};
pub struct Graph<V> {
lines_to_be_removed: Vec<String>,
columns: Vec<Column<V>>,
prefix_len: usize,
}

impl<V> Graph<V>
Expand All @@ -69,12 +70,26 @@ impl<V> Graph<V>
/// let _ :Graph<u8> = 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<u8> = 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
Expand Down Expand Up @@ -183,8 +198,7 @@ impl<V> Graph<V>

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
Expand Down Expand Up @@ -226,9 +240,9 @@ impl<V> Graph<V>
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 {
Expand Down Expand Up @@ -280,7 +294,7 @@ impl<V> Graph<V>
// 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::<String>(),
_ => iter::repeat(' ').take(self.prefix_len).collect::<String>(),
};
println!("{}{}", prefix_string, row.content);

Expand Down
9 changes: 9 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 68c4cb6

Please sign in to comment.