Skip to content

Commit

Permalink
feat(custom_layout): add yaml file support
Browse files Browse the repository at this point in the history
This commit adds support for loading custom layouts from yaml files, and
also moves the custom layout loading and validating logic into the
komorebi-core crate.

re #50
  • Loading branch information
LGUG2Z committed Oct 21, 2021
1 parent 5d6351f commit 6981d77
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 14 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions komorebi-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ clap = "3.0.0-beta.4"
color-eyre = "0.5"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
serde_yaml = "0.8"
strum = { version = "0.21", features = ["derive"] }
27 changes: 27 additions & 0 deletions komorebi-core/src/custom_layout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::ops::Deref;
use std::path::PathBuf;

use color_eyre::eyre::anyhow;
use color_eyre::Result;
use serde::Deserialize;
use serde::Serialize;

Expand All @@ -18,6 +23,28 @@ impl Deref for CustomLayout {
}

impl CustomLayout {
pub fn from_path_buf(path: PathBuf) -> Result<Self> {
let invalid_filetype = anyhow!("custom layouts must be json or yaml files");
let layout: Self = match path.extension() {
Some(extension) => {
if extension == "yaml" || extension == "yml" {
serde_yaml::from_reader(BufReader::new(File::open(path)?))?
} else if extension == "json" {
serde_json::from_reader(BufReader::new(File::open(path)?))?
} else {
return Err(invalid_filetype);
}
}
None => return Err(invalid_filetype),
};

if !layout.is_valid() {
return Err(anyhow!("the layout file provided was invalid"));
}

Ok(layout)
}

#[must_use]
pub fn column_with_idx(&self, idx: usize) -> (usize, Option<&Column>) {
let column_idx = self.column_for_container_idx(idx);
Expand Down
15 changes: 2 additions & 13 deletions komorebi/src/window_manager.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::VecDeque;
use std::fs::File;
use std::io::BufReader;
use std::io::ErrorKind;
use std::num::NonZeroUsize;
use std::path::PathBuf;
Expand Down Expand Up @@ -1059,11 +1057,8 @@ impl WindowManager {
#[tracing::instrument(skip(self))]
pub fn change_workspace_custom_layout(&mut self, path: PathBuf) -> Result<()> {
tracing::info!("changing layout");
let layout: CustomLayout = serde_json::from_reader(BufReader::new(File::open(path)?))?;
if !layout.is_valid() {
return Err(anyhow!("the layout file provided was invalid"));
}

let layout = CustomLayout::from_path_buf(path)?;
let workspace = self.focused_workspace_mut()?;

match workspace.layout() {
Expand Down Expand Up @@ -1182,13 +1177,7 @@ impl WindowManager {
path: PathBuf,
) -> Result<()> {
tracing::info!("setting workspace layout");
let file = File::open(path)?;
let reader = BufReader::new(file);
let layout: CustomLayout = serde_json::from_reader(reader)?;
if !layout.is_valid() {
return Err(anyhow!("the layout file provided was invalid"));
}

let layout = CustomLayout::from_path_buf(path)?;
let invisible_borders = self.invisible_borders;
let offset = self.work_area_offset;
let focused_monitor_idx = self.focused_monitor_idx();
Expand Down
2 changes: 1 addition & 1 deletion komorebic/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ struct Load {

#[derive(Clap, AhkFunction)]
struct LoadLayout {
/// File from which the custom layout definition should be loaded
/// JSON or YAML file from which the custom layout definition should be loaded
path: String,
}

Expand Down

0 comments on commit 6981d77

Please sign in to comment.