xarp 0.1.2

A zero-dependency, pure-Rust argument parser with rich 24-color and RGB terminal styling.
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
use crate::color::Color;
use crate::effect::Effects;
use core::fmt::{self, Display, Formatter};
use core::ops::BitOr;

/// Represents a combined text style (Effects + Foreground + Background).
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Style {
    /// Foreground color.
    pub fg: Option<Color>,
    /// Background color.
    pub bg: Option<Color>,
    /// Set of visual text effects.
    pub effects: Effects,
}

impl Style {
    /// Creates a blank/unstyled style.
    #[must_use]
    #[inline]
    pub const fn new() -> Self {
        Self {
            fg: None,
            bg: None,
            effects: Effects::NONE,
        }
    }

    /// Returns `true` if no color or text effects are applied.
    #[must_use]
    #[inline]
    pub const fn is_plain(&self) -> bool {
        self.fg.is_none() && self.bg.is_none() && self.effects.is_empty()
    }

    // --- Fluent Builders ---

    /// Sets the foreground color.
    #[must_use]
    #[inline]
    pub const fn fg(mut self, color: Color) -> Self {
        self.fg = Some(color);
        self
    }

    /// Sets the background color.
    #[must_use]
    #[inline]
    pub const fn bg(mut self, color: Color) -> Self {
        self.bg = Some(color);
        self
    }

    /// Adds text effects to the current style.
    #[must_use]
    #[inline]
    pub const fn effects(mut self, effects: Effects) -> Self {
        self.effects = self.effects.insert(effects);
        self
    }

    /// Applies bold styling.
    #[must_use]
    #[inline]
    pub const fn bold(self) -> Self {
        self.effects(Effects::BOLD)
    }

    /// Applies dim (faint) styling.
    #[must_use]
    #[inline]
    pub const fn dim(self) -> Self {
        self.effects(Effects::DIM)
    }

    /// Applies italic styling.
    #[must_use]
    #[inline]
    pub const fn italic(self) -> Self {
        self.effects(Effects::ITALIC)
    }

    /// Applies single underline styling.
    #[must_use]
    #[inline]
    pub const fn underline(self) -> Self {
        self.effects(Effects::UNDERLINE)
    }

    /// Applies slow blink styling.
    #[must_use]
    #[inline]
    pub const fn blink(self) -> Self {
        self.effects(Effects::BLINK)
    }

    /// Inverts foreground and background colors.
    #[must_use]
    #[inline]
    pub const fn invert(self) -> Self {
        self.effects(Effects::INVERT)
    }

    /// Hides the text.
    #[must_use]
    #[inline]
    pub const fn hidden(self) -> Self {
        self.effects(Effects::HIDDEN)
    }

    /// Applies strikethrough (crossed-out) styling.
    #[must_use]
    #[inline]
    pub const fn strikethrough(self) -> Self {
        self.effects(Effects::STRIKETHROUGH)
    }

    /// Applies double underline styling.
    #[must_use]
    #[inline]
    pub const fn double_underline(self) -> Self {
        self.effects(Effects::DOUBLE_UNDERLINE)
    }

    /// Wraps any `Display` value in a struct that automatically applies this style
    /// and resets formatting afterwards.
    ///
    /// Takes `self` by value (`Style` is `Copy`), so temporaries can be
    /// painted directly: `Style::new().bold().paint("hi")`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use xarp::style::Style;
    /// use xarp::color::Color;
    ///
    /// let styled = Style::new().bold().fg(Color::Red).paint("alert");
    /// assert!(styled.to_string().contains("alert"));
    /// ```
    #[must_use]
    #[inline]
    pub fn paint<T: Display + ?Sized>(self, target: &T) -> Styled<'_, T> {
        Styled {
            style: self,
            target,
        }
    }
}

// Ergonomic Operator Overloading
impl BitOr<Effects> for Style {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Effects) -> Self {
        self.effects(rhs)
    }
}

impl BitOr<Color> for Style {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Color) -> Self {
        self.fg(rhs)
    }
}

