Skip to content

Commit d8c775e

Browse files
committed
refactor(terminal): split terminal helpers into modules
Break the ccusage-terminal crate out of the single lib.rs file while keeping the public exports unchanged. Move table rendering, terminal styling, title rendering, width handling, and terminal width discovery into focused private modules so future terminal work has clearer ownership. The snapshot outputs are unchanged; their files move only because the tests now live beside the extracted modules.
1 parent d4b5c3c commit d8c775e

10 files changed

Lines changed: 770 additions & 730 deletions

‎rust/crates/ccusage-terminal/src/lib.rs‎

Lines changed: 10 additions & 726 deletions
Large diffs are not rendered by default.

rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__tests__snapshots_ansi_truncation_boundary.snap renamed to rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__table__tests__snapshots_ansi_truncation_boundary.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
source: crates/ccusage-terminal/src/lib.rs
2+
source: crates/ccusage-terminal/src/table.rs
3+
assertion_line: 506
34
expression: "truncate_visible(\"\\x1b[33mvery-long-value\\x1b[0m\", 8)"
45
---
56
very-lo…

rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__tests__snapshots_full_table_with_multiline_cells_and_separators.snap renamed to rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__table__tests__snapshots_full_table_with_multiline_cells_and_separators.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
source: crates/ccusage-terminal/src/lib.rs
2+
source: crates/ccusage-terminal/src/table.rs
3+
assertion_line: 465
34
expression: "table.render_lines().join(\"\\n\")"
45
---
56
┌─────────────────────────┬──────────────────────────────────┬───────────┬───────────┬─────────────┐

rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__tests__snapshots_narrow_table_with_wrapping_truncation_and_compact_dates.snap renamed to rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__table__tests__snapshots_narrow_table_with_wrapping_truncation_and_compact_dates.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
source: crates/ccusage-terminal/src/lib.rs
2+
source: crates/ccusage-terminal/src/table.rs
3+
assertion_line: 494
34
expression: "table.render_lines().join(\"\\n\")"
45
---
56
┌────────────┬────────────┬──────────┬──────────┬──────────┐

rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__tests__snapshots_multiline_box_title_layout.snap renamed to rust/crates/ccusage-terminal/src/snapshots/ccusage_terminal__title__tests__snapshots_multiline_box_title_layout.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
2-
source: crates/ccusage-terminal/src/lib.rs
2+
source: crates/ccusage-terminal/src/title.rs
3+
assertion_line: 80
34
expression: "lines.join(\"\\n\")"
45
---
56

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::{
2+
env,
3+
io::{self, IsTerminal},
4+
};
5+
6+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7+
pub enum Color {
8+
Blue,
9+
Green,
10+
Grey,
11+
Red,
12+
Yellow,
13+
}
14+
15+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
16+
pub struct TerminalStyle {
17+
pub color: bool,
18+
pub log_level: Option<u8>,
19+
pub no_color: bool,
20+
}
21+
22+
pub fn color(style: impl Into<TerminalStyle>, value: impl AsRef<str>, color: Color) -> String {
23+
let style = style.into();
24+
let value = value.as_ref();
25+
if !use_color(&style) {
26+
return value.to_string();
27+
}
28+
let code = match color {
29+
Color::Blue => 34,
30+
Color::Green => 32,
31+
Color::Grey => 90,
32+
Color::Red => 31,
33+
Color::Yellow => 33,
34+
};
35+
format!("\x1b[{code}m{value}\x1b[0m")
36+
}
37+
38+
fn use_color(style: &TerminalStyle) -> bool {
39+
if style.no_color || env::var_os("NO_COLOR").is_some() {
40+
return false;
41+
}
42+
style.color || env::var_os("FORCE_COLOR").is_some() || io::stdout().is_terminal()
43+
}
44+
45+
#[cfg(test)]
46+
mod tests {
47+
use super::*;
48+
49+
#[test]
50+
fn color_respects_explicit_no_color() {
51+
let style = TerminalStyle {
52+
color: true,
53+
log_level: None,
54+
no_color: true,
55+
};
56+
57+
assert_eq!(color(style, "Total", Color::Yellow), "Total");
58+
}
59+
}

0 commit comments

Comments
 (0)