xbp 10.32.0

XBP is a zero-config build pack that can also interact with proxies, kafka, sockets, synthetic monitors.
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
//! Styled help rendering for Clap-generated usage text.

use clap::CommandFactory;
use colored::Colorize;

/// Shared help layout for subcommands (no duplicate name/version header).
pub const XBP_HELP_TEMPLATE: &str = "\
{about-with-newline}\
Usage: {usage}\n\n\
{all-args}\
{after-help}";

/// Root help layout; the renderer adds a branded banner above Clap output.
pub const XBP_ROOT_HELP_TEMPLATE: &str = "\
{about-with-newline}\
Usage: {usage}\n\n\
{all-args}\
{after-help}";

pub const XBP_ROOT_AFTER_HELP: &str = "\
Quick start:
  xbp diag
  xbp services
  xbp workers list
  xbp workers logs -f
  xbp api health

Discover:
  xbp <command> -h          Command help and examples
  xbp <command> <sub> -h    Subcommand help
  xbp --commands            Full alphabetical command tree
  xbp install               Browse installable targets";

pub const CONFIG_AFTER_HELP: &str = "\
Examples:
  xbp config
  xbp config --project
  xbp config cloudflare
  xbp config cloudflare status
  xbp config openrouter set-key
  xbp config linear select-initiative";

pub const VERSION_AFTER_HELP: &str = "\
Examples:
  xbp version
  xbp version patch
  xbp version 1.2.3
  xbp version release
  xbp version workspace check
  xbp version workspace sync --version 3.16.5 --write";

pub const DIAG_AFTER_HELP: &str = "\
Examples:
  xbp diag
  xbp diag --nginx
  xbp diag --ports 80,443
  xbp diag --codetime --cursor";

pub const NGINX_AFTER_HELP: &str = "\
Examples:
  xbp nginx list
  xbp nginx enable api.example.com
  xbp nginx disable api.example.com
  xbp nginx upstream list";

pub const COMMIT_AFTER_HELP: &str = "\
Examples:
  xbp commit
  xbp commit --dry-run
  xbp commit --push
  xbp commit --scope cli";

pub const LOGS_AFTER_HELP: &str = "\
Examples:
  xbp logs
  xbp logs my-project
  xbp logs --ssh-host bastion.example.com";

pub const PUBLISH_AFTER_HELP: &str = "\
Examples:
  xbp publish --dry-run
  xbp publish --target npm
  xbp publish --allow-dirty";

pub const DOMAINS_AFTER_HELP: &str = "\
Examples:
  xbp domains list
  xbp domains check --domain example.com
  xbp domains search --query myapp --extension com";

pub const LOGIN_AFTER_HELP: &str = "\
Examples:
  xbp login
  xbp login status
  xbp login logout";

pub const SSH_AFTER_HELP: &str = "\
Examples:
  xbp ssh
  xbp ssh --host bastion.example.com
  xbp ssh --host 10.0.0.5 --command \"uptime\"";

pub const GENERATE_AFTER_HELP: &str = "\
Examples:
  xbp generate config
  xbp generate config --force
  xbp generate systemd";

pub const DONE_AFTER_HELP: &str = "\
Examples:
  xbp done
  xbp done --since \"7 days ago\"
  xbp done --output report.md";

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HelpScope {
    Root,
    Subcommand,
    Catalog,
    Auto,
}

pub fn emit_styled_help(raw: &str, scope: HelpScope) {
    crate::cli::ui::configure_color_output();
    let resolved_scope = match scope {
        HelpScope::Auto => detect_help_scope(raw),
        other => other,
    };

    let mut skip_about_line = None::<String>;
    let mut skip_until_usage = resolved_scope == HelpScope::Catalog;
    if resolved_scope == HelpScope::Root {
        print_root_banner(raw);
    } else if resolved_scope != HelpScope::Catalog {
        if let Some((_, tagline)) = parse_subcommand_heading(raw) {
            skip_about_line = Some(tagline);
            print_subcommand_banner(raw);
        }
    }

    let lines: Vec<&str> = raw.lines().collect();
    let mut index = 0usize;
    while index < lines.len() {
        let line = lines[index];
        let trimmed = line.trim();
        if skip_until_usage {
            if trimmed.starts_with("Usage:") {
                skip_until_usage = false;
            } else {
                index += 1;
                continue;
            }
        }
        if let Some(about) = skip_about_line.as_deref() {
            if trimmed == about {
                skip_about_line = None;
                index += 1;
                continue;
            }
        }

        if is_command_name_line(line)
            && index + 1 < lines.len()
            && is_indented_help_text(lines[index + 1], 4)
        {
            println!("{}", style_command_pair(line, lines[index + 1]));
            index += 2;
            continue;
        }

        if is_option_flags_line(line)
            && index + 1 < lines.len()
            && is_indented_help_text(lines[index + 1], 4)
        {
            println!("{}", style_option_pair(line, lines[index + 1]));
            index += 2;
            continue;
        }

        if is_option_entry(line)
            && index + 1 < lines.len()
            && is_indented_help_text(lines[index + 1], 8)
        {
            println!("{}", style_option_pair(line, lines[index + 1]));
            index += 2;
            continue;
        }

        println!("{}", style_help_line(line));
        index += 1;
    }
    println!();
}

