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

Feature/listeners #44

Merged
merged 5 commits into from
Jul 19, 2022
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
45 changes: 45 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ parking_lot = "0.12"
crossbeam-channel = "0.5"
once_cell = "1"
kanata-keyberon = "0.2.3"
serde = { version = "1", features = ["derive"] }
serde_json = "1"

[target.'cfg(target_os = "linux")'.dependencies]
evdev-rs = "0.4.0"
Expand Down
48 changes: 40 additions & 8 deletions src/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ pub type KanataLayout = Layout<256, 1, ACTUAL_NUM_LAYERS, CustomAction>;
pub struct Cfg {
pub mapped_keys: MappedKeys,
pub key_outputs: KeyOutputs,
pub layer_strings: Vec<String>,
pub layer_info: Vec<LayerInfo>,
pub items: HashMap<String, String>,
pub layout: KanataLayout,
}

impl Cfg {
pub fn new_from_file(p: &std::path::Path) -> Result<Self> {
let (items, mapped_keys, layer_strings, key_outputs, layout) = parse_cfg(p)?;
let (items, mapped_keys, layer_info, key_outputs, layout) = parse_cfg(p)?;
Ok(Self {
items,
mapped_keys,
layer_strings,
layer_info,
key_outputs,
layout,
})
Expand Down Expand Up @@ -172,22 +172,28 @@ fn disallow_nested_tap_hold() {
}
}

#[derive(Debug)]
pub struct LayerInfo {
pub name: String,
pub cfg_text: String,
}

#[allow(clippy::type_complexity)] // return type is not pub
fn parse_cfg(
p: &std::path::Path,
) -> Result<(
HashMap<String, String>,
MappedKeys,
Vec<String>,
Vec<LayerInfo>,
KeyOutputs,
KanataLayout,
)> {
let (cfg, src, layer_strings, klayers) = parse_cfg_raw(p)?;
let (cfg, src, layer_info, klayers) = parse_cfg_raw(p)?;

Ok((
cfg,
src,
layer_strings,
layer_info,
create_key_outputs(&klayers),
create_layout(klayers),
))
Expand All @@ -199,7 +205,7 @@ fn parse_cfg_raw(
) -> Result<(
HashMap<String, String>,
MappedKeys,
Vec<String>,
Vec<LayerInfo>,
Box<KanataLayers>,
)> {
let cfg = std::fs::read_to_string(p)?;
Expand Down Expand Up @@ -243,13 +249,32 @@ fn parse_cfg_raw(
.iter()
.filter(&deflayer_filter)
.collect::<Vec<_>>();

if layer_exprs.is_empty() {
bail!("No deflayer expressions exist. At least one layer must be defined.")
}
if layer_exprs.len() > MAX_LAYERS {
bail!("Exceeded the maximum layer count of {}", MAX_LAYERS)
}

let layer_idxs = parse_layer_indexes(&layer_exprs, mapping_order.len())?;
let mut sorted_idxs: Vec<(&String, &usize)> =
layer_idxs.iter().map(|tuple| (tuple.0, tuple.1)).collect();

sorted_idxs.sort_by_key(|f| f.1);

#[allow(clippy::needless_collect)]
// Clippy suggests using the sorted_idxs iter directly and manipulating it
// to produce the layer_names vec when creating Vec<LayerInfo> below
let layer_names = sorted_idxs
.into_iter()
.map(|(name, _)| (*name).clone())
.flat_map(|s| {
// Duplicate the same layer for `layer_strings` because the keyberon layout itself has
// two versions of each layer.
std::iter::repeat(s).take(2)
})
.collect::<Vec<_>>();

let layer_strings = root_expr_strs
.into_iter()
Expand All @@ -262,6 +287,12 @@ fn parse_cfg_raw(
})
.collect::<Vec<_>>();

let layer_info: Vec<LayerInfo> = layer_names
.into_iter()
.zip(layer_strings)
.map(|(name, cfg_text)| LayerInfo { name, cfg_text })
.collect();

let alias_exprs = root_exprs
.iter()
.filter(gen_first_atom_filter("defalias"))
Expand Down Expand Up @@ -296,7 +327,7 @@ fn parse_cfg_raw(

let klayers = parse_layers(&parsed_state)?;

Ok((cfg, src, layer_strings, klayers))
Ok((cfg, src, layer_info, klayers))
}

/// Return a closure that filters a root expression by the content of the first element. The
Expand Down Expand Up @@ -577,6 +608,7 @@ fn parse_layer_indexes(exprs: &[&Vec<SExpr>], expected_len: usize) -> Result<Lay
}
layer_indexes.insert(layer_name, i);
}

Ok(layer_indexes)
}

Expand Down
Loading