mirror of
https://gitlab.com/arnekeller/ascii-table.git
synced 2024-12-04 21:49:08 +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)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct AsciiTable {
|
pub struct AsciiTable {
|
||||||
pub max_width: usize,
|
pub max_width: usize,
|
||||||
|
pub draw_lines: bool,
|
||||||
pub columns: BTreeMap<usize, Column>
|
pub columns: BTreeMap<usize, Column>
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -87,6 +88,7 @@ impl Default for AsciiTable {
|
|||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
max_width: 80,
|
max_width: 80,
|
||||||
|
draw_lines: true,
|
||||||
columns: BTreeMap::new()
|
columns: BTreeMap::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -157,15 +159,23 @@ impl AsciiTable {
|
|||||||
let widths = self.column_widths(&header, &data, num_cols);
|
let widths = self.column_widths(&header, &data, num_cols);
|
||||||
|
|
||||||
let mut result = String::new();
|
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 {
|
if has_header {
|
||||||
result.push_str(&self.format_header_row(&header, &widths));
|
result.push_str(&self.format_header_row(&header, &widths, &(0..num_cols).map(|a|
|
||||||
result.push_str(&self.format_middle(&widths));
|
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 {
|
for row in data {
|
||||||
result.push_str(&self.format_row(&row, &widths));
|
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
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -269,14 +279,22 @@ impl AsciiTable {
|
|||||||
let conf = self.columns.get(&a).unwrap_or(default_conf);
|
let conf = self.columns.get(&a).unwrap_or(default_conf);
|
||||||
self.format_cell(cell, width, ' ', conf.align)
|
self.format_cell(cell, width, ' ', conf.align)
|
||||||
}).collect();
|
}).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 {
|
fn format_header_row(&self, row: &[SmartString], widths: &[usize], aligns: &[Align]) -> String {
|
||||||
let row: Vec<_> = row.iter().zip(widths.iter()).map(|(cell, &width)|
|
let row: Vec<_> = row.iter().zip(widths.iter()).zip(aligns.iter()).map(|((cell, &width), &align)|
|
||||||
self.format_cell(cell, width, ' ', Align::Left)
|
self.format_cell(cell, width, ' ', align)
|
||||||
).collect();
|
).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 {
|
fn format_last(&self, widths: &[usize]) -> String {
|
||||||
|
Loading…
Reference in New Issue
Block a user