0
点赞
收藏
分享

微信扫一扫

【Rust每周一库】prettytable-rs帮你轻松打印表单

相信大家在写代码的过程中,不时的会遇到一些往标准输出显示表格的需求,那prettytable-rs就可以帮你轻松搞定。废话不多说,让我们看看怎么使用它吧~

依赖

[dependencies]
prettytable-rs = "^0.8"

基础用法

一个表单由数个Row组成,而每个Row由数个Cell组成。

#[macro_use] extern crate prettytable;

use prettytable::{Cell, Row, Table};

fn main() {
// 创建表格
let mut table = Table::new();

// 添加行
table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
table.add_row(row!["foobar", "bar", "foo"]);
// 通过Cell添加行
table.add_row(Row::new(vec![
Cell::new("foobar2"),
Cell::new("bar2"),
Cell::new("foo2"),
]));

// 打印表格到标准输出
table.printstd();
}

屏幕显示

+---------+------+---------+
| ABC | DEFG | HIJKLMN |
+---------+------+---------+
| foobar | bar | foo |
+---------+------+---------+
| foobar2 | bar2 | foo2 |
+---------+------+---------+

两个宏

table宏可以使得输入更加简便。下面的例子将产生和之前一样的屏幕输出。

#[macro_use] extern crate prettytable;

fn main() {
let table = table!(["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]);

table.printstd();
}

ptable宏在创建表格之后还会执行输出。

#[macro_use] extern crate prettytable;

fn main() {
let table = ptable!(["ABC", "DEFG", "HIJKLMN"],
["foobar", "bar", "foo"],
["foobar2", "bar2", "foo2"]);
}

调整表格的格式

通过prettytable::format::TableFormat可以设定表格的格式。比如说如下的例子

#[macro_use] extern crate prettytable;

use prettytable::{format, Table};

fn main() {
let mut table = Table::new();
let format = format::FormatBuilder::new()
.column_separator('|')
.borders('|')
.separators(&[format::LinePosition::Top,
format::LinePosition::Bottom],
format::LineSeparator::new('-', '+', '+', '+'))
.padding(1, 1)
.build();
table.set_format(format);

table.set_titles(row!["Title 1", "Title 2"]);
table.add_row(row!["Value 1", "Value 2"]);
table.add_row(row!["Value three", "Value four"]);

table.printstd();
}

会显示这样的输出

+-------------+------------+
| Title 1 | Title 2 |
| Value 1 | Value 2 |
| Value three | Value four |
+-------------+------------+

但实际上prettytable::format::consts提供了很多常用的格式,基本已经够用了。 比如想要这样的输出

+-------------+------------+
| Title 1 | Title 2 |
+-------------+------------+
| Value 1 | Value 2 |
| Value three | Value four |
+-------------+------------+

可以用

use prettytable::format;

table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE;

而想要这样的输出

Title 1     | Title 2
------------+------------
Value 1 | Value 2
Value three | Value four

可以用

use prettytable::format;

table.set_forma(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);

CSV格式的读取/输出

表格可以被输出成CSV格式,也可以从CSV格式的文件创建表格。可以简单参考如下的例子

#[macro_use] extern crate prettytable;

use std::fs::File;
use prettytable::{Cell, Row, Table};

fn main() {
let mut table = Table::new();

table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
table.add_row(row!["foobar", "bar", "foo"]);

table.add_row(Row::new(vec![
Cell::new("foobar2"),
Cell::new("bar2"),
Cell::new("foo2"),
]));

let out = File::create("output.csv").unwrap();
// 写入
table.to_csv(out).unwrap();

// 读取
let table = Table::from_csv_file("output.csv").unwrap();
table.printstd();
}

注意产生/读取的CSV文件的分隔符是逗号,并且没有头。

小结

现在想必各位都已经快成为Rust的表格打印大师了。以后可以在命令行里没事就秀个表格亮瞎别人的狗眼啦~


举报

相关推荐

0 条评论