aboutsummaryrefslogtreecommitdiff
path: root/src/output/formats.rs
blob: 47da57ed619297470156340845516d32a7b6ceb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//! A crate defining different output formats for search results.
use std::fmt::Write;

use super::super::guilds;
use super::{FightOutcome, LogResult};

use chrono::Local;
use evtclib::GameMode;

/// An output format
pub trait Format: Sync + Send {
    /// Format a single log result
    fn format_result(&self, item: &LogResult) -> String;
}

/// The human readable, colored format.
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
pub struct HumanReadable {
    pub show_guilds: bool,
}

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.to_string_lossy()
        )
        .expect("writing to String failed");

        if item.game_mode != Some(GameMode::WvW) {
            let outcome = match item.outcome {
                FightOutcome::Success => "SUCCESS".green(),
                FightOutcome::Wipe => "WIPE".red(),
            };
            writeln!(
                result,
                "{}: {} - {}: {}{} {} after {}",
                "Date".green(),
                item.time
                    .with_timezone(&Local)
                    .format("%Y-%m-%d %H:%M:%S %a"),
                "Boss".green(),
                item.encounter
                    .map(|x| x.to_string())
                    .unwrap_or_else(|| "unknown".into()),
                if item.is_cm { " CM" } else { "" },
                outcome,
                humantime::Duration::from(item.duration.to_std().unwrap()),
            )
            .expect("writing to String failed");
        } else {
            writeln!(
                result,
                "{}: {} - {} ended after {}",
                "Date".green(),
                item.time
                    .with_timezone(&Local)
                    .format("%Y-%m-%d %H:%M:%S %a"),
                "World vs. World".green(),
                humantime::Duration::from(item.duration.to_std().unwrap()),
            )
            .expect("writing to String failed");
        }

        for player in item.players.iter().filter(|p| !p.account_name.is_empty()) {
            write!(
                result,
                "  {:2} {:20} {:19} {:12}",
                player.subgroup,
                player.account_name.yellow(),
                player.character_name.cyan(),
                player.profession,
            )
            .expect("writing to String failed");
            if self.show_guilds {
                let guild = player.guild_id.as_ref().and_then(|id| guilds::lookup(id));
                if let Some(guild) = guild {
                    write!(
                        result,
                        " [{}] {}",
                        guild.tag().magenta(),
                        guild.name().magenta(),
                    )
                    .expect("writing to String failed");
                }
            }
            writeln!(result).expect("writing to String failed");
        }
        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 {
        let filename = item.log_file.to_string_lossy();
        format!("{}\n", filename)
    }
}