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
/**
 * Flow - Realtime log analyzer
 * Copyright (C) 2016 Daniel Mircea
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

use std::cmp::{min, max};
use std::cell::Cell;
use std::ops::Index;

use core::line::{Line, LineCollection, Parser as LineParser};
use core::filter::Filter;
use ui::search::Query;

static DEFAULT_REVERSE_INDEX: usize = 0;
static MAX_LINES_RENDERED: usize = 2_000;

pub struct Buffer {
    pub filter: Filter,
    pub reverse_index: Cell<usize>,
}

impl Buffer {
    pub fn new(filter: Filter) -> Buffer {
        Buffer {
            filter: filter,
            reverse_index: Cell::new(DEFAULT_REVERSE_INDEX),
        }
    }

    pub fn with_lines<'a>(&'a self, lines: &'a LineCollection) -> BufferLines<'a> {
        BufferLines::new(self, lines)
    }

    pub fn increment_reverse_index(&self, value: i32, max_value: usize) {
        self.set_reverse_index(self.reverse_index.get() as i32 + value, max_value);
    }

    pub fn set_reverse_index(&self, value: i32, max_value: usize) {
        self.reverse_index.set(min(max(0, value) as usize, max_value));
    }

    pub fn is_scrolled(&self) -> bool {
        self.reverse_index.get() != DEFAULT_REVERSE_INDEX
    }

    pub fn reset_reverse_index(&self) {
        self.reverse_index.set(DEFAULT_REVERSE_INDEX);
    }
}

pub struct BufferLines<'a> {
    lines: &'a LineCollection,
    pub buffer: &'a Buffer,
    pub width: Option<usize>,
    pub query: Option<Query>,
}

impl<'a> BufferLines<'a> {
    fn new(buffer: &'a Buffer, lines: &'a LineCollection) -> BufferLines<'a> {
        BufferLines {
            buffer: buffer,
            lines: lines,
            width: None,
            query: None,
        }
    }

    pub fn set_context(&mut self, width: usize, query: Option<Query>) {
        self.width = Some(width);
        self.query = query;
    }
}

impl<'a> Index<usize> for BufferLines<'a> {
    type Output = Line;

    fn index(&self, _index: usize) -> &Line {
        self.into_iter()
            .skip(_index)
            .next()
            .unwrap()
    }
}

impl<'a> IntoIterator for &'a BufferLines<'a> {
    type Item = &'a Line;
    type IntoIter = ::std::vec::IntoIter<&'a Line>;

    fn into_iter(self) -> Self::IntoIter {
        let width = self.width.unwrap();
        let mut estimated_height = 0;

        let height_within_boundary = |line: &&Line| -> bool {
            estimated_height += line.guess_height(width);
            estimated_height <= MAX_LINES_RENDERED
        };

        let lines_iter = self.lines
            .entries
            .iter()
            .parse(self.buffer.filter.clone());

        let mut lines = match self.query {
            Some(ref value) => {
                lines_iter.filter(|line| {
                        !value.filter ||
                        (value.filter && line.content_without_ansi.contains(&value.text))
                    })
                    .take_while(height_within_boundary)
                    .collect::<Vec<_>>()
            }
            None => lines_iter.take_while(height_within_boundary).collect::<Vec<_>>(),
        };

        lines.reverse();
        lines.into_iter()
    }
}

pub struct BufferCollection {
    items: Vec<Buffer>,
    index: usize,
}

impl BufferCollection {
    pub fn from_filters(filters: Vec<Filter>) -> BufferCollection {
        let items = filters.iter().map(|e| Buffer::new(e.clone())).collect();

        BufferCollection {
            items: items,
            index: 0,
        }
    }

    pub fn selected_item(&self) -> &Buffer {
        self.items.get(self.index).unwrap()
    }

    pub fn select_previous(&mut self) {
        if self.index > 0 {
            self.index -= 1;
        }
    }

    pub fn select_next(&mut self) {
        if self.index + 1 < self.items.len() {
            self.index += 1;
        }
    }
}