Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: readable toml parsing error #854

Merged
merged 2 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions yazi-config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ pub static INPUT: RoCell<popup::Input> = RoCell::new();
pub static SELECT: RoCell<popup::Select> = RoCell::new();
pub static WHICH: RoCell<which::Which> = RoCell::new();

pub fn init() {
pub fn init() -> anyhow::Result<()> {
let config_dir = Xdg::config_dir();
MERGED_YAZI.init(Preset::yazi(&config_dir));
MERGED_KEYMAP.init(Preset::keymap(&config_dir));
MERGED_THEME.init(Preset::theme(&config_dir));
MERGED_YAZI.init(Preset::yazi(&config_dir)?);
MERGED_KEYMAP.init(Preset::keymap(&config_dir)?);
MERGED_THEME.init(Preset::theme(&config_dir)?);

LAYOUT.with(Default::default);

Expand Down Expand Up @@ -81,4 +81,5 @@ Add `disable_exec_warn = true` to your `yazi.toml` under `[headsup]` to suppress
"#
);
}
Ok(())
}
35 changes: 17 additions & 18 deletions yazi-config/src/preset.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
use std::{mem, path::{Path, PathBuf}};

use anyhow::Context;
use anyhow::{anyhow, Context, Result};
use toml::{Table, Value};

use crate::theme::Flavor;

pub(crate) struct Preset;

impl Preset {
pub(crate) fn yazi(p: &Path) -> String {
pub(crate) fn yazi(p: &Path) -> Result<String> {
Self::merge_path(p.join("yazi.toml"), include_str!("../preset/yazi.toml"))
}

pub(crate) fn keymap(p: &Path) -> String {
pub(crate) fn keymap(p: &Path) -> Result<String> {
Self::merge_path(p.join("keymap.toml"), include_str!("../preset/keymap.toml"))
}

pub(crate) fn theme(p: &Path) -> String {
pub(crate) fn theme(p: &Path) -> Result<String> {
let Ok(user) = std::fs::read_to_string(p.join("theme.toml")) else {
return include_str!("../preset/theme.toml").to_owned();
return Ok(include_str!("../preset/theme.toml").to_owned());
};
let Some(use_) = Flavor::parse_use(&user) else {
return Self::merge_str(&user, include_str!("../preset/theme.toml"));
};

let p = p.join(format!("flavors/{}.yazi/flavor.toml", use_));
let flavor = std::fs::read_to_string(&p)
.with_context(|| format!("Failed to load flavor {:?}", p))
.unwrap();
let p = p.join(format!("flavors/{use_}.yazi/flavor.toml"));
let flavor =
std::fs::read_to_string(&p).with_context(|| anyhow!("Failed to load flavor {p:?}"))?;

Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml")))
Self::merge_str(&user, &Self::merge_str(&flavor, include_str!("../preset/theme.toml"))?)
}

#[inline]
Expand All @@ -38,21 +37,21 @@ impl Preset {
}

#[inline]
pub(crate) fn merge_str(user: &str, base: &str) -> String {
let mut t = user.parse().unwrap();
Self::merge(&mut t, base.parse().unwrap(), 2);
pub(crate) fn merge_str(user: &str, base: &str) -> Result<String> {
let mut t = user.parse()?;
Self::merge(&mut t, base.parse()?, 2);

t.to_string()
Ok(t.to_string())
}

#[inline]
fn merge_path(user: PathBuf, base: &str) -> String {
let s = std::fs::read_to_string(user).unwrap_or_default();
fn merge_path(user: PathBuf, base: &str) -> Result<String> {
let s = std::fs::read_to_string(&user).unwrap_or_default();
if s.is_empty() {
return base.to_string();
return Ok(base.to_string());
}

Self::merge_str(&s, base)
Self::merge_str(&s, base).with_context(|| anyhow!("Loading {user:?}"))
}

fn merge(a: &mut Table, b: Table, max: u8) {
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async fn main() -> anyhow::Result<()> {

_ = fdlimit::raise_fd_limit();

yazi_config::init();
yazi_config::init()?;

yazi_adaptor::init();

Expand Down