diff options
author | Daniel <kingdread@gmx.de> | 2019-06-03 02:02:44 +0200 |
---|---|---|
committer | Daniel <kingdread@gmx.de> | 2019-06-03 02:02:44 +0200 |
commit | 5d2f51ab8593946a0f24db367a887a37258901d5 (patch) | |
tree | 498f2af9584046ed63f256375169bbf5756bfb7d /src/output/formats.rs | |
parent | c731b470fc162e56f6d81c475bacb41230a5e2d3 (diff) | |
download | raidgrep-5d2f51ab8593946a0f24db367a887a37258901d5.tar.gz raidgrep-5d2f51ab8593946a0f24db367a887a37258901d5.tar.bz2 raidgrep-5d2f51ab8593946a0f24db367a887a37258901d5.zip |
[WIP] rewrite output logic as a pipeline
Diffstat (limited to 'src/output/formats.rs')
-rw-r--r-- | src/output/formats.rs | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/output/formats.rs b/src/output/formats.rs new file mode 100644 index 0000000..fe6e982 --- /dev/null +++ b/src/output/formats.rs @@ -0,0 +1,68 @@ +//! A crate defining different output formats for search results. +use std::fmt::Write; + +use super::{LogResult, FightOutcome}; + +/// An output format +pub trait Format: Sync + Send { + /// Format a single log result + fn format_result(&self, item: &LogResult) -> String; +} + + +impl<T: Format + ?Sized> Format for Box<T> { + fn format_result(&self, item: &LogResult) -> String { + (&*self).format_result(item) + } +} + + +/// The human readable, colored format. +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +pub struct HumanReadable; + + +impl Format for HumanReadable { + fn format_result(&self, item: &LogResult) -> String { + use colored::Colorize; + let mut result = String::new(); + + writeln!(result, "{}: {:?}", "File".green(), item.log_file).unwrap(); + let outcome = match item.outcome { + FightOutcome::Success => "SUCCESS".green(), + FightOutcome::Wipe => "WIPE".red(), + }; + writeln!( + result, + "{}: {} - {}: {} {}", + "Date".green(), + item.time.format("%Y-%m-%d %H:%M:%S %a"), + "Boss".green(), + item.boss_name, + outcome, + ).unwrap(); + for player in &item.players { + writeln!( + result, + " {:2} {:20} {:19} {}", + player.subgroup, + player.account_name.yellow(), + player.character_name.cyan(), + player.profession + ).unwrap(); + } + result + } +} + + +/// A format which outputs only the file-name +#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)] +pub struct FileOnly; + + +impl Format for FileOnly { + fn format_result(&self, item: &LogResult) -> String { + item.log_file.to_string_lossy().into_owned() + } +} |