Skip to content

Commit e2e5cf9

Browse files
committed
v1.0.1
* TPrint::print() now returns io::Result<()> * Documentation improvements
1 parent 965f318 commit e2e5cf9

9 files changed

+104
-85
lines changed

Cargo.toml

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tprint"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
authors = ["Paul Jonkins <[email protected]>"]
55
edition = "2021"
66
description = "A simple crate to print tabular data"
@@ -19,17 +19,4 @@ chrono = "0.4.39"
1919

2020
[features]
2121
default = ["unicode"]
22-
unicode = ["dep:unicode-width"]
23-
24-
25-
[[example]]
26-
name = "basic_usage"
27-
path = "examples/basic_usage.rs"
28-
29-
[[example]]
30-
name = "file_output"
31-
path = "examples/file_output.rs"
32-
33-
[[example]]
34-
name = "html_output"
35-
path = "examples/html_output.rs"
22+
unicode = ["dep:unicode-width"]

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ A similar C library: [libtprint](https://github.com/wizzard/libtprint)
2727
```rust
2828
use tprint::{TPrint, TPrintAlign};
2929

30-
fn main() {
30+
fn main() -> std::io::Result<()> {
3131
TPrint::new(true, true, 10, 3).
3232
column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
3333
column_add("Age", TPrintAlign::Center, TPrintAlign::Center).
@@ -44,7 +44,7 @@ fn main() {
4444
add_data("Madrid").
4545
add_data("Accountant").
4646
add_data("❤️😴🤦🏼‍♂️").
47-
print();
47+
print()
4848
}
4949
```
5050

@@ -57,7 +57,7 @@ use tprint::{TPrint, TPrintAlign, TPrintOutputString, TPrintBordersUnicode};
5757
use std::cell::RefCell;
5858
use std::rc::Rc;
5959

60-
fn main() {
60+
fn main() -> std::io::Result<()> {
6161
let str_output = Rc::new(RefCell::new(TPrintOutputString::new()));
6262
let borders = Rc::new(RefCell::new(TPrintBordersUnicode {}));
6363
let mut str_tprint = TPrint::new_with_borders_output(borders, str_output.clone(), true, true, 0, 1);
@@ -79,6 +79,7 @@ fn main() {
7979
str_tprint.print();
8080

8181
println!("{}", str_output.borrow().get_str());
82+
Ok(())
8283
}
8384
```
8485

@@ -93,7 +94,7 @@ use std::rc::Rc;
9394

9495
const OUT_FILE: &str = "/tmp/tprint.html";
9596

96-
fn main() {
97+
fn main() -> std::io::Result<()> {
9798
let output = Rc::new(RefCell::new(TPrintOutputFile::new(OUT_FILE).unwrap()));
9899
let borders = Rc::new(RefCell::new(TPrintBordersHTML {}));
99100
TPrint::new_with_borders_output(borders, output, true, true, 0, 1).
@@ -112,8 +113,7 @@ fn main() {
112113
add_data("Madrid").
113114
add_data("Accountant").
114115
add_data("❤️😴🤦🏼‍♂️").
115-
print();
116-
println!();
116+
print()
117117
}
118118
```
119119

examples/basic_usage.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use tprint::{TPrint, TPrintAlign};
22

3-
fn main() {
3+
fn main() -> std::io::Result<()> {
44
println!("1. Basic usage example: a table with 5 columns and 2 rows, with 10 spaces of padding from the left:");
55
TPrint::new(true, true, 10, 3).
66
column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
@@ -18,10 +18,9 @@ fn main() {
1818
add_data("Madrid").
1919
add_data("Accountant").
2020
add_data("❤️😴🤦🏼‍♂️").
21-
print();
22-
println!();
21+
print()?;
2322

24-
println!("2. Basic usage example: a table without borders with 5 columns and 2 rows, with 10 spaces of padding from the left:");
23+
println!("\n2. Basic usage example: a table without borders with 5 columns and 2 rows, with 10 spaces of padding from the left:");
2524
TPrint::new(false, true, 10, 3).
2625
column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
2726
column_add("Age", TPrintAlign::Center, TPrintAlign::Center).
@@ -38,10 +37,9 @@ fn main() {
3837
add_data("Madrid").
3938
add_data("Accountant").
4039
add_data("❤️😴🤦🏼‍♂️").
41-
print();
42-
println!();
40+
print()?;
4341

44-
println!("3. Basic usage example: a table without borders and headers with 5 columns and 2 rows, with 10 spaces of padding from the left:");
42+
println!("\n3. Basic usage example: a table without borders and headers with 5 columns and 2 rows, with 10 spaces of padding from the left:");
4543
TPrint::new(false, false, 10, 3).
4644
column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
4745
column_add("Age", TPrintAlign::Center, TPrintAlign::Center).
@@ -58,6 +56,5 @@ fn main() {
5856
add_data("Madrid").
5957
add_data("Accountant").
6058
add_data("❤️😴🤦🏼‍♂️").
61-
print();
62-
println!();
59+
print()
6360
}

examples/file_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() -> io::Result<()> {
4343
f_tprint.add_data(file_date);
4444
}
4545

46-
f_tprint.print();
46+
f_tprint.print()?;
4747

4848
let output = fs::read_to_string(OUT_FILE)?;
4949
print!("{}", output);

examples/html_output.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::rc::Rc;
44

55
const OUT_FILE: &str = "/tmp/tprint.html";
66

7-
fn main() {
7+
fn main() -> std::io::Result<()> {
88
let output = Rc::new(RefCell::new(TPrintOutputFile::new(OUT_FILE).unwrap()));
99
let borders = Rc::new(RefCell::new(TPrintBordersHTML {}));
1010
TPrint::new_with_borders_output(borders, output, true, true, 0, 1).
@@ -23,6 +23,5 @@ fn main() {
2323
add_data("Madrid").
2424
add_data("Accountant").
2525
add_data("❤️😴🤦🏼‍♂️").
26-
print();
27-
println!();
26+
print()
2827
}

examples/string_output.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use tprint::{TPrint, TPrintAlign, TPrintOutputString, TPrintBordersUnicode};
22
use std::cell::RefCell;
33
use std::rc::Rc;
44

5-
fn main() {
5+
fn main() -> std::io::Result<()> {
66
let str_output = Rc::new(RefCell::new(TPrintOutputString::new()));
77
let borders = Rc::new(RefCell::new(TPrintBordersUnicode {}));
88
let mut str_tprint = TPrint::new_with_borders_output(borders, str_output.clone(), true, true, 0, 1);
@@ -21,7 +21,9 @@ fn main() {
2121
str_tprint.add_data(j.pow(i).to_string());
2222
str_tprint.add_data(j.pow(MAX-i-1).to_string());
2323
}
24-
str_tprint.print();
24+
str_tprint.print()?;
2525

2626
println!("{}", str_output.borrow().get_str());
27+
28+
Ok(())
2729
}

src/output.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io::{self, Write};
44

55
/// Defines the methods that a TPrintOutput object must implement.
66
pub trait TPrintOutput {
7-
fn print_str(&mut self, s: &str);
7+
fn print_str(&mut self, s: &str) -> Result<(), io::Error>;
88
}
99

1010
/// An struct that prints output to stdout (default).
@@ -13,8 +13,9 @@ pub struct TPrintOutputStdout {}
1313
impl TPrintOutputStdout {}
1414

1515
impl TPrintOutput for TPrintOutputStdout {
16-
fn print_str(&mut self, s: &str) {
16+
fn print_str(&mut self, s: &str) -> Result<(), io::Error> {
1717
print!("{}", s);
18+
Ok(())
1819
}
1920
}
2021

@@ -44,8 +45,9 @@ impl Default for TPrintOutputString {
4445
}
4546

4647
impl TPrintOutput for TPrintOutputString {
47-
fn print_str(&mut self, s: &str) {
48+
fn print_str(&mut self, s: &str) -> Result<(), io::Error> {
4849
self.str.push_str(s);
50+
Ok(())
4951
}
5052
}
5153

@@ -68,7 +70,7 @@ impl TPrintOutputFile {
6870
}
6971

7072
impl TPrintOutput for TPrintOutputFile {
71-
fn print_str(&mut self, s: &str) {
72-
self.f.write_all(s.as_bytes()).unwrap();
73+
fn print_str(&mut self, s: &str) -> io::Result<()> {
74+
self.f.write_all(s.as_bytes())
7375
}
7476
}

0 commit comments

Comments
 (0)