/// Print the complete Clap command tree with styled formatting.
pub fn print_command_catalog() {
    crate::cli::ui::configure_color_output();
    println!();
    println!("{}", "xbp commands".bright_magenta().bold());
    println!("{}", "Complete command reference".bright_black());
    crate::cli::ui::divider(32);

    let help = crate::cli::commands::Cli::command()
        .render_long_help()
        .to_string();
    emit_styled_help(&help, HelpScope::Catalog);
}

pub fn emit_version_line(version: &str) {
    crate::cli::ui::configure_color_output();
    println!(
        "{} {}",
        "xbp".bright_magenta().bold(),
        version.bright_white().bold()
    );
}

pub fn is_root_help_text(raw: &str) -> bool {
    raw.lines().any(|line| {
        let trimmed = line.trim();
        trimmed == "Usage: xbp [OPTIONS] [COMMAND]"
            || trimmed == "Usage: xbp.exe [OPTIONS] [COMMAND]"
            || trimmed.starts_with("Usage: xbp [OPTIONS] [COMMAND]")
            || trimmed.starts_with("Usage: xbp.exe [OPTIONS] [COMMAND]")
    })
}

fn detect_help_scope(raw: &str) -> HelpScope {
    if is_root_help_text(raw) {
        return HelpScope::Root;
    }
    let first_line = raw.lines().next().unwrap_or_default().trim();
    if first_line.starts_with("xbp ") && first_line.chars().any(|ch| ch.is_ascii_digit()) {
        return HelpScope::Root;
    }
    HelpScope::Subcommand
}

fn print_root_banner(raw: &str) {
    let version = parse_version_line(raw).unwrap_or(env!("CARGO_PKG_VERSION"));
    println!();
    println!(
        "{}  {}",
        "XBP".bright_magenta().bold(),
        format!("v{version}").bright_white()
    );
    println!("{}", "Deploy · operate · debug · ship".bright_black());
    crate::cli::ui::divider(44);
}

fn print_subcommand_banner(raw: &str) {
    let Some((command_path, tagline)) = parse_subcommand_heading(raw) else {
        return;
    };
    println!();
    println!("{}", command_path.bright_magenta().bold());
    if !tagline.is_empty() {
        println!("{}", tagline.bright_black());
    }
    crate::cli::ui::divider(command_path.len().max(28));
}

fn parse_version_line(raw: &str) -> Option<&str> {
    let first = raw.lines().next()?.trim();
    let rest = first.strip_prefix("xbp")?.trim();
    if rest.is_empty() {
        None
    } else {
        Some(rest)
    }
}

fn parse_subcommand_heading(raw: &str) -> Option<(String, String)> {
    let about = raw
        .lines()
        .map(str::trim)
        .find(|line| !line.is_empty() && !line.starts_with("Usage:"))?;

    let usage = raw
        .lines()
        .map(str::trim)
        .find(|line| line.starts_with("Usage:"))?;
    let command_path = extract_command_path_from_usage(usage)?;

    Some((command_path, about.to_string()))
}

fn extract_command_path_from_usage(usage: &str) -> Option<String> {
    let rest = usage.split_once(':')?.1.trim();
    let path = rest.split('[').next()?.trim();
    let normalized = path.replace(".exe", "");
    if normalized.is_empty() {
        None
    } else {
        Some(normalized)
    }
}

