Skip to content

Commit 858035b

Browse files
committed
add alignment attribute and inner variable
1 parent 101107c commit 858035b

7 files changed

Lines changed: 69 additions & 10 deletions

File tree

layouts/40.tvkl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#show_kps = $0
66
#show_caps = $0
7+
#alignment = "left"
78

89
:| Tab [#tab_len] | Q, 1 | W, 2 | E, 3 | R, 4 | T, 5
910
| Y, 6 | U, 7 | I, 8 | O, 9 | P, 0 | Back [$7] |-

layouts/60.tvkl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#esc_len = $5
44
#esc_color = @($201, $113, $217)
55

6+
#alignment = "center"
7+
68
:| Esc [#esc_len,#esc_color] | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | '-' | '=' | Back [$8] |-
7-
:| Tab [$7] | Q | W | E | R | T | Y | U | I | O | P | '[' | ']' | '\' [$6] |-
8-
:| Caps [$8] | A | S | D | F | G | H | J | K | L | ';' | "'" | Enter [$9] |-
9-
:| LShift [$10] | Z | X | C | V | B | N | M | ',' | '.' | '/' | RShift [$11] |-
9+
:| Tab [$7,,,"left"] | Q | W | E | R | T | Y | U | I | O | P | '[' | ']' | '\' [$6] |-
10+
:| Caps [$8,,,"left"] | A | S | D | F | G | H | J | K | L | ';' | "'" | Enter [$9] |-
11+
:| LShift [$10,,,"left"] | Z | X | C | V | B | N | M | ',' | '.' | '/' | RShift [$11] |-
1012
:| LCtrl [$7] | Win [$5] | LAlt [$6] | Space [$20] | RAlt [$6] | Win [$5] | App [$5] | RCtrl [$7] |-
1113

1214

layouts/60_blue.tvkl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#mod_len = $7
1414
#space_len = $20
1515

16+
1617
:| Esc [#esc_len,#esc_color]
1718
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0
1819
| '-' | '='

src/env.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use std::sync::Arc;
22
use std::collections::HashMap;
33

4-
#[derive(Debug, Copy, Clone)]
4+
#[derive(Debug, Clone)]
55
pub enum Value {
66
Number(u16),
7-
RGB(u8, u8, u8)
7+
RGB(u8, u8, u8),
8+
Str(Arc<str>)
89
}
910

1011
#[derive(Debug)]

src/layout.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct Attr {
1212
pub width: u16,
1313
pub border_color: Option<Color>,
1414
pub highlight: Option<Color>,
15+
pub alignment: Option<Arc<str>>,
1516
}
1617

1718
impl Attr {
@@ -24,6 +25,7 @@ impl Attr {
2425
width,
2526
border_color: None,
2627
highlight: None,
28+
alignment: None,
2729
}
2830
}
2931
}

src/parser.rs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ impl Parser {
7777
let num = self.consume(TokenType::Number)?;
7878
Ok(Value::Number(num.value.parse()?))
7979
}
80+
TokenType::Name => {
81+
let str = self.consume(TokenType::Name)?;
82+
Ok(Value::Str(str.value.clone().into()))
83+
}
8084
TokenType::At => {
8185
self.consume(TokenType::At)?;
8286
self.consume(TokenType::LParen)?;
@@ -96,7 +100,7 @@ impl Parser {
96100
let ident = self.consume(TokenType::Ident)?;
97101
let name = ident.value.clone();
98102
match env.get(name.as_str()) {
99-
Some(v) => Ok(*v),
103+
Some(v) => Ok(v.clone()),
100104
None => Err(ParserError::Err(format!("Unbounded Variable {:?}.", name))),
101105
}
102106
}
@@ -161,7 +165,7 @@ impl Parser {
161165
}
162166

163167
fn parse_attr(&mut self, attr: &mut Attr, env: &Env) -> Result<(), ParserError> {
164-
// [width, border_color, highlight]
168+
// [width, border_color, highlight, alignment]
165169
self.consume(TokenType::LBracket)?;
166170

167171
let mut pos = 0;
@@ -200,6 +204,15 @@ impl Parser {
200204
return Err(ParserError::Err("Highlight must be RGB".into()));
201205
}
202206
}
207+
3 => {
208+
// alignment
209+
if let Value::Str(v) = self.parse_value(env)? {
210+
attr.alignment = Some(v);
211+
} else {
212+
return Err(ParserError::Err("Alignment must be Name".into()));
213+
}
214+
}
215+
203216
_ => {
204217
self.advance()?;
205218
}
@@ -589,6 +602,7 @@ mod tests {
589602

590603
// #id = $10
591604
// #color = @($1, $2, $3)
605+
// #str = "hi"
592606
// :| A, C, D | B |-
593607
let tokens = vec![
594608
Token {
@@ -643,6 +657,18 @@ mod tests {
643657
token_type: TokenType::RParen,
644658
value: ")".into(),
645659
},
660+
Token {
661+
token_type: TokenType::Ident,
662+
value: "str".into(),
663+
},
664+
Token {
665+
token_type: TokenType::Equal,
666+
value: "=".into(),
667+
},
668+
Token {
669+
token_type: TokenType::Name,
670+
value: "hi".into(),
671+
},
646672
Token {
647673
token_type: TokenType::LineHead,
648674
value: ":".into(),
@@ -694,6 +720,11 @@ mod tests {
694720
_ => panic!("Variable 'color' not found or wrong type"),
695721
}
696722

723+
match env.get("str") {
724+
Some(Value::Str(v)) => assert_eq!(*v, "hi".into()),
725+
_ => panic!("Variable 'str' not found or wrong type"),
726+
}
727+
697728
assert_eq!(result.layer.len(), 1);
698729

699730
let button_1 = &result.layer[0][0];

src/render.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ pub fn render_ui(
6767
_ => default_caps_color,
6868
};
6969

70+
let global_alignment = match env.get("alignment") {
71+
Some(Value::Str(a)) => match a.as_ref() {
72+
"left" => Alignment::Left,
73+
"right" => Alignment::Right,
74+
_ => Alignment::Center,
75+
},
76+
_ => Alignment::Center,
77+
};
78+
7079
// Render Outer Container
7180
let outer_block = Block::default()
7281
.borders(Borders::ALL)
@@ -105,7 +114,11 @@ pub fn render_ui(
105114
);
106115

107116
// Render KPS
108-
let kps_text = if show_kps { format!("KPS: {}", kps) } else { String::new() };
117+
let kps_text = if show_kps {
118+
format!("KPS: {}", kps)
119+
} else {
120+
String::new()
121+
};
109122
f.render_widget(
110123
Paragraph::new(kps_text)
111124
.alignment(Alignment::Right)
@@ -139,7 +152,15 @@ pub fn render_ui(
139152
for (k_idx, button) in row.iter().enumerate() {
140153
let current_border = button.attr.border_color.unwrap_or(global_border_color);
141154
let current_highlight = button.attr.highlight.unwrap_or(global_highlight);
142-
155+
let current_alignment = match &button.attr.alignment {
156+
Some(a) => match a.as_ref() {
157+
"left" => Alignment::Left,
158+
"right" => Alignment::Right,
159+
"center" => Alignment::Center,
160+
_ => global_alignment,
161+
},
162+
None => global_alignment,
163+
};
143164
let active_bind_idx = button
144165
.binds
145166
.iter()
@@ -173,7 +194,7 @@ pub fn render_ui(
173194
};
174195

175196
let key_widget = Paragraph::new(display_name)
176-
.alignment(Alignment::Center)
197+
.alignment(current_alignment)
177198
.block(
178199
Block::default()
179200
.borders(Borders::ALL)

0 commit comments

Comments
 (0)