aboutsummaryrefslogtreecommitdiff
path: root/src/output.rs
diff options
context:
space:
mode:
authorDaniel Schadt <kingdread@gmx.de>2019-12-08 12:23:28 +0100
committerDaniel Schadt <kingdread@gmx.de>2019-12-08 12:23:28 +0100
commit3f24517793c59a61dbbb1513124eb177201c15ba (patch)
tree9da92ef8898104d8cc2abbbc0a3b449992845ab6 /src/output.rs
parentd452c64894d5067277a8842bbf0c8c9365f6b137 (diff)
downloadkondou-3f24517793c59a61dbbb1513124eb177201c15ba.tar.gz
kondou-3f24517793c59a61dbbb1513124eb177201c15ba.tar.bz2
kondou-3f24517793c59a61dbbb1513124eb177201c15ba.zip
add console output for build templates
Diffstat (limited to 'src/output.rs')
-rw-r--r--src/output.rs87
1 files changed, 87 insertions, 0 deletions
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<api::Skill>) -> String {
+ match *skill {
+ None => "none".to_owned(),
+ Some(ref skill) => skill.name.to_owned(),
+ }
+}
+
+fn format_traitline(traitline: &Option<Traitline>) -> 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<E: Error + ?Sized>(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(())
+}