fn style_help_line(line: &str) -> String {
    let trimmed = line.trim_start();
    if trimmed.is_empty() {
        return String::new();
    }

    if matches!(
        trimmed,
        "Commands:" | "Options:" | "Arguments:" | "Subcommands:"
    ) {
        return format!(
            "\n{} {}",
            "â–¸".bright_blue().bold(),
            trimmed.bright_blue().bold()
        );
    }

    if trimmed.starts_with("Usage:") {
        return style_usage_line(line);
    }

    if trimmed == "Discover:" {
        return format!(
            "\n{} {}",
            "â—‡".bright_green().bold(),
            trimmed.bright_green().bold()
        );
    }

    if is_example_section_header(trimmed) {
        return format!(
            "\n{} {}",
            "â—‡".bright_green().bold(),
            trimmed.bright_green().bold()
        );
    }

    if is_note_section_header(trimmed) {
        return format!(
            "\n{} {}",
            "â—‡".bright_yellow().bold(),
            trimmed.bright_yellow().bold()
        );
    }

    if is_command_entry(line) {
        return style_command_entry(line);
    }

    if is_option_flags_line(line) {
        return format!("  {}", line.trim().bright_yellow());
    }

    if is_option_entry(line) {
        return style_option_entry(line);
    }

    if is_command_name_line(line) {
        return format!("  {}", line.trim().bright_cyan().bold());
    }

    if is_indented_help_text(line, 4) || is_indented_help_text(line, 8) {
        return format!("      {}", trimmed.bright_black());
    }

    if is_example_command_line(trimmed) {
        return format!("  {}", highlight_inline_flags(trimmed).dimmed());
    }

    if trimmed.starts_with("Run `") || trimmed.starts_with("Use `") || trimmed.starts_with("Pass `")
    {
        return trimmed.bright_black().to_string();
    }

    line.to_string()
}

fn style_usage_line(line: &str) -> String {
    let (prefix, rest) = line.split_once(':').unwrap_or((line, ""));
    format!(
        "{} {}",
        format!("{prefix}:").bright_cyan().bold(),
        highlight_inline_flags(rest.trim()).bright_white()
    )
}

fn is_example_section_header(line: &str) -> bool {
    matches!(
        line,
        "Examples:"
            | "Quick start:"
            | "Discover:"
            | "Available targets:"
            | "List installable targets:"
    ) || line.starts_with("Quick start")
        || line.starts_with("List installable")
}

fn is_note_section_header(line: &str) -> bool {
    line == "Notes:"
        || line.starts_with("Tip:")
        || line.starts_with("More info:")
        || line.starts_with("Hint:")
}

fn is_command_entry(line: &str) -> bool {
    if !line.starts_with("  ") || line.starts_with("    ") {
        return false;
    }
    let trimmed = line.trim_start();
    let Some((name, _)) = trimmed.split_once("  ") else {
        return false;
    };
    !name.starts_with('-') && !name.contains('<') && name.len() <= 24
}

fn style_command_entry(line: &str) -> String {
    let trimmed = line.trim_start();
    let mut parts = trimmed.splitn(2, "  ");
    let name = parts.next().unwrap_or_default();
    let description = parts.next().unwrap_or_default().trim();
    let alias = extract_alias_suffix(description);
    let base_description = description
        .split_once('[')
        .map(|(left, _)| left.trim())
        .unwrap_or(description);

    let name = name.bright_cyan().bold();
    if alias.is_empty() {
        format!("  {name:<22} {}", base_description.bright_black())
    } else {
        format!(
            "  {name:<22} {} {}",
            base_description.bright_black(),
            alias.bright_black()
        )
    }
}

fn extract_alias_suffix(description: &str) -> String {
    let Some(start) = description.find('[') else {
        return String::new();
    };
    description[start..].to_string()
}

fn is_option_flags_line(line: &str) -> bool {
    let trimmed = line.trim_start();
    line.starts_with("  ")
        && !line.starts_with("    ")
        && (trimmed.starts_with("--") || trimmed.starts_with("-"))
}

fn is_option_entry(line: &str) -> bool {
    let trimmed = line.trim_start();
    line.starts_with("      ")
        && (trimmed.starts_with("--") || trimmed.starts_with("-"))
        && !trimmed.starts_with("Usage:")
}

fn is_command_name_line(line: &str) -> bool {
    if !line.starts_with("  ") || line.starts_with("    ") {
        return false;
    }
    let trimmed = line.trim();
    !trimmed.is_empty()
        && !trimmed.ends_with(':')
        && !trimmed.starts_with('-')
        && !trimmed.contains(' ')
        && trimmed.len() <= 24
}

