Skip to content

Commit 4d5588f

Browse files
committed
unicode.rs: Introduce GridCell
It is easier to understand than the mysterious [u8; 3]
1 parent 95d5c6f commit 4d5588f

1 file changed

Lines changed: 41 additions & 24 deletions

File tree

src/print/unicode.rs

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
130130
let mut grid = Grid::new(
131131
num_cols,
132132
graph.commits.len() + offset,
133-
[SPACE, WHITE, settings.branches.persistence.len() as u8 + 2],
133+
GridCell {
134+
character: SPACE,
135+
color: WHITE,
136+
pers: settings.branches.persistence.len() as u8 + 2
137+
},
134138
);
135139

136140
for (idx, info) in graph.commits.iter().enumerate() {
@@ -529,22 +533,20 @@ fn print_graph(
529533
let mut t_out = String::new();
530534

531535
if color {
532-
for arr in row {
533-
if arr[0] == SPACE {
534-
write!(g_out, "{}", characters.chars[arr[0] as usize])
536+
for cell in row {
537+
if cell.character == SPACE {
538+
write!(g_out, "{}", cell.char(characters))
535539
} else {
536-
write!(
537-
g_out,
538-
"{}",
539-
Paint::fixed(arr[1], characters.chars[arr[0] as usize])
540+
write!(g_out, "{}",
541+
Paint::fixed(cell.color, cell.char(characters))
540542
)
541543
}
542544
.unwrap();
543545
}
544546
} else {
545547
let str = row
546548
.iter()
547-
.map(|arr| characters.chars[arr[0] as usize])
549+
.map(|cell| cell.char(characters))
548550
.collect::<String>();
549551
write!(g_out, "{}", str).unwrap();
550552
}
@@ -704,22 +706,37 @@ fn sorted(v1: usize, v2: usize) -> (usize, usize) {
704706
}
705707
}
706708

707-
/// Two-dimensional grid used to produce the graph representation.
708-
#[allow(dead_code)]
709+
710+
/// One cell in a [Grid]
711+
#[derive(Clone, Copy)]
712+
struct GridCell {
713+
/// The symbol shown, encoded as in index into settings::Characters
714+
character: u8,
715+
/// Standard 8-bit terminal colour code
716+
color: u8,
717+
/// Persistence level. z-order, lower numbers take preceedence.
718+
pers: u8,
719+
}
720+
721+
impl GridCell {
722+
pub fn char(&self, characters: &Characters) -> char {
723+
characters.chars[self.character as usize]
724+
}
725+
}
726+
727+
/// Two-dimensional grid used to hold the graph layout.
728+
///
729+
/// This can be rendered as unicode text or as SVG.
709730
struct Grid {
710731
width: usize,
711732
height: usize,
712733

713-
/// Grid cells are stored in the data vector, layout row wise.
714-
/// For each cell in the grid, three values are stored:
715-
/// - Character (symbol)
716-
/// - Colour
717-
/// - Persistence level (z-order, lower numbers take preceedence)
718-
data: Vec<[u8; 3]>,
734+
/// Grid cells are stored in row-major order.
735+
data: Vec<GridCell>,
719736
}
720737

721738
impl Grid {
722-
pub fn new(width: usize, height: usize, initial: [u8; 3]) -> Self {
739+
pub fn new(width: usize, height: usize, initial: GridCell) -> Self {
723740
Grid {
724741
width,
725742
height,
@@ -736,11 +753,11 @@ impl Grid {
736753
}
737754
pub fn get_tuple(&self, x: usize, y: usize) -> (u8, u8, u8) {
738755
let v = self.data[self.index(x, y)];
739-
(v[0], v[1], v[2])
756+
(v.character, v.color, v.pers)
740757
}
741758
pub fn set(&mut self, x: usize, y: usize, character: u8, color: u8, pers: u8) {
742759
let idx = self.index(x, y);
743-
self.data[idx] = [character, color, pers];
760+
self.data[idx] = GridCell{character, color, pers};
744761
}
745762
pub fn set_opt(
746763
&mut self,
@@ -751,15 +768,15 @@ impl Grid {
751768
pers: Option<u8>,
752769
) {
753770
let idx = self.index(x, y);
754-
let arr = &mut self.data[idx];
771+
let cell = &mut self.data[idx];
755772
if let Some(character) = character {
756-
arr[0] = character;
773+
cell.character = character;
757774
}
758775
if let Some(color) = color {
759-
arr[1] = color;
776+
cell.color = color;
760777
}
761778
if let Some(pers) = pers {
762-
arr[2] = pers;
779+
cell.pers = pers;
763780
}
764781
}
765782
}

0 commit comments

Comments
 (0)