diff --git a/src/ops/editor.rs b/src/ops/editor.rs index 860934e2c4..674739f590 100644 --- a/src/ops/editor.rs +++ b/src/ops/editor.rs @@ -5,7 +5,6 @@ use crate::{ screen::NavMode, state::{root_menu, State}, term::Term, - Res, }; use std::rc::Rc; @@ -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, ); } @@ -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 { diff --git a/src/ops/mod.rs b/src/ops/mod.rs index dd10dadc0e..35c76c4761 100644 --- a/src/ops/mod.rs +++ b/src/ops/mod.rs @@ -220,25 +220,24 @@ 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, + on_success: fn(&mut State, &mut Term, &str) -> Res<()>, + create_default_value: fn(&State) -> Option, 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(()) @@ -246,13 +245,13 @@ pub(crate) fn create_prompt_with_default( } type DefaultFn = Box Option>; +type PromptAction = Box Res<()>>; -pub(crate) fn set_prompt( +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) { @@ -279,7 +278,7 @@ pub(crate) fn set_prompt( (value, _) => value, }; - callback(state, term, value, &context)?; + on_success(state, term, value)?; if hide_menu { state.unhide_menu();