From 3f24517793c59a61dbbb1513124eb177201c15ba Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sun, 8 Dec 2019 12:23:28 +0100 Subject: add console output for build templates --- src/output.rs | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/output.rs (limited to 'src/output.rs') diff --git a/src/output.rs b/src/output.rs new file mode 100644 index 0000000..2a2546f --- /dev/null +++ b/src/output.rs @@ -0,0 +1,87 @@ +use super::{ + api, + bt::{BuildTemplate, Traitline}, +}; +use std::{error::Error, io, io::Write}; +use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; + +const HEADER_COLOR: Color = Color::Cyan; + +fn format_skill(skill: &Option) -> String { + match *skill { + None => "none".to_owned(), + Some(ref skill) => skill.name.to_owned(), + } +} + +fn format_traitline(traitline: &Option) -> String { + match *traitline { + None => "none".to_owned(), + Some((ref spec, choices)) => format!( + "{} - {} - {} - {}", + spec.name, choices[0], choices[1], choices[2], + ), + } +} + +pub fn show_build_template(build: &BuildTemplate) -> io::Result<()> { + let mut stdout = StandardStream::stdout(ColorChoice::Auto); + let mut color_spec = ColorSpec::new(); + color_spec.set_fg(Some(HEADER_COLOR)); + color_spec.set_bold(true); + + let mut fields = vec![("Profession:", build.profession().to_string())]; + + fields.push(("Skills:", format_skill(&build.skills()[0]))); + for skill in build.skills().iter().skip(1) { + fields.push(("", format_skill(&skill))); + } + + fields.push(("Traitlines:", format_traitline(&build.traitlines()[0]))); + for traitline in build.traitlines().iter().skip(1) { + fields.push(("", format_traitline(&traitline))); + } + + for (header, field) in &fields { + stdout.set_color(&color_spec)?; + write!(stdout, "{:15}", header)?; + stdout.reset()?; + writeln!(stdout, "{}", field)?; + } + + writeln!(stdout)?; + stdout.set_color(&color_spec)?; + write!(stdout, "{:15}", "Chat code:")?; + stdout.reset()?; + writeln!(stdout, "{}", build.chatlink())?; + + Ok(()) +} + +pub fn show_error(error: &E) -> io::Result<()> { + let mut error_color = ColorSpec::new(); + error_color.set_fg(Some(Color::Red)); + let mut stderr = StandardStream::stderr(ColorChoice::Auto); + + stderr.set_color(&error_color)?; + write!(stderr, "[Error]")?; + stderr.reset()?; + writeln!(stderr, " {}", error)?; + + let mut source = error.source(); + if source.is_none() { + source = error.cause(); + } + while let Some(s) = source { + stderr.set_color(&error_color)?; + write!(stderr, " [caused by]")?; + stderr.reset()?; + writeln!(stderr, " {}", s)?; + + source = s.source(); + if source.is_none() { + source = s.cause(); + } + } + Ok(()) +} -- cgit v1.2.3