Rust ASCII表格生成库ascii_table的使用,快速创建格式化文本表格并支持自定义样式
Rust ASCII表格生成库ascii_table的使用,快速创建格式化文本表格并支持自定义样式
基本使用示例
use ascii_table::AsciiTable;
let ascii_table = AsciiTable::default();
let data = vec![&[1, 2, 3], &[4, 5, 6], &[7, 8, 9]];
ascii_table.print(data);
// 输出结果:
// ┌───┬───┬───┐
// │ 1 │ 2 │ 3 │
// │ 4 │ 5 │ 6 │
// │ 7 │ 8 │ 9 │
// └───┴───┴───┘
自定义样式示例
use std::fmt::Display;
use ascii_table::{AsciiTable, Align};
let mut ascii_table = AsciiTable::default();
ascii_table.set_max_width(26);
ascii_table.column(0).set_header("H1").set_align(Align::Left);
ascii_table.column(1).set_header("H2").set_align(Align::Center);
ascii_table.column(2).set_header("H3").set_align(Align::Right);
let data: Vec<Vec<&dyn Display>> = vec![
vec![&'v', &'v', &'v'],
vec![&123, &456, &789, &"abcdef"]
];
ascii_table.print(data);
// 输出结果:
// ┌─────┬─────┬─────┬──────┐
// │ H1 │ H2 │ H3 │ │
// ├─────┼─────┼─────┼──────┤
// │ v │ v │ v │ │
// │ 123 │ 456 │ 789 │ abc+ │
// └─────┴─────┴─────┴──────┘
完整示例代码
use std::fmt::Display;
use ascii_table::{AsciiTable, Align};
fn main() {
// 基本使用示例
println!("基本表格示例:");
let basic_table = AsciiTable::default();
let basic_data = vec![&["ID", "Name", "Age"], &[1, "Alice", 25], &[2, "Bob", 30]];
basic_table.print(basic_data);
println!("\n自定义表格示例:");
// 创建表格实例
let mut table = AsciiTable::default();
// 设置表格最大宽度
table.set_max_width(40);
// 自定义列样式
table.column(0)
.set_header("ID")
.set_align(Align::Center);
table.column(1)
.set_header("Name")
.set_align(Align::Left);
table.column(2)
.set_header("Age")
.set_align(Align::Right);
// 准备数据
let data: Vec<Vec<&dyn Display>> = vec![
vec![&1, &"Alice", &25],
vec![&2, &"Bob", &30],
vec![&3, &"Charlie", &35],
vec![&4, &"David with a very long name", &40],
];
// 打印表格
table.print(data);
}
特性
auto_table_width
: 将ASCII表的默认最大宽度设置为终端的宽度color_codes
: 当存在终端颜色代码时正确计算字符串宽度wide_characters
: 当存在宽字符(如表情符号)时正确计算字符串宽度
安装
在项目目录中运行以下Cargo命令:
cargo add ascii_table
或者在Cargo.toml中添加:
ascii_table = "4.0.7"
这个库提供了简单易用的API来生成格式良好的ASCII表格,支持列对齐、表头设置和宽度控制等自定义功能。
1 回复
Rust ASCII表格生成库ascii_table的使用指南
ascii_table
是一个简单易用的Rust库,用于生成格式化的ASCII文本表格。它可以让你快速创建美观的表格输出,并支持自定义样式。
安装
在Cargo.toml中添加依赖:
[dependencies]
ascii_table = "0.8"
基本用法
创建简单表格
use ascii_table::{AsciiTable, Align};
fn main() {
let mut ascii_table = AsciiTable::default();
let data: Vec<Vec<String>> = vec![
vec!["Name".to_string(), "Age".to_string(), "Occupation".to_string()],
vec!["Alice".to_string(), "28".to_string(), "Engineer".to_string()],
vec!["Bob".to_string(), "35".to_string(), "Doctor".to_string()],
vec!["Charlie".to_string(), "42".to_string(), "Teacher".to_string()],
];
ascii_table.print(data);
}
输出结果:
+---------+-----+------------+
| Name | Age | Occupation |
+---------+-----+------------+
| Alice | 28 | Engineer |
| Bob | 35 | Doctor |
| Charlie | 42 | Teacher |
+---------+-----+------------+
自定义表格样式
修改对齐方式
use ascii_table::{AsciiTable, Align};
fn main() {
let mut ascii_table = AsciiTable::default();
// 设置列对齐方式
ascii_table.set_align(&[
Align::Left, // 第一列左对齐
Align::Center, // 第二列居中对齐
Align::Right // 第三列右对齐
]);
let data = vec![
vec!["Product".to_string(), "Price".to_string(), "Stock".to_string()],
vec!["Laptop".to_string(), "$999".to_string(), "15".to_string()],
vec!["Phone".to_string(), "$699".to_string(), "32"].to_string()],
vec!["Tablet".to_string(), "$399".to_string(), "7".to_string()],
];
ascii_table.print(data);
}
输出结果:
+---------+-------+-------+
| Product | Price | Stock |
+---------+-------+-------+
| Laptop | $999 | 15 |
| Phone | $699 | 32 |
| Tablet | $399 | 7 |
+---------+-------+-------+
自定义边框样式
use ascii_table::{AsciiTable, Align};
fn main() {
let mut ascii_table = AsciiTable::default();
// 自定义边框字符
ascii_table.set_border('*', '*', '*', '*', '*', '*', '*', '*');
let data = vec![
vec!["ID".to_string(), "Name".to_string(), "Score".to_string()],
vec!["1".to_string(), "Alice".to_string(), "95".to_string()],
vec!["2".to_string(), "Bob".to_string(), "88".to_string()],
];
ascii_table.print(data);
}
输出结果:
***************
* ID * Name * Score *
***************
* 1 * Alice * 95 *
* 2 * Bob * 88 *
***************
高级功能
从结构体生成表格
use ascii_table::{AsAsciiTable, AsciiTable};
#[derive(AsAsciiTable)]
struct Person {
name: String,
age: u8,
occupation: String,
}
fn main() {
let people = vec![
Person {
name: "Alice".to_string(),
age: 28,
occupation: "Engineer".to_string(),
},
Person {
name: "Bob".to_string(),
age: 35,
occupation: "Doctor".to_string(),
},
];
let mut ascii_table = AsciiTable::default();
ascii_table.print(people);
}
输出结果:
+-------+-----+------------+
| name | age | occupation |
+-------+-----+------------+
| Alice | 28 | Engineer |
| Bob | 35 | Doctor |
+-------+-----+------------+
设置列宽
use ascii_table::AsciiTable;
fn main() {
let mut ascii_table = AsciiTable::default();
// 设置最小列宽
ascii_table.set_min_column_width(15);
let data = vec![
vec!["Title".to_string(), "Author".to_string(), "Year".to_string()],
vec!["Rust Programming".to_string(), "Steve Klabnik".to_string(), "2018".to_string()],
vec!["The Book of Rust".to_string(), "Carol Nichols".to_string(), "2020".to_string()],
];
ascii_table.print(data);
}
输出结果:
+------------------+----------------+----------------+
| Title | Author | Year |
+------------------+----------------+----------------+
| Rust Programming | Steve Klabnik | 2018 |
| The Book of Rust | Carol Nichols | 2020 |
+------------------+----------------+----------------+
完整示例
下面是一个结合了多种功能的完整示例:
use ascii_table::{AsAsciiTable, AsciiTable, Align};
// 定义学生结构体
#[derive(AsAsciiTable)]
struct Student {
id: u32,
name: String,
score: f32,
passed: bool,
}
fn main() {
// 创建学生数据
let students = vec![
Student {
id: 1,
name: "Alice".to_string(),
score: 95.5,
passed: true,
},
Student {
id: 2,
name: "Bob".to_string(),
score: 58.0,
passed: false,
},
Student {
id: 3,
name: "Charlie".to_string(),
score: 72.3,
passed: true,
},
];
// 创建表格并设置样式
let mut ascii_table = AsciiTable::default();
// 设置对齐方式
ascii_table.set_align(&[
Align::Center, // ID列居中对齐
Align::Left, // 姓名左对齐
Align::Right, // 分数右对齐
Align::Center, // 是否通过居中对齐
]);
// 设置最小列宽
ascii_table.set_min_column_width(10);
// 自定义边框样式
ascii_table.set_border('=', '|', '=', '=', '|', '|', '=', '=');
// 打印表格
println!("学生成绩表:");
ascii_table.print(students);
}
输出结果:
学生成绩表:
============|============|============|============
| id | name | score | passed |
============|============|============|============
| 1 | Alice | 95.5 | true |
| 2 | Bob | 58.0 | false |
| 3 | Charlie | 72.3 | true |
============|============|============|============
总结
ascii_table
库提供了简单而强大的功能来创建格式化的ASCII表格。你可以:
- 快速创建基本表格
- 自定义对齐方式和边框样式
- 从结构体自动生成表格
- 控制列宽和其他显示属性
这个库非常适合需要在终端或文本文件中展示表格数据的Rust应用程序。