Skip to content
Draft
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
41 changes: 35 additions & 6 deletions src/external/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub fn parse_stdout(stdout: &str) -> (Vec<Widget>, String) {
let param_regex =
Regex::new(r#"(\w+)\s*=\s*\"?([^\",]+)\"?,?\s*"#).unwrap();
let text_regex = Regex::new(r#"TEXT\("(.*)"\)"#).unwrap();
let text_v2_regex = Regex::new(r"TEXT\s*\((.*?)\)").unwrap();
let data_regex = Regex::new(r#"DATA\("(.*)"\)"#).unwrap();
let quit_regex = Regex::new(r#"QUIT\("([^"]*)"\)"#).unwrap();

Expand All @@ -41,19 +42,18 @@ pub fn parse_stdout(stdout: &str) -> (Vec<Widget>, String) {
if let Some(captures) = input_regex.captures(line) {
// found input widget, initialize parameters to default values
let params_str = captures.get(1).unwrap().as_str();
let mut max_width = 32;
let mut filter = Filter::Off;
let mut label = String::new();
let mut placeholder = String::new();
let mut content = String::new();
let mut selectable = true;
let mut hidden = false;

for param_match in param_regex.captures_iter(params_str) {
let param = param_match.get(1).unwrap().as_str();
let value = param_match.get(2).unwrap().as_str();

// set parameter variable for each valid parameter
match param {
| "max_width" => max_width = value.parse().unwrap_or(32),
| "filter" => {
filter = value.parse().unwrap_or_else(|_| {
eprintln!(
Expand All @@ -64,20 +64,21 @@ pub fn parse_stdout(stdout: &str) -> (Vec<Widget>, String) {
})
}
| "label" => label = value.to_string(),
| "placeholder" => placeholder = value.to_string(),
| "content" => content = value.to_string(),
| "selectable" => selectable = value.eq("true"),
| "hidden" => hidden = value.eq("true"),
| _ => {}
}
}

// add a new input widget to the model
widgets.push(Widget::Input {
y: level,
max_width,
filter,
label,
placeholder,
content,
selectable,
hidden,
id: unique_id,
});
} else if let Some(captures) = text_regex.captures(line) {
Expand All @@ -89,6 +90,34 @@ pub fn parse_stdout(stdout: &str) -> (Vec<Widget>, String) {
y: level,
content,
show: true,
selectable: true,
id: unique_id,
});
} else if let Some(captures) = text_v2_regex.captures(line) {
// TODO: merge text_v2 with text one day...
// found parameterized text widget, initialize parameters to default values
let params_str = captures.get(1).unwrap().as_str();
let mut content = String::new();
let mut selectable = true;

for param_match in param_regex.captures_iter(params_str) {
let param = param_match.get(1).unwrap().as_str();
let value = param_match.get(2).unwrap().as_str();

// set parameter variable for each valid parameter
match param {
| "content" => content = value.to_string(),
| "selectable" => selectable = value.eq("true"),
| _ => {}
}
}

// add a new input widget to the model
widgets.push(Widget::Text {
y: level,
content,
show: true,
selectable,
id: unique_id,
});
} else if let Some(captures) = data_regex.captures(line) {
Expand Down
5 changes: 3 additions & 2 deletions src/external/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ impl FromStr for Filter {
pub enum Widget {
Input {
y: i32,
max_width: usize,
filter: Filter,
label: String,
placeholder: String,
content: String,
selectable: bool,
hidden: bool,
id: usize,
},
Text {
y: i32,
content: String,
selectable: bool,
show: bool,
id: usize,
},
Expand Down
50 changes: 22 additions & 28 deletions src/interface/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,35 +206,24 @@ fn wait_for_input(
break;
}
| Some(Input::KeyEnter) | Some(Input::Character('\n')) => {
// enter/return pressed, prepare program to quit
break_condition = BreakCondition::QUIT;

for widget in model {
match widget {
| Widget::Input { y, id, .. }
if *y == (cursor.y as i32) =>
{
// selected widget is input, set break condition and selected id
current_widget = *id;
break_condition = BreakCondition::INPUT;
break;
}
| Widget::Text { y, id, .. }
if *y == (cursor.y as i32) =>
{
// selected widget is text, set break condition and selected id
current_widget = *id;
break_condition = BreakCondition::SELECTION;
break;
}
| _ => {
break_condition = BreakCondition::QUIT;
}
if let Some(Widget::Input { id, selectable, .. }) =
find_widget_by_y(model, cursor.y as i32)
{
if *selectable {
current_widget = *id;
break_condition = BreakCondition::INPUT;
break;
}
}
if let Some(Widget::Text { id, selectable, .. }) =
find_widget_by_y(model, cursor.y as i32)
{
if *selectable {
current_widget = *id;
break_condition = BreakCondition::SELECTION;
break;
}
}

// conditions have been set, exit loop
break;
}
| Some(Input::KeyUp) => {
// up arrow pressed, move cursor up
Expand Down Expand Up @@ -372,28 +361,33 @@ fn wait_for_input(

let mut content;
let filter;
let hidden;
let current_level = cursor.y;
let mut label_len = 0;

if let Some(Widget::Input {
content: ref_content,
filter: ref_filter,
hidden: ref_hidden,
..
}) = find_widget_by_y(model, cursor.y as i32).cloned()
{
// current row is input, get its content and filter type
content = ref_content.clone();
filter = ref_filter.clone();
hidden = ref_hidden.clone();
} else {
// current row is text, nothing to type so skip
continue;
}

let display_char = if hidden { '*' } else { c };

// insert typed character to screen
window.mvinsch(
cursor.y as i32,
(cursor.x + left_margin) as i32,
c as chtype,
display_char as chtype,
);
cursor.x += 1;

Expand Down
16 changes: 16 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ fn main() {
data = new_data;
}

// for widget in model {
// match widget {
// | Widget::Input {
// y, selectable, ..
// } => {
// println!("{}", selectable);
// }
// | Widget::Text {
// y, selectable, ..
// } => {
// println!("{}", selectable);
// }
// }
// }
// exit(1);

let (break_condition, match_id) = init(&mut model);

if break_condition == BreakCondition::SELECTION {
Expand Down