mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-09 09:49:10 +02:00
Move custom_pages_dir business logic from RawConfig to Config
This also introduces the path source information for that directory, which was previously unknown.
This commit is contained in:
parent
7338933de5
commit
fb89303e85
2 changed files with 52 additions and 39 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use std::{
|
||||
env, fs,
|
||||
env, fmt, fs,
|
||||
io::{Read, Write},
|
||||
path::PathBuf,
|
||||
path::{Path, PathBuf},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
|
|
@ -162,7 +162,7 @@ impl Default for RawUpdatesConfig {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
|
||||
struct RawDirectoriesConfig {
|
||||
#[serde(default)]
|
||||
pub cache_dir: Option<PathBuf>,
|
||||
|
|
@ -170,20 +170,6 @@ struct RawDirectoriesConfig {
|
|||
pub custom_pages_dir: Option<PathBuf>,
|
||||
}
|
||||
|
||||
impl Default for RawDirectoriesConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
cache_dir: None,
|
||||
custom_pages_dir: get_app_root(AppDataType::UserData, &crate::APP_INFO)
|
||||
.map(|path| {
|
||||
// Note: The `join("")` call ensures that there's a trailing slash
|
||||
path.join("pages").join("")
|
||||
})
|
||||
.ok(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
|
||||
#[serde(default)]
|
||||
struct RawConfig {
|
||||
|
|
@ -246,10 +232,22 @@ pub struct PathWithSource {
|
|||
pub source: PathSource,
|
||||
}
|
||||
|
||||
impl PathWithSource {
|
||||
pub fn path(&self) -> &Path {
|
||||
&self.path
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for PathWithSource {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "{} ({})", self.path.display(), self.source)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct DirectoriesConfig {
|
||||
pub cache_dir: PathWithSource,
|
||||
pub custom_pages_dir: Option<PathBuf>,
|
||||
pub custom_pages_dir: Option<PathWithSource>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
|
|
@ -316,9 +314,27 @@ impl Config {
|
|||
// If everything fails, give up
|
||||
bail!("Could not determine user cache directory");
|
||||
};
|
||||
let custom_pages_dir = raw_config
|
||||
.directories
|
||||
.custom_pages_dir
|
||||
.map(|path| PathWithSource {
|
||||
path,
|
||||
source: PathSource::OsConvention,
|
||||
})
|
||||
.or_else(|| {
|
||||
get_app_root(AppDataType::UserData, &crate::APP_INFO)
|
||||
.map(|path| {
|
||||
// Note: The `join("")` call ensures that there's a trailing slash
|
||||
PathWithSource {
|
||||
path: path.join("pages").join(""),
|
||||
source: PathSource::ConfigFile,
|
||||
}
|
||||
})
|
||||
.ok()
|
||||
});
|
||||
let directories = DirectoriesConfig {
|
||||
cache_dir,
|
||||
custom_pages_dir: raw_config.directories.custom_pages_dir,
|
||||
custom_pages_dir,
|
||||
};
|
||||
|
||||
Ok(Self {
|
||||
|
|
|
|||
37
src/main.rs
37
src/main.rs
|
|
@ -43,7 +43,7 @@ mod utils;
|
|||
use crate::{
|
||||
cache::{Cache, CacheFreshness, PageLookupResult, TLDR_PAGES_DIR},
|
||||
cli::Args,
|
||||
config::{get_config_dir, get_config_path, make_default_config, Config},
|
||||
config::{get_config_dir, get_config_path, make_default_config, Config, PathWithSource},
|
||||
extensions::Dedup,
|
||||
output::print_page,
|
||||
types::{ColorOptions, PlatformType},
|
||||
|
|
@ -161,24 +161,17 @@ fn show_paths(config: &Config) {
|
|||
|e| format!("[Error: {}]", e),
|
||||
|(path, _)| path.display().to_string(),
|
||||
);
|
||||
let cache_dir = format!(
|
||||
"{} ({})",
|
||||
config.directories.cache_dir.path.display(),
|
||||
config.directories.cache_dir.source
|
||||
);
|
||||
let cache_dir = config.directories.cache_dir.to_string();
|
||||
let pages_dir = {
|
||||
let mut path = config.directories.cache_dir.path.clone();
|
||||
path.push(TLDR_PAGES_DIR);
|
||||
path.push(""); // Trailing path separator
|
||||
path.display().to_string()
|
||||
};
|
||||
let custom_pages_dir = config.directories.custom_pages_dir.as_deref().map_or_else(
|
||||
|| "[None]".to_string(),
|
||||
|path| {
|
||||
path.to_str()
|
||||
.map_or_else(|| "[Invalid]".to_string(), ToString::to_string)
|
||||
},
|
||||
);
|
||||
let custom_pages_dir = match config.directories.custom_pages_dir {
|
||||
Some(ref path_with_source) => path_with_source.to_string(),
|
||||
None => "[None]".to_string(),
|
||||
};
|
||||
println!("Config dir: {}", config_dir);
|
||||
println!("Config path: {}", config_path);
|
||||
println!("Cache dir: {}", cache_dir);
|
||||
|
|
@ -361,12 +354,12 @@ fn main() {
|
|||
|
||||
// List cached commands and exit
|
||||
if args.list {
|
||||
println!(
|
||||
"{}",
|
||||
cache
|
||||
.list_pages(config.directories.custom_pages_dir.as_deref())
|
||||
.join("\n")
|
||||
);
|
||||
let custom_pages_dir = config
|
||||
.directories
|
||||
.custom_pages_dir
|
||||
.as_ref()
|
||||
.map(PathWithSource::path);
|
||||
println!("{}", cache.list_pages(custom_pages_dir).join("\n"));
|
||||
process::exit(0);
|
||||
}
|
||||
|
||||
|
|
@ -386,7 +379,11 @@ fn main() {
|
|||
if let Some(lookup_result) = cache.find_page(
|
||||
&command,
|
||||
&languages,
|
||||
config.directories.custom_pages_dir.as_deref(),
|
||||
config
|
||||
.directories
|
||||
.custom_pages_dir
|
||||
.as_ref()
|
||||
.map(PathWithSource::path),
|
||||
) {
|
||||
if let Err(ref e) =
|
||||
print_page(&lookup_result, args.raw, enable_styles, args.pager, &config)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue