diff options
author | qkzk <qu3nt1n@gmail.com> | 2023-03-21 07:36:32 +0100 |
---|---|---|
committer | qkzk <qu3nt1n@gmail.com> | 2023-03-21 07:36:32 +0100 |
commit | 84d8e706728d0e3f62c074ed55a732fc1c224ca7 (patch) | |
tree | 1f61c45e888d9174bcde1a028351f0f4b398e0f7 /src | |
parent | 8ff03cff581f7f05015cd3567cf9814e4b87dec1 (diff) |
include opener in help
Diffstat (limited to 'src')
-rw-r--r-- | src/help.rs | 21 | ||||
-rw-r--r-- | src/main.rs | 18 | ||||
-rw-r--r-- | src/opener.rs | 30 | ||||
-rw-r--r-- | src/status.rs | 12 |
4 files changed, 58 insertions, 23 deletions
diff --git a/src/help.rs b/src/help.rs index d2e8e39..6aabcf3 100644 --- a/src/help.rs +++ b/src/help.rs @@ -2,6 +2,7 @@ use anyhow::Result; use strfmt::strfmt; use crate::keybindings::Bindings; +use crate::opener::Opener; /// Help message to be displayed when help key is pressed. /// Default help key is `'h'`. @@ -27,7 +28,17 @@ static HELP_TO_FORMAT: &str = " {ToggleDisplayFull}: toggle metadata on files {ToggleHidden}: toggle hidden {Shell}: shell in current directory -{OpenFile}: open the selected file +{OpenFile}: open the selected file with : + - default {Default} + - audio {Audio} + - images {Bitmap} + - office {Office} + - pdf, ebooks {Readable} + - text {Text} + - video {Video} + - vectorials {Vectorial} + - compressed files are decompressed + - iso images are mounted {NvimFilepicker}: open in current nvim session {NvimSetAddress}: setup the nvim rpc address {Preview}: preview this file @@ -114,8 +125,12 @@ impl Help { /// Creates an Help instance from keybindings. /// If multiple keybindings are bound to the same action, the last one /// is displayed. - pub fn from_keybindings(binds: &Bindings) -> Result<Self> { - let help = strfmt(HELP_TO_FORMAT, &binds.keybind_reversed())?; + pub fn from_keybindings(binds: &Bindings, opener: &Opener) -> Result<Self> { + let mut strings = binds.keybind_reversed(); + let openers = opener.opener_association.as_map_of_strings(); + log::info!("{openers:?}"); + strings.extend(openers); + let help = strfmt(HELP_TO_FORMAT, &strings)?; Ok(Self { help }) } } diff --git a/src/main.rs b/src/main.rs index 1ca67fc..f6ec13a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,11 +2,12 @@ use std::sync::Arc; use anyhow::Result; use clap::Parser; +use fm::opener::{load_opener, Opener}; use log::info; use fm::args::Args; use fm::config::load_config; -use fm::constant_strings_paths::CONFIG_PATH; +use fm::constant_strings_paths::{CONFIG_PATH, OPENER_PATH}; use fm::event_dispatch::EventDispatcher; use fm::help::Help; use fm::log::set_loggers; @@ -32,15 +33,14 @@ fn main() -> Result<()> { let term = Arc::new(init_term()?); let event_dispatcher = EventDispatcher::new(config.binds.clone()); let event_reader = EventReader::new(term.clone()); - let help = Help::from_keybindings(&config.binds)?.help; + let opener = load_opener(OPENER_PATH, &config.terminal).unwrap_or_else(|_| { + eprintln!("Couldn't read the opener config file at {OPENER_PATH}. See https://raw.githubusercontent.com/qkzk/fm/master/config_files/fm/opener.yaml for an example. Using default."); + info!("Couldn't read opener file at {OPENER_PATH}. Using default."); + Opener::new(&config.terminal) + }); + let help = Help::from_keybindings(&config.binds, &opener)?.help; let mut display = Display::new(term.clone()); - let mut status = Status::new( - Args::parse(), - display.height()?, - term.clone(), - help, - &config.terminal, - )?; + let mut status = Status::new(Args::parse(), display.height()?, term.clone(), help, opener)?; let colors = config.colors.clone(); drop(config); diff --git a/src/opener.rs b/src/opener.rs index 1dc02a0..cc6967d 100644 --- a/src/opener.rs +++ b/src/opener.rs @@ -6,6 +6,7 @@ use std::process::{Command, Stdio}; use anyhow::{anyhow, Context, Result}; use log::info; use serde_yaml; +use strum_macros::{Display, EnumIter, EnumString}; use crate::constant_strings_paths::{ DEFAULT_AUDIO_OPENER, DEFAULT_IMAGE_OPENER, DEFAULT_OFFICE_OPENER, DEFAULT_OPENER, @@ -31,8 +32,9 @@ where } /// Different kind of extensions for default openers. -#[derive(Clone, Hash, Eq, PartialEq, Debug)] +#[derive(Clone, Hash, Eq, PartialEq, Debug, Display, Default, EnumString, EnumIter)] pub enum ExtensionKind { + #[default] Audio, Bitmap, Office, @@ -153,6 +155,21 @@ impl OpenerAssociation { ]), } } + + pub fn as_map_of_strings(&self) -> std::collections::HashMap<String, String> { + let mut associations: std::collections::HashMap<String, String> = self + .association + .iter() + .map(|(k, v)| (k.to_string(), v.to_string())) + .collect(); + + for s in EnumIter::enum_iter!(ExtensionKind) { + if !associations.contains_key(&s) { + associations.insert(s, "".to_owned()); + } + } + associations + } } macro_rules! open_file_with { @@ -196,8 +213,9 @@ impl OpenerAssociation { /// Some kind of files are "opened" using internal methods. /// ATM only one kind of files is supported, compressed ones, which use /// libarchive internally. -#[derive(Clone, Hash, PartialEq, Eq, Debug)] +#[derive(Clone, Hash, PartialEq, Eq, Debug, Default)] pub enum InternalVariant { + #[default] DecompressZip, DecompressXz, DecompressGz, @@ -245,6 +263,14 @@ impl OpenerInfo { yaml.get("use_term")?.as_bool()?, ))) } + + fn to_string(&self) -> String { + if let Some(external) = &self.external_program { + external.to_owned() + } else { + "".to_owned() + } + } } /// Holds the associations between different kind of files and opener method diff --git a/src/status.rs b/src/status.rs index e30aa8b..9694620 100644 --- a/src/status.rs +++ b/src/status.rs @@ -17,14 +17,13 @@ use crate::args::Args; use crate::bulkrename::Bulk; use crate::compress::Compresser; use crate::config::Colors; -use crate::constant_strings_paths::{OPENER_PATH, TUIS_PATH}; +use crate::constant_strings_paths::TUIS_PATH; use crate::copy_move::{copy_move, CopyMove}; use crate::cryptsetup::CryptoDeviceOpener; use crate::flagged::Flagged; use crate::iso::IsoMounter; -// use crate::keybindings::to_keyname; use crate::marks::Marks; -use crate::opener::{load_opener, Opener}; +use crate::opener::Opener; use crate::preview::{Directory, Preview}; use crate::shell_menu::{load_shell_menu, ShellMenu}; use crate::skim::Skimer; @@ -94,13 +93,8 @@ impl Status { height: usize, term: Arc<Term>, help: String, - terminal: &str, + opener: Opener, ) -> Result<Self> { - let opener = load_opener(OPENER_PATH, terminal).unwrap_or_else(|_| { - eprintln!("Couldn't read the opener config file at {OPENER_PATH}. See https://raw.githubusercontent.com/qkzk/fm/master/config_files/fm/opener.yaml for an example. Using default."); - info!("Couldn't read opener file at {OPENER_PATH}. Using default."); - Opener::new(terminal) - }); let Ok(shell_menu) = load_shell_menu(TUIS_PATH) else { eprintln!("Couldn't load the TUIs config file at {TUIS_PATH}. See https://raw.githubusercontent.com/qkzk/fm/master/config_files/fm/tuis.yaml for an example"); info!("Couldn't read tuis file at {TUIS_PATH}. Exiting"); |