Skip to content

Commit

Permalink
feat: add case_sensitive_ext option for mimetype, theme and icons
Browse files Browse the repository at this point in the history
  • Loading branch information
Akmadan23 committed Feb 22, 2024
1 parent f6d1f71 commit b9e6954
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/joshuto.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use_trash = true
watch_files = true
xdg_open = false
xdg_open_fork = false
case_sensitive_ext = false

custom_commands = []

Expand Down
4 changes: 4 additions & 0 deletions docs/configuration/joshuto.toml.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ xdg_open = false
# Fork xdg_open so you can continue using joshuto with application open
xdg_open_fork = false

# If true, all file extensions checks will be case sensitive.
# Applies to `[extension]` in `mimetype.toml` and `[ext]` in `theme.toml` and `icons.toml`
case_sensitive_ext = false

# Use system trash can instead of permanently removing files
use_trash = true

Expand Down
2 changes: 2 additions & 0 deletions src/config/clean/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct AppConfig {
pub use_trash: bool,
pub xdg_open: bool,
pub xdg_open_fork: bool,
pub case_sensitive_ext: bool,
pub watch_files: bool,
pub custom_commands: Vec<CustomCommand>,
pub focus_on_create: bool,
Expand Down Expand Up @@ -84,6 +85,7 @@ impl From<AppConfigRaw> for AppConfig {
use_trash: raw.use_trash,
xdg_open: raw.xdg_open,
xdg_open_fork: raw.xdg_open_fork,
case_sensitive_ext: raw.case_sensitive_ext,
watch_files: raw.watch_files,
cmd_aliases: raw.cmd_aliases,
focus_on_create: raw.focus_on_create,
Expand Down
7 changes: 6 additions & 1 deletion src/config/clean/mimetype/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use crate::config::{raw::mimetype::AppProgramRegistryRaw, ConfigType, TomlConfigFile};
use crate::CONFIG_T;

use super::{ExtensionAppList, MimetypeAppList};

Expand All @@ -16,7 +17,11 @@ pub struct AppProgramRegistry {

impl AppProgramRegistry {
pub fn app_list_for_ext(&self, extension: &str) -> Option<&ExtensionAppList> {
self._extension.get(extension)
if CONFIG_T.case_sensitive_ext {
self._extension.get(extension)
} else {
self._extension.get(&extension.to_lowercase())
}
}

pub fn app_list_for_mimetype(&self, mimetype: &str) -> Option<&MimetypeAppList> {
Expand Down
2 changes: 2 additions & 0 deletions src/config/raw/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub struct AppConfigRaw {
#[serde(default)]
pub xdg_open: bool,
#[serde(default)]
pub case_sensitive_ext: bool,
#[serde(default)]
pub xdg_open_fork: bool,
#[serde(default = "default_true")]
pub watch_files: bool,
Expand Down
16 changes: 10 additions & 6 deletions src/fs/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};

#[cfg(feature = "devicons")]
use crate::ICONS_T;
use crate::{CONFIG_T, ICONS_T};

#[derive(Clone, Debug)]
pub struct JoshutoDirEntry {
Expand Down Expand Up @@ -155,11 +155,15 @@ fn create_icon_label(name: &str, metadata: &JoshutoMetadata) -> String {
.get(name)
.cloned()
.unwrap_or(match name.rsplit_once('.') {
Some((_, ext)) => ICONS_T
.ext
.get(ext)
.unwrap_or(&ICONS_T.default_file)
.to_string(),
Some((_, ext)) => {
let icon = if CONFIG_T.case_sensitive_ext {
ICONS_T.ext.get(ext)
} else {
ICONS_T.ext.get(&ext.to_lowercase())
};

icon.unwrap_or(&ICONS_T.default_file).to_string()
}
None => ICONS_T.default_file.clone(),
}),
};
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ lazy_static! {

config_dirs
};

static ref CONFIG_T: AppConfig = AppConfig::get_config();
static ref THEME_T: AppTheme = AppTheme::get_config();
static ref MIMETYPE_T: AppProgramRegistry = AppProgramRegistry::get_config();
static ref PREVIEW_T: FileEntryPreview = FileEntryPreview::get_config();
Expand Down Expand Up @@ -157,6 +159,7 @@ fn run_main(args: Args) -> Result<i32, AppError> {
// make sure all configs have been loaded before starting
let config = AppConfig::get_config();
let keymap = AppKeyMapping::get_config();
lazy_static::initialize(&CONFIG_T);
lazy_static::initialize(&THEME_T);
lazy_static::initialize(&MIMETYPE_T);
lazy_static::initialize(&PREVIEW_T);
Expand Down
10 changes: 8 additions & 2 deletions src/util/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::path::Path;
use crate::fs::{FileType, JoshutoDirEntry, LinkType};
use crate::util::unix;

use crate::THEME_T;
use crate::{CONFIG_T, THEME_T};

/// Allows patching a ratatui style if there is `Some` style, otherwise returns a clone.
pub trait PathStyleIfSome {
Expand Down Expand Up @@ -109,7 +109,13 @@ fn file_style(entry: &JoshutoDirEntry) -> Style {
.file_path()
.extension()
.and_then(|s| s.to_str())
.and_then(|s| THEME_T.ext.get(s))
.and_then(|s| {
if CONFIG_T.case_sensitive_ext {
THEME_T.ext.get(s)
} else {
THEME_T.ext.get(&s.to_lowercase())
}
})
.map(|theme| {
Style::default()
.fg(theme.fg)
Expand Down

0 comments on commit b9e6954

Please sign in to comment.