Struct pretable::PreTable

source ·
pub struct PreTable { /* private fields */ }
Expand description

A PreTable struct used for table representation

Implementations§

source§

impl PreTable

source

pub fn new() -> Self

Creates a new instance of PreTable

Examples found in repository?
examples/example/main.rs (line 6)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let mut table = PreTable::new();

    table.set_header_with_alignment(vec![
        ("NAME", Alignment::Left),
        ("FORMAL", Alignment::Center),
        ("HEIGHT", Alignment::Right),
    ]);

    table.add_body(vec!["Everest", "Chomolungma", "8848m"]);
    table.add_body(vec!["K2", "Karakorum No.2", "8611m"]);
    table.add_body(vec!["Kanchenjunga", "", "8505m"]);

    println!("{}", table.output());
}
More examples
Hide additional examples
examples/large_case.rs (line 10)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub fn main() {
    let n = 1_000_000;

    let numbers = (0..n).map(|i| (i + 1).to_string()).collect::<Vec<_>>();

    let mut table = PreTable::new();
    table.set_header(vec!["i"]);
    for n in numbers.iter() {
        table.add_body(vec![n]);
    }

    let output = table.output();

    // 処理が削除されないように output を使うフリをする。
    let t = output.as_bytes();
    println!("{}", t[3] + t[t.len() - 3]);
}
source

pub fn add_header(&mut self, v: &str)

Adds a header to the table with default alignment

source

pub fn add_header_with_alignment(&mut self, v: &str, alignment: Alignment)

Adds a header with a specified alignment

Examples
let mut table = PreTable::new();
table.add_header_with_alignment("KEY", Alignment::Left);
source

pub fn set_header(&mut self, v: Vec<&str>)

Sets the headers of the table with default alignment

Examples
let mut table = PreTable::new();
table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
Examples found in repository?
examples/large_case.rs (line 11)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub fn main() {
    let n = 1_000_000;

    let numbers = (0..n).map(|i| (i + 1).to_string()).collect::<Vec<_>>();

    let mut table = PreTable::new();
    table.set_header(vec!["i"]);
    for n in numbers.iter() {
        table.add_body(vec![n]);
    }

    let output = table.output();

    // 処理が削除されないように output を使うフリをする。
    let t = output.as_bytes();
    println!("{}", t[3] + t[t.len() - 3]);
}
source

pub fn set_header_with_alignment(&mut self, v: Vec<(&str, Alignment)>)

Sets the headers of the table with a specified alignment

Examples
let mut table = PreTable::new();
table.set_header_with_alignment(vec![
    ("KEY", Alignment::Left),
    ("VALUE", Alignment::Center),
    ("DESCRIPTION", Alignment::Right),
]);
Examples found in repository?
examples/example/main.rs (lines 8-12)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let mut table = PreTable::new();

    table.set_header_with_alignment(vec![
        ("NAME", Alignment::Left),
        ("FORMAL", Alignment::Center),
        ("HEIGHT", Alignment::Right),
    ]);

    table.add_body(vec!["Everest", "Chomolungma", "8848m"]);
    table.add_body(vec!["K2", "Karakorum No.2", "8611m"]);
    table.add_body(vec!["Kanchenjunga", "", "8505m"]);

    println!("{}", table.output());
}
source

pub fn add_body(&mut self, v: Vec<&str>)

Adds a row to the body of the table

Examples
let mut table = PreTable::new();
table.set_header(vec!["KEY", "VALUE", "DESCRIPTION"]);
table.add_body(vec!["key1", "value1", "description1"]);
Examples found in repository?
examples/example/main.rs (line 14)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let mut table = PreTable::new();

    table.set_header_with_alignment(vec![
        ("NAME", Alignment::Left),
        ("FORMAL", Alignment::Center),
        ("HEIGHT", Alignment::Right),
    ]);

    table.add_body(vec!["Everest", "Chomolungma", "8848m"]);
    table.add_body(vec!["K2", "Karakorum No.2", "8611m"]);
    table.add_body(vec!["Kanchenjunga", "", "8505m"]);

    println!("{}", table.output());
}
More examples
Hide additional examples
examples/large_case.rs (line 13)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub fn main() {
    let n = 1_000_000;

    let numbers = (0..n).map(|i| (i + 1).to_string()).collect::<Vec<_>>();

    let mut table = PreTable::new();
    table.set_header(vec!["i"]);
    for n in numbers.iter() {
        table.add_body(vec![n]);
    }

    let output = table.output();

    // 処理が削除されないように output を使うフリをする。
    let t = output.as_bytes();
    println!("{}", t[3] + t[t.len() - 3]);
}
source

pub fn line(&self) -> String

Generates a line string based on the current state of table

source

pub fn output(&self) -> String

Returns the complete table as a string

Examples found in repository?
examples/example/main.rs (line 18)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    let mut table = PreTable::new();

    table.set_header_with_alignment(vec![
        ("NAME", Alignment::Left),
        ("FORMAL", Alignment::Center),
        ("HEIGHT", Alignment::Right),
    ]);

    table.add_body(vec!["Everest", "Chomolungma", "8848m"]);
    table.add_body(vec!["K2", "Karakorum No.2", "8611m"]);
    table.add_body(vec!["Kanchenjunga", "", "8505m"]);

    println!("{}", table.output());
}
More examples
Hide additional examples
examples/large_case.rs (line 16)
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
pub fn main() {
    let n = 1_000_000;

    let numbers = (0..n).map(|i| (i + 1).to_string()).collect::<Vec<_>>();

    let mut table = PreTable::new();
    table.set_header(vec!["i"]);
    for n in numbers.iter() {
        table.add_body(vec![n]);
    }

    let output = table.output();

    // 処理が削除されないように output を使うフリをする。
    let t = output.as_bytes();
    println!("{}", t[3] + t[t.len() - 3]);
}
source

pub fn set_show_header(&mut self, b: bool)

Decides whether to show the header in output

source

pub fn set_body_split(&mut self, b: bool)

Decides whether to split the body in output

source

pub fn set_line_char(&mut self, c: char)

Sets the character used for line separation

source

pub fn set_vertical_char(&mut self, c: char)

Sets the character used for vertical separation

source

pub fn set_corner_char(&mut self, c: char)

Sets the character used for corners

source

pub fn set_default_alignment(&mut self, a: Alignment)

Sets the alignment used for format align

Trait Implementations§

source§

impl Debug for PreTable

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.