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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
use crate::{At, Entry, Format, Record};
use core::fmt::{self, Write};
#[cfg(feature = "std")]
use std::time::SystemTime;

/// Configurable display formatting for the [`Record`].
pub struct Display<'a, E, S> {
    record: &'a Record<E, S>,
    format: Format,
    #[cfg(feature = "std")]
    st_fmt: &'a dyn Fn(SystemTime, SystemTime) -> String,
}

impl<'a, E, S> Display<'a, E, S> {
    /// Show colored output (on by default).
    ///
    /// Requires the `colored` feature to be enabled.
    #[cfg(feature = "colored")]
    pub fn colored(&mut self, on: bool) -> &mut Self {
        self.format.colored = on;
        self
    }

    /// Show detailed output (on by default).
    pub fn detailed(&mut self, on: bool) -> &mut Self {
        self.format.detailed = on;
        self
    }

    /// Show the current position in the output (on by default).
    pub fn head(&mut self, on: bool) -> &mut Self {
        self.format.head = on;
        self
    }

    /// Show the saved edit (on by default).
    pub fn saved(&mut self, on: bool) -> &mut Self {
        self.format.saved = on;
        self
    }

    /// Sets the format used to display [`SystemTime`]s.
    ///
    /// The first input parameter is the current system time.
    /// The second input parameter is the system time of the event.
    #[cfg(feature = "std")]
    pub fn set_st_fmt(
        &mut self,
        st_fmt: &'a dyn Fn(SystemTime, SystemTime) -> String,
    ) -> &mut Self {
        self.st_fmt = st_fmt;
        self
    }
}

impl<E: fmt::Display, S> Display<'_, E, S> {
    fn fmt_list(
        &self,
        f: &mut fmt::Formatter,
        index: usize,
        entry: Option<&Entry<E>>,
        #[cfg(feature = "std")] now: SystemTime,
    ) -> fmt::Result {
        self.format.index(f, index)?;

        #[cfg(feature = "std")]
        if let Some(entry) = entry {
            if self.format.detailed {
                let st_fmt = self.st_fmt;
                let string = st_fmt(now, entry.st_edit());
                self.format.elapsed(f, string)?;
            }
        }

        self.format.labels(
            f,
            At::no_root(index),
            At::no_root(self.record.index),
            self.record.saved.map(At::no_root),
        )?;

        if let Some(entry) = entry {
            if self.format.detailed {
                writeln!(f)?;
                self.format.message(f, entry, None)?;
            } else {
                f.write_char(' ')?;
                self.format.message(f, entry, None)?;
                writeln!(f)?;
            }
        }
        Ok(())
    }
}

impl<'a, E, S> From<&'a Record<E, S>> for Display<'a, E, S> {
    fn from(record: &'a Record<E, S>) -> Self {
        Display {
            record,
            format: Format::default(),
            #[cfg(feature = "std")]
            st_fmt: &crate::format::default_st_fmt,
        }
    }
}

impl<E: fmt::Display, S> fmt::Display for Display<'_, E, S> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        #[cfg(feature = "std")]
        let now = SystemTime::now();
        for (i, entry) in self.record.entries.iter().enumerate().rev() {
            self.fmt_list(
                f,
                i + 1,
                Some(entry),
                #[cfg(feature = "std")]
                now,
            )?;
        }
        self.fmt_list(
            f,
            0,
            None,
            #[cfg(feature = "std")]
            now,
        )
    }
}