impl BitOr<Effects> for Color {
    type Output = Style;
    #[inline]
    fn bitor(self, rhs: Effects) -> Style {
        Style::new().fg(self).effects(rhs)
    }
}

impl BitOr<Color> for Effects {
    type Output = Style;
    #[inline]
    fn bitor(self, rhs: Color) -> Style {
        Style::new().fg(rhs).effects(self)
    }
}

/// Merges two styles: the right-hand side `fg`/`bg` win when set, and effects
/// are combined with a union.
///
/// # Example
///
/// ```rust
/// use xarp::style::Style;
/// use xarp::color::Color;
/// use xarp::effect::Effects;
///
/// let merged = Style::new().fg(Color::Red) | Style::new().bold();
/// assert_eq!(merged.fg, Some(Color::Red));
/// assert!(merged.effects.contains(Effects::BOLD));
/// ```
impl BitOr<Style> for Style {
    type Output = Self;
    #[inline]
    fn bitor(self, rhs: Self) -> Self {
        Self {
            fg: rhs.fg.or(self.fg),
            bg: rhs.bg.or(self.bg),
            effects: self.effects | rhs.effects,
        }
    }
}

/// Combines effects with a style: effects are unioned, the style's
/// foreground and background colors are kept.
impl BitOr<Style> for Effects {
    type Output = Style;
    #[inline]
    fn bitor(self, rhs: Style) -> Style {
        Style::new().effects(self) | rhs
    }
}

/// Combines a color with a style: the style's foreground wins when set,
/// otherwise the color becomes the foreground; backgrounds and effects merge.
impl BitOr<Style> for Color {
    type Output = Style;
    #[inline]
    fn bitor(self, rhs: Style) -> Style {
        Style::new().fg(self) | rhs
    }
}

// === ANSI Output Formatter ===

impl Display for Style {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        // Alternate format `{style:#}` renders the ANSI Reset code
        if f.alternate() {
            return write!(f, "\x1b[0m");
        }

        if self.is_plain() {
            return Ok(());
        }

        write!(f, "\x1b[")?;
        let mut first = true;

        let mut write_effect = |f: &mut Formatter<'_>, code: u8| -> fmt::Result {
            if !first {
                write!(f, ";")?;
            }
            first = false;
            write!(f, "{code}")
        };

        if self.effects.contains(Effects::BOLD) {
            write_effect(f, 1)?;
        }
        if self.effects.contains(Effects::DIM) {
            write_effect(f, 2)?;
        }
        if self.effects.contains(Effects::ITALIC) {
            write_effect(f, 3)?;
        }
        if self.effects.contains(Effects::UNDERLINE) {
            write_effect(f, 4)?;
        }
        if self.effects.contains(Effects::BLINK) {
            write_effect(f, 5)?;
        }
        if self.effects.contains(Effects::RAPID_BLINK) {
            write_effect(f, 6)?;
        }
        if self.effects.contains(Effects::INVERT) {
            write_effect(f, 7)?;
        }
        if self.effects.contains(Effects::HIDDEN) {
            write_effect(f, 8)?;
        }
        if self.effects.contains(Effects::STRIKETHROUGH) {
            write_effect(f, 9)?;
        }
        if self.effects.contains(Effects::DOUBLE_UNDERLINE) {
            write_effect(f, 21)?;
        }

        if let Some(fg) = self.fg {
            if !first {
                write!(f, ";")?;
            }
            first = false;
            fg.write_ansi(f, false)?;
        }

        if let Some(bg) = self.bg {
            if !first {
                write!(f, ";")?;
            }
            bg.write_ansi(f, true)?;
        }

        write!(f, "m")
    }
}

/// Helper struct returned by `Style::paint`.
///
/// Owns a copy of the style (`Style` is `Copy`), so painted values from
/// temporaries can be bound to variables.
pub struct Styled<'a, T: ?Sized> {
    style: Style,
    target: &'a T,
}

