mirror of
https://github.com/chmln/handlr.git
synced 2024-11-14 21:49:27 +01:00
list - add detailed output option
This commit is contained in:
parent
39965313d2
commit
53278c0a7e
4 changed files with 38 additions and 18 deletions
|
@ -3,20 +3,24 @@ use crate::{
|
|||
Result,
|
||||
};
|
||||
use mime::Mime;
|
||||
use std::{collections::HashMap, convert::TryFrom, ffi::OsStr};
|
||||
use std::{
|
||||
collections::{HashMap, VecDeque},
|
||||
convert::TryFrom,
|
||||
ffi::OsStr,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct SystemApps(pub HashMap<Mime, Vec<Handler>>);
|
||||
pub struct SystemApps(pub HashMap<Mime, VecDeque<Handler>>);
|
||||
|
||||
impl SystemApps {
|
||||
pub fn get_handlers(&self, mime: &Mime) -> Option<Vec<Handler>> {
|
||||
pub fn get_handlers(&self, mime: &Mime) -> Option<VecDeque<Handler>> {
|
||||
Some(self.0.get(mime)?.clone())
|
||||
}
|
||||
pub fn get_handler(&self, mime: &Mime) -> Option<Handler> {
|
||||
Some(self.get_handlers(mime)?.get(0).unwrap().clone())
|
||||
}
|
||||
pub fn populate() -> Result<Self> {
|
||||
let mut map = HashMap::<Mime, Vec<Handler>>::with_capacity(50);
|
||||
let mut map = HashMap::<Mime, VecDeque<Handler>>::with_capacity(50);
|
||||
|
||||
xdg::BaseDirectories::new()?
|
||||
.get_data_dirs()
|
||||
|
@ -35,9 +39,9 @@ impl SystemApps {
|
|||
.for_each(|entry| {
|
||||
let (file_name, mimes) = (entry.file_name, entry.mimes);
|
||||
mimes.into_iter().for_each(|mime| {
|
||||
map.entry(mime)
|
||||
.or_default()
|
||||
.push(Handler::assume_valid(file_name.clone()));
|
||||
map.entry(mime).or_default().push_back(
|
||||
Handler::assume_valid(file_name.clone()),
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -205,17 +205,30 @@ impl MimeApps {
|
|||
writer.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
pub fn print(&self) -> Result<()> {
|
||||
pub fn print(&self, detailed: bool) -> Result<()> {
|
||||
use itertools::Itertools;
|
||||
|
||||
let rows = self
|
||||
.default_apps
|
||||
.iter()
|
||||
let to_rows = |map: &HashMap<Mime, VecDeque<Handler>>| {
|
||||
map.iter()
|
||||
.sorted()
|
||||
.map(|(k, v)| vec![k.to_string(), v.iter().join(", ")])
|
||||
.collect::<Vec<_>>();
|
||||
.collect::<Vec<_>>()
|
||||
};
|
||||
|
||||
ascii_table::AsciiTable::default().print(rows);
|
||||
let table = ascii_table::AsciiTable::default();
|
||||
|
||||
if detailed {
|
||||
println!("Default Apps");
|
||||
table.print(to_rows(&self.default_apps));
|
||||
if !self.added_associations.is_empty() {
|
||||
println!("Added Associations");
|
||||
table.print(to_rows(&self.added_associations));
|
||||
}
|
||||
println!("System Apps");
|
||||
table.print(to_rows(&self.system_apps.0));
|
||||
} else {
|
||||
table.print(to_rows(&self.default_apps));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -6,7 +6,10 @@ use crate::common::{Handler, MimeOrExtension};
|
|||
#[clap(version = clap::crate_version!())]
|
||||
pub enum Cmd {
|
||||
/// List default apps and the associated handlers
|
||||
List,
|
||||
List {
|
||||
#[clap(long, short)]
|
||||
all: bool,
|
||||
},
|
||||
|
||||
/// Open a path/URL with its default handler
|
||||
Open {
|
||||
|
|
|
@ -39,8 +39,8 @@ fn main() -> Result<()> {
|
|||
let mime = MimeType::try_from(paths[0].as_str())?.0;
|
||||
apps.get_handler(&mime)?.open(paths)?;
|
||||
}
|
||||
Cmd::List => {
|
||||
apps.print()?;
|
||||
Cmd::List { all } => {
|
||||
apps.print(all)?;
|
||||
}
|
||||
Cmd::Unset { mime } => {
|
||||
apps.remove_handler(&mime.0)?;
|
||||
|
|
Loading…
Reference in a new issue