mirror of
https://github.com/chmln/handlr.git
synced 2025-04-25 09:33:01 +02: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,
|
Result,
|
||||||
};
|
};
|
||||||
use mime::Mime;
|
use mime::Mime;
|
||||||
use std::{collections::HashMap, convert::TryFrom, ffi::OsStr};
|
use std::{
|
||||||
|
collections::{HashMap, VecDeque},
|
||||||
|
convert::TryFrom,
|
||||||
|
ffi::OsStr,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct SystemApps(pub HashMap<Mime, Vec<Handler>>);
|
pub struct SystemApps(pub HashMap<Mime, VecDeque<Handler>>);
|
||||||
|
|
||||||
impl SystemApps {
|
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())
|
Some(self.0.get(mime)?.clone())
|
||||||
}
|
}
|
||||||
pub fn get_handler(&self, mime: &Mime) -> Option<Handler> {
|
pub fn get_handler(&self, mime: &Mime) -> Option<Handler> {
|
||||||
Some(self.get_handlers(mime)?.get(0).unwrap().clone())
|
Some(self.get_handlers(mime)?.get(0).unwrap().clone())
|
||||||
}
|
}
|
||||||
pub fn populate() -> Result<Self> {
|
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()?
|
xdg::BaseDirectories::new()?
|
||||||
.get_data_dirs()
|
.get_data_dirs()
|
||||||
|
@ -35,9 +39,9 @@ impl SystemApps {
|
||||||
.for_each(|entry| {
|
.for_each(|entry| {
|
||||||
let (file_name, mimes) = (entry.file_name, entry.mimes);
|
let (file_name, mimes) = (entry.file_name, entry.mimes);
|
||||||
mimes.into_iter().for_each(|mime| {
|
mimes.into_iter().for_each(|mime| {
|
||||||
map.entry(mime)
|
map.entry(mime).or_default().push_back(
|
||||||
.or_default()
|
Handler::assume_valid(file_name.clone()),
|
||||||
.push(Handler::assume_valid(file_name.clone()));
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -205,17 +205,30 @@ impl MimeApps {
|
||||||
writer.flush()?;
|
writer.flush()?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn print(&self) -> Result<()> {
|
pub fn print(&self, detailed: bool) -> Result<()> {
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
let rows = self
|
let to_rows = |map: &HashMap<Mime, VecDeque<Handler>>| {
|
||||||
.default_apps
|
map.iter()
|
||||||
.iter()
|
|
||||||
.sorted()
|
.sorted()
|
||||||
.map(|(k, v)| vec![k.to_string(), v.iter().join(", ")])
|
.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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,10 @@ use crate::common::{Handler, MimeOrExtension};
|
||||||
#[clap(version = clap::crate_version!())]
|
#[clap(version = clap::crate_version!())]
|
||||||
pub enum Cmd {
|
pub enum Cmd {
|
||||||
/// List default apps and the associated handlers
|
/// List default apps and the associated handlers
|
||||||
List,
|
List {
|
||||||
|
#[clap(long, short)]
|
||||||
|
all: bool,
|
||||||
|
},
|
||||||
|
|
||||||
/// Open a path/URL with its default handler
|
/// Open a path/URL with its default handler
|
||||||
Open {
|
Open {
|
||||||
|
|
|
@ -39,8 +39,8 @@ fn main() -> Result<()> {
|
||||||
let mime = MimeType::try_from(paths[0].as_str())?.0;
|
let mime = MimeType::try_from(paths[0].as_str())?.0;
|
||||||
apps.get_handler(&mime)?.open(paths)?;
|
apps.get_handler(&mime)?.open(paths)?;
|
||||||
}
|
}
|
||||||
Cmd::List => {
|
Cmd::List { all } => {
|
||||||
apps.print()?;
|
apps.print(all)?;
|
||||||
}
|
}
|
||||||
Cmd::Unset { mime } => {
|
Cmd::Unset { mime } => {
|
||||||
apps.remove_handler(&mime.0)?;
|
apps.remove_handler(&mime.0)?;
|
||||||
|
|
Loading…
Add table
Reference in a new issue