Skip to main content

Crate console_table

Crate console_table 

Source
Expand description

Console table rendering with Unicode/CJK-aware alignment, truncation, and ANSI color support.

§Ambiguous-width characters

Characters like Φ (U+03A6) have Unicode East_Asian_Width=Ambiguous. Terminals render them as 1 or 2 columns depending on locale and terminal settings. unicode_width reports them as 1. To align with the terminal, this crate counts them as 2 when it believes the output target is a CJK context, and 1 otherwise.

Default behavior: ambiguous characters are counted as 2 columns (AmbiguousWidth::Wide). This matches CJK terminals, which are the primary target of this crate. If your terminal renders Φ as 1 column, call [Table::cjk_context(false)] or Table::ambiguous_width to override.

§Example

use console_table::{Align, Style, Table};

let rows = vec![
    vec!["#".to_string(), "source".to_string(), "latency".to_string()],
    vec!["1".to_string(), "rsproxy".to_string(), "57 ms".to_string()],
    vec!["2".to_string(), "tuna".to_string(), "16 ms".to_string()],
];

let table = Table::new(rows)
    .style(Style::Boxed)
    .aligns(vec![Align::Right, Align::Left, Align::Auto]);

println!("{}", table.render());

Structs§

Table
Table configuration.

Enums§

Align
Horizontal alignment of a cell.
AmbiguousWidth
How to treat Unicode East_Asian_Width=Ambiguous characters.
Color
ANSI foreground color.
Style
Border style of the table.

Functions§

display_width
Terminal display width of s, ignoring ANSI escape sequences.
is_numeric_like
Return true if s looks like a number, for Align::Auto.
pad_left_with_spaces
Pad s on the left with spaces up to width display columns.
pad_right_with_spaces
Pad s on the right with spaces up to width display columns.
render_table
Convenience function: render a table directly.
strip_ansi
Strip CSI-style ANSI escape sequences (e.g. \x1b[...m) from s.
truncate_visible
Truncate s to at most max_width display columns.
truncate_visible_with
Truncate s to at most max_width display columns, using aw for ambiguous characters.