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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use std::cell::RefCell;
use std::collections::HashMap;
use std::io::Write;
use std::io::{stdin, stdout, Stdout};
use termion::event::Key;
use termion::input::{MouseTerminal, TermRead};
use termion::raw::{IntoRawMode, RawTerminal};
use termion::screen::AlternateScreen;

type Terminal = RefCell<MouseTerminal<AlternateScreen<RawTerminal<Stdout>>>>;

pub struct Moins<'a> {
    lines: Vec<&'a str>,
    height: u16,
    current_line: usize,
    scroll: usize,
    screen: Terminal,
    options: Option<PagerOptions>,
}

/// options for `Moins` see the examples
pub struct PagerOptions {
    /// add color to the matching term
    pub colors: HashMap<String, Color>,
    pub search: bool,
    pub line_number: bool,
}

impl<'a> Moins<'a> {
    /// run moins pager
    pub fn run(content: &'a mut String, options: Option<PagerOptions>) {
        let stdout = stdout().into_raw_mode().unwrap();
        let screen = MouseTerminal::from(AlternateScreen::from(stdout));
        let screen = RefCell::new(screen);

        let mut pager = Moins::new(content, screen, options);

        let stdin = stdin();

        pager.clear();
        pager.write();

        for c in stdin.keys() {
            // Input
            match c.unwrap() {
                Key::Char('q') => break,
                Key::Down | Key::Char('j') => pager.scroll_down(),
                Key::Up | Key::Char('k') => pager.scroll_up(),
                _ => (),
            }
        }
    }

    fn new(content: &'a mut String, screen: Terminal, options: Option<PagerOptions>) -> Self {
        let size = termion::terminal_size().unwrap();
        let width = size.0 as usize;
        let mut lines = vec![];

        content.lines().for_each(|line| {
            if line.len() > width {
                lines.push(&line[0..width]);
                lines.push(&line[width..line.len()]);
            } else {
                lines.push(line);
            }
        });

        let height = size.1 as usize;

        let scroll = if lines.len() <= height {
            lines.len()
        } else {
            height
        };

        Moins {
            lines,
            scroll,
            screen,
            current_line: 0,
            height: size.1,
            options,
        }
    }

    fn color(&self, line: String) -> String {
        if let Some(options) = &self.options {
            let reset = Color::Reset;
            let mut colored_line = line.clone();

            options.colors.iter().for_each(|(term, color)| {
                let mut find_idx = 0;

                while let Some(term_idx) =
                    colored_line[find_idx..colored_line.len()].rfind(term.as_str())
                {
                    let color = color.get();
                    colored_line.insert_str(term_idx, color);
                    find_idx = term_idx + color.len() + term.len();
                    colored_line.insert_str(find_idx, reset.get());
                    find_idx = find_idx + reset.get().len();
                }
            });
            colored_line
        } else {
            line
        }
    }

    fn clear(&mut self) {
        write!(
            self.screen.borrow_mut(),
            "{}{}{}",
            termion::clear::All,
            termion::cursor::Goto(1, 1),
            termion::cursor::Hide,
        )
        .unwrap();
    }

    fn flush(&mut self) {
        self.screen.borrow_mut().flush().unwrap();
    }

    fn scroll_as_u16(&self) -> u16 {
        self.scroll as u16
    }

    fn write(&mut self) {
        write!(
            self.screen.borrow_mut(),
            "{}{}",
            termion::cursor::Goto(1, self.scroll_as_u16()),
            termion::clear::CurrentLine,
        )
        .unwrap();

        let height = self.height as usize;

        let offset = if self.current_line + height > self.lines.len() {
            self.lines.len()
        } else {
            self.current_line + height
        };

        self.lines[self.current_line..offset]
            .into_iter()
            .enumerate()
            .for_each(|(idx, line)| {
                write!(
                    self.screen.borrow_mut(),
                    "{}{}{}{}",
                    termion::cursor::Goto(1, (idx + 1) as u16),
                    termion::clear::CurrentLine,
                    self.color(line.to_string()),
                    termion::cursor::Hide
                )
                .unwrap();
            });
        self.flush();
    }

    fn scroll_down(&mut self) {
        self.scroll = if self.scroll == self.height as usize {
            self.height as usize
        } else {
            self.scroll + 1
        };

        let height = self.height as usize;

        self.current_line = if self.lines.len() < height {
            0
        } else if self.current_line == self.lines.len() - height {
            self.lines.len() - height
        } else {
            self.current_line + 1
        };

        print!("{}", termion::scroll::Up(self.scroll_as_u16()));

        self.write();
    }

    fn scroll_up(&mut self) {
        self.scroll = if self.scroll == 1 { 1 } else { self.scroll - 1 };

        self.current_line = if self.current_line == 0 {
            0
        } else {
            self.current_line - 1
        };

        print!("{}", termion::scroll::Up(self.scroll_as_u16()));

        self.write();
    }
}

pub enum Color {
    Black,
    LightBlack,
    Blue,
    LightBlue,
    Cyan,
    LightCyan,
    Green,
    LightGreen,
    Magenta,
    LightMagenta,
    Red,
    LightRed,
    White,
    LightWhite,
    Yellow,
    LightYellow,
    Reset,
}

impl Color {
    fn get(&self) -> &'static str {
        // this might seem a litle bit absurd but we don't want our user to reimport termion colors
        match self {
            Color::Black => termion::color::Black::fg_str(&termion::color::Black {}),
            Color::LightBlack => termion::color::LightBlack::fg_str(&termion::color::LightBlack),
            Color::Blue => termion::color::Blue::fg_str(&termion::color::Blue),
            Color::LightBlue => termion::color::LightBlue::fg_str(&termion::color::LightBlue),
            Color::Cyan => termion::color::Cyan::fg_str(&termion::color::Cyan),
            Color::LightCyan => termion::color::LightCyan::fg_str(&termion::color::LightCyan),
            Color::Green => termion::color::Green::fg_str(&termion::color::Green),
            Color::LightGreen => termion::color::LightGreen::fg_str(&termion::color::LightGreen),
            Color::Magenta => termion::color::Magenta::fg_str(&termion::color::Magenta),
            Color::LightMagenta => {
                termion::color::LightMagenta::fg_str(&termion::color::LightMagenta)
            }
            Color::Red => termion::color::Red::fg_str(&termion::color::Red),
            Color::LightRed => termion::color::LightRed::fg_str(&termion::color::LightRed),
            Color::White => termion::color::White::fg_str(&termion::color::White),
            Color::LightWhite => termion::color::LightWhite::fg_str(&termion::color::LightWhite),
            Color::Yellow => termion::color::Yellow::fg_str(&termion::color::Yellow),
            Color::LightYellow => termion::color::LightYellow::fg_str(&termion::color::LightYellow),
            Color::Reset => termion::color::Reset::fg_str(termion::color::Reset),
        }
    }
}