qpprint

Check-in Differences
Login

Check-in Differences

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Difference From qpprint-0.2.1 To qpprint-0.3.0

2025-05-30
11:07
Merge. check-in: be1d708848 user: jan tags: trunk
2025-05-16
22:15
Start work on fixing cell width when using multibyte utf-8 characters. check-in: 764f26c1b7 user: jan tags: unicode
21:08
Release maintenance. check-in: 735e2540e6 user: jan tags: qpprint-0.3.0, trunk
21:05
Update edition & msrv. Happy clippy. check-in: bd0251b533 user: jan tags: trunk
2024-10-21
19:03
Change log. check-in: 61459c149a user: jan tags: trunk
18:59
Up version. check-in: 8a7eb568ae user: jan tags: qpprint-0.2.1, trunk
18:57
Meta. check-in: b20e59d7f8 user: jan tags: trunk

Changes to Cargo.toml.
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
[package]
name = "qpprint"
version = "0.2.1"
authors = ["Jan Danielsson <jan.danielsson@qrnch.com>"]
edition = "2021"
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"
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" }

[lints.clippy]
all = { level = "deny", 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
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.3.0"

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.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]
terminal_size = { version = "0.4.2" }
yansi = { version = "1.0.1" }

[lints.clippy]
all = { level = "warn", priority = -1 }
pedantic = { level = "warn", priority = -1 }
nursery = { level = "warn", priority = -1 }
cargo = { level = "warn", priority = -1 }

multiple_crate_versions = "allow"

Changes to examples/newtbl.rs.
1
2
3
4
5
6
7
8
9
10
use qpprint::tbl::{Align, CellValue, Column, Data, Renderer};

use colored::{Color, ColoredString, Colorize};

fn main() {
  table1();
  println!();
  table2();
  println!();
  table3();


|







1
2
3
4
5
6
7
8
9
10
use qpprint::tbl::{Align, CellValue, Column, Data, Renderer};

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
}


/// 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);
    if let CellValue::U64(id) = cv {
      if *id > 20 {
        cs.color(Color::Blue)
      } else {
        cs
      }
    } else {
      cs
    }
  });
  let namecol = Column::new("Name", ToString::to_string).stylize(|_cv, s| {
    let cs = ColoredString::from(s);
    if s == "world" {
      cs.color(Color::Red)
    } else {
      cs
    }
  });
  let columns = [idcol, namecol];

  let mut data = Data::new(columns.len());







|


|








|

|







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 = Painted::new(s.to_string());
    if let CellValue::U64(id) = cv {
      if *id > 20 {
        cs.fg(Color::Blue)
      } else {
        cs
      }
    } else {
      cs
    }
  });
  let namecol = Column::new("Name", ToString::to_string).stylize(|_cv, s| {
    let cs = Painted::new(s.to_string());
    if s == "world" {
      cs.fg(Color::Red)
    } else {
      cs
    }
  });
  let columns = [idcol, namecol];

  let mut data = Data::new(columns.len());
Changes to src/lib.rs.
1
2
3
4


5
6
7
8
9
10
11
pub mod tbl;

use terminal_size::{terminal_size, Height, Width};



pub enum Types {
  Paragraph(String)
}

pub enum Align {
  Left,
  Center,




>
>







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
    Self {
      indent: 0,
      hang: 0,
      maxwidth
    }
  }

  pub 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 {
    self.hang = hang;
    self
  }

  /*
  pub(crate) fn set_maxwidth(&mut self, maxwidth: u16) -> &mut Self {
    self.maxwidth = maxwidth;







|





|







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 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 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;
Changes to src/tbl.rs.
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};

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>>,
  title_align: Align,
  cell_align: Align
}

impl Column {
  #[allow(clippy::needless_pass_by_value)]
  pub fn new(






|












|







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}
};

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) -> 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
      assert!(max >= min);
    }
    self.max_width = Some(max);
    self
  }

  #[must_use]
  pub 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 {
    self.trunc_len = len;
    self.trunc_ch = ch;
    self
  }

  #[must_use]
  pub fn stylize(
    mut self,
    f: impl Fn(&CellValue, &str) -> ColoredString + 'static
  ) -> Self {
    self.stylize = Some(Box::new(f));
    self
  }

  #[must_use]
  pub 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 {
    self.title_align = align;
    self
  }

  #[must_use]
  pub 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 {
    self.cell_align = align;
    self
  }
}


pub enum CellValue {







|




|








|






|




|





|




|







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 const fn trunc_style(mut self, len: usize, ch: char) -> Self {
    self.trunc_style_ref(len, ch);
    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) -> Painted<String> + 'static
  ) -> Self {
    self.stylize = Some(Box::new(f));
    self
  }

  #[must_use]
  pub const fn title_align(mut self, align: Align) -> Self {
    self.title_align_ref(align);
    self
  }

  pub const fn title_align_ref(&mut self, align: Align) -> &mut Self {
    self.title_align = align;
    self
  }

  #[must_use]
  pub const fn cell_align(mut self, align: Align) -> Self {
    self.cell_align_ref(align);
    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
    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::U64(v) => write!(f, "{v}")
    }
  }
}


pub struct Data {







|







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(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
      col_spacing: 2,
      cols,
      data: &data.cells
    }
  }

  #[must_use]
  pub fn header(mut self, underline: Option<char>) -> Self {
    self.header_ref(underline);
    self
  }

  pub 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 {
    self.column_spacing_ref(n);
    self
  }

  pub fn column_spacing_ref(&mut self, n: usize) -> &mut Self {
    self.col_spacing = n;
    self
  }

  pub fn print(&self) {
    println!("{}", self);
  }
}


impl fmt::Display for Renderer<'_> {
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    //







|




|






|




|





|







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 const fn header(mut self, underline: Option<char>) -> Self {
    self.header_ref(underline);
    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 const fn column_spacing(mut self, n: usize) -> Self {
    self.column_spacing_ref(n);
    self
  }

  pub const fn column_spacing_ref(&mut self, n: usize) -> &mut Self {
    self.col_spacing = n;
    self
  }

  pub fn print(&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

      //
      // 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>();

          fields.push(line);
        }
        writeln!(f, "{}", fields.join(&colspace))?;
      }
    }









|
>







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 = 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
  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 s = format!("{trunc}{cont}");
    Cow::from(s)
  } else {
    Cow::from(s)
  }
}








|
>







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 = std::iter::repeat_n(trunc_ch, trunc_len).collect::<String>();
    let s = format!("{trunc}{cont}");
    Cow::from(s)
  } else {
    Cow::from(s)
  }
}

Changes to www/changelog.md.
1
2
3
4
5
6












7











8
9
10
11
12
13
14
15
16
17
18
# Change Log

⚠️  indicates a breaking change.

## [Unreleased]













[Details](/vdiff?from=qpprint-0.2.0&to=trunk)












### Added

- Add a new table implementation under `tbl::*`.

### Changed

### Removed

---







>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>





<
<
<
<
<
<
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






# Change Log

⚠️  indicates a breaking change.

## [Unreleased]

[Details](/vdiff?from=qpprint-0.3.0&to=trunk)

### Added

### Changed

### Removed

---

## [0.3.0] - 2025-05-16

[Details](/vdiff?from=qpprint-0.2.1&to=qpprint-0.3.0)

### Changed

- ⚠️ Switch from `colored` to `yansi` for coloring.
- Edition 2024 & MSRV `1.85`

---

## [0.2.1] - 2024-10-21

[Details](/vdiff?from=qpprint-0.2.0&to=qpprint-0.2.1)

### Added

- Add a new table implementation under `tbl::*`.