Skip to content

Commit

Permalink
refactor: make iterators one line use
Browse files Browse the repository at this point in the history
  • Loading branch information
null8626 authored Feb 2, 2024
1 parent f4f45db commit 5da5513
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions src/util/md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ impl MarkdownTable {
let mut lines = vec![];

lines.push({
let mut cols = vec![];
for (idx, width) in col_lengths.iter().enumerate() {
cols.push(format!("{:width$}", self.headers[idx]));
}
let cols = col_lengths
.iter()
.enumerate()
.map(|(idx, width)| {
format!("{:width$}", self.headers[idx]))
})
.collect::<Vec<_>>();

"| ".to_owned() + &cols.join(" | ") + " |"
format!("| {} |", cols.join(" | "))
});

lines.push({
Expand All @@ -76,43 +79,44 @@ impl MarkdownTable {
.map(|length| format!("{:-^width$}", "", width = length))
.collect::<Vec<_>>();

"| ".to_owned() + &cols.join(" | ") + " |"
format!("| {} |", cols.join(" | "))
});

lines.extend(self.rows.iter().map(|row| {
let cols = (0..row.len())
.map(|idx| format!("{:width$}", row[idx], width = col_lengths[idx]))
.collect::<Vec<_>>();

"| ".to_owned() + &cols.join(" | ") + " |"
format!("| {} |", cols.join(" | "))
}));

lines.join("\n")
}

pub fn render_ascii_lines(&self, headers: bool) -> Vec<String> {
let mut col_lengths = vec![];

for idx in 0..self.headers.len() {
let mut li = vec![self.headers[idx].len()];

li.extend(
self.rows
.iter()
.map(|row| row.get(idx).unwrap_or(&String::new()).len()),
);

col_lengths.push(li.into_iter().max().expect("col lengths iter max none"));
}
let col_lengths = (0..self.headers.len())
.map(|idx| {
let mut li = vec![self.headers[idx].len()];

li.extend(
self.rows
.iter()
.map(|row| row.get(idx).unwrap_or(&String::new()).len()),
);

li.into_iter().max().expect("col lengths iter max none")
})
.collect::<Vec<_>>();

let mut lines = vec![];

if headers {
lines.push({
let mut cols = vec![];
for (idx, width) in col_lengths.iter().enumerate() {
cols.push(format!("{:width$}", self.headers[idx]));
}
let cols = col_lengths
.iter()
.enumerate()
.map(|(idx, width)| format!("{:width$}", self.headers[idx]))
.collect::<Vec<_>>();

cols.join(" ")
});
Expand Down

0 comments on commit 5da5513

Please sign in to comment.