Skip to content

Commit f8dff54

Browse files
committed
Fixing ascii art widths.
- Create parse module - Simple parser for *.ascii format - AsciiArt struct allows for correcting the width to negate spacing on the left and right. - default spacing of 1 character between the logo and the info
1 parent 58a15c9 commit f8dff54

File tree

2 files changed

+289
-85
lines changed

2 files changed

+289
-85
lines changed

Diff for: src/main.rs

+16-85
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use git2::{Repository, Oid};
1818
use license::License;
1919
use clap::{App, Arg};
2020
use std::{
21-
cmp,
2221
collections::HashMap,
2322
convert::From,
2423
ffi::OsStr,
@@ -31,6 +30,9 @@ use std::{
3130
};
3231
use strum::{IntoEnumIterator, EnumCount};
3332

33+
mod parse;
34+
use parse::AsciiArt;
35+
3436
type Result<T> = result::Result<T, Error>;
3537

3638
struct Info {
@@ -207,97 +209,26 @@ impl fmt::Display for Info {
207209
" ".on_bright_white(),
208210
)?;
209211

210-
let logo = self.get_ascii();
211-
let mut logo_lines = logo.lines();
212-
let mut info_lines = buffer.lines();
213-
let left_pad = logo.lines().map(|l| true_len(l)).max().unwrap_or(0);
214-
215-
for _ in 0..cmp::max(count_newlines(logo), count_newlines(&buffer)) {
216-
let logo_line = match logo_lines.next() {
217-
Some(line) => line,
218-
None => "",
219-
};
220212

221-
let info_line = match info_lines.next() {
222-
Some(line) => line,
223-
None => "",
224-
};
213+
let mut logo_lines = AsciiArt::new(self.get_ascii(), self.colors());
214+
let mut info_lines = buffer.lines();
225215

226-
let (logo_line, extra_pad) = colorize_str(logo_line, self.colors());
227-
// If the string is empty the extra padding should not be added
228-
let pad = if logo_line.is_empty() {
229-
left_pad
230-
} else {
231-
left_pad + extra_pad
232-
};
233-
writeln!(f, "{:<width$} {:^}", logo_line, info_line, width = pad,)?;
234-
}
216+
loop {
217+
match (logo_lines.next(), info_lines.next()) {
218+
(Some(logo_line), Some(info_line)) =>
219+
writeln!(f, "{} {:^}", logo_line, info_line)?,
220+
(Some(logo_line), None) =>
221+
writeln!(f, "{}", logo_line)?,
222+
(None, Some(info_line)) =>
223+
writeln!(f, "{:<width$} {:^}", "", info_line, width = logo_lines.width())?,
224+
(None, None) => break,
225+
}
226+
};
235227

236228
Ok(())
237229
}
238230
}
239231

240-
fn count_newlines(s: &str) -> usize {
241-
bytecount::count(s.as_bytes(), b'\n')
242-
}
243-
244-
/// Transforms a string with color format into one with proper
245-
/// escape characters for color display.
246-
///
247-
/// Colors are specified with {0}, {1}... where the number represents
248-
/// the nth element in the colors Vec provided to the function.
249-
/// If there are more colors in the ascii than in the Vec it
250-
/// defaults to white.
251-
/// The usize in the tuple refers to the extra padding needed
252-
/// which comes from the added escape characters.
253-
fn colorize_str(line: &str, colors: Vec<Color>) -> (String, usize) {
254-
// Extract colors from string coded with {n}
255-
let mut colors_in_str: Vec<Color> = line.split('{').fold(Vec::new(), |mut acc, s| {
256-
if s.len() > 2 {
257-
let i = s.chars().nth(0).unwrap_or('0').to_digit(10).unwrap_or(0);
258-
acc.push(*colors.get(i as usize).unwrap_or(&Color::White));
259-
}
260-
acc
261-
});
262-
263-
if colors_in_str.is_empty() {
264-
colors_in_str.push(match colors.get(0) {
265-
Some(&c) => c,
266-
None => Color::White,
267-
});
268-
}
269-
270-
let mut colors_iter = colors_in_str.iter();
271-
272-
let out_str = line.split('{').fold(String::new(), |mut acc, s| {
273-
if s.len() > 2 {
274-
let s: String = s.chars().skip(2).collect();
275-
let c = match colors_iter.next() {
276-
Some(&c) => c,
277-
None => Color::White,
278-
};
279-
acc.push_str(&format!("{}", s.color(c)));
280-
}
281-
acc
282-
});
283-
(out_str, colors_in_str.len() * 9)
284-
}
285-
286-
/// Returns the true length of a string after substracting the {n}
287-
/// color declarations.
288-
fn true_len(line: &str) -> usize {
289-
line.split('{')
290-
.fold(String::new(), |mut acc, s| {
291-
if s.len() > 2 {
292-
acc.push_str(&s.chars().skip(2).collect::<String>());
293-
} else {
294-
acc.push_str(s);
295-
}
296-
acc
297-
})
298-
.len()
299-
}
300-
301232
struct CommitInfo {
302233
commit: Oid,
303234
refs: Vec<String>,

0 commit comments

Comments
 (0)