Skip to content

Commit

Permalink
Add command for printing embedded configs (#487)
Browse files Browse the repository at this point in the history
* Add command for printing embedded configs

* Typo

* Use doccomments instead of procmacro

* Typo
  • Loading branch information
sushi-shi authored Feb 13, 2024
1 parent 203bd89 commit 96418c0
Show file tree
Hide file tree
Showing 18 changed files with 185 additions and 83 deletions.
2 changes: 1 addition & 1 deletion config/joshuto.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ show_icons = true
# none, absolute, relative
line_number_style = "none"

# size, mtime, user, gourp, perm. can be combined with |.
# size, mtime, user, gourp, perm. can be combined with |.
# `none` to disable, `all` to enable all
# all and none can't be combined with other options
linemode = "size"
Expand Down
2 changes: 1 addition & 1 deletion config/theme.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ bold = true
##########################################
## File list - Override style by extension
##########################################
# This sections allows to override the basic
# This sections allows to override the basic
# style with a specific style for the file's
# extension.

Expand Down
13 changes: 7 additions & 6 deletions src/commands/bookmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ratatui::widgets::Clear;
use termion::event::Event;

use crate::config::raw::bookmarks::{BookmarkRaw, BookmarksRaw};
use crate::config::search_directories;
use crate::config::{search_directories, ConfigType};
use crate::context::AppContext;
use crate::error::AppResult;
use crate::event::{process_event, AppEvent};
Expand All @@ -17,7 +17,7 @@ use crate::ui::widgets::TuiMenu;
use crate::ui::AppBackend;
use crate::util::unix;

use crate::{BOOKMARKS_FILE, BOOKMARKS_T, CONFIG_HIERARCHY};
use crate::{BOOKMARKS_T, CONFIG_HIERARCHY};

use super::change_directory::change_directory;

Expand All @@ -33,10 +33,11 @@ fn find_bookmark_file() -> Option<path::PathBuf> {
pub fn add_bookmark(context: &mut AppContext, backend: &mut AppBackend) -> AppResult {
let cwd = std::env::current_dir()?;

let bookmark_path = match search_directories(BOOKMARKS_FILE, &CONFIG_HIERARCHY) {
Some(file_path) => Some(file_path),
None => find_bookmark_file(),
};
let bookmark_path =
match search_directories(ConfigType::Bookmarks.as_filename(), &CONFIG_HIERARCHY) {
Some(file_path) => Some(file_path),
None => find_bookmark_file(),
};

if let Some(bookmark_path) = bookmark_path {
let key = poll_for_bookmark_key(context, backend);
Expand Down
9 changes: 5 additions & 4 deletions src/config/clean/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use std::collections::HashMap;

use crate::{
config::{
parse_config_or_default,
raw::app::{AppConfigRaw, CustomCommand},
TomlConfigFile,
ConfigType, TomlConfigFile,
},
error::AppResult,
};
Expand Down Expand Up @@ -71,8 +70,10 @@ impl std::default::Default for AppConfig {
}

impl TomlConfigFile for AppConfig {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<AppConfigRaw, AppConfig>(file_name)
type Raw = AppConfigRaw;

fn get_type() -> ConfigType {
ConfigType::App
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/clean/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod tab;
pub use config::*;

#[cfg(not(target_os = "windows"))]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/joshuto.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/joshuto.toml");

#[cfg(target_os = "windows")]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\joshuto.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\joshuto.toml");
10 changes: 5 additions & 5 deletions src/config/clean/bookmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ use termion::event::Event;
use std::collections::HashMap;

use crate::config::raw::bookmarks::BookmarksRaw;
use crate::config::TomlConfigFile;
use crate::config::{ConfigType, TomlConfigFile};
use crate::util::keyparse;

use crate::config::parse_config_or_default;

pub type Bookmarks = HashMap<Event, String>;

impl TomlConfigFile for Bookmarks {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<BookmarksRaw, Bookmarks>(file_name)
type Raw = BookmarksRaw;

fn get_type() -> ConfigType {
ConfigType::Bookmarks
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/config/clean/icon/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::{
config::{parse_config_or_default, raw::icon::IconsRaw, TomlConfigFile},
config::{raw::icon::IconsRaw, ConfigType, TomlConfigFile},
error::AppResult,
};

Expand Down Expand Up @@ -30,8 +30,10 @@ impl std::default::Default for Icons {
}

impl TomlConfigFile for Icons {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<IconsRaw, Icons>(file_name)
type Raw = IconsRaw;

fn get_type() -> ConfigType {
ConfigType::Icons
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/config/clean/icon/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod config;

pub use config::*;

const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/icons.toml");
#[cfg(not(target_os = "windows"))]
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/icons.toml");

#[cfg(target_os = "windows")]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\icons.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\icons.toml");
8 changes: 5 additions & 3 deletions src/config/clean/keymap/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::str::FromStr;
use termion::event::Event;

use crate::config::raw::keymap::{AppKeyMappingRaw, CommandKeymap};
use crate::config::{parse_config_or_default, TomlConfigFile};
use crate::config::{ConfigType, TomlConfigFile};
use crate::error::AppResult;
use crate::key_command::{Command, CommandKeybind};
use crate::traits::ToString;
Expand Down Expand Up @@ -113,8 +113,10 @@ impl From<AppKeyMappingRaw> for AppKeyMapping {
}

impl TomlConfigFile for AppKeyMapping {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<AppKeyMappingRaw, AppKeyMapping>(file_name)
type Raw = AppKeyMappingRaw;

fn get_type() -> ConfigType {
ConfigType::Keymap
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/clean/keymap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod config;
pub use self::config::*;

#[cfg(not(target_os = "windows"))]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/keymap.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/keymap.toml");

#[cfg(target_os = "windows")]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\keymap.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\keymap.toml");
10 changes: 5 additions & 5 deletions src/config/clean/mimetype/config.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::collections::HashMap;

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

use super::{ExtensionAppList, MimetypeAppList};

Expand Down Expand Up @@ -69,7 +67,9 @@ impl From<AppProgramRegistryRaw> for AppProgramRegistry {
}

impl TomlConfigFile for AppProgramRegistry {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<AppProgramRegistryRaw, AppProgramRegistry>(file_name)
type Raw = AppProgramRegistryRaw;

fn get_type() -> ConfigType {
ConfigType::Mimetype
}
}
8 changes: 5 additions & 3 deletions src/config/clean/preview/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use serde::Deserialize;

use crate::config::{parse_config_or_default, raw::preview::FileEntryPreviewRaw, TomlConfigFile};
use crate::config::{raw::preview::FileEntryPreviewRaw, ConfigType, TomlConfigFile};

#[derive(Debug, Deserialize)]
pub struct FileEntryPreviewEntry {
Expand All @@ -17,8 +17,10 @@ pub struct FileEntryPreview {
}

impl TomlConfigFile for FileEntryPreview {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<FileEntryPreviewRaw, FileEntryPreview>(file_name)
type Raw = FileEntryPreviewRaw;

fn get_type() -> ConfigType {
ConfigType::Preview
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/config/clean/theme/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashMap;

use crate::config::raw::theme::AppThemeRaw;
use crate::config::{parse_config_or_default, TomlConfigFile};
use crate::config::{ConfigType, TomlConfigFile};
use crate::error::AppResult;

use super::style::AppStyle;
Expand Down Expand Up @@ -30,8 +30,10 @@ impl AppTheme {
}

impl TomlConfigFile for AppTheme {
fn get_config(file_name: &str) -> Self {
parse_config_or_default::<AppThemeRaw, AppTheme>(file_name)
type Raw = AppThemeRaw;

fn get_type() -> ConfigType {
ConfigType::Theme
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/config/clean/theme/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod tab;
pub use config::*;

#[cfg(not(target_os = "windows"))]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/theme.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("../../../../config/theme.toml");

#[cfg(target_os = "windows")]
const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\theme.toml");
pub const DEFAULT_CONFIG_FILE_PATH: &str = include_str!("..\\..\\..\\..\\config\\theme.toml");
75 changes: 75 additions & 0 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#[derive(Copy, Clone, Debug)]
pub enum ConfigType {
App,
Mimetype,
Keymap,
Theme,
Preview,
Bookmarks,
Icons,
}

impl std::fmt::Display for ConfigType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}

impl clap::ValueEnum for ConfigType {
fn value_variants<'a>() -> &'a [Self] {
Self::enumerate()
}

fn to_possible_value(&self) -> Option<clap::builder::PossibleValue> {
Some(clap::builder::PossibleValue::new(self.as_str()))
}
}

impl ConfigType {
pub const fn enumerate() -> &'static [Self] {
&[
Self::App,
Self::Mimetype,
Self::Keymap,
Self::Theme,
Self::Preview,
Self::Bookmarks,
Self::Icons,
]
}

pub const fn as_str(&self) -> &'static str {
match self {
Self::App => "joshuto",
Self::Mimetype => "mimetype",
Self::Keymap => "keymap",
Self::Theme => "theme",
Self::Preview => "preview",
Self::Bookmarks => "bookmarks",
Self::Icons => "icons",
}
}

pub const fn as_filename(&self) -> &'static str {
match self {
Self::App => "joshuto.toml",
Self::Mimetype => "mimetype.toml",
Self::Keymap => "keymap.toml",
Self::Theme => "theme.toml",
Self::Preview => "preview.toml",
Self::Bookmarks => "bookmarks.toml",
Self::Icons => "icons.toml",
}
}

pub const fn embedded_config(&self) -> Option<&'static str> {
use super::clean;
match self {
Self::App => Some(clean::app::DEFAULT_CONFIG_FILE_PATH),
Self::Keymap => Some(clean::keymap::DEFAULT_CONFIG_FILE_PATH),
Self::Theme => Some(clean::theme::DEFAULT_CONFIG_FILE_PATH),
Self::Icons => Some(clean::icon::DEFAULT_CONFIG_FILE_PATH),
Self::Mimetype | Self::Preview | Self::Bookmarks => None,
}
}
}
33 changes: 19 additions & 14 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
pub mod clean;
pub mod raw;

pub mod config_type;
pub use config_type::ConfigType;

use serde::de::DeserializeOwned;
use std::fs;
use std::path::{Path, PathBuf};

use crate::error::AppResult;
use crate::CONFIG_HIERARCHY;

pub trait TomlConfigFile {
fn get_config(file_name: &str) -> Self;
pub trait TomlConfigFile: Sized + Default {
type Raw: Into<Self> + DeserializeOwned;

fn get_type() -> config_type::ConfigType;

fn get_config() -> Self {
parse_config_or_default::<Self::Raw, Self>(Self::get_type().as_filename())
}
}

// searches a list of folders for a given file in order of preference
pub fn search_directories<P>(file_name: &str, directories: &[P]) -> Option<PathBuf>
where
P: AsRef<Path>,
{
for path in directories.iter() {
let file_path = path.as_ref().join(file_name);
if file_path.exists() {
return Some(file_path);
}
}
None
directories
.iter()
.map(|path| path.as_ref().join(file_name))
.find(|path| path.exists())
}

pub fn search_config_directories(file_name: &str) -> Option<PathBuf> {
Expand All @@ -32,18 +38,17 @@ pub fn search_config_directories(file_name: &str) -> Option<PathBuf> {

fn parse_file_to_config<T, S>(file_path: &Path) -> AppResult<S>
where
T: DeserializeOwned,
S: From<T>,
T: DeserializeOwned + Into<S>,
{
let file_contents = fs::read_to_string(file_path)?;
let config = toml::from_str::<T>(&file_contents)?;
Ok(S::from(config))
Ok(config.into())
}

pub fn parse_config_or_default<T, S>(file_name: &str) -> S
where
T: DeserializeOwned,
S: From<T> + std::default::Default,
T: DeserializeOwned + Into<S>,
S: std::default::Default,
{
match search_config_directories(file_name) {
Some(file_path) => match parse_file_to_config::<T, S>(&file_path) {
Expand Down
Loading

0 comments on commit 96418c0

Please sign in to comment.