typub-ui 0.1.0

CLI output utilities for typub
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
//! Unified UI module for CLI output
//!
//! Following rust-cli standards:
//! - Data → stdout, logs/errors → stderr
//! - Respects NO_COLOR environment variable
//! - TTY detection before coloring
//! - Uses owo-colors for styling
//! - Uses indicatif for progress bars
//!
//! Per [[ADR-0004]], this crate re-exports `typub-log` for convenience and
//! provides `IndicatifReporter` implementing `ProgressReporter`.

use comfy_table::Table as ComfyTable;
use indicatif::{ProgressBar, ProgressStyle};
use owo_colors::OwoColorize;
use std::io::{self, IsTerminal};
use std::time::Duration;

// Re-export typub-log for convenience
pub use typub_log::{FnReporter, NullReporter, ProgressReporter};
pub use typub_log::{debug, error, info, init, is_verbose, trace, warn};

pub mod i18n;

/// Check if colors should be used
fn use_colors() -> bool {
    io::stdout().is_terminal() && std::env::var("NO_COLOR").is_err()
}

// ============ Status Icons ============

mod icons {
    pub const SUCCESS: &str = "";
    pub const ERROR: &str = "";
    pub const WARNING: &str = "";
    pub const INFO: &str = "";
    pub const ARROW: &str = "";
    pub const PENDING: &str = "";
    pub const DONE: &str = "";
    pub const SKIP: &str = "";
}

// ============ Styled Output Helpers ============

/// Apply color conditionally based on terminal and NO_COLOR
macro_rules! styled {
    ($text:expr, $color:ident) => {
        if use_colors() {
            format!("{}", $text.$color())
        } else {
            $text.to_string()
        }
    };
    ($text:expr, $color:ident, bold) => {
        if use_colors() {
            format!("{}", $text.$color().bold())
        } else {
            $text.to_string()
        }
    };
}

// ============ Public API - Messages ============

/// Print a success message (to stderr for logging)
pub fn success(message: &str) {
    eprintln!("{} {}", styled!(icons::SUCCESS, green), message);
}

/// Print an error message (to stderr)
pub fn error(message: &str) {
    eprintln!("{} {}", styled!(icons::ERROR, red), styled!(message, red));
}

/// Print a warning message (to stderr)
pub fn warn(message: &str) {
    eprintln!(
        "{} {}",
        styled!(icons::WARNING, yellow),
        styled!(message, yellow)
    );
}

/// Print an info message (to stderr for logging)
pub fn info(message: &str) {
    eprintln!("{} {}", styled!(icons::INFO, blue), message);
}

/// Print a debug message (only in verbose mode, to stderr)
pub fn debug(message: &str) {
    if is_verbose() {
        eprintln!("{} {}", styled!("[debug]", bright_black), message);
    }
}

// ============ Public API - Structured Output ============

/// Print a header/section title (to stderr)
pub fn header(title: &str) {
    eprintln!();
    eprintln!("{}", styled!(title, cyan, bold));
    let separator = "".repeat(title.len().max(40));
    eprintln!("{}", styled!(&separator, bright_black));
}

/// Print a step in a multi-step process (to stderr)
pub fn step(number: usize, total: usize, message: &str) {
    let step_info = format!("[{}/{}]", number, total);
    eprintln!("{} {}", styled!(&step_info, cyan), message);
}

/// Print a sub-item with label and value (to stderr)
pub fn item(label: &str, value: &str) {
    eprintln!(
        "  {} {}: {}",
        styled!(icons::ARROW, bright_black),
        styled!(label, cyan),
        value
    );
}

/// Print platform status line (to stderr)
pub fn platform_status(platform: &str, published: bool, url: Option<&str>) {
    let (icon, platform_styled) = if published {
        (styled!(icons::DONE, green), styled!(platform, green))
    } else {
        (
            styled!(icons::PENDING, bright_black),
            styled!(platform, bright_black),
        )
    };

    if let Some(url) = url {
        eprintln!(
            "  {} {} {}",
            icon,
            platform_styled,
            styled!(url, bright_black)
        );
    } else {
        eprintln!("  {} {}", icon, platform_styled);
    }
}

// ============ Public API - Publish Logging ============

/// Log start of publish operation
pub fn log_publish_start(title: &str, platforms: &[&str]) {
    header(&format!("Publishing: {}", title));
    info(&format!("Targets: {}", platforms.join(", ")));
}

