cargo-e 0.3.2

e is for Example. A command-line tool for running and exploring source, examples, and binaries from Rust projects. It will run the first example, if no options are given.
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
use crate::e_cargocommand_ext::CargoProcessResult;
use comfy_table::{Cell, ContentArrangement, Row, Table};
use git2::{Error, Repository};
use std::fs::File;
use std::io::{self, Write};
use std::process::Command;

fn current_remote_and_short_sha() -> Result<(String, String, String), Error> {
    let repo = Repository::discover(".")?;

    // Get HEAD OID and shorten to 7 chars
    let oid = repo
        .head()?
        .target()
        .ok_or_else(|| Error::from_str("no HEAD target"))?;
    let short = repo
        .find_object(oid, None)?
        .short_id()?
        .as_str()
        .ok_or_else(|| Error::from_str("invalid short id"))?
        .to_string();

    // Look up the "origin" remote URL
    let remote = repo
        .find_remote("origin")
        .or_else(|_| {
            repo.remotes()?
                .get(0)
                .and_then(|name| repo.find_remote(name).ok())
                .ok_or_else(|| Error::from_str("no remotes configured"))
        })?
        .url()
        .unwrap_or_default()
        .to_string();

    // Extract the repository name from the remote URL
    let repo_name = remote
        .split('/')
        .last()
        .and_then(|name| name.strip_suffix(".git"))
        .unwrap_or("Unknown")
        .to_string();

    Ok((remote, short, repo_name))
}

