Skip to main content

sel/format/
mod.rs

1//! Output formatting.
2
3pub mod ansi;
4pub mod fragment;
5pub mod plain;
6
7pub use fragment::FragmentFormatter;
8pub use plain::PlainFormatter;
9
10use crate::Emit;
11use std::io;
12
13/// A formatter serializes one `Emit` into bytes.
14pub trait Formatter {
15    fn write(&mut self, sink: &mut dyn io::Write, emit: &Emit) -> io::Result<()>;
16}
17
18/// Common configuration shared by plain and fragment formatters.
19#[derive(Debug, Clone)]
20pub struct FormatOpts {
21    pub show_line_numbers: bool,
22    pub show_filename: bool,
23    pub filename: Option<String>,
24    pub color: bool,
25    /// Prepend `"> "` (colorized green) before target lines.
26    /// Set `true` only when mixing target and context lines (i.e. `-c N`).
27    pub target_marker: bool,
28}
29
30impl FormatOpts {
31    pub fn prefix(&self, line_no: u64) -> String {
32        let mut p = String::new();
33        if self.show_filename
34            && let Some(f) = &self.filename
35        {
36            p.push_str(f);
37            p.push(':');
38        }
39        if self.show_line_numbers {
40            p.push_str(&line_no.to_string());
41            p.push(':');
42        }
43        p
44    }
45}