-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcore.rs
79 lines (74 loc) · 3.73 KB
/
core.rs
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
use crate::cli::Opt;
use itertools::Itertools;
use std::cmp;
use std::io::{self, BufRead};
use std::ops::RangeInclusive;
pub fn print_minimap(reader: Box<dyn BufRead>, opt: &Opt) -> io::Result<()> {
let (hscale, vscale) = (opt.hscale, opt.vscale);
let mut frame = vec![0..=0; 4];
for chunk in &reader
.lines()
.enumerate()
.map(|(i, line)| (scale(i, vscale), line))
.group_by(|(i, _)| *i)
.into_iter()
.chunks(4)
{
for (i, (_, group)) in chunk.enumerate() {
let (mut beg, mut end) = (usize::max_value(), 0);
for (_, line) in group {
let line: String = line?;
beg = cmp::min(beg, line.find(|c: char| !c.is_whitespace()).unwrap_or(beg));
end = cmp::max(end, line.rfind(|c: char| !c.is_whitespace()).unwrap_or(end));
}
frame[i] = beg..=end;
}
scale_frame(&mut frame, hscale);
print_frame(&frame, opt.padding);
}
Ok(())
}
fn print_frame(frame: &Vec<RangeInclusive<usize>>, padding: Option<usize>) {
let idx = |pos| {
frame
.iter()
.enumerate()
.fold(0, |acc, (i, x)| if x.contains(&pos) { acc + (1 << i) } else { acc })
};
let end = frame.iter().max_by_key(|range| range.end()).unwrap().end();
let line: String = (0..=*end)
.step_by(2)
.map(|i| BRAILLE_MATRIX[(idx(i)) + (idx(i + 1) << 4)])
.collect();
match padding {
Some(padding) => println!("{0:<1$}", line, padding),
None => println!("{}", line),
}
}
fn scale_frame(frame: &mut Vec<RangeInclusive<usize>>, factor: f64) {
for x in frame.iter_mut() {
*x = RangeInclusive::new(scale(*x.start(), factor), scale(*x.end(), factor));
}
}
fn scale(x: usize, factor: f64) -> usize {
(x as f64 * factor) as usize
}
#[cfg_attr(rustfmt, rustfmt_skip)]
const BRAILLE_MATRIX : [char; 256] = [
'⠀', '⠁', '⠂', '⠃', '⠄', '⠅', '⠆', '⠇', '⡀', '⡁', '⡂', '⡃', '⡄', '⡅', '⡆', '⡇',
'⠈', '⠉', '⠊', '⠋', '⠌', '⠍', '⠎', '⠏', '⡈', '⡉', '⡊', '⡋', '⡌', '⡍', '⡎', '⡏',
'⠐', '⠑', '⠒', '⠓', '⠔', '⠕', '⠖', '⠗', '⡐', '⡑', '⡒', '⡓', '⡔', '⡕', '⡖', '⡗',
'⠘', '⠙', '⠚', '⠛', '⠜', '⠝', '⠞', '⠟', '⡘', '⡙', '⡚', '⡛', '⡜', '⡝', '⡞', '⡟',
'⠠', '⠡', '⠢', '⠣', '⠤', '⠥', '⠦', '⠧', '⡠', '⡡', '⡢', '⡣', '⡤', '⡥', '⡦', '⡧',
'⠨', '⠩', '⠪', '⠫', '⠬', '⠭', '⠮', '⠯', '⡨', '⡩', '⡪', '⡫', '⡬', '⡭', '⡮', '⡯',
'⠰', '⠱', '⠲', '⠳', '⠴', '⠵', '⠶', '⠷', '⡰', '⡱', '⡲', '⡳', '⡴', '⡵', '⡶', '⡷',
'⠸', '⠹', '⠺', '⠻', '⠼', '⠽', '⠾', '⠿', '⡸', '⡹', '⡺', '⡻', '⡼', '⡽', '⡾', '⡿',
'⢀', '⢁', '⢂', '⢃', '⢄', '⢅', '⢆', '⢇', '⣀', '⣁', '⣂', '⣃', '⣄', '⣅', '⣆', '⣇',
'⢈', '⢉', '⢊', '⢋', '⢌', '⢍', '⢎', '⢏', '⣈', '⣉', '⣊', '⣋', '⣌', '⣍', '⣎', '⣏',
'⢐', '⢑', '⢒', '⢓', '⢔', '⢕', '⢖', '⢗', '⣐', '⣑', '⣒', '⣓', '⣔', '⣕', '⣖', '⣗',
'⢘', '⢙', '⢚', '⢛', '⢜', '⢝', '⢞', '⢟', '⣘', '⣙', '⣚', '⣛', '⣜', '⣝', '⣞', '⣟',
'⢠', '⢡', '⢢', '⢣', '⢤', '⢥', '⢦', '⢧', '⣠', '⣡', '⣢', '⣣', '⣤', '⣥', '⣦', '⣧',
'⢨', '⢩', '⢪', '⢫', '⢬', '⢭', '⢮', '⢯', '⣨', '⣩', '⣪', '⣫', '⣬', '⣭', '⣮', '⣯',
'⢰', '⢱', '⢲', '⢳', '⢴', '⢵', '⢶', '⢷', '⣰', '⣱', '⣲', '⣳', '⣴', '⣵', '⣶', '⣷',
'⢸', '⢹', '⢺', '⢻', '⢼', '⢽', '⢾', '⢿', '⣸', '⣹', '⣺', '⣻', '⣼', '⣽', '⣾', '⣿',
];