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
use itertools::Itertools;
use std::cell::RefCell;
use std::cmp;
use std::io::{self, BufRead, Write};
use std::ops::Range;
use std::rc::Rc;
use std::vec::Vec;

/// Write minimap to output
pub fn write(
    output: Rc<RefCell<dyn Write>>,
    reader: Box<dyn BufRead>,
    hscale: f64,
    vscale: f64,
    padding: Option<usize>,
) -> io::Result<()> {
    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)
    {
        let mut chunk_size = 0;
        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 + 1);
            chunk_size += 1;
        }
        frame.iter_mut().skip(chunk_size).for_each(|row| *row = 0..0);
        scale_frame(&mut frame, hscale);
        write_frame(output.clone(), &frame, padding)?;
    }
    Ok(())
}

/// Print minimap to stdout
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::io;
/// use std::io::BufReader;
///
/// let reader = Box::new(BufReader::new(io::stdin()));
/// code_minimap::print(reader, 1.0, 1.0, None).unwrap();
/// ```
pub fn print(reader: Box<dyn BufRead>, hscale: f64, vscale: f64, padding: Option<usize>) -> io::Result<()> {
    write(Rc::new(RefCell::new(io::stdout())), reader, hscale, vscale, padding)
}

/// Write minimap to string
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use std::io;
/// use std::io::BufReader;
///
/// let reader = Box::new(BufReader::new(io::stdin()));
/// let s = code_minimap::write_to_string(reader, 1.0, 1.0, None).unwrap();
/// print!("{}", s);
/// ```
pub fn write_to_string(
    reader: Box<dyn BufRead>,
    hscale: f64,
    vscale: f64,
    padding: Option<usize>,
) -> io::Result<String> {
    let buf = Rc::new(RefCell::new(Vec::new()));
    write(buf.clone(), reader, hscale, vscale, padding)?;
    let buf = Rc::try_unwrap(buf).unwrap().into_inner();
    Ok(String::from_utf8(buf).unwrap())
}

fn write_frame(output: Rc<RefCell<dyn Write>>, frame: &[Range<usize>], padding: Option<usize>) -> std::io::Result<()> {
    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) => writeln!(output.borrow_mut(), "{0:<1$}", line, padding),
        None => writeln!(output.borrow_mut(), "{}", line),
    }
}

fn scale_frame(frame: &mut [Range<usize>], factor: f64) {
    for x in frame.iter_mut() {
        *x = scale(x.start, factor)..scale(x.end, factor);
    }
}

fn scale(x: usize, factor: f64) -> usize {
    (x as f64 * factor) as usize
}

#[rustfmt::skip]
const BRAILLE_MATRIX : [char; 256] = [
    '⠀', '⠁', '⠂', '⠃', '⠄', '⠅', '⠆', '⠇', '⡀', '⡁', '⡂', '⡃', '⡄', '⡅', '⡆', '⡇',
    '⠈', '⠉', '⠊', '⠋', '⠌', '⠍', '⠎', '⠏', '⡈', '⡉', '⡊', '⡋', '⡌', '⡍', '⡎', '⡏',
    '⠐', '⠑', '⠒', '⠓', '⠔', '⠕', '⠖', '⠗', '⡐', '⡑', '⡒', '⡓', '⡔', '⡕', '⡖', '⡗',
    '⠘', '⠙', '⠚', '⠛', '⠜', '⠝', '⠞', '⠟', '⡘', '⡙', '⡚', '⡛', '⡜', '⡝', '⡞', '⡟',
    '⠠', '⠡', '⠢', '⠣', '⠤', '⠥', '⠦', '⠧', '⡠', '⡡', '⡢', '⡣', '⡤', '⡥', '⡦', '⡧',
    '⠨', '⠩', '⠪', '⠫', '⠬', '⠭', '⠮', '⠯', '⡨', '⡩', '⡪', '⡫', '⡬', '⡭', '⡮', '⡯',
    '⠰', '⠱', '⠲', '⠳', '⠴', '⠵', '⠶', '⠷', '⡰', '⡱', '⡲', '⡳', '⡴', '⡵', '⡶', '⡷',
    '⠸', '⠹', '⠺', '⠻', '⠼', '⠽', '⠾', '⠿', '⡸', '⡹', '⡺', '⡻', '⡼', '⡽', '⡾', '⡿',
    '⢀', '⢁', '⢂', '⢃', '⢄', '⢅', '⢆', '⢇', '⣀', '⣁', '⣂', '⣃', '⣄', '⣅', '⣆', '⣇',
    '⢈', '⢉', '⢊', '⢋', '⢌', '⢍', '⢎', '⢏', '⣈', '⣉', '⣊', '⣋', '⣌', '⣍', '⣎', '⣏',
    '⢐', '⢑', '⢒', '⢓', '⢔', '⢕', '⢖', '⢗', '⣐', '⣑', '⣒', '⣓', '⣔', '⣕', '⣖', '⣗',
    '⢘', '⢙', '⢚', '⢛', '⢜', '⢝', '⢞', '⢟', '⣘', '⣙', '⣚', '⣛', '⣜', '⣝', '⣞', '⣟',
    '⢠', '⢡', '⢢', '⢣', '⢤', '⢥', '⢦', '⢧', '⣠', '⣡', '⣢', '⣣', '⣤', '⣥', '⣦', '⣧',
    '⢨', '⢩', '⢪', '⢫', '⢬', '⢭', '⢮', '⢯', '⣨', '⣩', '⣪', '⣫', '⣬', '⣭', '⣮', '⣯',
    '⢰', '⢱', '⢲', '⢳', '⢴', '⢵', '⢶', '⢷', '⣰', '⣱', '⣲', '⣳', '⣴', '⣵', '⣶', '⣷',
    '⢸', '⢹', '⢺', '⢻', '⢼', '⢽', '⢾', '⢿', '⣸', '⣹', '⣺', '⣻', '⣼', '⣽', '⣾', '⣿',
];