summaryrefslogtreecommitdiff
path: root/src/library/direction.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-09 13:29:04 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-09 13:29:04 +0100
commit7e980224354880cfda1797136a1ff886d6642662 (patch)
treec0137dcca82526faa71fd1d980a90c68dac798c8 /src/library/direction.rs
parent64f938b449b7ff5e53b6a06ed943bf9dedc1014b (diff)
Bad stack layouter 🚑
Diffstat (limited to 'src/library/direction.rs')
-rw-r--r--src/library/direction.rs60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/library/direction.rs b/src/library/direction.rs
new file mode 100644
index 00000000..3b9956d1
--- /dev/null
+++ b/src/library/direction.rs
@@ -0,0 +1,60 @@
+use crate::func::prelude::*;
+use super::maps::ConsistentMap;
+use super::keys::AxisKey;
+
+function! {
+ /// `direction`: Sets the directions for the layouting axes.
+ #[derive(Debug, PartialEq)]
+ pub struct Direction {
+ body: Option<SyntaxTree>,
+ map: ConsistentMap<AxisKey, Axis>,
+ }
+
+ parse(args, body, ctx) {
+ let mut map = ConsistentMap::new();
+
+ map.add_opt_span(AxisKey::Primary, args.get_pos_opt::<Axis>()?)?;
+ map.add_opt_span(AxisKey::Secondary, args.get_pos_opt::<Axis>()?)?;
+
+ for arg in args.keys() {
+ let axis = AxisKey::from_ident(&arg.v.key)?;
+ let value = Axis::from_expr(arg.v.value)?;
+
+ map.add(axis, value)?;
+ }
+
+ Direction {
+ body: parse!(optional: body, ctx),
+ map,
+ }
+ }
+
+ layout(self, mut ctx) {
+ let axes = ctx.axes;
+
+ let map = self.map.dedup(|key, &direction| {
+ Ok((match key {
+ AxisKey::Primary => GenericAxisKind::Primary,
+ AxisKey::Secondary => GenericAxisKind::Secondary,
+ AxisKey::Horizontal => axes.horizontal(),
+ AxisKey::Vertical => axes.vertical(),
+ }, direction))
+ })?;
+
+ map.with(GenericAxisKind::Primary, |&val| ctx.axes.primary = val);
+ map.with(GenericAxisKind::Secondary, |&val| ctx.axes.secondary = val);
+
+ if ctx.axes.primary.is_horizontal() == ctx.axes.secondary.is_horizontal() {
+ error!(
+ "aligned primary and secondary axes: `{}`, `{}`",
+ format!("{:?}", ctx.axes.primary).to_lowercase(),
+ format!("{:?}", ctx.axes.secondary).to_lowercase(),
+ );
+ }
+
+ match &self.body {
+ Some(body) => vec![AddMultiple(layout_tree(&body, ctx)?)],
+ None => vec![Command::SetAxes(ctx.axes)],
+ }
+ }
+}