pmat 3.16.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
39
40
41
42
43
44
45
46
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
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
#![cfg_attr(coverage_nightly, coverage(off))]
//! Shared ANSI color constants and formatting helpers for CLI output.
//!
//! All CLI handlers should use these constants for consistent colorized output.
//! Colors are unconditionally emitted — the `--color` flag is handled at the
//! top-level dispatcher by stripping ANSI sequences when `--color never`.

// ── ANSI escape sequences ───────────────────────────────────────────────────

pub const RESET: &str = "\x1b[0m";
pub const BOLD: &str = "\x1b[1m";
pub const DIM: &str = "\x1b[2m";
pub const ITALIC: &str = "\x1b[3m";
pub const UNDERLINE: &str = "\x1b[4m";

// Foreground colors
pub const RED: &str = "\x1b[31m";
pub const GREEN: &str = "\x1b[32m";
pub const YELLOW: &str = "\x1b[33m";
pub const BLUE: &str = "\x1b[34m";
pub const MAGENTA: &str = "\x1b[35m";
pub const CYAN: &str = "\x1b[36m";
pub const WHITE: &str = "\x1b[37m";

// Bright / bold foreground
pub const BOLD_RED: &str = "\x1b[1;31m";
pub const BOLD_GREEN: &str = "\x1b[1;32m";
pub const BOLD_YELLOW: &str = "\x1b[1;33m";
pub const BOLD_BLUE: &str = "\x1b[1;34m";
pub const BOLD_CYAN: &str = "\x1b[1;36m";
pub const BOLD_WHITE: &str = "\x1b[1;37m";

// Dim foreground
pub const DIM_WHITE: &str = "\x1b[2;37m";
pub const DIM_CYAN: &str = "\x1b[2;36m";

// ── Semantic formatting helpers ─────────────────────────────────────────────

/// Format a section header (bold + underline)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn header(text: &str) -> String {
    format!("{BOLD}{UNDERLINE}{text}{RESET}")
}

/// Format a subheader (bold)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn subheader(text: &str) -> String {
    format!("{BOLD}{text}{RESET}")
}

/// Format a success/pass item
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn pass(text: &str) -> String {
    format!("{GREEN}{RESET} {text}")
}

/// Format a warning item
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn warn(text: &str) -> String {
    format!("{YELLOW}{RESET} {text}")
}

/// Format a failure/error item
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn fail(text: &str) -> String {
    format!("{RED}{RESET} {text}")
}

/// Format a skipped item
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn skip(text: &str) -> String {
    format!("{DIM}{RESET} {DIM}{text}{RESET}")
}

/// Format a dimmed/secondary text
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn dim(text: &str) -> String {
    format!("{DIM}{text}{RESET}")
}

/// Format a file path (cyan, like rg/fd)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn path(text: &str) -> String {
    format!("{CYAN}{text}{RESET}")
}

/// Format a number/score (bold white)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn number(text: &str) -> String {
    format!("{BOLD_WHITE}{text}{RESET}")
}

/// Format a label (bold)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn label(text: &str) -> String {
    format!("{BOLD}{text}{RESET}")
}

/// Format a grade with appropriate color
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn grade(g: &str) -> String {
    let color = match g.chars().next() {
        Some('A') => GREEN,
        Some('B') => YELLOW,
        Some('C') => YELLOW,
        Some('D') => RED,
        Some('F') => BOLD_RED,
        _ => WHITE,
    };
    format!("{color}{g}{RESET}")
}

/// Format a percentage with threshold coloring (higher is better)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn pct(value: f64, good_threshold: f64, warn_threshold: f64) -> String {
    let color = if value >= good_threshold {
        GREEN
    } else if value >= warn_threshold {
        YELLOW
    } else {
        RED
    };
    format!("{color}{value:.1}%{RESET}")
}

/// Format a percentage with inverted threshold coloring (lower is better, e.g. pressure)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn pct_inverse(value: f64, good_threshold: f64, warn_threshold: f64) -> String {
    let color = if value <= good_threshold {
        GREEN
    } else if value <= warn_threshold {
        YELLOW
    } else {
        RED
    };
    format!("{color}{value:.1}%{RESET}")
}

