Skip to content

Commit ea3a583

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

1 file changed

Lines changed: 43 additions & 25 deletions

File tree

src/print/unicode.rs

Lines changed: 43 additions & 25 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,18 @@ 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-
)
540+
write!(g_out, "{}", Paint::fixed(cell.color, cell.char(characters)))
541541
}
542542
.unwrap();
543543
}
544544
} else {
545545
let str = row
546546
.iter()
547-
.map(|arr| characters.chars[arr[0] as usize])
547+
.map(|cell| cell.char(characters))
548548
.collect::<String>();
549549
write!(g_out, "{}", str).unwrap();
550550
}
@@ -704,22 +704,36 @@ fn sorted(v1: usize, v2: usize) -> (usize, usize) {
704704
}
705705
}
706706

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

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]>,
731+
/// Grid cells are stored in row-major order.
732+
data: Vec<GridCell>,
719733
}
720734

721735
impl Grid {
722-
pub fn new(width: usize, height: usize, initial: [u8; 3]) -> Self {
736+
pub fn new(width: usize, height: usize, initial: GridCell) -> Self {
723737
Grid {
724738
width,
725739
height,
@@ -736,11 +750,15 @@ impl Grid {
736750
}
737751
pub fn get_tuple(&self, x: usize, y: usize) -> (u8, u8, u8) {
738752
let v = self.data[self.index(x, y)];
739-
(v[0], v[1], v[2])
753+
(v.character, v.color, v.pers)
740754
}
741755
pub fn set(&mut self, x: usize, y: usize, character: u8, color: u8, pers: u8) {
742756
let idx = self.index(x, y);
743-
self.data[idx] = [character, color, pers];
757+
self.data[idx] = GridCell {
758+
character,
759+
color,
760+
pers,
761+
};
744762
}
745763
pub fn set_opt(
746764
&mut self,
@@ -751,15 +769,15 @@ impl Grid {
751769
pers: Option<u8>,
752770
) {
753771
let idx = self.index(x, y);
754-
let arr = &mut self.data[idx];
772+
let cell = &mut self.data[idx];
755773
if let Some(character) = character {
756-
arr[0] = character;
774+
cell.character = character;
757775
}
758776
if let Some(color) = color {
759-
arr[1] = color;
777+
cell.color = color;
760778
}
761779
if let Some(pers) = pers {
762-
arr[2] = pers;
780+
cell.pers = pers;
763781
}
764782
}
765783
}

0 commit comments

Comments
 (0)