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
use super::Glyph;
use std::fs::File;
use std::io::{self, Read};
use std::path::Path;
/// Read a frame from file.
pub fn from_file<P>(filename: &P) -> Option<(usize, Vec<Glyph>)>
where
P: AsRef<Path>,
{
let mut result = None;
if let Ok(file) = File::open(filename) {
let mut read_string = String::with_capacity(1024);
let mut br = io::BufReader::new(file);
if br.read_to_string(&mut read_string).is_ok() {
let mut cs = 0;
let mut rs = 0;
let mut glyph = Glyph::default();
let mut frame = Vec::new();
for line in read_string.lines() {
rs += 1;
let mut style_started = false;
let mut style_definition = String::new();
for char in line.chars() {
match char {
'\x1b' => {
if !style_definition.is_empty() {
glyph.update_from_str(&style_definition);
style_definition.clear();
}
style_started = true;
style_definition.push(char);
}
'm' => {
style_definition.push(char);
if style_started {
style_started = false;
} else {
glyph.update_from_str(&style_definition);
frame.push(glyph);
style_definition.clear();
cs += 1;
}
}
'\n' => {
continue;
}
_ => {
style_definition.push(char);
if !style_started {
glyph.update_from_str(&style_definition);
frame.push(glyph);
style_definition.clear();
cs += 1;
}
}
}
}
}
cs /= rs;
if !frame.is_empty() {
result = Some((cs, frame));
} else {
eprintln!("Frame empty!");
}
} else {
eprintln!("Unable to read file!");
}
}
result
}