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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
extern crate term;

use crate::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>,
    last_color: Color,
}

const CR: u8 = 0x0d;
const LF: u8 = 0x0a;

impl Default for Console {
    fn default() -> Self {
        Self::new()
    }
}

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,
            last_color: term::color::BLACK,
        }
    }

    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 get_line_beg(src: &[u8], beg: usize) -> usize {
        let mut ret = beg;
        while ret > 0 {
            if src[ret] == CR || src[ret] == LF {
                ret += 1;
                break;
            }
            ret -= 1;
        }
        ret
    }

    pub fn get_line_end(src: &[u8], end: usize) -> usize {
        let mut ret = end;
        while src.len() > ret {
            if src[ret] == CR || src[ret] == LF {
                ret -= 1;
                break;
            }
            ret += 1;
        }
        if src.len() <= ret {
            ret = src.len()
        } else {
            ret += 1
        };
        ret
    }

    pub fn write_to_linebreak(&mut self, src: &[u8], beg: usize, end: usize) {
        if beg < end {
            self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[beg..end]));
        }
        self.write(ConsoleTextKind::Text, "\n");
    }

    pub fn write_match_part(&mut self, src: &[u8], m: &Match, beg: usize) {
        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]));
    }

    pub fn write_match_line(&mut self, src: &[u8], m: &Match) {
        let beg = Console::get_line_beg(src, m.beg);
        let end = Console::get_line_end(src, m.end);

        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");
    }

    pub fn write_replace_line(&mut self, src: &[u8], m: &Match, rep: &[u8]) {
        let beg = Console::get_line_beg(src, m.beg);
        let end = Console::get_line_end(src, m.end);

        if beg < m.beg {
            self.write(ConsoleTextKind::Text, &String::from_utf8_lossy(&src[beg..m.beg]));
        }
        self.write(ConsoleTextKind::MatchText, &String::from_utf8_lossy(rep));
        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 && self.last_color != color {
            self.term_stdout.fg(color).unwrap_or_else(|_| {
                process::exit(1);
            });
            self.last_color = color;
        }

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

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

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