/// Log successful publish
pub fn log_publish_success(platform: &str, url: Option<&str>) {
    if let Some(url) = url {
        eprintln!(
            "  {} {} {} {}",
            styled!(icons::SUCCESS, green),
            styled!(platform, green, bold),
            styled!(icons::ARROW, bright_black),
            styled!(url, cyan)
        );
    } else {
        eprintln!(
            "  {} {}",
            styled!(icons::SUCCESS, green),
            styled!(platform, green, bold)
        );
    }
}

/// Log skipped operation
pub fn log_skip(platform: &str, reason: &str) {
    eprintln!(
        "  {} {} ({})",
        styled!(icons::SKIP, bright_black),
        styled!(platform, bright_black),
        styled!(reason, bright_black)
    );
}

/// Log dry run
pub fn log_dry_run(platform: &str) {
    eprintln!(
        "  {} Would publish to: {}",
        styled!("[DRY RUN]", yellow),
        platform
    );
}

// ============ Progress Bars (using indicatif) ============

/// Create a spinner for operations with unknown duration
pub fn spinner(message: &str) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    let mut style = ProgressStyle::default_spinner();
    if let Ok(s) = style.clone().template("{spinner:.cyan} {msg}") {
        style = s.tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏");
    }
    pb.set_style(style);
    pb.set_message(message.to_string());
    pb.enable_steady_tick(Duration::from_millis(80));
    pb
}

/// Finish spinner with success
pub fn spinner_success(pb: ProgressBar, message: &str) {
    if let Ok(style) = ProgressStyle::default_spinner().template("{msg}") {
        pb.set_style(style);
    }
    pb.finish_with_message(format!("{} {}", styled!(icons::SUCCESS, green), message));
}

/// Finish spinner with error
pub fn spinner_error(pb: ProgressBar, message: &str) {
    if let Ok(style) = ProgressStyle::default_spinner().template("{msg}") {
        pb.set_style(style);
    }
    pb.finish_with_message(format!(
        "{} {}",
        styled!(icons::ERROR, red),
        styled!(message, red)
    ));
}

/// Create a progress bar for known-length operations
pub fn progress_bar(len: u64, message: &str) -> ProgressBar {
    let pb = ProgressBar::new(len);
    let mut style = ProgressStyle::default_bar();
    if let Ok(s) = style
        .clone()
        .template("{msg} [{bar:30.cyan/bright_black}] {pos}/{len}")
    {
        style = s.progress_chars("━━─");
    }
    pb.set_style(style);
    pb.set_message(message.to_string());
    pb
}

// ============ Multi-Step Progress ============

/// Progress tracker for multi-step operations
pub struct MultiProgress {
    name: String,
    current: usize,
    total: usize,
    spinner: Option<ProgressBar>,
}

impl MultiProgress {
    pub fn new(name: &str, total: usize) -> Self {
        header(name);
        Self {
            name: name.to_string(),
            current: 0,
            total,
            spinner: None,
        }
    }

    /// Start a new step
    pub fn step(&mut self, message: &str) {
        // Finish previous spinner if any
        if let Some(pb) = self.spinner.take() {
            spinner_success(pb, "Done");
        }

        self.current += 1;
        let step_msg = format!("[{}/{}] {}", self.current, self.total, message);
        self.spinner = Some(spinner(&step_msg));
    }

    /// Finish all steps successfully
    pub fn finish(self) {
        if let Some(pb) = self.spinner {
            spinner_success(pb, "Done");
        }
        success(&format!("{} completed", self.name));
    }

    /// Finish with error
    pub fn finish_error(self, err: &str) {
        if let Some(pb) = self.spinner {
            spinner_error(pb, err);
        }
        error(&format!("{} failed: {}", self.name, err));
    }
}

// ============ Table Output (to stdout for data) ============

/// Print a simple table (data goes to stdout)
pub struct Table {
    headers: Vec<String>,
    rows: Vec<Vec<String>>,
    widths: Vec<usize>,
}

impl Table {
    pub fn new(headers: &[&str]) -> Self {
        let headers: Vec<String> = headers.iter().map(|s| s.to_string()).collect();
        let widths = headers.iter().map(|h| h.len()).collect();
        Self {
            headers,
            rows: Vec::new(),
            widths,
        }
    }

