From ea3a583775c793765193e7fc813cc173e19e226a Mon Sep 17 00:00:00 2001 From: Peer Sommerlund Date: Thu, 3 Jul 2025 07:05:28 +0200 Subject: [PATCH 1/2] unicode.rs: Introduce GridCell It is easier to understand than the mysterious [u8; 3] --- src/print/unicode.rs | 68 ++++++++++++++++++++++++++++---------------- 1 file changed, 43 insertions(+), 25 deletions(-) diff --git a/src/print/unicode.rs b/src/print/unicode.rs index 77757d0..191e36c 100644 --- a/src/print/unicode.rs +++ b/src/print/unicode.rs @@ -130,7 +130,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result(); write!(g_out, "{}", str).unwrap(); } @@ -704,22 +704,36 @@ fn sorted(v1: usize, v2: usize) -> (usize, usize) { } } -/// Two-dimensional grid used to produce the graph representation. -#[allow(dead_code)] +/// One cell in a [Grid] +#[derive(Clone, Copy)] +struct GridCell { + /// The symbol shown, encoded as in index into settings::Characters + character: u8, + /// Standard 8-bit terminal colour code + color: u8, + /// Persistence level. z-order, lower numbers take preceedence. + pers: u8, +} + +impl GridCell { + pub fn char(&self, characters: &Characters) -> char { + characters.chars[self.character as usize] + } +} + +/// Two-dimensional grid used to hold the graph layout. +/// +/// This can be rendered as unicode text or as SVG. struct Grid { width: usize, height: usize, - /// Grid cells are stored in the data vector, layout row wise. - /// For each cell in the grid, three values are stored: - /// - Character (symbol) - /// - Colour - /// - Persistence level (z-order, lower numbers take preceedence) - data: Vec<[u8; 3]>, + /// Grid cells are stored in row-major order. + data: Vec, } impl Grid { - pub fn new(width: usize, height: usize, initial: [u8; 3]) -> Self { + pub fn new(width: usize, height: usize, initial: GridCell) -> Self { Grid { width, height, @@ -736,11 +750,15 @@ impl Grid { } pub fn get_tuple(&self, x: usize, y: usize) -> (u8, u8, u8) { let v = self.data[self.index(x, y)]; - (v[0], v[1], v[2]) + (v.character, v.color, v.pers) } pub fn set(&mut self, x: usize, y: usize, character: u8, color: u8, pers: u8) { let idx = self.index(x, y); - self.data[idx] = [character, color, pers]; + self.data[idx] = GridCell { + character, + color, + pers, + }; } pub fn set_opt( &mut self, @@ -751,15 +769,15 @@ impl Grid { pers: Option, ) { let idx = self.index(x, y); - let arr = &mut self.data[idx]; + let cell = &mut self.data[idx]; if let Some(character) = character { - arr[0] = character; + cell.character = character; } if let Some(color) = color { - arr[1] = color; + cell.color = color; } if let Some(pers) = pers { - arr[2] = pers; + cell.pers = pers; } } } From 7d6f2157d1a9ac92c3e50b18fb678adc12afe22b Mon Sep 17 00:00:00 2001 From: Peer Sommerlund Date: Thu, 18 Sep 2025 06:06:51 +0200 Subject: [PATCH 2/2] refactor: Remove unused field Grid.height --- src/print/unicode.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/print/unicode.rs b/src/print/unicode.rs index 191e36c..a25120d 100644 --- a/src/print/unicode.rs +++ b/src/print/unicode.rs @@ -726,7 +726,6 @@ impl GridCell { /// This can be rendered as unicode text or as SVG. struct Grid { width: usize, - height: usize, /// Grid cells are stored in row-major order. data: Vec, @@ -736,7 +735,6 @@ impl Grid { pub fn new(width: usize, height: usize, initial: GridCell) -> Self { Grid { width, - height, data: vec![initial; width * height], } }