Skip to content

Commit 9881854

Browse files
authored
Add basic example of nested table + remove trailing newline from table (#22)
1 parent a044206 commit 9881854

File tree

6 files changed

+41
-34
lines changed

6 files changed

+41
-34
lines changed

cli-table/examples/nested.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use std::io::Result;
2+
3+
use cli_table::{format::Justify, Cell, Style, Table};
4+
5+
fn main() -> Result<()> {
6+
let nested_table = vec![vec![20.cell()]].table();
7+
8+
let table = vec![
9+
vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
10+
vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
11+
vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
12+
vec![
13+
"Nested".cell(),
14+
nested_table.display()?.cell().justify(Justify::Right),
15+
],
16+
]
17+
.table()
18+
.title(vec![
19+
"Name".cell().bold(true),
20+
"Age (in years)".cell().bold(true),
21+
])
22+
.bold(true);
23+
24+
println!("{}", table.display()?);
25+
26+
Ok(())
27+
}

cli-table/src/buffers.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ impl<'a> Buffers<'a> {
3939
Ok(())
4040
}
4141

42-
pub fn end(&mut self) -> Result<()> {
43-
if let Some(mut current_buffer) = self.current_buffer.take() {
44-
current_buffer.reset()?;
45-
self.buffers.push(current_buffer);
46-
}
47-
48-
Ok(())
49-
}
50-
5142
pub fn into_vec(self) -> Result<Vec<Buffer>> {
5243
let mut buffers = self.buffers;
5344

cli-table/src/display.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ impl TableDisplay {
1515

1616
impl fmt::Display for TableDisplay {
1717
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18-
write!(f, "{}", self.inner)
18+
write!(f, "{}", self.inner.trim())
1919
}
2020
}

cli-table/src/row.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
buffers::Buffers,
77
cell::{Cell, CellStruct, Dimension as CellDimension},
88
table::{Dimension as TableDimension, TableFormat},
9-
utils::{print_char, print_vertical_line, println_str, transpose},
9+
utils::{print_char, print_vertical_line, println, transpose},
1010
};
1111

1212
/// Concrete row of a table
@@ -72,9 +72,7 @@ impl RowStruct {
7272
}
7373
}
7474

75-
println_str(&mut buffers, "", color_spec)?;
76-
77-
buffers.end()?;
75+
println(&mut buffers)?;
7876
}
7977

8078
buffers.into_vec()

cli-table/src/table.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl TableStruct {
122122
&self.format,
123123
&color_spec,
124124
)?;
125+
println(&mut buffers)?;
125126

126127
if let Some(ref title) = self.title {
127128
let title_dimension = row_dimensions.next().unwrap();
@@ -147,6 +148,8 @@ impl TableStruct {
147148
&color_spec,
148149
)?
149150
}
151+
152+
println(&mut buffers)?;
150153
}
151154

152155
let mut rows = self.rows.iter().zip(row_dimensions).peekable();
@@ -172,6 +175,10 @@ impl TableStruct {
172175
&color_spec,
173176
)?,
174177
}
178+
179+
if rows.peek().is_some() {
180+
println(&mut buffers)?;
181+
}
175182
}
176183

177184
buffers.into_vec()

cli-table/src/utils.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ pub(crate) fn print_horizontal_line(
9797
}
9898
None => {
9999
if table_format.border.right.is_some() {
100-
println_char(buffers, line.right_end, color_spec)?;
100+
print_char(buffers, line.right_end, color_spec)?;
101101
} else {
102-
println_str(buffers, "", color_spec)?;
102+
print_str(buffers, "", color_spec)?;
103103
}
104104
}
105105
}
@@ -126,28 +126,12 @@ pub(crate) fn print_str(buffers: &mut Buffers<'_>, s: &str, color_spec: &ColorSp
126126
buffers.reset()
127127
}
128128

129-
pub(crate) fn println_str(
130-
buffers: &mut Buffers<'_>,
131-
s: &str,
132-
color_spec: &ColorSpec,
133-
) -> Result<()> {
134-
buffers.set_color(color_spec)?;
135-
writeln!(buffers, "{}", s)?;
136-
buffers.reset()
137-
}
138-
139129
pub(crate) fn print_char(buffers: &mut Buffers<'_>, c: char, color_spec: &ColorSpec) -> Result<()> {
140130
buffers.set_color(color_spec)?;
141131
write!(buffers, "{}", c)?;
142132
buffers.reset()
143133
}
144134

145-
pub(crate) fn println_char(
146-
buffers: &mut Buffers<'_>,
147-
c: char,
148-
color_spec: &ColorSpec,
149-
) -> Result<()> {
150-
buffers.set_color(color_spec)?;
151-
writeln!(buffers, "{}", c)?;
152-
buffers.reset()
135+
pub(crate) fn println(buffers: &mut Buffers<'_>) -> Result<()> {
136+
writeln!(buffers)
153137
}

0 commit comments

Comments
 (0)