impl<T: Display + ?Sized> Display for Styled<'_, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        if self.style.is_plain() {
            self.target.fmt(f)
        } else {
            write!(f, "{}{}{:#}", self.style, self.target, self.style)
        }
    }
}

// === CLI Styles / Theme Definition (Clap Alternative Core) ===

/// Styling configuration for CLI argument parsers (Help messages, Usage, Errors).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Styles {
    /// Style applied to section headers.
    pub header: Style,
    /// Style applied to usage instructions.
    pub usage: Style,
    /// Style applied to command literals and flags.
    pub literal: Style,
    /// Style applied to argument value placeholders.
    pub placeholder: Style,
    /// Style applied to error messages.
    pub error: Style,
    /// Style applied to valid values or states.
    pub valid: Style,
    /// Style applied to invalid values or warnings.
    pub invalid: Style,
    /// Style applied to warning messages.
    pub warning: Style,
    /// Style applied to secondary or dimmed text.
    pub muted: Style,
}

impl Styles {
    /// Returns uncolored / plain styles.
    #[must_use]
    pub const fn plain() -> Self {
        Self {
            header: Style::new(),
            usage: Style::new(),
            literal: Style::new(),
            placeholder: Style::new(),
            error: Style::new(),
            valid: Style::new(),
            invalid: Style::new(),
            warning: Style::new(),
            muted: Style::new(),
        }
    }

    /// Default styled CLI theme (Cargo / Clap v4 styled look).
    ///
    /// Automatically returns plain styles when the `NO_COLOR` environment
    /// variable is present, including when it is set to an empty string
    /// (any presence disables color, per <https://no-color.org>).
    #[must_use]
    pub fn styled() -> Self {
        if std::env::var_os("NO_COLOR").is_some() {
            return Self::plain();
        }

        Self {
            header: Style::new().bold().underline(),
            usage: Style::new().bold().underline(),
            literal: Style::new().bold().fg(Color::BrightCyan),
            placeholder: Style::new().fg(Color::Cyan),
            error: Style::new().bold().fg(Color::BrightRed),
            valid: Style::new().bold().fg(Color::BrightGreen),
            invalid: Style::new().bold().fg(Color::BrightYellow),
            warning: Style::new().bold().fg(Color::Yellow),
            muted: Style::new().dim(),
        }
    }

    /// Sets the header style.
    #[must_use]
    pub const fn header(mut self, style: Style) -> Self {
        self.header = style;
        self
    }

    /// Sets the usage style.
    #[must_use]
    pub const fn usage(mut self, style: Style) -> Self {
        self.usage = style;
        self
    }

    /// Sets the literal style.
    #[must_use]
    pub const fn literal(mut self, style: Style) -> Self {
        self.literal = style;
        self
    }

    /// Sets the placeholder style.
    #[must_use]
    pub const fn placeholder(mut self, style: Style) -> Self {
        self.placeholder = style;
        self
    }

    /// Sets the error style.
    #[must_use]
    pub const fn error(mut self, style: Style) -> Self {
        self.error = style;
        self
    }

    /// Sets the valid style.
    #[must_use]
    pub const fn valid(mut self, style: Style) -> Self {
        self.valid = style;
        self
    }

    /// Sets the invalid style.
    #[must_use]
    pub const fn invalid(mut self, style: Style) -> Self {
        self.invalid = style;
        self
    }

    /// Sets the warning style.
    #[must_use]
    pub const fn warning(mut self, style: Style) -> Self {
        self.warning = style;
        self
    }

    /// Sets the muted style.
    #[must_use]
    pub const fn muted(mut self, style: Style) -> Self {
        self.muted = style;
        self
    }
}

