Skip to content

Commit

Permalink
refactor(ops): make create_prompt.. functions simpler
Browse files Browse the repository at this point in the history
  • Loading branch information
altsem committed Oct 20, 2024
1 parent 905f901 commit cb8379c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
25 changes: 12 additions & 13 deletions src/ops/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::{
screen::NavMode,
state::{root_menu, State},
term::Term,
Res,
};
use std::rc::Rc;

Expand Down Expand Up @@ -108,13 +107,24 @@ impl OpTrait for ToggleArg {
});
}

let arg_name = arg_name.clone();
let parse_and_set_arg =
Box::new(move |state: &mut State, _term: &mut Term, value: &str| {
if let Some(menu) = &mut state.pending_menu {
if let Some(entry) = menu.args.get_mut(arg_name.as_str()) {
return entry.set(value);
}
}

Ok(())
});

if let Some(display) = need_prompt {
set_prompt(
state,
display,
parse_and_set_arg,
Box::new(move |_| default.clone()),
arg_name.clone(),
false,
);
}
Expand All @@ -128,17 +138,6 @@ impl OpTrait for ToggleArg {
}
}

fn parse_and_set_arg(state: &mut State, _term: &mut Term, value: &str, arg: &String) -> Res<()> {
let key: &str = arg;
if let Some(menu) = &mut state.pending_menu {
if let Some(entry) = menu.args.get_mut(key) {
return entry.set(value);
}
}

Ok(())
}

pub(crate) struct ToggleSection;
impl OpTrait for ToggleSection {
fn get_action(&self, _target: Option<&TargetData>) -> Option<Action> {
Expand Down
21 changes: 10 additions & 11 deletions src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,39 +220,38 @@ pub(crate) fn create_y_n_prompt(mut action: Action, prompt: &'static str) -> Act

pub(crate) fn create_prompt(
prompt: &'static str,
callback: fn(&mut State, &mut Term, &str) -> Res<()>,
on_success: fn(&mut State, &mut Term, &str) -> Res<()>,
hide_menu: bool,
) -> Action {
create_prompt_with_default(prompt, callback, |_| None, hide_menu)
create_prompt_with_default(prompt, on_success, |_| None, hide_menu)
}

pub(crate) fn create_prompt_with_default(
prompt: &'static str,
callback: fn(&mut State, &mut Term, &str) -> Res<()>,
default_fn: fn(&State) -> Option<String>,
on_success: fn(&mut State, &mut Term, &str) -> Res<()>,
create_default_value: fn(&State) -> Option<String>,
hide_menu: bool,
) -> Action {
Rc::new(move |state: &mut State, _term: &mut Term| {
set_prompt(
state,
prompt,
|state, term, value, context| context(state, term, value),
Box::new(default_fn),
callback,
Box::new(on_success),
Box::new(create_default_value),
hide_menu,
);
Ok(())
})
}

type DefaultFn = Box<dyn Fn(&State) -> Option<String>>;
type PromptAction = Box<dyn Fn(&mut State, &mut Term, &str) -> Res<()>>;

pub(crate) fn set_prompt<T: 'static>(
pub(crate) fn set_prompt(
state: &mut State,
prompt: &'static str,
callback: fn(&mut State, &mut Term, &str, &T) -> Res<()>,
on_success: PromptAction,
default_fn: DefaultFn,
context: T,
hide_menu: bool,
) {
let prompt_text = if let Some(default) = default_fn(state) {
Expand All @@ -279,7 +278,7 @@ pub(crate) fn set_prompt<T: 'static>(
(value, _) => value,
};

callback(state, term, value, &context)?;
on_success(state, term, value)?;

if hide_menu {
state.unhide_menu();
Expand Down

0 comments on commit cb8379c

Please sign in to comment.