1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
-
+
-
-
+
-
+
-
-
+
+
-
+
|
[package]
name = "qpprint"
version = "0.2.1"
version = "0.3.0"
authors = ["Jan Danielsson <jan.danielsson@qrnch.com>"]
edition = "2021"
edition = "2024"
license = "0BSD"
# https://crates.io/category_slugs
categories = [ "text-processing" ]
keywords = [ "cli", "console", "terminal", "format", "print" ]
repository = "https://repos.qrnch.tech/pub/qpprint"
description = "Simple console printing/formatting."
rust-version = "1.56"
rust-version = "1.85"
exclude = [
".fossil-settings",
".efiles",
".fslckout",
"examples",
"www",
"bacon.toml",
"rustfmt.toml"
]
# https://doc.rust-lang.org/cargo/reference/manifest.html#the-badges-section
[badges]
maintenance = { status = "experimental" }
[dependencies]
colored = { version = "2.1.0" }
terminal_size = { version = "0.4.0" }
terminal_size = { version = "0.4.2" }
yansi = { version = "1.0.1" }
[lints.clippy]
all = { level = "deny", priority = -1 }
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }
multiple_crate_versions = "allow"
|
1
2
3
4
5
6
7
8
9
10
|
1
2
3
4
5
6
7
8
9
10
|
-
+
|
use qpprint::tbl::{Align, CellValue, Column, Data, Renderer};
use colored::{Color, ColoredString, Colorize};
use yansi::{Color, Painted};
fn main() {
table1();
println!();
table2();
println!();
table3();
|
︙ | | |
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
-
+
-
+
-
+
-
+
|
}
/// Print `Id` as blue if it is greater than `20`. Print `Name` as red if it
/// is equal to `world`.
fn table2() {
let idcol = Column::new("Id", ToString::to_string).stylize(|cv, s| {
let cs = ColoredString::from(s);
let cs = Painted::new(s.to_string());
if let CellValue::U64(id) = cv {
if *id > 20 {
cs.color(Color::Blue)
cs.fg(Color::Blue)
} else {
cs
}
} else {
cs
}
});
let namecol = Column::new("Name", ToString::to_string).stylize(|_cv, s| {
let cs = ColoredString::from(s);
let cs = Painted::new(s.to_string());
if s == "world" {
cs.color(Color::Red)
cs.fg(Color::Red)
} else {
cs
}
});
let columns = [idcol, namecol];
let mut data = Data::new(columns.len());
|
︙ | | |
1
2
3
4
5
6
7
8
9
10
11
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
+
+
|
pub mod tbl;
use terminal_size::{terminal_size, Height, Width};
pub use yansi::{Color, Painted};
pub enum Types {
Paragraph(String)
}
pub enum Align {
Left,
Center,
|
︙ | | |
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
-
+
-
+
|
Self {
indent: 0,
hang: 0,
maxwidth
}
}
pub fn set_indent(&mut self, indent: u16) -> &mut Self {
pub const fn set_indent(&mut self, indent: u16) -> &mut Self {
self.indent = indent;
self
}
/// Set a relative offset for the first line in a paragraph.
pub fn set_hang(&mut self, hang: i16) -> &mut Self {
pub const fn set_hang(&mut self, hang: i16) -> &mut Self {
self.hang = hang;
self
}
/*
pub(crate) fn set_maxwidth(&mut self, maxwidth: u16) -> &mut Self {
self.maxwidth = maxwidth;
|
︙ | | |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
-
+
-
+
|
use std::{
borrow::Cow,
fmt,
iter::{self, zip}
};
pub use colored::{Color, ColoredString, Colorize};
use yansi::Painted;
pub use super::Align;
#[allow(clippy::type_complexity)]
pub struct Column {
title: String,
min_width: Option<usize>,
max_width: Option<usize>,
trunc_len: usize,
trunc_ch: char,
renderer: Box<dyn Fn(&CellValue) -> String>,
stylize: Option<Box<dyn Fn(&CellValue, &str) -> ColoredString>>,
stylize: Option<Box<dyn Fn(&CellValue, &str) -> Painted<String>>>,
title_align: Align,
cell_align: Align
}
impl Column {
#[allow(clippy::needless_pass_by_value)]
pub fn new(
|
︙ | | |
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
|
-
+
-
+
-
+
-
+
-
+
-
+
-
+
|
assert!(max >= min);
}
self.max_width = Some(max);
self
}
#[must_use]
pub fn trunc_style(mut self, len: usize, ch: char) -> Self {
pub const fn trunc_style(mut self, len: usize, ch: char) -> Self {
self.trunc_style_ref(len, ch);
self
}
pub fn trunc_style_ref(&mut self, len: usize, ch: char) -> &mut Self {
pub const fn trunc_style_ref(&mut self, len: usize, ch: char) -> &mut Self {
self.trunc_len = len;
self.trunc_ch = ch;
self
}
#[must_use]
pub fn stylize(
mut self,
f: impl Fn(&CellValue, &str) -> ColoredString + 'static
f: impl Fn(&CellValue, &str) -> Painted<String> + 'static
) -> Self {
self.stylize = Some(Box::new(f));
self
}
#[must_use]
pub fn title_align(mut self, align: Align) -> Self {
pub const fn title_align(mut self, align: Align) -> Self {
self.title_align_ref(align);
self
}
pub fn title_align_ref(&mut self, align: Align) -> &mut Self {
pub const fn title_align_ref(&mut self, align: Align) -> &mut Self {
self.title_align = align;
self
}
#[must_use]
pub fn cell_align(mut self, align: Align) -> Self {
pub const fn cell_align(mut self, align: Align) -> Self {
self.cell_align_ref(align);
self
}
pub fn cell_align_ref(&mut self, align: Align) -> &mut Self {
pub const fn cell_align_ref(&mut self, align: Align) -> &mut Self {
self.cell_align = align;
self
}
}
pub enum CellValue {
|
︙ | | |
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
-
+
|
Self::U64(val)
}
}
impl fmt::Display for CellValue {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Str(ref s) => write!(f, "{s}"),
Self::Str(s) => write!(f, "{s}"),
Self::U64(v) => write!(f, "{v}")
}
}
}
pub struct Data {
|
︙ | | |
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
-
+
-
+
-
+
-
+
-
+
|
col_spacing: 2,
cols,
data: &data.cells
}
}
#[must_use]
pub fn header(mut self, underline: Option<char>) -> Self {
pub const fn header(mut self, underline: Option<char>) -> Self {
self.header_ref(underline);
self
}
pub fn header_ref(&mut self, underline: Option<char>) -> &mut Self {
pub const fn header_ref(&mut self, underline: Option<char>) -> &mut Self {
self.show_header = true;
self.header_underline = underline;
self
}
#[must_use]
pub fn column_spacing(mut self, n: usize) -> Self {
pub const fn column_spacing(mut self, n: usize) -> Self {
self.column_spacing_ref(n);
self
}
pub fn column_spacing_ref(&mut self, n: usize) -> &mut Self {
pub const fn column_spacing_ref(&mut self, n: usize) -> &mut Self {
self.col_spacing = n;
self
}
pub fn print(&self) {
println!("{}", self);
println!("{self}");
}
}
impl fmt::Display for Renderer<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
//
|
︙ | | |
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
-
+
+
|
//
// Print heading underline
//
if let Some(ch) = self.header_underline {
fields.clear();
for cw in &col_widths {
let line = iter::repeat(ch).take(*cw).collect::<String>();
//let line = iter::repeat(ch).take(*cw).collect::<String>();
let line = std::iter::repeat_n(ch, *cw).collect::<String>();
fields.push(line);
}
writeln!(f, "{}", fields.join(&colspace))?;
}
}
|
︙ | | |
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
-
+
+
|
s: &str,
width: usize,
trunc_len: usize,
trunc_ch: char
) -> Cow<str> {
if s.len() > width {
let trunc = s[..s.len() - trunc_len - 1].to_string();
let cont = iter::repeat(trunc_ch).take(trunc_len).collect::<String>();
//let cont = iter::repeat(trunc_ch).take(trunc_len).collect::<String>();
let cont = std::iter::repeat_n(trunc_ch, trunc_len).collect::<String>();
let s = format!("{trunc}{cont}");
Cow::from(s)
} else {
Cow::from(s)
}
}
|
︙ | | |