impl Default for Styles {
    fn default() -> Self {
        Self::styled()
    }
}

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

    #[test]
    fn paint_temporary_can_be_bound() {
        let painted = Style::new().bold().paint("hi");
        assert!(painted.to_string().contains("hi"));
    }

    #[test]
    fn style_merge_prefers_right_colors_and_unions_effects() {
        let merged = Style::new().fg(Color::Red) | Style::new().bold();
        assert_eq!(merged.fg, Some(Color::Red));
        assert!(merged.effects.contains(Effects::BOLD));

        let overridden = Style::new().fg(Color::Red) | Style::new().fg(Color::Blue);
        assert_eq!(overridden.fg, Some(Color::Blue));
    }

    #[test]
    fn effects_and_colors_merge_with_styles() {
        let from_effects = Effects::BOLD | Style::new().fg(Color::Red);
        assert_eq!(from_effects.fg, Some(Color::Red));
        assert!(from_effects.effects.contains(Effects::BOLD));

        let from_color = Color::Red | Style::new().bold();
        assert_eq!(from_color.fg, Some(Color::Red));
        assert!(from_color.effects.contains(Effects::BOLD));
    }

    #[test]
    fn builders_set_each_effect() {
        assert!(Style::new().bold().effects.contains(Effects::BOLD));
        assert!(Style::new().dim().effects.contains(Effects::DIM));
        assert!(Style::new().italic().effects.contains(Effects::ITALIC));
        assert!(
            Style::new()
                .underline()
                .effects
                .contains(Effects::UNDERLINE)
        );
        assert!(Style::new().blink().effects.contains(Effects::BLINK));
        assert!(Style::new().invert().effects.contains(Effects::INVERT));
        assert!(Style::new().hidden().effects.contains(Effects::HIDDEN));
        assert!(
            Style::new()
                .strikethrough()
                .effects
                .contains(Effects::STRIKETHROUGH)
        );
        assert!(
            Style::new()
                .double_underline()
                .effects
                .contains(Effects::DOUBLE_UNDERLINE)
        );
    }

    #[test]
    fn fg_bg_and_effects_union() {
        let style = Style::new()
            .fg(Color::Red)
            .bg(Color::Blue)
            .effects(Effects::BOLD | Effects::ITALIC);
        assert_eq!(style.fg, Some(Color::Red));
        assert_eq!(style.bg, Some(Color::Blue));
        assert!(style.effects.contains(Effects::BOLD));
        assert!(style.effects.contains(Effects::ITALIC));
        assert!(!style.is_plain());
        assert!(Style::new().is_plain());
    }

    #[test]
    fn display_bold_dim_italic_codes() {
        assert_eq!(Style::new().bold().to_string(), "\x1b[1m");
        assert_eq!(Style::new().dim().to_string(), "\x1b[2m");
        assert_eq!(Style::new().italic().to_string(), "\x1b[3m");
    }

    #[test]
    fn display_underline_blink_rapid_codes() {
        assert_eq!(Style::new().underline().to_string(), "\x1b[4m");
        assert_eq!(Style::new().blink().to_string(), "\x1b[5m");
        assert_eq!(
            Style::new().effects(Effects::RAPID_BLINK).to_string(),
            "\x1b[6m"
        );
    }

    #[test]
    fn display_invert_hidden_strike_double_codes() {
        assert_eq!(Style::new().invert().to_string(), "\x1b[7m");
        assert_eq!(Style::new().hidden().to_string(), "\x1b[8m");
        assert_eq!(Style::new().strikethrough().to_string(), "\x1b[9m");
        assert_eq!(Style::new().double_underline().to_string(), "\x1b[21m");
    }

    #[test]
    fn display_standard_foreground_codes() {
        let cases = [
            (Color::Black, "30"),
            (Color::Red, "31"),
            (Color::Green, "32"),
            (Color::Yellow, "33"),
            (Color::Blue, "34"),
            (Color::Magenta, "35"),
            (Color::Cyan, "36"),
            (Color::White, "37"),
        ];
        for (color, code) in cases {
            assert_eq!(Style::new().fg(color).to_string(), format!("\x1b[{code}m"));
        }
    }

    #[test]
    fn display_bright_foreground_codes() {
        let cases = [
            (Color::BrightBlack, "90"),
            (Color::BrightRed, "91"),
            (Color::BrightGreen, "92"),
            (Color::BrightYellow, "93"),
            (Color::BrightBlue, "94"),
            (Color::BrightMagenta, "95"),
            (Color::BrightCyan, "96"),
            (Color::BrightWhite, "97"),
        ];
        for (color, code) in cases {
            assert_eq!(Style::new().fg(color).to_string(), format!("\x1b[{code}m"));
        }
    }

    #[test]
    fn display_extended_palette_codes() {
        let cases = [
            (Color::Orange, "208"),
            (Color::Purple, "129"),
            (Color::Pink, "205"),
            (Color::Teal, "30"),
            (Color::Gold, "220"),
            (Color::Silver, "248"),
            (Color::Lime, "118"),
            (Color::Indigo, "54"),
        ];
        for (color, code) in cases {
            assert_eq!(
                Style::new().fg(color).to_string(),
                format!("\x1b[38;5;{code}m")
            );
        }
    }

    #[test]
    fn display_ansi256_and_rgb_codes() {
        assert_eq!(
            Style::new().fg(Color::Ansi256(141)).to_string(),
            "\x1b[38;5;141m"
        );
        assert_eq!(
            Style::new().fg(Color::Rgb(255, 110, 64)).to_string(),
            "\x1b[38;2;255;110;64m"
        );
    }

    #[test]
    fn display_background_codes() {
        assert_eq!(Style::new().bg(Color::Red).to_string(), "\x1b[41m");
        assert_eq!(Style::new().bg(Color::BrightCyan).to_string(), "\x1b[106m");
        assert_eq!(
            Style::new().bg(Color::Ansi256(7)).to_string(),
            "\x1b[48;5;7m"
        );
        assert_eq!(
            Style::new().bg(Color::Rgb(1, 2, 3)).to_string(),
            "\x1b[48;2;1;2;3m"
        );
    }

    #[test]
    fn display_combines_effects_and_colors() {
        assert_eq!(Style::new().bold().fg(Color::Red).to_string(), "\x1b[1;31m");
        assert_eq!(
            Style::new().fg(Color::Green).bg(Color::Black).to_string(),
            "\x1b[32;40m"
        );
    }

    #[test]
    fn alternate_format_renders_reset() {
        assert_eq!(format!("{:#}", Style::new().bold()), "\x1b[0m");
    }

    #[test]
    fn plain_style_displays_empty_and_paints_through() {
        assert_eq!(Style::new().to_string(), String::new());
        assert_eq!(Style::new().paint("text").to_string(), "text");
    }

    #[test]
    fn painted_output_wraps_with_reset() {
        let out = Style::new().bold().paint("hi").to_string();
        assert!(out.starts_with("\x1b[1m"));
        assert!(out.ends_with("\x1b[0m"));
        assert!(out.contains("hi"));
    }

    #[test]
    fn preset_constants_carry_codes() {
        assert!(crate::color::RED.to_string().contains("\x1b[31m"));
        assert!(crate::color::GREEN.paint("ok").to_string().contains("ok"));
        assert!(crate::color::BOLD.to_string().contains("\x1b[1m"));
    }

    #[test]
    fn plain_theme_has_no_styled_slots() {
        let plain = Styles::plain();
        for slot in [
            plain.header,
            plain.usage,
            plain.literal,
            plain.placeholder,
            plain.error,
            plain.valid,
            plain.invalid,
            plain.warning,
            plain.muted,
        ] {
            assert!(slot.is_plain());
        }
    }

    #[test]
    fn theme_setters_replace_slots() {
        let custom = Styles::plain()
            .header(Style::new().bold())
            .literal(Style::new().fg(Color::Red))
            .muted(Style::new().dim());
        assert!(custom.header.effects.contains(Effects::BOLD));
        assert_eq!(custom.literal.fg, Some(Color::Red));
        assert!(custom.muted.effects.contains(Effects::DIM));
        assert!(custom.usage.is_plain());
    }
}