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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use console::{Console, ConsoleTextKind};
use crossbeam_channel::{Receiver, Sender};
use memmap::Mmap;
use pipeline::{Pipeline, PipelineInfo};
use pipeline_matcher::PathMatch;
use std::fs::File;
use std::io::Error;
use std::ops::Deref;
use std::time::{Duration, Instant};
use util::{catch, decode_error};

// ---------------------------------------------------------------------------------------------------------------------
// PipelinePrinter
// ---------------------------------------------------------------------------------------------------------------------

pub struct PipelinePrinter {
    pub is_color: bool,
    pub print_file: bool,
    pub print_column: bool,
    pub print_row: bool,
    pub infos: Vec<String>,
    pub errors: Vec<String>,
    console: Console,
    time_beg: Instant,
    time_bsy: Duration,
}

impl PipelinePrinter {
    pub fn new() -> Self {
        PipelinePrinter {
            is_color: true,
            print_file: true,
            print_column: false,
            print_row: false,
            infos: Vec::new(),
            errors: Vec::new(),
            console: Console::new(),
            time_beg: Instant::now(),
            time_bsy: Duration::new(0, 0),
        }
    }

    fn print_match(&mut self, pm: PathMatch) {
        if pm.matches.is_empty() {
            return;
        }
        self.console.is_color = self.is_color;

        let result = catch::<_, (), Error>(|| {
            let file = try!(File::open(&pm.path));
            let mmap = try!(unsafe { Mmap::map(&file) });
            let src = mmap.deref();

            let mut pos = 0;
            let mut column = 0;
            let mut last_lf = 0;
            for m in &pm.matches {
                if self.print_file {
                    self.console.write(ConsoleTextKind::Filename, pm.path.to_str().unwrap());
                    self.console.write(ConsoleTextKind::Filename, ":");
                }
                if self.print_column | self.print_row {
                    while pos < m.beg {
                        if src[pos] == 0x0a {
                            column += 1;
                            last_lf = pos;
                        }
                        pos += 1;
                    }
                    if self.print_column {
                        self.console.write(ConsoleTextKind::Other, &format!("{}:", column + 1));
                    }
                    if self.print_row {
                        self.console
                            .write(ConsoleTextKind::Other, &format!("{}:", m.beg - last_lf));
                    }
                }

                self.console.write_match_line(src, m);
            }

            Ok(())
        });
        match result {
            Ok(_) => (),
            Err(e) => self.console.write(
                ConsoleTextKind::Error,
                &format!("Error: {} @ {:?}\n", decode_error(e.kind()), pm.path),
            ),
        }
    }
}

impl Pipeline<PathMatch, ()> for PipelinePrinter {
    fn setup(&mut self, id: usize, rx: Receiver<PipelineInfo<PathMatch>>, tx: Sender<PipelineInfo<()>>) {
        self.infos = Vec::new();
        self.errors = Vec::new();
        let mut seq_beg_arrived = false;

        loop {
            match rx.recv() {
                Ok(PipelineInfo::SeqDat(x, pm)) => {
                    watch_time!(self.time_bsy, {
                        self.print_match(pm);
                        let _ = tx.send(PipelineInfo::SeqDat(x, ()));
                    });
                }

                Ok(PipelineInfo::SeqBeg(x)) => {
                    if !seq_beg_arrived {
                        self.time_beg = Instant::now();
                        let _ = tx.send(PipelineInfo::SeqBeg(x));
                        seq_beg_arrived = true;
                    }
                }

                Ok(PipelineInfo::SeqEnd(x)) => {
                    for i in &self.infos {
                        let _ = tx.send(PipelineInfo::MsgInfo(id, i.clone()));
                    }
                    for e in &self.errors {
                        let _ = tx.send(PipelineInfo::MsgErr(id, e.clone()));
                    }

                    let _ = tx.send(PipelineInfo::MsgTime(id, self.time_bsy, self.time_beg.elapsed()));
                    let _ = tx.send(PipelineInfo::SeqEnd(x));
                    break;
                }

                Ok(PipelineInfo::MsgInfo(i, e)) => {
                    let _ = tx.send(PipelineInfo::MsgInfo(i, e));
                }
                Ok(PipelineInfo::MsgErr(i, e)) => {
                    let _ = tx.send(PipelineInfo::MsgErr(i, e));
                }
                Ok(PipelineInfo::MsgTime(i, t0, t1)) => {
                    let _ = tx.send(PipelineInfo::MsgTime(i, t0, t1));
                }
                Err(_) => break,
            }
        }
    }
}