use anyhow::Result;
use clap::Clap;
use console::Style;
use crate::cfg::Config;
use crate::cli::color;
use crate::cli::draw_boxed;
use crate::cmd::Command;
use crate::ssh::SshSession;
#[derive(Clap, Debug)]
pub struct List {
#[clap(long, short)]
details: bool,
#[clap(long, short)]
filenames: bool,
#[clap(long, short = 'F', value_name = "regex")]
filter: Option<String>,
#[clap()]
indices: Vec<i64>,
#[clap(short = 'n', long)]
last: Option<usize>,
#[clap(long = "indices", short = 'i', conflicts_with = "url-only")]
print_indices: bool,
#[clap(long, short)]
reverse: bool,
#[clap(long = "newer")]
select_newer: Option<String>,
#[clap(long = "older")]
select_older: Option<String>,
#[clap(long, short = 'S')]
sort_size: bool,
#[clap(long, short = 'T')]
sort_time: bool,
#[clap(short, long = "url-only", conflicts_with = "indices")]
url_only: bool,
#[clap(long, short = 't')]
with_time: bool,
#[clap(long, short = 's')]
with_size: bool,
}
impl Command for List {
fn run(&self, session: &SshSession, _config: &Config) -> Result<()> {
let host = &session.host;
let to_list = session
.list_files()?
.by_indices(&self.indices[..])?
.by_filter(self.filter.as_deref())?
.with_all_if_none(true)
.select_newer(self.select_newer.as_deref())?
.select_older(self.select_older.as_deref())?
.sort_by_size(self.sort_size)?
.sort_by_time(self.sort_time)?
.revert(self.reverse)
.last(self.last)
.with_stats(self.details || self.with_time || self.with_size)?;
if self.url_only {
for (_, file, _) in to_list.iter()? {
println!("{}", host.get_url(&format!("{}", file.display()))?);
}
} else if self.print_indices {
for idx in to_list.indices {
print!("{} ", idx);
}
println!("");
} else {
let content = to_list.format_files(
Some(&session.host),
self.filenames,
self.details || self.with_size,
self.details || self.with_time,
)?;
draw_boxed(
format!(
"{listing} remote files:",
listing = Style::new().bold().green().bright().apply_to("Listing")
),
content.iter().map(|s| s.as_ref()),
&color::frame,
)?;
}
Ok(())
}
}
impl List {}