FormatBuilder

Struct FormatBuilder 

Source
pub struct FormatBuilder { /* private fields */ }
Expand description

A builder to create a TableFormat

Implementations§

Source§

impl FormatBuilder

Source

pub fn new() -> FormatBuilder

Creates a new builder

Examples found in repository?
examples/formatting.rs (line 57)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}
Source

pub fn padding(self, left: usize, right: usize) -> Self

Set left and right padding

Examples found in repository?
examples/formatting.rs (line 64)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}
Source

pub fn column_separator(self, separator: char) -> Self

Set the character used for internal column separation

Examples found in repository?
examples/formatting.rs (line 58)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}
Source

pub fn borders(self, border: char) -> Self

Set the character used for table borders

Examples found in repository?
examples/formatting.rs (line 59)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}
Source

pub fn left_border(self, border: char) -> Self

Set the character used for left table border

Source

pub fn right_border(self, border: char) -> Self

Set the character used for right table border

Source

pub fn separator(self, what: LinePosition, separator: LineSeparator) -> Self

Set a line separator format

Source

pub fn separators(self, what: &[LinePosition], separator: LineSeparator) -> Self

Set separator format for multiple kind of line separators

Examples found in repository?
examples/formatting.rs (lines 60-63)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}
Source

pub fn indent(self, spaces: usize) -> Self

Set global indentation in spaces used when rendering a table

Source

pub fn build(&self) -> TableFormat

Return the generated TableFormat

Examples found in repository?
examples/formatting.rs (line 65)
7fn main() {
8  let mut table = table!(["Value 1", "Value 2"], ["Value three", "Value four"]);
9  table.set_titles(row!["Title 1", "Title 2"]);
10
11  // Print
12  // +-------------+------------+
13  // | Title 1     | Title 2    |
14  // +-------------+------------+
15  // | Value 1     | Value 2    |
16  // | Value three | Value four |
17  // +-------------+------------+
18  println!("FORMAT_NO_LINESEP_WITH_TITLE :");
19  table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
20  table.printstd();
21  println!();
22
23  // Print
24  // -------------------------
25  //  Title 1      Title 2
26  // =========================
27  //  Value 1      Value 2
28  // -------------------------
29  //  Value three  Value four
30  // -------------------------
31  println!("FORMAT_NO_COLSEP :");
32  table.set_format(*format::consts::FORMAT_NO_COLSEP);
33  table.printstd();
34  println!();
35
36  // Print
37  // +-------------------------+
38  // | Title 1      Title 2    |
39  // +=========================+
40  // | Value 1      Value 2    |
41  // | Value three  Value four |
42  // +-------------------------+
43  println!("FORMAT_BORDERS_ONLY :");
44  table.set_format(*format::consts::FORMAT_BORDERS_ONLY);
45  table.printstd();
46  println!();
47
48  // Custom format can be implemented using `prettytable::format::FormatBuilder`
49  // Example to print
50  // +-------------+------------+
51  // | Title 1     | Title 2    |
52  // | Value 1     | Value 2    |
53  // | Value three | Value four |
54  // +-------------+------------+
55  println!("Custom :");
56  table.set_format(
57    format::FormatBuilder::new()
58      .column_separator('|')
59      .borders('|')
60      .separators(
61        &[format::LinePosition::Top, format::LinePosition::Bottom],
62        format::LineSeparator::new('-', '+', '+', '+'),
63      )
64      .padding(1, 1)
65      .build(),
66  );
67  table.printstd();
68
69  // Customized format with unicode
70  // Example to print
71  // ┌─────────────┬────────────┐
72  // │ Title 1     │ Title 2    │
73  // ├─────────────┼────────────┤
74  // │ Value 1     │ Value 2    │
75  // ├─────────────┼────────────┤
76  // │ Value three │ Value four │
77  // └─────────────┴────────────┘
78  println!("With unicode:");
79  table.set_format(
80    format::FormatBuilder::new()
81      .column_separator('│')
82      .borders('│')
83      .separators(
84        &[format::LinePosition::Top],
85        format::LineSeparator::new('─', '┬', '┌', '┐'),
86      )
87      .separators(
88        &[format::LinePosition::Intern],
89        format::LineSeparator::new('─', '┼', '├', '┤'),
90      )
91      .separators(
92        &[format::LinePosition::Bottom],
93        format::LineSeparator::new('─', '┴', '└', '┘'),
94      )
95      .padding(1, 1)
96      .build(),
97  );
98  table.printstd();
99
100  // Customized format with unicode and different padding
101  // Example to print
102  // ┌───────────────┬──────────────┐
103  // │  Title 1      │  Title 2     │
104  // ├───────────────┼──────────────┤
105  // │  Value 1      │  Value 2     │
106  // ├───────────────┼──────────────┤
107  // │  Value three  │  Value four  │
108  // └───────────────┴──────────────┘
109  // Change individual format settings
110  println!("With unicode and padding:");
111  table.get_format().padding(2, 2);
112  table.printstd();
113}

Trait Implementations§

Source§

impl Default for FormatBuilder

Source§

fn default() -> FormatBuilder

Returns the “default value” for a type. Read more
Source§

impl From<TableFormat> for FormatBuilder

Source§

fn from(fmt: TableFormat) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.