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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
extern crate term;

use matcher::Match;
use std::io;
use std::io::Write;
use std::process;
use term::color::Color;
use term::{StderrTerminal, StdoutTerminal};

// ---------------------------------------------------------------------------------------------------------------------
// Console
// ---------------------------------------------------------------------------------------------------------------------

pub enum ConsoleTextKind {
    Filename,
    Text,
    MatchText,
    Other,
    Info,
    Error,
}

pub struct Console {
    pub is_color: bool,
    term_stdout: Box<StdoutTerminal>,
    term_stderr: Box<StderrTerminal>,
    color_out: Color,
    color_err: Color,
    colored_out: bool,
    colored_err: bool,
}

impl Console {
    pub fn new() -> Self {
        Console {
            term_stdout: term::stdout().unwrap_or_else(|| {
                process::exit(1);
            }),
            term_stderr: term::stderr().unwrap_or_else(|| {
                process::exit(1);
            }),
            is_color: true,
            color_out: term::color::BLACK,
            color_err: term::color::BLACK,
            colored_out: false,
            colored_err: false,
        }
    }

    pub fn carriage_return(&mut self) {
        let _ = self.term_stdout.carriage_return();
    }

    pub fn cursor_up(&mut self) {
        let _ = self.term_stdout.cursor_up();
    }

    pub fn delete_line(&mut self) {
        let _ = self.term_stdout.delete_line();
    }

    pub fn write_with_clear(&mut self, kind: ConsoleTextKind, val: &str) {
        self.carriage_return();
        self.delete_line();
        self.write(kind, val);
    }

    pub fn write(&mut self, kind: ConsoleTextKind, val: &str) {
        let color = match kind {
            ConsoleTextKind::Filename => term::color::BRIGHT_GREEN,
            ConsoleTextKind::Text => term::color::WHITE,
            ConsoleTextKind::MatchText => term::color::BRIGHT_YELLOW,
            ConsoleTextKind::Other => term::color::BRIGHT_CYAN,
            ConsoleTextKind::Info => term::color::BRIGHT_CYAN,
            ConsoleTextKind::Error => term::color::BRIGHT_RED,
        };

        match kind {
            ConsoleTextKind::Error => self.write_stderr(val, color),
            ConsoleTextKind::Info => self.write_stderr(val, color),
            _ => self.write_stdout(val, color),
        }
    }

    pub fn flush(&mut self) {
        let _ = io::stdout().flush();
        let _ = io::stderr().flush();
    }

    pub fn reset(&mut self) {
        self.term_stdout.reset().unwrap_or_else(|_| {
            process::exit(1);
        });
        self.term_stderr.reset().unwrap_or_else(|_| {
            process::exit(1);
        });
    }

    pub fn write_match_line(&mut self, src: &[u8], m: &Match) {
        let mut beg = m.beg;
        let mut end = m.end;
        while beg > 0 {
            if src[beg] == 0x0d || src[beg] == 0x0a {
                beg += 1;
                break;
            }
            beg -= 1;
        }
        while src.len() > end {
            if src[end] == 0x0d || src[end] == 0x0a {
                end -= 1;
                break;
            }
            end += 1;
        }
        if src.len() <= end {
            end = src.len()
        } else {
            end += 1
        };

        if beg < m.beg {
            self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[beg..m.beg]));
        }
        self.write(ConsoleTextKind::MatchText, &String::from_utf8_lossy(&src[m.beg..m.end]));
        if m.end < end {
            self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[m.end..end]));
        }
        self.write(ConsoleTextKind::Text, "\n");
    }

    fn write_stdout(&mut self, val: &str, color: Color) {
        if self.is_color {
            if self.color_out != color {
                self.term_stdout.fg(color).unwrap_or_else(|_| {
                    process::exit(1);
                });
                self.color_out = color;
                self.colored_out = true;
            }
        }

        write!(self.term_stdout, "{}", val).unwrap_or_else(|_| {
            process::exit(1);
        });

        //if self.is_color {
        //    self.term_stdout.reset().unwrap_or_else( |_| { process::exit( 1 ); } );
        //}

        //let _ = io::stdout().flush();
    }

    fn write_stderr(&mut self, val: &str, color: Color) {
        if self.is_color {
            if self.color_err != color {
                self.term_stderr.fg(color).unwrap_or_else(|_| {
                    process::exit(1);
                });
                self.color_err = color;
                self.colored_err = true;
            }
        }

        write!(self.term_stderr, "{}", val).unwrap_or_else(|_| {
            process::exit(1);
        });

        //if self.is_color {
        //    self.term_stderr.reset().unwrap_or_else( |_| { process::exit( 1 ); } );
        //}

        //let _ = io::stderr().flush();
    }
}