use chrono::Duration;
use clap::ArgMatches;
use ffsend_api::config::SEND_DEFAULT_EXPIRE_TIME;
use prettytable::{format::FormatBuilder, Cell, Row, Table};
use crate::client::to_duration;
use crate::cmd::matcher::{debug::DebugMatcher, main::MainMatcher, Matcher};
use crate::error::ActionError;
#[cfg(feature = "clipboard-bin")]
use crate::util::ClipboardType;
use crate::util::{api_version_list, features_list, format_bool, format_duration};
pub struct Debug<'a> {
cmd_matches: &'a ArgMatches<'a>,
}
impl<'a> Debug<'a> {
pub fn new(cmd_matches: &'a ArgMatches<'a>) -> Self {
Self { cmd_matches }
}
pub fn invoke(&self) -> Result<(), ActionError> {
let matcher_main = MainMatcher::with(self.cmd_matches).unwrap();
let matcher_debug = DebugMatcher::with(self.cmd_matches).unwrap();
let mut table = Table::new();
table.set_format(FormatBuilder::new().padding(0, 2).build());
table.add_row(Row::new(vec![
Cell::new("Version:"),
Cell::new(crate_version!()),
]));
table.add_row(Row::new(vec![
Cell::new("Host:"),
Cell::new(matcher_debug.host().as_str()),
]));
#[cfg(feature = "history")]
table.add_row(Row::new(vec![
Cell::new("History file:"),
Cell::new(matcher_main.history().to_str().unwrap_or("?")),
]));
table.add_row(Row::new(vec![
Cell::new("Timeout:"),
Cell::new(
&to_duration(matcher_main.timeout())
.map(|t| {
format_duration(
Duration::from_std(t).expect("failed to convert timeout duration"),
)
})
.unwrap_or("disabled".into()),
),
]));
table.add_row(Row::new(vec![
Cell::new("Transfer timeout:"),
Cell::new(
&to_duration(matcher_main.transfer_timeout())
.map(|t| {
format_duration(
Duration::from_std(t)
.expect("failed to convert transfer timeout duration"),
)
})
.unwrap_or("disabled".into()),
),
]));
table.add_row(Row::new(vec![
Cell::new("Default expiry:"),
Cell::new(&format_duration(Duration::seconds(
SEND_DEFAULT_EXPIRE_TIME as i64,
))),
]));
table.add_row(Row::new(vec![
Cell::new("Features:"),
Cell::new(&features_list().join(", ")),
]));
table.add_row(Row::new(vec![
Cell::new("API support:"),
Cell::new(&api_version_list().join(", ")),
]));
table.add_row(Row::new(vec![
Cell::new("Crypto backend:"),
#[cfg(feature = "crypto-ring")]
Cell::new("ring"),
#[cfg(feature = "crypto-openssl")]
Cell::new("OpenSSL"),
]));
#[cfg(feature = "clipboard-bin")]
table.add_row(Row::new(vec![
Cell::new("Clipboard:"),
Cell::new(&format!("{}", ClipboardType::select())),
]));
table.add_row(Row::new(vec![
Cell::new("Quiet:"),
Cell::new(format_bool(matcher_main.quiet())),
]));
table.add_row(Row::new(vec![
Cell::new("Verbose:"),
Cell::new(format_bool(matcher_main.verbose())),
]));
table.printstd();
Ok(())
}
}