office-automation 0.3.2

Windows CLI tool that automates PowerPoint and Excel via COM
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
//! `oa info` — inspect a PPTX file and show shape/link/chart counts.

use std::collections::HashMap;
use std::path::Path;

use console::Style;

use crate::com::dispatch::Dispatch;
use crate::com::session::{create_instance, init_com_sta, spawn_dialog_dismisser, stop_dialog_dismisser};
use crate::com::variant::Variant;
use crate::config::Config;
use crate::error::OaResult;
use crate::office::constants::MsoTriState;
use crate::shapes::inventory::build_inventory;
use crate::shapes::matcher::{delta_set, template_name_for_set};
use crate::utils::link_parser::extract_file_path;

/// Per-slide shape counts for verbose output.
struct SlideBreakdown {
    slide: i32,
    ole: usize,
    chart: usize,
    ntbl: usize,
    htmp: usize,
    trns: usize,
    delt: usize,
    ccst: usize,
}

impl SlideBreakdown {
    fn total(&self) -> usize {
        self.ole + self.chart + self.ntbl + self.htmp + self.trns + self.delt + self.ccst
    }
}

/// Run the `oa info` command — inspect a PPTX file.
pub fn run_info(pptx_path: &str, verbose: bool) -> OaResult<()> {
    let path = Path::new(pptx_path);
    if !path.exists() {
        eprintln!("File not found: {pptx_path}");
        std::process::exit(1);
    }

    let abs_path = path.canonicalize()?;
    // GOTCHA #26: Strip \\?\ UNC prefix
    let path_str = abs_path.to_string_lossy().to_string();
    let path_str = path_str.strip_prefix(r"\\?\").unwrap_or(&path_str);

    // Initialize COM
    let _com = init_com_sta()?;
    let (stop, handle) = spawn_dialog_dismisser();

    // Create PowerPoint
    let mut ppt = create_instance("PowerPoint.Application")?;
    ppt.put("DisplayAlerts", Variant::from(0i32))?;

    // Open presentation read-only
    let mut presentations = Dispatch::new(ppt.get("Presentations")?.as_dispatch()?);
    let pres_variant = presentations.call("Open", &[
        Variant::from(path_str),
        Variant::from(MsoTriState::True as i32),  // ReadOnly
        Variant::from(MsoTriState::True as i32),  // Untitled
        Variant::from(MsoTriState::False as i32), // WithWindow
    ])?;
    let mut presentation = Dispatch::new(pres_variant.as_dispatch()?);

    // Get slide count
    let mut slides_obj = Dispatch::new(presentation.get("Slides")?.as_dispatch()?);
    let slide_count = slides_obj.get("Count")?.as_i32()?;

    // Build inventory
    let inventory = build_inventory(&mut presentation);

    // Collect OLE source file paths
    let mut ole_sources: HashMap<String, usize> = HashMap::new();
    for ole_ref in &inventory.ole_shapes {
        let mut shape = ole_ref.dispatch.clone();
        let source = shape.nav("LinkFormat")
            .and_then(|mut lf| lf.get("SourceFullName"))
            .and_then(|v| v.as_string())
            .map(|s| extract_file_path(&s))
            .unwrap_or_else(|_| "(unknown)".to_string());
        *ole_sources.entry(source).or_insert(0) += 1;
    }

    // Find template shapes on slide 1
    let config = Config::default();
    let template_names = [
        &config.delta.template_positive,
        &config.delta.template_negative,
        &config.delta.template_none,
    ];
    let mut template_found = Vec::new();
    for name in &template_names {
        let found = find_template_shape(&mut presentation, name, 1);
        template_found.push((name.to_string(), found));
    }

    // Numbered delta sets (delt2_, delt3_, ...) present in the deck.
    // Each set N ≥ 2 needs its own tmpl<N>_delta_* triple on the template slide.
    let mut extra_sets: Vec<u32> = inventory.delts.values()
        .filter_map(|d| delta_set(&d.name))
        .filter(|&s| s >= 2)
        .collect();
    extra_sets.sort_unstable();
    extra_sets.dedup();

    let extra_set_counts: Vec<(u32, usize)> = extra_sets.iter().map(|&set| {
        let n = inventory.delts.values()
            .filter(|d| delta_set(&d.name) == Some(set))
            .count();
        (set, n)
    }).collect();

    for &set in &extra_sets {
        let derived = [
            template_name_for_set(&config.delta.template_positive, set, "pos"),
            template_name_for_set(&config.delta.template_negative, set, "neg"),
            template_name_for_set(&config.delta.template_none, set, "none"),
        ];
        for name in derived {
            let found = find_template_shape(&mut presentation, &name, 1);
            template_found.push((name, found));
        }
    }

    // Count unlinked charts
    let unlinked_charts = count_unlinked_charts(&mut presentation);

    // Collect per-slide breakdown from inventory (before dropping COM refs)
    let per_slide = if verbose {
        collect_per_slide_breakdown(&inventory, slide_count)
    } else {
        Vec::new()
    };

    // --- Cleanup COM before printing ---
    // GOTCHA #21: Drop all refs before Quit
    drop(slides_obj);
    presentation.call("Close", &[])?;
    drop(presentation);
    drop(presentations);
    ppt.call0("Quit")?;
    drop(ppt);
    stop_dialog_dismisser(stop, handle);

    // --- Print results ---
    let s_cyan = Style::new().cyan();
    let s_dim = Style::new().dim();

    let file_name = Path::new(pptx_path).file_name().unwrap_or_default().to_string_lossy();
    println!();
    println!("  {} {}", s_cyan.apply_to(""), s_cyan.apply_to(&*file_name));
    println!("  {}", s_dim.apply_to("".repeat(61)));

    // File size
    let file_size = std::fs::metadata(pptx_path).map(|m| m.len()).unwrap_or(0);
    println!();
    info_row_str("File size", &format_file_size(file_size), false);

    // Slides
    info_row("Slides", slide_count as usize, false);

    // OLE links
    let total_ole: usize = ole_sources.values().sum();
    println!();
    info_row("OLE links", total_ole, false);
    let mut sorted: Vec<_> = ole_sources.iter().collect();
    sorted.sort_by(|a, b| b.1.cmp(a.1));
    for (src, count) in &sorted {
        let display_name = Path::new(src.as_str())
            .file_name()
            .map(|f| f.to_string_lossy().to_string())
            .unwrap_or_else(|| src.to_string());
        info_row(&display_name, **count, true);
    }

    // Charts
    let total_charts = inventory.charts.len() + unlinked_charts;
    println!();
    info_row("Charts", total_charts, false);
    info_row("Linked", inventory.charts.len(), true);
    info_row("Unlinked", unlinked_charts, true);
    let empty_cache = count_empty_cache_charts(path);
    if empty_cache > 0 {
        info_row("Empty cache", empty_cache, true);
    }

    // Special shapes
    let total_special = inventory.count_ntbl + inventory.count_htmp
        + inventory.count_trns + inventory.count_delt + inventory.count_ccst;
    println!();
    info_row("Special shapes", total_special, false);
    info_row("ntbl_ normal tables", inventory.count_ntbl, true);
    info_row("htmp_ heatmap tables", inventory.count_htmp, true);
    info_row("trns_ transposed tables", inventory.count_trns, true);
    info_row("delt_ delta indicators", inventory.count_delt, true);
    for (set, n) in &extra_set_counts {
        info_row(&format!("delt{set}_ delta indicators (set {set})"), *n, true);
    }
    info_row("_ccst color-coded", inventory.count_ccst, true);

    // Delta templates
    println!();
    println!("  {}", s_dim.apply_to("Delta templates"));
    for (name, found) in &template_found {
        info_row_status(name, *found);
    }

    // Per-slide breakdown (verbose only)
    if verbose && !per_slide.is_empty() {
        print_per_slide_breakdown(&per_slide, slide_count);
    }

    Ok(())
}

/// Check if a template shape exists on the given slide.
fn find_template_shape(presentation: &mut Dispatch, name: &str, slide_index: i32) -> bool {
    let slide = {
        let mut slides_disp = match presentation.get("Slides") {
            Ok(v) => match v.as_dispatch() {
                Ok(d) => Dispatch::new(d),
                Err(_) => return false,
            },
            Err(_) => return false,
        };
        match slides_disp.call("Item", &[Variant::from(slide_index)]) {
            Ok(v) => match v.as_dispatch() {
                Ok(d) => Ok(Dispatch::new(d)),
                Err(e) => Err(e),
            },
            Err(e) => Err(e),
        }
    };

    let mut slide = match slide {
        Ok(s) => s,
        Err(_) => return false,
    };

    let mut shapes = match slide.get("Shapes") {
        Ok(v) => match v.as_dispatch() {
            Ok(d) => Dispatch::new(d),
            Err(_) => return false,
        },
        Err(_) => return false,
    };

    let count = shapes.get("Count")
        .and_then(|v| v.as_i32())
        .unwrap_or(0);

    for i in 1..=count {
        if let Ok(v) = shapes.call("Item", &[Variant::from(i)])
            && let Ok(d) = v.as_dispatch() {
                let mut shp = Dispatch::new(d);
                if let Ok(n) = shp.get("Name")
                    && let Ok(shape_name) = n.as_string()
                        && shape_name == name {
                            return true;
                        }
            }
    }

    false
}

/// Print a dot-leader row with a right-aligned string value (e.g., "12.4 MB").
fn info_row_str(label: &str, value: &str, indent: bool) {
    let s_dim = Style::new().dim();
    let s_count = Style::new().white().bold();
    let prefix = if indent { "" } else { "  " };
    let target_col: usize = 48;

    let display_len = prefix.chars().count() + label.chars().count() + 1;
    let leader_len = target_col.saturating_sub(display_len);
    let padded = format!("{prefix}{label} {}", "·".repeat(leader_len));

    // Right-align the string in 4+ chars
    println!("{} {:>4}",
        s_dim.apply_to(&padded),
        s_count.apply_to(value));
}

/// Format a byte count as human-readable file size.
fn format_file_size(bytes: u64) -> String {
    if bytes < 1024 {
        format!("{bytes} B")
    } else if bytes < 1024 * 1024 {
        format!("{:.1} KB", bytes as f64 / 1024.0)
    } else if bytes < 1024 * 1024 * 1024 {
        format!("{:.1} MB", bytes as f64 / (1024.0 * 1024.0))
    } else {
        format!("{:.1} GB", bytes as f64 / (1024.0 * 1024.0 * 1024.0))
    }
}

/// Count charts with empty numCache (ptCount but no pt elements).
fn count_empty_cache_charts(pptx_path: &Path) -> usize {
    let cache = match crate::zip_ops::chart_data::read_all_chart_cache(pptx_path) {
        Ok(m) => m,
        Err(_) => return 0,
    };
    let mut count = 0;
    for series_list in cache.values() {
        for (_ref_str, values) in series_list {
            if values.is_empty() {
                count += 1;
                break; // Count the chart once, not per-series
            }
        }
    }
    count
}

/// Print a dot-leader row with right-aligned number.
///
/// `indent=true` adds `╰` prefix for sub-items.
fn info_row(label: &str, count: usize, indent: bool) {
    let s_dim = Style::new().dim();
    let s_count = Style::new().white().bold();
    let prefix = if indent { "" } else { "  " };
    let target_col: usize = 48;

    // Use char count (not byte count) — ╰ is 3 bytes but 1 display char
    let display_len = prefix.chars().count() + label.chars().count() + 1; // +1 for trailing space
    let leader_len = target_col.saturating_sub(display_len);
    let padded = format!("{prefix}{label} {}", "·".repeat(leader_len));

    println!("{} {:>4}",
        s_dim.apply_to(&padded),
        s_count.apply_to(count));
}

/// Print a dot-leader row with ✓/✗ status instead of a number.
fn info_row_status(label: &str, found: bool) {
    let s_dim = Style::new().dim();
    let prefix = "";
    let target_col: usize = 48;

    let display_len = prefix.chars().count() + label.chars().count() + 1;
    let leader_len = target_col.saturating_sub(display_len);
    let padded = format!("{prefix}{label} {}", "·".repeat(leader_len));

    let icon = if found {
        Style::new().green().apply_to("")
    } else {
        Style::new().red().apply_to("")
    };

    println!("{}    {}",
        s_dim.apply_to(&padded),
        icon);
}

/// Count unlinked charts in the presentation.
fn count_unlinked_charts(presentation: &mut Dispatch) -> usize {
    let mut count = 0;
    let mut slides = match presentation.get("Slides") {
        Ok(v) => match v.as_dispatch() {
            Ok(d) => Dispatch::new(d),
            Err(_) => return 0,
        },
        Err(_) => return 0,
    };

    let slide_count = slides.get("Count")
        .and_then(|v| v.as_i32())
        .unwrap_or(0);

    for s in 1..=slide_count {
        let mut slide = match slides.call("Item", &[Variant::from(s)]) {
            Ok(v) => match v.as_dispatch() {
                Ok(d) => Dispatch::new(d),
                Err(_) => continue,
            },
            Err(_) => continue,
        };

        let mut shapes = match slide.get("Shapes") {
            Ok(v) => match v.as_dispatch() {
                Ok(d) => Dispatch::new(d),
                Err(_) => continue,
            },
            Err(_) => continue,
        };

        let shape_count = shapes.get("Count")
            .and_then(|v| v.as_i32())
            .unwrap_or(0);

        for i in 1..=shape_count {
            if let Ok(v) = shapes.call("Item", &[Variant::from(i)])
                && let Ok(d) = v.as_dispatch() {
                    let mut shp = Dispatch::new(d);
                    let has_chart = shp.get("HasChart")
                        .and_then(|v| v.as_i32())
                        .unwrap_or(0);

                    if has_chart != 0 {
                        let is_linked = shp.nav("Chart.ChartData")
                            .and_then(|mut cd| cd.get("IsLinked"))
                            .and_then(|v| v.as_bool())
                            .unwrap_or(false);

                        if !is_linked {
                            count += 1;
                        }
                    }
                }
        }
    }

    count
}

/// Collect per-slide shape counts from the inventory.
fn collect_per_slide_breakdown(
    inventory: &crate::shapes::inventory::SlideInventory,
    slide_count: i32,
) -> Vec<SlideBreakdown> {
    use crate::shapes::matcher::TableType;

    let mut slides: HashMap<i32, SlideBreakdown> = HashMap::new();

    for s in 1..=slide_count {
        slides.insert(s, SlideBreakdown {
            slide: s, ole: 0, chart: 0, ntbl: 0, htmp: 0, trns: 0, delt: 0, ccst: 0,
        });
    }

    for ole in &inventory.ole_shapes {
        if let Some(entry) = slides.get_mut(&ole.slide_index) {
            entry.ole += 1;
        }
    }

    for chart in &inventory.charts {
        if let Some(entry) = slides.get_mut(&chart.slide_index) {
            entry.chart += 1;
        }
    }

    for ((slide_idx, _), table_info) in &inventory.tables {
        if let Some(entry) = slides.get_mut(slide_idx) {
            match table_info.table_type {
                TableType::Normal => entry.ntbl += 1,
                TableType::Heatmap => entry.htmp += 1,
                TableType::Transposed => entry.trns += 1,
            }
        }
    }

    for (slide_idx, _) in inventory.delts.keys() {
        if let Some(entry) = slides.get_mut(slide_idx) {
            entry.delt += 1;
        }
    }

    for ccst in &inventory.ccst_tables {
        if let Some(entry) = slides.get_mut(&ccst.slide_index) {
            entry.ccst += 1;
        }
    }

    let mut result: Vec<SlideBreakdown> = slides.into_values().collect();
    result.sort_by_key(|s| s.slide);
    result
}

/// Print the per-slide breakdown table.
fn print_per_slide_breakdown(per_slide: &[SlideBreakdown], total_slides: i32) {
    let s_dim = Style::new().dim();
    let s_count = Style::new().white().bold();

    // Use exactly {:>6} for every column — header and data use the same format
    let divider = "".repeat(63);

    println!();
    println!("  {}", s_dim.apply_to("Per-slide breakdown"));
    println!("  {}", s_dim.apply_to(&divider));

    // Header row — same {:>6} format as data rows
    println!("  {} {} {} {} {} {} {} {} {}",
        s_dim.apply_to(format!("{:>6}", "slide")),
        s_dim.apply_to(format!("{:>6}", "ole")),
        s_dim.apply_to(format!("{:>6}", "chart")),
        s_dim.apply_to(format!("{:>6}", "ntbl")),
        s_dim.apply_to(format!("{:>6}", "htmp")),
        s_dim.apply_to(format!("{:>6}", "trns")),
        s_dim.apply_to(format!("{:>6}", "delt")),
        s_dim.apply_to(format!("{:>6}", "ccst")),
        s_dim.apply_to(format!("{:>6}", "total")),
    );
    println!();

    let mut active = 0usize;
    for row in per_slide {
        let total = row.total();
        if total > 0 { active += 1; }

        // Show ALL slides — empty slides get dots in every column
        let total_cell = if total == 0 {
            format!("{}", s_dim.apply_to(format!("{:>6}", "·")))
        } else {
            format!("{}", s_count.apply_to(format!("{:>6}", total)))
        };

        println!("  {} {} {} {} {} {} {} {} {}",
            s_dim.apply_to(format!("{:>6}", row.slide)),
            fmt_cell(row.ole, &s_dim, &s_count),
            fmt_cell(row.chart, &s_dim, &s_count),
            fmt_cell(row.ntbl, &s_dim, &s_count),
            fmt_cell(row.htmp, &s_dim, &s_count),
            fmt_cell(row.trns, &s_dim, &s_count),
            fmt_cell(row.delt, &s_dim, &s_count),
            fmt_cell(row.ccst, &s_dim, &s_count),
            total_cell,
        );
    }

    let empty = total_slides as usize - active;
    println!("  {}", s_dim.apply_to(&divider));
    println!("  {} {} {} {} {} {}",
        s_count.apply_to(total_slides),
        s_dim.apply_to("slides ·"),
        s_count.apply_to(active),
        s_dim.apply_to("active ·"),
        s_count.apply_to(empty),
        s_dim.apply_to("empty"),
    );
}

/// Format a single cell in the per-slide table: {:>6} aligned.
fn fmt_cell(value: usize, s_dim: &Style, s_count: &Style) -> String {
    if value == 0 {
        format!("{}", s_dim.apply_to(format!("{:>6}", "·")))
    } else {
        format!("{}", s_count.apply_to(format!("{:>6}", value)))
    }
}