aboutsummaryrefslogtreecommitdiff
path: root/src/output.rs
blob: 148c08878cad7145886c6cd3d0287bcd2310760d (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
use super::errors::RuntimeError;
use super::{FightOutcome, LogResult, Opt};

use std::io::Write;

/// Write the output to the given stream, according to the command line flags.
pub fn output<W: Write>(mut f: W, opt: &Opt, item: &LogResult) -> Result<(), RuntimeError> {
    if opt.file_name_only {
        writeln!(f, "{}", item.log_file.to_string_lossy())?;
    } else {
        colored(f, item)?;
    }
    Ok(())
}

/// Write the given log result to the given stream, using ANSI colors.
pub fn colored<W: Write>(mut f: W, item: &LogResult) -> Result<(), RuntimeError> {
    use colored::Colorize;
    writeln!(f, "{}: {:?}", "File".green(), item.log_file)?;
    let outcome = match item.outcome {
        FightOutcome::Success => "SUCCESS".green(),
        FightOutcome::Wipe => "WIPE".red(),
    };
    writeln!(
        f,
        "{}: {} - {}: {} {}",
        "Date".green(),
        item.time.format("%Y-%m-%d %H:%M:%S %a"),
        "Boss".green(),
        item.boss_name,
        outcome,
    )?;
    for player in &item.players {
        writeln!(
            f,
            "  {:2} {:20} {:19} {}",
            player.subgroup,
            player.account_name.yellow(),
            player.character_name.cyan(),
            player.profession
        )?;
    }
    writeln!(f)?;
    Ok(())
}