diff options
author | Lars Wirzenius <liw@liw.fi> | 2019-09-22 21:39:38 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2019-09-22 21:39:38 +0300 |
commit | 2936f3cf609bff77ef0851ebf55b35c74d31a67a (patch) | |
tree | 638667b1b383c69d4b434a00ea60d00efc520687 /src | |
parent | 2e87edb1382f8e00f52b0ca3e11c3aff39c57a7f (diff) | |
download | roadmap-2936f3cf609bff77ef0851ebf55b35c74d31a67a.tar.gz |
Change: wrap long labels to 30 chars
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/roadmap2dot.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 49 |
2 files changed, 34 insertions, 19 deletions
diff --git a/src/bin/roadmap2dot.rs b/src/bin/roadmap2dot.rs index 4e40bc6..16ec0f0 100644 --- a/src/bin/roadmap2dot.rs +++ b/src/bin/roadmap2dot.rs @@ -17,6 +17,8 @@ use std::path::PathBuf; use std::fs::File; use std::io::Read; +const LABEL_WIDTH: usize = 30; + #[derive(Debug, StructOpt)] #[structopt(name = "roadmap2dot", about = "Create a dot graph of a roadmap in YAML")] struct Opt { @@ -33,7 +35,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> { f.read_to_string(&mut text)?; let r = Roadmap::from_yaml(&text)?; - println!("{}", r.as_dot().unwrap()); + println!("{}", r.as_dot(LABEL_WIDTH).unwrap()); Ok(()) } @@ -24,14 +24,15 @@ //! assert_eq!(n.len(), 2); //! assert!(n.contains(&"first")); //! assert!(n.contains(&"endgoal")); -//! +//! //! # Ok(()) //! # } //! ``` -use std::collections::HashMap; use serde_yaml; use serde_yaml::Value; +use std::collections::HashMap; +use textwrap::fill; /// A step in a roadmap. #[derive(Clone)] @@ -62,7 +63,7 @@ impl Step { } /// Return vector of names of dependencies for a step. - pub fn dependencies(&self) -> impl Iterator<Item=&String> { + pub fn dependencies(&self) -> impl Iterator<Item = &String> { self.depends.iter() } @@ -115,7 +116,7 @@ impl Roadmap { return Ok(step); } Ok(Step::new(name, "")) - }, + } _ => Err("step is not a mapping"), } } @@ -147,22 +148,26 @@ impl Roadmap { } // Get iterator over refs to steps. - pub fn iter(&self) -> impl Iterator<Item=&Step> { + pub fn iter(&self) -> impl Iterator<Item = &Step> { self.steps.iter() } // Get iterator over mut refs to steps. - pub fn iter_mut(&mut self) -> impl Iterator<Item=&mut Step> { + pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut Step> { self.steps.iter_mut() } /// Get a Graphviz dot language representation of a roadmap. This /// is the textual representation, and the caller needs to use the /// Graphviz dot(1) tool to create an image from it. - pub fn as_dot(self) -> Result<String, Box<dyn std::error::Error>> { - let labels = self.steps.iter().map(|step| - format!("{} [label=\"{}\"];\n", step.name(), step.label()) - ); + pub fn as_dot(self, label_width: usize) -> Result<String, Box<dyn std::error::Error>> { + let labels = self.steps.iter().map(|step| { + format!( + "{} [label=\"{}\"];\n", + step.name(), + fill(&step.label(), label_width).replace("\n", "\\n") + ) + }); let mut dot = String::new(); dot.push_str("digraph \"roadmap\" {\n"); @@ -183,10 +188,9 @@ impl Roadmap { } } - #[cfg(test)] mod tests { - use super::{Roadmap,Step}; + use super::{Roadmap, Step}; #[test] fn new_step() { @@ -232,9 +236,12 @@ mod tests { #[test] fn empty_dot() { let roadmap = Roadmap::new(); - assert_eq!(roadmap.as_dot().unwrap(), "digraph \"roadmap\" { + assert_eq!( + roadmap.as_dot(999).unwrap(), + "digraph \"roadmap\" { } -"); +" + ); } #[test] @@ -245,12 +252,15 @@ mod tests { second.add_dependency("first"); roadmap.add_step(&first).unwrap(); roadmap.add_step(&second).unwrap(); - assert_eq!(roadmap.as_dot().unwrap(), "digraph \"roadmap\" { + assert_eq!( + roadmap.as_dot(999).unwrap(), + "digraph \"roadmap\" { first [label=\"\"]; second [label=\"\"]; first -> second; } -"); +" + ); } #[test] @@ -261,14 +271,17 @@ first -> second; #[test] fn from_nonempty_yaml() { - let roadmap = Roadmap::from_yaml(" + let roadmap = Roadmap::from_yaml( + " first: label: the first step second: label: the second step depends: - first -").unwrap(); +", + ) + .unwrap(); let names = roadmap.step_names(); assert_eq!(names.len(), 2); |