use std::str;
use anyhow::Result;
use prettytable::{Cell, Row};
use crate::{
data::Command,
exporter::data::{chunk, extract_time, Exporter, LIMIT_COMMAND},
};
#[derive(Default)]
pub struct Table {}
impl Exporter for Table {
fn sensitive_data(&self, findings: &[Command]) -> Result<()> {
let mut out = Vec::new();
Self::prepare_sensitive_data(&mut out, findings)?;
print!("{}", str::from_utf8(&out)?);
Ok(())
}
}
impl Table {
fn prepare_sensitive_data(out: &mut Vec<u8>, findings: &[Command]) -> Result<()> {
let mut table = prettytable::Table::new();
table.add_row(Row::new(vec![
Cell::new("#"),
Cell::new("Shell"),
Cell::new("Time"),
Cell::new("Name"),
Cell::new("Command"),
]));
let mut count = 0;
let rows = findings
.iter()
.map(|f| {
count += 1;
vec![
Cell::new(&format!("{count:?}")),
Cell::new(&format!("{:?}", f.shell_type)),
Cell::new(&extract_time(f).unwrap_or_else(|_| String::new())),
Cell::new(
f.detections
.iter()
.map(|f| f.name.clone())
.collect::<Vec<_>>()
.join("\r\n")
.as_ref(),
),
Cell::new(&chunk(&f.command, LIMIT_COMMAND)),
]
})
.collect::<Vec<_>>();
let should_print = &rows.is_empty();
for row in rows {
table.add_row(Row::new(row));
}
if !should_print {
table.print(out)?;
}
Ok(())
}
}