    pub fn add_row(&mut self, row: &[&str]) {
        let row: Vec<String> = row.iter().map(|s| s.to_string()).collect();
        for (i, cell) in row.iter().enumerate() {
            if i < self.widths.len() {
                self.widths[i] = self.widths[i].max(cell.len());
            }
        }
        self.rows.push(row);
    }

    /// Print table to stdout (data output)
    pub fn print(&self) {
        // Header
        let header_cells: Vec<String> = self
            .headers
            .iter()
            .enumerate()
            .map(|(i, h)| format!("{:width$}", h, width = self.widths[i]))
            .collect();
        let header_line = header_cells.join("  ");
        println!("{}", styled!(&header_line, bold));

        // Separator
        let sep: Vec<String> = self.widths.iter().map(|w| "".repeat(*w)).collect();
        println!("{}", styled!(&sep.join("──"), bright_black));

        // Rows
        for row in &self.rows {
            let cells: Vec<String> = row
                .iter()
                .enumerate()
                .map(|(i, cell)| {
                    let width = self.widths.get(i).copied().unwrap_or(cell.len());
                    format!("{:width$}", cell, width = width)
                })
                .collect();
            println!("{}", cells.join("  "));
        }
    }
}

// ============ IndicatifReporter ============

/// A progress reporter using indicatif progress bars.
///
/// Implements [[ADR-0004]] Phase 4: ProgressReporter for typub-ui.
pub struct IndicatifReporter {
    bar: ProgressBar,
}

impl IndicatifReporter {
    /// Create a new reporter with a spinner (indeterminate progress).
    pub fn spinner(message: &str) -> Self {
        let bar = spinner(message);
        Self { bar }
    }

    /// Create a new reporter with a progress bar (known length).
    pub fn progress(len: u64, message: &str) -> Self {
        let bar = progress_bar(len, message);
        Self { bar }
    }
}

impl ProgressReporter for IndicatifReporter {
    fn set_message(&self, message: &str) {
        self.bar.set_message(message.to_string());
    }

    fn set_progress(&self, current: u64, total: u64) {
        if total > 0 {
            self.bar.set_length(total);
            self.bar.set_position(current);
        }
    }

    fn finish_success(&self, message: &str) {
        spinner_success_ref(&self.bar, message);
    }

    fn finish_error(&self, message: &str) {
        spinner_error_ref(&self.bar, message);
    }

    fn inc(&self, delta: u64) {
        self.bar.inc(delta);
    }
}

/// Finish spinner with success (by reference).
fn spinner_success_ref(pb: &ProgressBar, message: &str) {
    if let Ok(style) = ProgressStyle::default_spinner().template("{msg}") {
        pb.set_style(style);
    }
    pb.finish_with_message(format!("{} {}", styled!(icons::SUCCESS, green), message));
}

/// Finish spinner with error (by reference).
fn spinner_error_ref(pb: &ProgressBar, message: &str) {
    if let Ok(style) = ProgressStyle::default_spinner().template("{msg}") {
        pb.set_style(style);
    }
    pb.finish_with_message(format!(
        "{} {}",
        styled!(icons::ERROR, red),
        styled!(message, red)
    ));
}

// ============ Asset Analysis Display ============

/// Format bytes into human-readable string.
fn format_size(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.1} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.1} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}

/// Display asset analysis in a formatted table.
///
/// Shows summary of assets: total count, new (will upload), cached (reused).
pub fn log_asset_analysis(
    title: &str,
    total_count: usize,
    new_count: usize,
    new_size_bytes: u64,
    cached_count: usize,
    cached_size_bytes: u64,
) {
    let mut table = ComfyTable::new();
    table.load_preset(comfy_table::presets::NOTHING);

    // Header
    table.set_header(vec!["", "Count", "Size"]);

    // Total row
    table.add_row(vec![
        "📦 Total",
        &total_count.to_string(),
        &format_size(new_size_bytes + cached_size_bytes),
    ]);

    // New row (will upload)
    table.add_row(vec![
        "✅ New (will upload)",
        &new_count.to_string(),
        &format_size(new_size_bytes),
    ]);

    // Cached row (will skip)
    table.add_row(vec![
        "🔄 Cached (will skip)",
        &cached_count.to_string(),
        &format_size(cached_size_bytes),
    ]);

    eprintln!();
    eprintln!("{}", styled!(title, cyan, bold));
    eprintln!("{}", table);
}