1 | use crate::config::Position; |
2 | |
3 | /// The representation of data, rows and columns of a grid. |
4 | pub trait PeekableRecords { |
5 | /// Returns a text of a cell by an index. |
6 | fn get_text(&self, pos: Position) -> &str; |
7 | |
8 | /// Returns a line of a text of a cell by an index. |
9 | fn get_line(&self, pos: Position, line: usize) -> &str { |
10 | self.get_text(pos).lines().nth(line).unwrap() |
11 | } |
12 | |
13 | /// Returns an amount of lines of a text of a cell by an index. |
14 | fn count_lines(&self, pos: Position) -> usize { |
15 | self.get_text(pos).lines().count() |
16 | } |
17 | |
18 | /// Returns a width of a text of a cell by an index. |
19 | fn get_width(&self, pos: Position) -> usize { |
20 | crate::util::string::string_width_multiline(self.get_text(pos)) |
21 | } |
22 | |
23 | /// Returns a width of line of a text of a cell by an index. |
24 | fn get_line_width(&self, pos: Position, line: usize) -> usize { |
25 | crate::util::string::string_width(self.get_line(pos, line)) |
26 | } |
27 | } |
28 | |
29 | impl<R> PeekableRecords for &R |
30 | where |
31 | R: PeekableRecords, |
32 | { |
33 | fn get_text(&self, pos: Position) -> &str { |
34 | R::get_text(self, pos) |
35 | } |
36 | |
37 | fn get_line(&self, pos: Position, line: usize) -> &str { |
38 | R::get_line(self, pos, line) |
39 | } |
40 | |
41 | fn count_lines(&self, pos: Position) -> usize { |
42 | R::count_lines(self, pos) |
43 | } |
44 | |
45 | fn get_width(&self, pos: Position) -> usize { |
46 | R::get_width(self, pos) |
47 | } |
48 | |
49 | fn get_line_width(&self, pos: Position, line: usize) -> usize { |
50 | R::get_line_width(self, pos, line) |
51 | } |
52 | } |
53 | |