use super::lanes::Step;
use crate::style::{c, LANES, RESET};
pub struct Glyphs {
pub commit: char,
pub merge: char,
pub vert: char,
pub horiz: char,
pub cross: char,
pub slide: char,
pub fold_left: char,
pub fold_right: char,
pub branch_right: char,
pub branch_left: char,
pub tee_right: char,
pub tee_left: char,
pub tee_both: char,
pub tee_down: char,
pub tee_up: char,
}
pub const UNICODE: Glyphs = Glyphs {
commit: '\u{25cf}',
merge: '\u{25cb}',
vert: '\u{2502}',
horiz: '\u{2500}',
cross: '\u{253c}',
slide: '\u{2571}',
fold_left: '\u{256f}',
fold_right: '\u{2570}',
branch_right: '\u{256e}',
branch_left: '\u{256d}',
tee_right: '\u{251c}',
tee_left: '\u{2524}',
tee_both: '\u{253c}',
tee_down: '\u{252c}',
tee_up: '\u{2534}',
};
pub const ASCII: Glyphs = Glyphs {
commit: '*',
merge: 'o',
vert: '|',
horiz: '-',
cross: '+',
slide: '/',
fold_left: '/',
fold_right: '\\',
branch_right: '\\',
branch_left: '/',
tee_right: '+',
tee_left: '+',
tee_both: '+',
tee_down: '+',
tee_up: '+',
};
struct Grid<'a> {
cells: Vec<char>,
colors: Vec<usize>,
g: &'a Glyphs,
}
impl<'a> Grid<'a> {
fn new(lanes: usize, g: &'a Glyphs) -> Grid<'a> {
let w = if lanes == 0 { 0 } else { lanes * 2 - 1 };
Grid {
cells: vec![' '; w],
colors: vec![0; w],
g,
}
}
fn put(&mut self, at: usize, ch: char, color: usize) {
if at >= self.cells.len() {
return;
}
let existing = self.cells[at];
let crossing = (existing == self.g.vert && ch == self.g.horiz)
|| (existing == self.g.horiz && ch == self.g.vert);
self.cells[at] = if existing == ' ' {
ch
} else if crossing {
self.g.cross
} else {
ch
};
if !crossing {
self.colors[at] = color;
}
}
fn render(&self, min_width: usize) -> String {
let mut out = String::new();
let mut last: Option<usize> = None;
for (i, ch) in self.cells.iter().enumerate() {
if *ch == ' ' {
if last.is_some() {
out.push_str(c(RESET));
last = None;
}
out.push(' ');
continue;
}
let color = self.colors[i];
if last != Some(color) {
out.push_str(c(LANES[color % LANES.len()]));
last = Some(color);
}
out.push(*ch);
}
if last.is_some() {
out.push_str(c(RESET));
}
for _ in self.cells.len()..min_width {
out.push(' ');
}
out
}
}
fn cell(lane: usize) -> usize {
lane * 2
}
fn occupied(row: &[bool], lane: usize) -> bool {
row.get(lane).copied().unwrap_or(false)
}
fn lane_color(map: &[usize], lane: usize) -> usize {
map.get(lane).copied().unwrap_or(lane)
}
pub fn fold_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
if step.closing.is_empty() {
return None;
}
let mut grid = Grid::new(step.width(), g);
for j in 0..step.width() {
if j != step.col && occupied(&step.before, j) && occupied(&step.at, j) {
grid.put(cell(j), g.vert, lane_color(&step.colors, j));
}
}
connect(&mut grid, g, step, &step.closing, &step.before, true);
Some(grid.render(min_width))
}
pub fn commit_row(step: &Step, g: &Glyphs, min_width: usize) -> String {
let mut grid = Grid::new(step.width(), g);
for j in 0..step.width() {
if occupied(&step.at, j) {
grid.put(cell(j), g.vert, lane_color(&step.colors, j));
}
}
let dot = if step.is_merge { g.merge } else { g.commit };
grid.put(cell(step.col), dot, step.dot_color);
grid.render(min_width)
}
pub fn branch_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
if step.opening.is_empty() {
return None;
}
let mut grid = Grid::new(step.width(), g);
for j in 0..step.width() {
if j != step.col && occupied(&step.at, j) && occupied(&step.pre_shift, j) {
grid.put(cell(j), g.vert, lane_color(&step.colors, j));
}
}
connect(&mut grid, g, step, &step.opening, &step.at, false);
Some(grid.render(min_width))
}
pub fn shift_row(step: &Step, g: &Glyphs, min_width: usize) -> Option<String> {
if step.shifts.is_empty() {
return None;
}
let mut grid = Grid::new(step.width(), g);
for j in 0..step.width() {
let moving = step.shifts.iter().any(|(from, _)| *from == j);
if occupied(&step.pre_shift, j) && !moving {
grid.put(cell(j), g.vert, lane_color(&step.colors, j));
}
}
for &(from, to) in &step.shifts {
grid.put(cell(to) + 1, g.slide, lane_color(&step.colors, from));
}
Some(grid.render(min_width))
}
fn connect(
grid: &mut Grid,
g: &Glyphs,
step: &Step,
targets: &[usize],
existing: &[bool],
folding: bool,
) {
let col = step.col;
let color = step.dot_color;
for &k in targets {
run(grid, g, col, k, color);
}
let end_right = targets.iter().copied().filter(|k| *k > col).max();
let end_left = targets.iter().copied().filter(|k| *k < col).min();
for &k in targets {
let right = k > col;
let at_end = if right {
Some(k) == end_right
} else {
Some(k) == end_left
};
let glyph = if occupied(existing, k) && !folding {
if right {
g.tee_left
} else {
g.tee_right
}
} else if !at_end {
if folding {
g.tee_up
} else {
g.tee_down
}
} else if folding {
if right {
g.fold_left
} else {
g.fold_right
}
} else if right {
g.branch_right
} else {
g.branch_left
};
grid.put(cell(k), glyph, color);
}
let junction = match (end_left.is_some(), end_right.is_some()) {
(true, true) => g.tee_both,
(false, true) => g.tee_right,
(true, false) => g.tee_left,
(false, false) => g.vert,
};
grid.put(cell(col), junction, color);
}
fn run(grid: &mut Grid, g: &Glyphs, from_lane: usize, to_lane: usize, color: usize) {
let (lo, hi) = if from_lane < to_lane {
(cell(from_lane), cell(to_lane))
} else {
(cell(to_lane), cell(from_lane))
};
for p in (lo + 1)..hi {
grid.put(p, g.horiz, color);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::lanes::Lanes;
fn rows(step: &Step) -> Vec<String> {
let mut v = Vec::new();
if let Some(r) = fold_row(step, &ASCII, 0) {
v.push(r.trim_end().to_string());
}
v.push(commit_row(step, &ASCII, 0).trim_end().to_string());
if let Some(r) = branch_row(step, &ASCII, 0) {
v.push(r.trim_end().to_string());
}
if let Some(r) = shift_row(step, &ASCII, 0) {
v.push(r.trim_end().to_string());
}
v
}
#[test]
fn linear_history_draws_a_single_column() {
let mut l = Lanes::new();
let s = l.advance("a", &["b"]);
assert_eq!(rows(&s), vec!["*"]);
}
#[test]
fn merge_branches_out_below_the_dot() {
let mut l = Lanes::new();
let s = l.advance("m", &["a", "side"]);
assert_eq!(rows(&s), vec!["o", "+-\\"], "merges get a hollow dot");
}
#[test]
fn fork_folds_in_above_the_dot() {
let mut l = Lanes::new();
l.advance("m", &["a", "s"]);
l.advance("a", &["r"]);
l.advance("s", &["r"]);
let s = l.advance("r", &[]);
assert_eq!(rows(&s), vec!["+-/", "*"]);
}
#[test]
fn a_commit_can_fold_in_and_branch_out_at_once() {
let mut l = Lanes::new();
l.advance("top", &["m", "b"]); l.advance("b", &["m"]); let s = l.advance("m", &["p", "side"]);
assert_eq!(
rows(&s),
vec!["+-/", "o", "+-\\"],
"fold above, dot, branch below"
);
}
#[test]
fn a_freed_column_is_closed_up_by_a_diagonal() {
let mut l = Lanes::new();
l.advance("m", &["a", "s"]); let s = l.advance("a", &[]); assert_eq!(rows(&s), vec!["* |", " /"]);
}
#[test]
fn horizontal_crossing_a_lane_does_not_erase_it() {
let mut l = Lanes::new();
l.advance("m", &["a", "s"]); let s = l.advance("a", &["p", "far"]); assert_eq!(
branch_row(&s, &ASCII, 0).unwrap().trim_end(),
"+-+-\\",
"lane 1 survives as a crossing"
);
}
#[test]
fn a_lane_keeps_one_colour_across_a_shift() {
let mut l = Lanes::new();
let m = l.advance("m", &["a", "s"]);
let side_color = m.colors_after[1];
let s = l.advance("a", &[]); assert_eq!(s.shifts, vec![(1, 0)]);
assert_eq!(
s.colors_after[0], side_color,
"the branch keeps its colour in its new column"
);
}
#[test]
fn min_width_pads_so_commit_text_lines_up() {
let mut l = Lanes::new();
let s = l.advance("a", &["b"]);
assert_eq!(commit_row(&s, &ASCII, 5), "* ");
}
}
#[cfg(test)]
mod octopus_tests {
use super::*;
use crate::graph::lanes::Lanes;
#[test]
fn every_opened_lane_gets_its_own_connector() {
let mut l = Lanes::new();
let s = l.advance("m", &["p1", "p2", "p3"]);
assert_eq!(s.opening, vec![1, 2]);
assert_eq!(
branch_row(&s, &ASCII, 0).unwrap().trim_end(),
"+-+-\\",
"lane 1 must have a connector, not a bare horizontal"
);
let uni = branch_row(&s, &UNICODE, 0).unwrap();
assert!(
uni.contains('\u{252c}'),
"the intermediate lane should get a downward tee: {:?}",
uni.trim_end()
);
}
#[test]
fn every_folded_lane_gets_its_own_connector() {
let mut l = Lanes::new();
l.advance("m", &["a", "b", "c"]);
l.advance("a", &["z"]);
l.advance("b", &["z"]);
l.advance("c", &["z"]);
let s = l.advance("z", &[]);
assert_eq!(s.closing, vec![1, 2]);
assert_eq!(
fold_row(&s, &ASCII, 0).unwrap().trim_end(),
"+-+-/",
"lane 1 must have a connector, not a bare horizontal"
);
let uni = fold_row(&s, &UNICODE, 0).unwrap();
assert!(
uni.contains('\u{2534}'),
"the intermediate lane should get an upward tee: {:?}",
uni.trim_end()
);
}
}