pub fn generate_comfy_report(results: &[CargoProcessResult]) -> String {
    let mut system = sysinfo::System::new_all();
    system.refresh_all();

    let system_name = sysinfo::System::name().unwrap_or_else(|| "Unknown".to_string());
    let system_version = sysinfo::System::os_version().unwrap_or_else(|| "Unknown".to_string());
    let system_long_version =
        sysinfo::System::long_os_version().unwrap_or_else(|| "Unknown".to_string());

    let rustc_version = Command::new("rustc")
        .arg("--version")
        .output()
        .ok()
        .and_then(|output| {
            if output.status.success() {
                Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
            } else {
                None
            }
        })
        .unwrap_or_else(|| "Unknown".to_string());

    let (repo_remote, repo_sha, repo_name) = current_remote_and_short_sha().unwrap_or_else(|_| {
        (
            "Unknown".to_string(),
            "Unknown".to_string(),
            "Unknown".to_string(),
        )
    });

    let current_time = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();

    // Metadata Table
    let mut metadata_table = Table::new();
    metadata_table.set_content_arrangement(ContentArrangement::Dynamic);
    metadata_table.set_width(80);
    metadata_table.add_row(Row::from(vec![
        Cell::new("Run Report"),
        Cell::new(format!("{} ({} {})", repo_name, repo_remote, repo_sha)),
    ]));
    metadata_table.add_row(Row::from(vec![
        Cell::new("generated on"),
        Cell::new(current_time),
    ]));
    metadata_table.add_row(Row::from(vec![
        Cell::new("cargo-e version"),
        Cell::new(env!("CARGO_PKG_VERSION")),
    ]));
    metadata_table.add_row(Row::from(vec![
        Cell::new("rustc version"),
        Cell::new(rustc_version),
    ]));
    metadata_table.add_row(Row::from(vec![
        Cell::new("system info"),
        Cell::new(format!(
            "{} - {} - {}",
            system_name, system_version, system_long_version
        )),
    ]));

    let mut report = metadata_table.to_string();
    report.push_str("\n\n");

    // Results Table
    let mut cnt = 0;
    for result in results {
        cnt += 1;
        let mut result_table = Table::new();
        result_table.set_content_arrangement(ContentArrangement::Dynamic);
        result_table.set_width(100);

        let start_time = result
            .start_time
            .map(|t| {
                chrono::DateTime::<chrono::Local>::from(t)
                    .format("%H:%M:%S")
                    .to_string()
            })
            .unwrap_or_else(|| "-".to_string());
        let end_time = result
            .end_time
            .map(|t| {
                chrono::DateTime::<chrono::Local>::from(t)
                    .format("%H:%M:%S")
                    .to_string()
            })
            .unwrap_or_else(|| "-".to_string());
        let duration = result
            .elapsed_time
            .map(|d| format!("{:.2?}", d))
            .unwrap_or_else(|| "-".to_string());
        let exit_code = result.exit_status.map_or("-".to_string(), |s| {
            s.code().map_or("-".to_string(), |c| c.to_string())
        });
        let success = result
            .exit_status
            .map_or("No", |s| if s.success() { "Yes" } else { "No" });

        report.push_str(&format!("## {}. {}\n\n", cnt, result.target_name));
        report.push_str(&format!("{} {}\n", result.cmd, result.args.join(" ")));
        result_table.add_row(Row::from(vec![
            Cell::new(result.target_name.clone()),
            // Cell::new(format!("{} {}", result.cmd, result.args.join(" "))),
        ]));
        result_table.add_row(Row::from(vec![
            Cell::new("Start Time"),
            Cell::new(start_time),
        ]));
        result_table.add_row(Row::from(vec![Cell::new("End Time"), Cell::new(end_time)]));
        result_table.add_row(Row::from(vec![Cell::new("Duration"), Cell::new(duration)]));
        result_table.add_row(Row::from(vec![
            Cell::new("Exit Code"),
            Cell::new(exit_code),
        ]));
        result_table.add_row(Row::from(vec![Cell::new("Success"), Cell::new(success)]));
        report.push_str(&result_table.to_string());
        report.push_str("\n\n");

        // Diagnostics Table
        if !result.diagnostics.is_empty() {
            let mut diagnostics_table = Table::new();
            diagnostics_table.set_content_arrangement(ContentArrangement::Dynamic);
            diagnostics_table.set_width(100);

            diagnostics_table.add_row(Row::from(vec![
                Cell::new("Level"),
                Cell::new("Lineref"),
                Cell::new("Error Code"),
                Cell::new("Message"),
            ]));

            for diagnostic in &result.diagnostics {
                diagnostics_table.add_row(Row::from(vec![
                    Cell::new(format!(
                        "{}{}",
                        diagnostic
                            .level
                            .chars()
                            .next()
                            .unwrap_or_default()
                            .to_uppercase(),
                        diagnostic.diag_number.unwrap_or_default()
                    )),
                    Cell::new(&diagnostic.lineref),
                    Cell::new(
                        diagnostic
                            .error_code
                            .clone()
                            .unwrap_or_else(|| "-".to_string()),
                    ),
                    Cell::new(&diagnostic.message),
                ]));
            }
            report.push_str(&diagnostics_table.to_string());
            report.push_str("\n");

            // Add a row with the full debug print of the diagnostic
            let mut detail_table = Table::new();
            detail_table.set_content_arrangement(ContentArrangement::Dynamic);
            detail_table.set_width(100);
            for diagnostic in &result.diagnostics {
                let mut nocolor = diagnostic.clone();
                nocolor.uses_color = false; // Disable color for detailed output
                detail_table.add_row(Row::from(vec![Cell::new(format!("{:?}", nocolor))]));
            }

            report.push_str(&detail_table.to_string());
            report.push_str("\n\n");
        }
    }

    report
}

