mirror of
https://gitlab.com/arnekeller/ascii-table.git
synced 2024-12-04 13:39:06 +00:00
Configurable line drawing + header alignment
This commit is contained in:
parent
9fb258f9be
commit
7fffb5d93b
36
src/lib.rs
36
src/lib.rs
@ -79,6 +79,7 @@ const DEFAULT_COLUMN: Column = Column {
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub struct AsciiTable {
|
||||
pub max_width: usize,
|
||||
pub draw_lines: bool,
|
||||
pub columns: BTreeMap<usize, Column>
|
||||
}
|
||||
|
||||
@ -87,6 +88,7 @@ impl Default for AsciiTable {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
max_width: 80,
|
||||
draw_lines: true,
|
||||
columns: BTreeMap::new()
|
||||
}
|
||||
}
|
||||
@ -157,15 +159,23 @@ impl AsciiTable {
|
||||
let widths = self.column_widths(&header, &data, num_cols);
|
||||
|
||||
let mut result = String::new();
|
||||
result.push_str(&self.format_first(&widths));
|
||||
if self.draw_lines {
|
||||
result.push_str(&self.format_first(&widths));
|
||||
}
|
||||
if has_header {
|
||||
result.push_str(&self.format_header_row(&header, &widths));
|
||||
result.push_str(&self.format_middle(&widths));
|
||||
result.push_str(&self.format_header_row(&header, &widths, &(0..num_cols).map(|a|
|
||||
self.columns.get(&a).map(|x| x.align).unwrap_or(Align::Left)
|
||||
).collect::<Vec<_>>()));
|
||||
if self.draw_lines {
|
||||
result.push_str(&self.format_middle(&widths));
|
||||
}
|
||||
}
|
||||
for row in data {
|
||||
result.push_str(&self.format_row(&row, &widths));
|
||||
}
|
||||
result.push_str(&self.format_last(&widths));
|
||||
if self.draw_lines {
|
||||
result.push_str(&self.format_last(&widths));
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
@ -269,14 +279,22 @@ impl AsciiTable {
|
||||
let conf = self.columns.get(&a).unwrap_or(default_conf);
|
||||
self.format_cell(cell, width, ' ', conf.align)
|
||||
}).collect();
|
||||
self.format_line(&row, &format!("{}{}", NS, ' '), &format!("{}{}{}", ' ', NS, ' '), &format!("{}{}", ' ', NS))
|
||||
if self.draw_lines {
|
||||
self.format_line(&row, &format!("{}{}", NS, ' '), &format!("{}{}{}", ' ', NS, ' '), &format!("{}{}", ' ', NS))
|
||||
} else {
|
||||
self.format_line(&row, "", " ", "")
|
||||
}
|
||||
}
|
||||
|
||||
fn format_header_row(&self, row: &[SmartString], widths: &[usize]) -> String {
|
||||
let row: Vec<_> = row.iter().zip(widths.iter()).map(|(cell, &width)|
|
||||
self.format_cell(cell, width, ' ', Align::Left)
|
||||
fn format_header_row(&self, row: &[SmartString], widths: &[usize], aligns: &[Align]) -> String {
|
||||
let row: Vec<_> = row.iter().zip(widths.iter()).zip(aligns.iter()).map(|((cell, &width), &align)|
|
||||
self.format_cell(cell, width, ' ', align)
|
||||
).collect();
|
||||
self.format_line(&row, &format!("{}{}", NS, ' '), &format!("{}{}{}", ' ', NS, ' '), &format!("{}{}", ' ', NS))
|
||||
if self.draw_lines {
|
||||
self.format_line(&row, &format!("{}{}", NS, ' '), &format!("{}{}{}", ' ', NS, ' '), &format!("{}{}", ' ', NS))
|
||||
} else {
|
||||
self.format_line(&row, "", " ", "")
|
||||
}
|
||||
}
|
||||
|
||||
fn format_last(&self, widths: &[usize]) -> String {
|
||||
|
Loading…
Reference in New Issue
Block a user