fn is_indented_help_text(line: &str, min_spaces: usize) -> bool {
    let leading = line.chars().take_while(|ch| *ch == ' ').count();
    leading >= min_spaces && !line.trim_start().starts_with('-') && !line.trim().is_empty()
}

fn style_command_pair(name_line: &str, description_line: &str) -> String {
    let name = name_line.trim().bright_cyan().bold();
    let description = description_line.trim();
    let alias = extract_alias_suffix(description);
    let base_description = description
        .split_once('[')
        .map(|(left, _)| left.trim())
        .unwrap_or(description);

    if alias.is_empty() {
        format!("  {name:<22} {}", base_description.bright_black())
    } else {
        format!(
            "  {name:<22} {} {}",
            base_description.bright_black(),
            alias.bright_black()
        )
    }
}

fn style_option_pair(flags_line: &str, description_line: &str) -> String {
    let flags = flags_line.trim();
    let styled_flags = flags
        .split(", ")
        .map(|flag| flag.bright_yellow().to_string())
        .collect::<Vec<_>>()
        .join(", ");
    format!(
        "      {styled_flags:<28} {}",
        description_line.trim().bright_black()
    )
}

fn style_option_entry(line: &str) -> String {
    let trimmed = line.trim_start();
    let mut parts = trimmed.splitn(2, "  ");
    let flags = parts.next().unwrap_or_default();
    let description = parts.next().unwrap_or_default().trim();

    let styled_flags = flags
        .split(", ")
        .map(|flag| flag.bright_yellow().to_string())
        .collect::<Vec<_>>()
        .join(", ");

    if description.is_empty() {
        format!("      {styled_flags}")
    } else {
        format!("      {styled_flags:<28} {}", description.bright_black())
    }
}

fn is_example_command_line(line: &str) -> bool {
    line.starts_with("xbp ") || line.starts_with("cargo ") || line.starts_with("git ")
}

fn highlight_inline_flags(text: &str) -> String {
    let mut output = String::new();
    let mut current = String::new();
    let mut chars = text.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '-' && matches!(chars.peek(), Some('-' | 'f' | 'h' | 'l' | 'p' | 'v' | 'n')) {
            if !current.is_empty() {
                output.push_str(&current);
                current.clear();
            }
            let mut flag = String::from('-');
            if chars.peek() == Some(&'-') {
                flag.push(chars.next().expect("dash"));
            }
            while let Some(&next) = chars.peek() {
                if next.is_ascii_alphanumeric() || next == '-' {
                    flag.push(chars.next().expect("flag char"));
                } else {
                    break;
                }
            }
            output.push_str(&flag.bright_yellow().to_string());
            continue;
        }

        if ch == '<' {
            if !current.is_empty() {
                output.push_str(&current);
                current.clear();
            }
            let mut placeholder = String::from('<');
            while let Some(next) = chars.next() {
                placeholder.push(next);
                if next == '>' {
                    break;
                }
            }
            output.push_str(&placeholder.bright_green().to_string());
            continue;
        }

        current.push(ch);
    }

    if !current.is_empty() {
        output.push_str(&current);
    }
    output
}

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

    #[test]
    fn detects_root_help_scope() {
        let raw = "xbp 10.30.3\n\nAbout\nUsage: xbp [OPTIONS] [COMMAND]";
        assert_eq!(detect_help_scope(raw), HelpScope::Root);
    }

    #[test]
    fn workers_help_is_not_root() {
        let raw = "Manage workers\nUsage: xbp.exe workers [OPTIONS] <COMMAND>";
        assert!(!is_root_help_text(raw));
    }

    #[test]
    fn styles_usage_line_with_flags() {
        let styled = style_help_line("Usage: xbp workers logs [OPTIONS]");
        assert!(styled.contains("Usage:"));
        assert!(styled.contains("workers"));
    }

    #[test]
    fn styles_command_entry_line() {
        let styled = style_help_line("  list      List workers [aliases: ls]");
        assert!(styled.contains("list"));
    }

    #[test]
    fn catalog_scope_skips_duplicate_about() {
        let raw = "Deploy services\nUsage: xbp [OPTIONS] [COMMAND]\n\nCommands:\n  diag\n      Run diagnostics";
        emit_styled_help(raw, HelpScope::Catalog);
    }
}