pub fn generate_markdown_report(results: &[CargoProcessResult]) -> String {
    return generate_comfy_report(results);
    #[allow(unreachable_code)]
    let mut system = sysinfo::System::new_all();
    system.refresh_all();

    let system_name = sysinfo::System::name().unwrap_or_else(|| "Unknown".to_string());
    let system_version = sysinfo::System::os_version().unwrap_or_else(|| "Unknown".to_string());
    let system_long_version =
        sysinfo::System::long_os_version().unwrap_or_else(|| "Unknown".to_string());

    let rustc_version = Command::new("rustc")
        .arg("--version")
        .output()
        .ok()
        .and_then(|output| {
            if output.status.success() {
                Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
            } else {
                None
            }
        })
        .unwrap_or_else(|| "Unknown".to_string());

    let (repo_remote, repo_sha, repo_name) = current_remote_and_short_sha().unwrap_or_else(|_| {
        (
            "Unknown".to_string(),
            "Unknown".to_string(),
            "Unknown".to_string(),
        )
    });

    let current_time = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();

    // Create the main table
    let mut table = Table::new();
    table.set_content_arrangement(ContentArrangement::Dynamic);
    table.set_width(100);

    // Add metadata rows
    table.add_row(Row::from(vec![
        Cell::new("Run Report"),
        Cell::new(format!("{} ({}@{})", repo_name, repo_remote, repo_sha)),
    ]));
    table.add_row(Row::from(vec![
        Cell::new("Generated On"),
        Cell::new(current_time),
    ]));
    table.add_row(Row::from(vec![
        Cell::new("Cargo-e Version"),
        Cell::new(env!("CARGO_PKG_VERSION")),
    ]));
    table.add_row(Row::from(vec![
        Cell::new("Rustc Version"),
        Cell::new(rustc_version),
    ]));
    table.add_row(Row::from(vec![
        Cell::new("System Info"),
        Cell::new(format!(
            "{} - {} - {}",
            system_name, system_version, system_long_version
        )),
    ]));

    // Add a separator row
    table.add_row(Row::from(vec![Cell::new(""), Cell::new("")]));

    // Add results for each target
    for (index, result) in results.iter().enumerate() {
        let start_time = result
            .start_time
            .map(|t| {
                chrono::DateTime::<chrono::Local>::from(t)
                    .format("%H:%M:%S")
                    .to_string()
            })
            .unwrap_or_else(|| "-".to_string());
        let end_time = result
            .end_time
            .map(|t| {
                chrono::DateTime::<chrono::Local>::from(t)
                    .format("%H:%M:%S")
                    .to_string()
            })
            .unwrap_or_else(|| "-".to_string());
        let duration = result
            .elapsed_time
            .map(|d| format!("{:.2?}", d))
            .unwrap_or_else(|| "-".to_string());
        let exit_code = result.exit_status.map_or("-".to_string(), |s| {
            s.code().map_or("-".to_string(), |c| c.to_string())
        });
        let success = result
            .exit_status
            .map_or("No", |s| if s.success() { "Yes" } else { "No" });

        table.add_row(Row::from(vec![
            Cell::new(format!("{}. {}", index + 1, result.target_name)),
            Cell::new(""),
        ]));
        table.add_row(Row::from(vec![
            Cell::new("Start Time"),
            Cell::new(start_time),
        ]));
        table.add_row(Row::from(vec![Cell::new("End Time"), Cell::new(end_time)]));
        table.add_row(Row::from(vec![Cell::new("Duration"), Cell::new(duration)]));
        table.add_row(Row::from(vec![
            Cell::new("Exit Code"),
            Cell::new(exit_code),
        ]));
        table.add_row(Row::from(vec![Cell::new("Success"), Cell::new(success)]));

        // Add diagnostics if available
        if !result.diagnostics.is_empty() {
            table.add_row(Row::from(vec![Cell::new("Diagnostics"), Cell::new("")]));
            for diagnostic in &result.diagnostics {
                table.add_row(Row::from(vec![
                    Cell::new(format!("Level: {}", diagnostic.level)),
                    Cell::new(&diagnostic.message),
                ]));
            }
        }

        // Add a separator row
        table.add_row(Row::from(vec![Cell::new(""), Cell::new("")]));
    }

    // Convert the table to a string
    table.to_string()
}

// pub fn generate_markdown_report(results: &[CargoProcessResult]) -> String {
//         let mut system = sysinfo::System::new_all();
//     system.refresh_all();
//     let system_name = sysinfo::System::name().unwrap_or_else(|| "Unknown".to_string());
//     let system_long_version = sysinfo::System::long_os_version().unwrap_or_else(|| "Unknown".to_string());
//     let system_version = sysinfo::System::os_version().unwrap_or_else(|| "Unknown".to_string());

