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

Create directory for themes #92

Merged
merged 6 commits into from
Jul 5, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ Added:
- Support for RAW command
- Messages from other users containing your nickname are now highlighted using the `info` colour
- Previously sent messages can be accessed per buffer in the text input with up / down arrows
- Themes directory where users can add their own theme files

Changed:

- Default channel in `config.yaml` has been changed to `#halloy` (from `##rust`)
- `palette` field has been deprecated and replaced by `theme` in `config.yaml`
- Sorting channel nicknames
- Title headers has been changed to also display user count for channels

Expand Down
11 changes: 11 additions & 0 deletions assets/themes/ferra.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
name: "Ferra"

palette:
background: "#2b292d"
text: "#fecdb2"
action: "#b1b695"
accent: "#d1d1e0"
alert: "#ffa07a"
error: "#e06b75"
info: "#f5d76e"
success: "#b1b695"
17 changes: 8 additions & 9 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
# Linux: `$HOME`/.config/halloy
# macOS: `$HOME`/Library/Application Support/halloy
# Windows: `{FOLDERID_RoamingAppData}`\halloy
palette:
background: "#2b292d"
text: "#fecdb2"
action: "#b1b695"
accent: "#d1d1e0"
alert: "#ffa07a"
error: "#e06b75"
info: "#f5d76e"
success: "#b1b695"

# Theme
# - Add theme files to the themes directory and fill this with the filename
# without the .yaml extension to select the theme you want
# - Default is Halloy and provided by this application
# - For theme examples, please refer to:
# https://github.com/squidowl/halloy/wiki/Themes
theme: "ferra"

# For more fields under server, please refer to:
# https://docs.rs/irc/0.15.0/irc/client/data/config/struct.Config.html#fields
Expand Down
78 changes: 72 additions & 6 deletions data/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,15 @@ use crate::palette::Palette;
use crate::{buffer, dashboard, environment, server};

const CONFIG_TEMPLATE: &[u8] = include_bytes!("../../config.yaml");
const DEFAULT_THEME: (&str, &[u8]) = ("ferra", include_bytes!("../../assets/themes/ferra.yaml"));

#[derive(Debug, Clone, Default, Deserialize)]
#[derive(Debug, Clone, Default)]
pub struct Config {
#[serde(default)]
pub palette: Palette,
pub servers: server::Map,
#[serde(default)]
pub font: Font,
/// Default settings when creating a new buffer
#[serde(default)]
pub new_buffer: buffer::Settings,
#[serde(default)]
pub dashboard: dashboard::Config,
}

Expand All @@ -42,15 +39,75 @@ impl Config {
dir
}

fn themes_dir() -> PathBuf {
let dir = Self::config_dir().join("themes");

if !dir.exists() {
std::fs::create_dir(dir.as_path())
.expect("expected permissions to create themes folder");
}

dir
}

fn path() -> PathBuf {
Self::config_dir().join("config.yaml")
}

pub fn load() -> Result<Self, Error> {
#[derive(Deserialize)]
pub struct Configuration {
#[serde(default)]
pub theme: String,
pub servers: server::Map,
#[serde(default)]
pub font: Font,
/// Default settings when creating a new buffer
#[serde(default)]
pub new_buffer: buffer::Settings,
#[serde(default)]
pub dashboard: dashboard::Config,
}

let path = Self::path();
let file = File::open(path).map_err(|e| Error::Read(e.to_string()))?;

serde_yaml::from_reader(BufReader::new(file)).map_err(|e| Error::Parse(e.to_string()))
let Configuration {
theme,
servers,
font,
new_buffer,
dashboard,
} = serde_yaml::from_reader(BufReader::new(file))
.map_err(|e| Error::Parse(e.to_string()))?;

// If theme fails to load, use default Palette (Halloy theme)
let palette = Self::load_theme(&theme).unwrap_or_default();

Ok(Config {
palette,
servers,
font,
new_buffer,
dashboard,
})
}

fn load_theme(theme: &str) -> Result<Palette, Error> {
#[derive(Deserialize)]
pub struct Theme {
#[serde(default)]
pub name: String,
#[serde(default)]
pub palette: Palette,
}

let path = Self::themes_dir().join(format!("{theme}.yaml"));
let file = File::open(path).map_err(|e| Error::Read(e.to_string()))?;
let Theme { palette, .. } = serde_yaml::from_reader(BufReader::new(file))
.map_err(|e| Error::Parse(e.to_string()))?;

Ok(palette)
}

pub fn create_template_config() {
Expand All @@ -66,6 +123,15 @@ impl Config {
}
}

pub fn create_themes_dir() {
// Create default theme file.
let (theme, content) = DEFAULT_THEME;
let file = Config::themes_dir().join(format!("{theme}.yaml"));
if !file.exists() {
let _ = fs::write(file, content);
}
}

#[derive(Debug, Error, Clone)]
pub enum Error {
#[error("config could not be read: {0}")]
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ pub fn main() -> iced::Result {
data::environment::formatted_version()
);

// Create themes directory
config::create_themes_dir();

let config_load = Config::load();

// DANGER ZONE - font must be set using config
Expand Down