/// Format a delta value (positive = green/improvement, negative = red/regression)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn delta(value: f64) -> String {
    let color = if value > 0.0 {
        GREEN
    } else if value < 0.0 {
        RED
    } else {
        DIM
    };
    format!("{color}{value:+.1}{RESET}")
}

/// Format a score fraction (e.g., "14.5/15.0") with threshold coloring
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "score_range")]
pub fn score(earned: f64, max: f64, good_pct: f64, warn_pct: f64) -> String {
    let percentage = if max > 0.0 { earned / max * 100.0 } else { 0.0 };
    let color = if percentage >= good_pct {
        GREEN
    } else if percentage >= warn_pct {
        YELLOW
    } else {
        RED
    };
    format!("{color}{earned:.1}{RESET}/{DIM}{max:.1}{RESET}")
}

/// Format a horizontal rule
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn rule() -> String {
    format!("{DIM}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━{RESET}")
}

/// Format a section separator (thin)
#[inline]
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub fn separator() -> String {
    format!("{DIM}───────────────────────────────────────────────────{RESET}")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_header_wraps_with_bold_underline_and_reset() {
        let s = header("Title");
        assert!(s.starts_with(BOLD));
        assert!(s.contains(UNDERLINE));
        assert!(s.contains("Title"));
        assert!(s.ends_with(RESET));
    }

    #[test]
    fn test_subheader_wraps_with_bold_and_reset() {
        let s = subheader("Sub");
        assert!(s.starts_with(BOLD));
        assert!(s.contains("Sub"));
        assert!(s.ends_with(RESET));
    }

    #[test]
    fn test_pass_starts_with_green_check() {
        let s = pass("ok");
        assert!(s.starts_with(GREEN));
        assert!(s.contains(''));
        assert!(s.contains("ok"));
    }

    #[test]
    fn test_warn_starts_with_yellow_warning() {
        let s = warn("hmm");
        assert!(s.starts_with(YELLOW));
        assert!(s.contains(''));
        assert!(s.contains("hmm"));
    }

    #[test]
    fn test_fail_starts_with_red_cross() {
        let s = fail("bad");
        assert!(s.starts_with(RED));
        assert!(s.contains(''));
        assert!(s.contains("bad"));
    }

    #[test]
    fn test_skip_uses_dim_double_wrap() {
        let s = skip("later");
        assert!(s.starts_with(DIM));
        assert!(s.contains(''));
        assert!(s.contains("later"));
    }

    #[test]
    fn test_dim_wraps_with_dim_and_reset() {
        let s = dim("note");
        assert_eq!(s, format!("{DIM}note{RESET}"));
    }

    #[test]
    fn test_path_uses_cyan() {
        let s = path("src/lib.rs");
        assert!(s.starts_with(CYAN));
        assert!(s.contains("src/lib.rs"));
        assert!(s.ends_with(RESET));
    }

    #[test]
    fn test_number_uses_bold_white() {
        let s = number("42");
        assert!(s.starts_with(BOLD_WHITE));
        assert!(s.contains("42"));
    }

    #[test]
    fn test_label_uses_bold() {
        let s = label("Name:");
        assert!(s.starts_with(BOLD));
        assert!(s.contains("Name:"));
    }

    // ── grade arms ──────────────────────────────────────────────────────────

    #[test]
    fn test_grade_a_is_green() {
        let s = grade("A+");
        assert!(s.starts_with(GREEN));
        assert!(s.contains("A+"));
    }

    #[test]
    fn test_grade_b_is_yellow() {
        let s = grade("B");
        assert!(s.starts_with(YELLOW));
        assert!(s.contains('B'));
    }

    #[test]
    fn test_grade_c_is_yellow() {
        let s = grade("C-");
        assert!(s.starts_with(YELLOW));
        assert!(s.contains("C-"));
    }

    #[test]
    fn test_grade_d_is_red() {
        let s = grade("D+");
        assert!(s.starts_with(RED));
        assert!(s.contains("D+"));
    }

    #[test]
    fn test_grade_f_is_bold_red() {
        let s = grade("F");
        assert!(s.starts_with(BOLD_RED));
        assert!(s.contains('F'));
    }

    #[test]
    fn test_grade_other_is_white() {
        // Non-letter prefix and empty fall through to WHITE.
        let s_q = grade("?");
        assert!(s_q.starts_with(WHITE));
        let s_empty = grade("");
        assert!(s_empty.starts_with(WHITE));
    }

    // ── pct (higher is better) ──────────────────────────────────────────────

    #[test]
    fn test_pct_above_good_is_green() {
        let s = pct(95.0, 90.0, 70.0);
        assert!(s.starts_with(GREEN));
        assert!(s.contains("95.0%"));
    }

    #[test]
    fn test_pct_at_good_threshold_is_green() {
        let s = pct(90.0, 90.0, 70.0);
        assert!(s.starts_with(GREEN));
    }

    #[test]
    fn test_pct_between_thresholds_is_yellow() {
        let s = pct(80.0, 90.0, 70.0);
        assert!(s.starts_with(YELLOW));
        assert!(s.contains("80.0%"));
    }

    #[test]
    fn test_pct_below_warn_is_red() {
        let s = pct(50.0, 90.0, 70.0);
        assert!(s.starts_with(RED));
        assert!(s.contains("50.0%"));
    }

    // ── pct_inverse (lower is better) ───────────────────────────────────────

    #[test]
    fn test_pct_inverse_below_good_is_green() {
        let s = pct_inverse(5.0, 10.0, 30.0);
        assert!(s.starts_with(GREEN));
        assert!(s.contains("5.0%"));
    }

    #[test]
    fn test_pct_inverse_between_thresholds_is_yellow() {
        let s = pct_inverse(20.0, 10.0, 30.0);
        assert!(s.starts_with(YELLOW));
    }

    #[test]
    fn test_pct_inverse_above_warn_is_red() {
        let s = pct_inverse(50.0, 10.0, 30.0);
        assert!(s.starts_with(RED));
        assert!(s.contains("50.0%"));
    }

    // ── delta (signed) ──────────────────────────────────────────────────────

    #[test]
    fn test_delta_positive_is_green() {
        let s = delta(2.5);
        assert!(s.starts_with(GREEN));
        assert!(s.contains("+2.5"));
    }

    #[test]
    fn test_delta_negative_is_red() {
        let s = delta(-3.0);
        assert!(s.starts_with(RED));
        assert!(s.contains("-3.0"));
    }

    #[test]
    fn test_delta_zero_is_dim() {
        let s = delta(0.0);
        assert!(s.starts_with(DIM));
        // sign prefix on zero is "+" per `:+.1`
        assert!(s.contains("+0.0"));
    }

    // ── score ───────────────────────────────────────────────────────────────

    #[test]
    fn test_score_above_good_pct_is_green() {
        let s = score(14.0, 15.0, 80.0, 60.0); // 93.3%
        assert!(s.starts_with(GREEN));
        assert!(s.contains("14.0"));
        assert!(s.contains("15.0"));
    }

    #[test]
    fn test_score_between_pct_thresholds_is_yellow() {
        let s = score(10.5, 15.0, 80.0, 60.0); // 70%
        assert!(s.starts_with(YELLOW));
    }

    #[test]
    fn test_score_below_warn_pct_is_red() {
        let s = score(6.0, 15.0, 80.0, 60.0); // 40%
        assert!(s.starts_with(RED));
    }

    #[test]
    fn test_score_max_zero_treats_as_red_zero_pct() {
        // max == 0 short-circuits to 0% which is below any positive threshold → RED.
        let s = score(0.0, 0.0, 80.0, 60.0);
        assert!(s.starts_with(RED));
        assert!(s.contains("0.0"));
    }

    // ── rule / separator ────────────────────────────────────────────────────

    #[test]
    fn test_rule_returns_dim_horizontal_line() {
        let s = rule();
        assert!(s.starts_with(DIM));
        assert!(s.contains(''));
        assert!(s.ends_with(RESET));
    }

    #[test]
    fn test_separator_returns_dim_thin_line() {
        let s = separator();
        assert!(s.starts_with(DIM));
        assert!(s.contains(''));
        assert!(s.ends_with(RESET));
    }
}