//         let rustc_version = Command::new("rustc")
//         .arg("--version")
//         .output()
//         .ok()
//         .and_then(|output| {
//             if output.status.success() {
//                 Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
//             } else {
//                 None
//             }
//         })
//         .unwrap_or_else(|| "Unknown".to_string());
//         let (repo_remote, repo_sha, repo_name) = current_remote_and_short_sha()
//         .unwrap_or_else(|_| ("-".to_string(), "-".to_string(), "-".to_string()));
//     let current_time = chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string();
//         let mut report = String::from(format!(
//         "# Run Report for {} ({}@{})\n\nGenerated on: {}\n\n",
//         repo_name, repo_remote, repo_sha, current_time
//     ));
//     report.push_str(&format!("**repo info** {} @ {}\n", repo_remote, repo_sha));
//     report.push_str(&format!("**rustc version** {}\n", rustc_version));
//     report.push_str(&format!("**system name** {} - {} - {}\n", system_name, system_version, system_long_version));
//     report.push_str(&format!("**cargo-e version** {}\n", env!("CARGO_PKG_VERSION")));
//     report.push_str("\n");
//     let mut cnt = 0;
//     for result in results {
//         cnt=cnt + 1;
//     report.push_str(&format!("## {}. {}\n\n", cnt, result.target_name));
//     report.push_str("| Target Name | Start Time | End Time | Duration | Exit Code | Success |\n");
//     report.push_str("|-------------|------------|----------|----------|-----------|---------|\n");

//         let start_time = result
//             .start_time
//             .map(|t| chrono::DateTime::<chrono::Local>::from(t).format("%H:%M:%S").to_string())
//             .unwrap_or_else(|| "-".to_string());
//         let end_time = result
//             .end_time
//             .map(|t| chrono::DateTime::<chrono::Local>::from(t).format("%H:%M:%S").to_string())
//             .unwrap_or_else(|| "-".to_string());
//         let duration = result
//             .elapsed_time
//             .map(|d| format!("{:.2?}", d))
//             .unwrap_or_else(|| "-".to_string());
//         let exit_code = result.exit_status.map_or("-".to_string(), |s| s.code().map_or("-".to_string(), |c| c.to_string()));
//         let success = result.exit_status.map_or("No", |s| if s.success() { "Yes" } else { "No" });

//         report.push_str(&format!(
//             "| {} | {} | {} | {} | {} | {} |\n",
//             result.target_name, start_time, end_time, duration, exit_code, success
//         ));

//         if !result.diagnostics.is_empty() {
//             report.push_str("\n| Level | Message                            |\n");
//             report.push_str("|----------------------------------------------|\n");
//             for diagnostic in &result.diagnostics {
//                 report.push_str(&format!("| **{}** | {} |\n",diagnostic.level, diagnostic.message));
//             }
//             report.push_str("|----------------------------------------------------------------------|\n");
//         }
//         report.push_str("\n");
//     }

//     report
// }

pub fn save_report_to_file(report: &str, file_path: &str) -> io::Result<()> {
    let mut file = File::create(file_path)?;
    file.write_all(report.as_bytes())?;
    Ok(())
}

pub fn create_gist(content: &str, description: &str) -> io::Result<()> {
    crate::e_installer::ensure_github_gh().map_err(|e| io::Error::new(io::ErrorKind::Other, e))?; // check for gh CLI
    let output = Command::new("gh")
        .args(&["gist", "create", "--public", "--desc", description])
        .stdin(std::process::Stdio::piped())
        .spawn()?
        .stdin
        .as_mut()
        .unwrap()
        .write_all(content.as_bytes());

    output.map_err(|e| {
        io::Error::new(
            io::ErrorKind::Other,
            format!("Failed to create gist: {}", e),
        )
    })
}