ZShell settings add

This commit is contained in:
2026-05-12 19:51:47 +02:00
parent 2903819626
commit 075cd42064
5 changed files with 27 additions and 175 deletions
+6 -50
View File
@@ -2,35 +2,20 @@ use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
/// Top-level application configuration.
/// Serialized to/from ~/.config/rs-pictures/config.toml
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
/// Visual effects applied to images.
#[serde(rename = "screenshot")]
pub effects: EffectsConfig,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EffectsConfig {
/// Apply rounded corners to the image.
pub rounded_corners: bool,
/// Radius in pixels for rounded corners.
pub corner_radius: f32,
/// Apply a drop shadow beneath the image.
pub drop_shadow: bool,
/// Blur radius for the drop shadow (higher = softer shadow).
pub shadow_blur_radius: f32,
/// Horizontal offset of the shadow in pixels.
pub shadow_offset_x: f32,
/// Vertical offset of the shadow in pixels.
pub shadow_offset_y: f32,
/// Shadow color as [R, G, B, A] in 0..=255.
pub shadow_color: [u8; 4],
}
@@ -57,52 +42,23 @@ impl Default for Config {
}
impl Config {
/// Returns the path to the config file: ~/.config/rs-pictures/config.toml
pub fn config_path() -> Option<PathBuf> {
let home = std::env::var("HOME").ok()?;
Some(
PathBuf::from(home)
.join(".config")
.join("rs-pictures")
.join("config.toml"),
.join("zshell")
.join("config.json"),
)
}
/// Load config from disk, or write and return the default config if the file doesn't exist.
pub fn load() -> Result<Self> {
let path = match Self::config_path() {
Some(p) => p,
None => return Ok(Self::default()),
};
if !path.exists() {
let config = Self::default();
config.save()?;
return Ok(config);
}
let path = Self::config_path().context("Could not determine HOME directory")?;
let raw = std::fs::read_to_string(&path)
.with_context(|| format!("Failed to read config at {}", path.display()))?;
toml::from_str(&raw)
.with_context(|| format!("Failed to parse config at {}", path.display()))
}
/// Persist the current config to disk.
pub fn save(&self) -> Result<()> {
let path =
Self::config_path().context("Could not determine config directory (HOME not set)")?;
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)
.with_context(|| format!("Failed to create config dir {}", parent.display()))?;
}
let serialized = toml::to_string_pretty(self).context("Failed to serialize config")?;
std::fs::write(&path, serialized)
.with_context(|| format!("Failed to write config to {}", path.display()))?;
Ok(())
serde_json::from_str(&raw)
.with_context(|| format!("Failed to parse JSON config at {}", path.display()))
}
}