Skip to content

Commit 1e06286

Browse files
committed
Extract fn build_commit_lines_and_map
Note, total_rows computation was a complicated way to count lines found in text_lines.
1 parent 312e59d commit 1e06286

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

src/print/unicode.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,7 @@ graph-lines, text-lines, start-row
5353
3. start_row: `Vec<usize>`: Starting row for commit in the `graph.commits` vector.
5454
*/
5555
pub type UnicodeGraphInfo = (Vec<String>, Vec<String>, Vec<usize>);
56-
type RefactorData = (
57-
/* offset */ usize,
58-
/* index_map */ Vec<usize>,
59-
/* text_lines */ Vec<Option<String>>,
60-
);
56+
6157
/// Creates a text-based visual representation of a graph.
6258
pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGraphInfo, String> {
6359
if graph.all_branches.is_empty() {
@@ -82,14 +78,11 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
8278
};
8379

8480
// 3. Compute commit text and index map
85-
// TODO
86-
87-
// REFACTOR IN PROGRESS - call old function to get missing variables
88-
let (offset, index_map, mut text_lines) =
89-
old_print_unicode(graph, settings, &inserts, wrap_options)?;
81+
let (mut text_lines, index_map) =
82+
build_commit_lines_and_map(graph, settings, &inserts, &wrap_options)?;
9083

9184
// 4. Calculate total rows and initialize/draw the grid
92-
let total_rows = graph.commits.len() + offset;
85+
let total_rows = text_lines.len();
9386

9487
let mut grid = draw_graph_lines(graph, settings, num_cols, &inserts, &index_map, total_rows);
9588

@@ -116,23 +109,25 @@ fn calculate_graph_dimensions(graph: &GitGraph) -> usize {
116109
+ 1
117110
}
118111

119-
/// This is the remaining old code, that gradually will be moved to separate
120-
/// functions or the new print_unicode
121-
fn old_print_unicode<'a>(
112+
/// Iterates through commits to compute text lines, blank line inserts, and the index map.
113+
fn build_commit_lines_and_map<'a>(
122114
graph: &GitGraph,
123115
settings: &Settings,
124116
inserts: &HashMap<usize, Vec<Vec<Occ>>>,
125-
wrap_options: Option<Options<'a>>,
126-
) -> Result<RefactorData, String> {
117+
wrap_options: &Option<Options<'a>>,
118+
) -> Result<(Vec<Option<String>>, Vec<usize>), String> {
127119
let head_idx = graph.indices.get(&graph.head.oid);
128120

129121
// Compute commit text into text_lines and add blank rows
130122
// if needed to match branch graph inserts.
131123
let mut index_map = vec![];
132124
let mut text_lines = vec![];
133125
let mut offset = 0;
126+
134127
for (idx, info) in graph.commits.iter().enumerate() {
135128
index_map.push(idx + offset);
129+
130+
// Calculate needed graph inserts (for ranges only)
136131
let cnt_inserts = if let Some(inserts) = inserts.get(&idx) {
137132
inserts
138133
.iter()
@@ -153,27 +148,28 @@ fn old_print_unicode<'a>(
153148
None
154149
};
155150

151+
// Format the commit message lines
156152
let lines = format(
157153
&settings.format,
158154
graph,
159155
info,
160156
head,
161157
settings.colored,
162-
&wrap_options,
158+
wrap_options,
163159
)?;
164160

165161
let num_lines = if lines.is_empty() { 0 } else { lines.len() - 1 };
166162
let max_inserts = max(cnt_inserts, num_lines);
167163
let add_lines = max_inserts - num_lines;
168164

165+
// Extend text_lines with commit lines and blank lines for padding
169166
text_lines.extend(lines.into_iter().map(Some));
170167
text_lines.extend((0..add_lines).map(|_| None));
171168

172169
offset += max_inserts;
173170
}
174171

175-
// REFACTOR IN PROGRESS
176-
Ok((offset, index_map, text_lines))
172+
Ok((text_lines, index_map))
177173
}
178174

179175
/// Initializes the grid and draws all commit/branch connections.

0 commit comments

Comments
 (0)