Skip to content

Commit f9a05d1

Browse files
authored
Introduce GridCell (#115)
Introduce struct GridCell as a replacement for the mysterious [u8; 3]
1 parent 2da281c commit f9a05d1

1 file changed

Lines changed: 44 additions & 28 deletions

File tree

src/print/unicode.rs

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
132132
let mut grid = Grid::new(
133133
num_cols,
134134
graph.commits.len() + offset,
135-
[SPACE, WHITE, settings.branches.persistence.len() as u8 + 2],
135+
GridCell {
136+
character: SPACE,
137+
color: WHITE,
138+
pers: settings.branches.persistence.len() as u8 + 2,
139+
},
136140
);
137141

138142
// Compute branch lines in grid
@@ -594,22 +598,18 @@ fn print_graph(
594598
let mut t_out = String::new();
595599

596600
if color {
597-
for arr in row {
598-
if arr[0] == SPACE {
599-
write!(g_out, "{}", characters.chars[arr[0] as usize])
601+
for cell in row {
602+
if cell.character == SPACE {
603+
write!(g_out, "{}", cell.char(characters))
600604
} else {
601-
write!(
602-
g_out,
603-
"{}",
604-
Paint::fixed(arr[1], characters.chars[arr[0] as usize])
605-
)
605+
write!(g_out, "{}", Paint::fixed(cell.color, cell.char(characters)))
606606
}
607607
.unwrap();
608608
}
609609
} else {
610610
let str = row
611611
.iter()
612-
.map(|arr| characters.chars[arr[0] as usize])
612+
.map(|cell| cell.char(characters))
613613
.collect::<String>();
614614
write!(g_out, "{}", str).unwrap();
615615
}
@@ -769,25 +769,37 @@ fn sorted(v1: usize, v2: usize) -> (usize, usize) {
769769
}
770770
}
771771

772-
/// Two-dimensional grid used to produce the graph representation.
773-
#[allow(dead_code)]
772+
/// One cell in a [Grid]
773+
#[derive(Clone, Copy)]
774+
struct GridCell {
775+
/// The symbol shown, encoded as in index into settings::Characters
776+
character: u8,
777+
/// Standard 8-bit terminal colour code
778+
color: u8,
779+
/// Persistence level. z-order, lower numbers take preceedence.
780+
pers: u8,
781+
}
782+
783+
impl GridCell {
784+
pub fn char(&self, characters: &Characters) -> char {
785+
characters.chars[self.character as usize]
786+
}
787+
}
788+
789+
/// Two-dimensional grid used to hold the graph layout.
790+
///
791+
/// This can be rendered as unicode text or as SVG.
774792
struct Grid {
775793
width: usize,
776-
height: usize,
777-
778-
/// Grid cells are stored in the data vector, layout row wise.
779-
/// For each cell in the grid, three values are stored:
780-
/// - Character (symbol)
781-
/// - Colour
782-
/// - Persistence level (z-order, lower numbers take preceedence)
783-
data: Vec<[u8; 3]>,
794+
795+
/// Grid cells are stored in row-major order.
796+
data: Vec<GridCell>,
784797
}
785798

786799
impl Grid {
787-
pub fn new(width: usize, height: usize, initial: [u8; 3]) -> Self {
800+
pub fn new(width: usize, height: usize, initial: GridCell) -> Self {
788801
Grid {
789802
width,
790-
height,
791803
data: vec![initial; width * height],
792804
}
793805
}
@@ -801,11 +813,15 @@ impl Grid {
801813
}
802814
pub fn get_tuple(&self, x: usize, y: usize) -> (u8, u8, u8) {
803815
let v = self.data[self.index(x, y)];
804-
(v[0], v[1], v[2])
816+
(v.character, v.color, v.pers)
805817
}
806818
pub fn set(&mut self, x: usize, y: usize, character: u8, color: u8, pers: u8) {
807819
let idx = self.index(x, y);
808-
self.data[idx] = [character, color, pers];
820+
self.data[idx] = GridCell {
821+
character,
822+
color,
823+
pers,
824+
};
809825
}
810826
pub fn set_opt(
811827
&mut self,
@@ -816,15 +832,15 @@ impl Grid {
816832
pers: Option<u8>,
817833
) {
818834
let idx = self.index(x, y);
819-
let arr = &mut self.data[idx];
835+
let cell = &mut self.data[idx];
820836
if let Some(character) = character {
821-
arr[0] = character;
837+
cell.character = character;
822838
}
823839
if let Some(color) = color {
824-
arr[1] = color;
840+
cell.color = color;
825841
}
826842
if let Some(pers) = pers {
827-
arr[2] = pers;
843+
cell.pers = pers;
828844
}
829845
}
830846
}

0 commit comments

Comments
 (0)