Skip to content

Commit d4f717e

Browse files
committed
Add override for color preference when printing tables + polish some APIs
1 parent e352baf commit d4f717e

File tree

10 files changed

+131
-88
lines changed

10 files changed

+131
-88
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "cli-table"
3-
version = "0.3.1"
3+
version = "0.3.2"
44
authors = ["Devashish Dixit <devashishdxt@gmail.com>"]
55
license = "MIT/Apache-2.0"
66
description = "A crate for printing tables on command line"

examples/simple.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ fn main() -> Result<(), Box<dyn Error>> {
3030
Cell::new("25", justify_right),
3131
]),
3232
],
33-
TableFormat::default().foreground(Color::Red),
33+
TableFormat::builder()
34+
.foreground_color(Some(Color::Red))
35+
.build(),
3436
)?;
3537

3638
table.print_stdout()?;

src/cell.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use unicode_width::UnicodeWidthStr;
88

99
use crate::format::{Align, CellFormat, Justify, Padding};
1010

11-
/// A `Cell` in a [`Table`](struct.Table.html)
11+
/// A `Cell` in a [`Table`](crate::Table)
1212
#[derive(Debug)]
1313
pub struct Cell {
1414
pub(crate) data: Vec<String>,
@@ -18,7 +18,7 @@ pub struct Cell {
1818
}
1919

2020
impl Cell {
21-
/// Creates a new [`Cell`](struct.Cell.html)
21+
/// Creates a new [`Cell`](crate::Cell)
2222
pub fn new<T: Display + ?Sized>(data: &T, format: CellFormat) -> Self {
2323
let data: Vec<String> = data.to_string().lines().map(ToString::to_string).collect();
2424
let height = data.len() + format.padding.top + format.padding.bottom;

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
/// Errors returned by functions in this crate
44
#[derive(Debug)]
55
pub enum Error {
6-
/// Returned when there is a mismatch in number of columns in different rows of a [`Table`](struct.Table.html)
6+
/// Returned when there is a mismatch in number of columns in different rows of a [`Table`](crate::Table)
77
MismatchedColumns,
88
}
99

src/format.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
//! Utilities for formatting of a [`Table`](struct.Table.html)
1+
//! Utilities for formatting of a [`Table`](crate::Table)
22
mod cell;
33
mod table;
44

55
pub use self::cell::{
66
Align, CellFormat, CellFormatBuilder, Color, Justify, Padding, PaddingBuilder,
77
};
88
pub use self::table::{
9-
Border, BorderBuilder, HorizontalLine, Separator, SeparatorBuilder, TableFormat, VerticalLine,
9+
Border, BorderBuilder, HorizontalLine, Separator, SeparatorBuilder, TableFormat,
10+
TableFormatBuilder, VerticalLine,
1011
};
1112

12-
/// Format with borders, column separators and row separators (calling `Default::default()` on [`TableFormat`](struct.TableFormat.html)
13+
/// Format with borders, column separators and row separators (calling `Default::default()` on [`TableFormat`](crate::format::TableFormat)
1314
/// also returns this format)
1415
///
1516
/// ```markdown
@@ -24,7 +25,7 @@ pub use self::table::{
2425
/// +------------+----------------+
2526
/// ```
2627
pub const BORDER_COLUMN_ROW: TableFormat = TableFormat {
27-
foreground: None,
28+
foreground_color: None,
2829
border: Border {
2930
top: Some(HorizontalLine {
3031
left_end: '+',
@@ -65,7 +66,7 @@ pub const BORDER_COLUMN_ROW: TableFormat = TableFormat {
6566
/// +------------+----------------+
6667
/// ```
6768
pub const BORDER_COLUMN_TITLE: TableFormat = TableFormat {
68-
foreground: None,
69+
foreground_color: None,
6970
border: Border {
7071
top: Some(HorizontalLine {
7172
left_end: '+',
@@ -105,7 +106,7 @@ pub const BORDER_COLUMN_TITLE: TableFormat = TableFormat {
105106
/// +------------+----------------+
106107
/// ```
107108
pub const BORDER_COLUMN_NO_ROW: TableFormat = TableFormat {
108-
foreground: None,
109+
foreground_color: None,
109110
border: Border {
110111
top: Some(HorizontalLine {
111112
left_end: '+',
@@ -139,7 +140,7 @@ pub const BORDER_COLUMN_NO_ROW: TableFormat = TableFormat {
139140
/// Scooby Doo | 25
140141
/// ```
141142
pub const NO_BORDER_COLUMN_TITLE: TableFormat = TableFormat {
142-
foreground: None,
143+
foreground_color: None,
143144
border: Border {
144145
top: None,
145146
bottom: None,
@@ -170,7 +171,7 @@ pub const NO_BORDER_COLUMN_TITLE: TableFormat = TableFormat {
170171
/// Scooby Doo | 25
171172
/// ```
172173
pub const NO_BORDER_COLUMN_ROW: TableFormat = TableFormat {
173-
foreground: None,
174+
foreground_color: None,
174175
border: Border {
175176
top: None,
176177
bottom: None,
@@ -203,7 +204,7 @@ pub const NO_BORDER_COLUMN_ROW: TableFormat = TableFormat {
203204
/// +----------------------------+
204205
/// ```
205206
pub const BORDER_NO_COLUMN_ROW: TableFormat = TableFormat {
206-
foreground: None,
207+
foreground_color: None,
207208
border: Border {
208209
top: Some(HorizontalLine {
209210
left_end: '+',

src/format/cell.rs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub use termcolor::Color;
22

3-
/// Struct for configuring a [`Cell`](struct.Cell.html)'s format
3+
/// Struct for configuring a [`Cell`](crate::Cell)'s format
44
#[derive(Debug, Clone, Copy, Default)]
55
pub struct CellFormat {
66
pub(crate) justify: Justify,
@@ -13,75 +13,75 @@ pub struct CellFormat {
1313
}
1414

1515
impl CellFormat {
16-
/// Creates a new builder for [`CellFormat`](struct.CellFormat.html)
16+
/// Creates a new builder for [`CellFormat`](crate::format::CellFormat)
1717
#[inline]
1818
pub fn builder() -> CellFormatBuilder {
1919
Default::default()
2020
}
2121
}
2222

23-
/// Builder for [`CellFormat`](struct.CellFormat.html)
23+
/// Builder for [`CellFormat`](crate::format::CellFormat)
2424
#[derive(Debug, Default)]
2525
pub struct CellFormatBuilder(CellFormat);
2626

2727
impl CellFormatBuilder {
28-
/// Justify contents of a [`Cell`](struct.Cell.html)
28+
/// Justify contents of a [`Cell`](crate::Cell)
2929
#[inline]
3030
pub fn justify(mut self, justify: Justify) -> Self {
3131
self.0.justify = justify;
3232
self
3333
}
3434

35-
/// Align contents of a [`Cell`](struct.Cell.html)
35+
/// Align contents of a [`Cell`](crate::Cell)
3636
#[inline]
3737
pub fn align(mut self, align: Align) -> Self {
3838
self.0.align = align;
3939
self
4040
}
4141

42-
/// Sets padding of a [`Cell`](struct.Cell.html)
42+
/// Sets padding of a [`Cell`](crate::Cell)
4343
#[inline]
4444
pub fn padding(mut self, padding: Padding) -> Self {
4545
self.0.padding = padding;
4646
self
4747
}
4848

49-
/// Set foreground color of a [`Cell`](struct.Cell.html)
49+
/// Set foreground color of a [`Cell`](crate::Cell)
5050
#[inline]
5151
pub fn foreground_color(mut self, foreground_color: Option<Color>) -> Self {
5252
self.0.foreground_color = foreground_color;
5353
self
5454
}
5555

56-
/// Set background color of a [`Cell`](struct.Cell.html)
56+
/// Set background color of a [`Cell`](crate::Cell)
5757
#[inline]
5858
pub fn background_color(mut self, background_color: Option<Color>) -> Self {
5959
self.0.background_color = background_color;
6060
self
6161
}
6262

63-
/// Set contents of [`Cell`](struct.Cell.html) to be bold
63+
/// Set contents of [`Cell`](crate::Cell) to be bold
6464
#[inline]
6565
pub fn bold(mut self, bold: bool) -> Self {
6666
self.0.bold = bold;
6767
self
6868
}
6969

70-
/// Set contents of [`Cell`](struct.Cell.html) to be underlined
70+
/// Set contents of [`Cell`](crate::Cell) to be underlined
7171
#[inline]
7272
pub fn underline(mut self, underline: bool) -> Self {
7373
self.0.underline = underline;
7474
self
7575
}
7676

77-
/// Build [`CellFormat`](struct.CellFormat.html)
77+
/// Build [`CellFormat`](crate::format::CellFormat)
7878
#[inline]
7979
pub fn build(self) -> CellFormat {
8080
self.0
8181
}
8282
}
8383

84-
/// Used to horizontally justify contents of a [`Cell`](struct.Cell.html)
84+
/// Used to horizontally justify contents of a [`Cell`](crate::Cell)
8585
#[derive(Debug, Clone, Copy)]
8686
pub enum Justify {
8787
/// Justifies contents to left
@@ -99,7 +99,7 @@ impl Default for Justify {
9999
}
100100
}
101101

102-
/// Used to vertically align contents of a [`Cell`](struct.Cell.html)
102+
/// Used to vertically align contents of a [`Cell`](crate::Cell)
103103
#[derive(Debug, Clone, Copy)]
104104
pub enum Align {
105105
/// Aligns contents to top
@@ -117,7 +117,7 @@ impl Default for Align {
117117
}
118118
}
119119

120-
/// Used to add padding to the contents of a [`Cell`](struct.Cell.html)
120+
/// Used to add padding to the contents of a [`Cell`](crate::Cell)
121121
#[derive(Debug, Clone, Copy, Default)]
122122
pub struct Padding {
123123
/// Left padding
@@ -131,46 +131,46 @@ pub struct Padding {
131131
}
132132

133133
impl Padding {
134-
/// Creates a new builder for [`Padding`](struct.Padding.html)
134+
/// Creates a new builder for [`Padding`](crate::format::Padding)
135135
pub fn builder() -> PaddingBuilder {
136136
Default::default()
137137
}
138138
}
139139

140-
/// Builder for [`Padding`](struct.Padding.html)
140+
/// Builder for [`Padding`](crate::format::Padding)
141141
#[derive(Debug, Default)]
142142
pub struct PaddingBuilder(Padding);
143143

144144
impl PaddingBuilder {
145-
/// Sets left padding of a [`Cell`](struct.Cell.html)
145+
/// Sets left padding of a [`Cell`](crate::Cell)
146146
#[inline]
147147
pub fn left(mut self, left_padding: usize) -> Self {
148148
self.0.left = left_padding;
149149
self
150150
}
151151

152-
/// Sets right padding of a [`Cell`](struct.Cell.html)
152+
/// Sets right padding of a [`Cell`](crate::Cell)
153153
#[inline]
154154
pub fn right(mut self, right_padding: usize) -> Self {
155155
self.0.right = right_padding;
156156
self
157157
}
158158

159-
/// Sets top padding of a [`Cell`](struct.Cell.html)
159+
/// Sets top padding of a [`Cell`](crate::Cell)
160160
#[inline]
161161
pub fn top(mut self, top_padding: usize) -> Self {
162162
self.0.top = top_padding;
163163
self
164164
}
165165

166-
/// Sets bottom padding of a [`Cell`](struct.Cell.html)
166+
/// Sets bottom padding of a [`Cell`](crate::Cell)
167167
#[inline]
168168
pub fn bottom(mut self, bottom_padding: usize) -> Self {
169169
self.0.bottom = bottom_padding;
170170
self
171171
}
172172

173-
/// Build [`Padding`](struct.Padding.html)
173+
/// Build [`Padding`](crate::format::Padding)
174174
#[inline]
175175
pub fn build(self) -> Padding {
176176
self.0

0 commit comments

Comments
 (0)