| 1 | pub struct Buffer { |
| 2 | pub file: Option<String>, |
| 3 | pub lines: Vec<String> |
| 4 | } |
| 5 | |
| 6 | impl Buffer { |
| 7 | pub fn from_file(file: Option<String>) -> Self { |
| 8 | let lines = match &file { |
| 9 | Some(file) => std::fs::read_to_string(file) |
| 10 | .unwrap() |
| 11 | .lines() |
| 12 | .map(|s| s.to_string()) |
| 13 | .collect(), |
| 14 | None => vec![], |
| 15 | }; |
| 16 | |
| 17 | Self { file, lines } |
| 18 | } |
| 19 | |
| 20 | pub fn get(&self, line: usize) -> Option<String> { |
| 21 | if self.lines.len() >= line { |
| 22 | return Some(self.lines[line].clone()) |
| 23 | } |
| 24 | |
| 25 | None |
| 26 | } |
| 27 | |
| 28 | } |
