diff options
author | Canop <cano.petrole@gmail.com> | 2021-04-19 20:59:15 +0200 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2021-04-19 20:59:15 +0200 |
commit | 6da21634bf925b9e3bce497b5d8e6218786f2628 (patch) | |
tree | 00a1d150a8f8847933266791b86672a050481818 /src/app | |
parent | aa1dc4a944ad1f11cf8292c49cb771dd1384c6c7 (diff) |
improve command previsualization in status for multiselection
If a group is common to all paths, it will be kept
Diffstat (limited to 'src/app')
-rw-r--r-- | src/app/panel_state.rs | 66 | ||||
-rw-r--r-- | src/app/selection.rs | 16 |
2 files changed, 43 insertions, 39 deletions
diff --git a/src/app/panel_state.rs b/src/app/panel_state.rs index 7e41d06..d0802b3 100644 --- a/src/app/panel_state.rs +++ b/src/app/panel_state.rs @@ -464,20 +464,12 @@ pub trait PanelState { verb: &Verb, external_execution: &ExternalExecution, invocation: Option<&VerbInvocation>, - _app_state: &mut AppState, + app_state: &mut AppState, cc: &CmdContext, ) -> Result<CmdResult, ProgramError> { - let selection = match self.selection() { - Some(selection) => selection, - None => { - // this function is overriden for the state which doesn't have a - // single selection (stage_state), so this error should not happen - return Ok(CmdResult::error("no selection (EV)")); - } - }; let exec_builder = ExecutionStringBuilder::from_invocation( &verb.invocation_parser, - Some(selection), + self.sel_info(app_state), &cc.app.other_path, if let Some(inv) = invocation { &inv.args @@ -494,31 +486,21 @@ pub trait PanelState { verb: &Verb, seq_ex: &SequenceExecution, invocation: Option<&VerbInvocation>, - _app_state: &mut AppState, + app_state: &mut AppState, cc: &CmdContext, ) -> Result<CmdResult, ProgramError> { - let selection = match self.selection() { - Some(selection) => selection, - None => { - // this function is overriden for the state which doesn't have a - // single selection (stage_state), so this error should not happen - return Ok(CmdResult::error("no selection (EV)")); - } - }; - let exec_builder = || { - ExecutionStringBuilder::from_invocation( - &verb.invocation_parser, - Some(selection), - &cc.app.other_path, - if let Some(inv) = invocation { - &inv.args - } else { - &None - }, - ) - }; + let exec_builder = ExecutionStringBuilder::from_invocation( + &verb.invocation_parser, + self.sel_info(app_state), + &cc.app.other_path, + if let Some(inv) = invocation { + &inv.args + } else { + &None + }, + ); let sequence = Sequence { - raw: exec_builder().shell_exec_string(&ExecPattern::from_string(&seq_ex.sequence.raw)), + raw: exec_builder.shell_exec_string(&ExecPattern::from_string(&seq_ex.sequence.raw)), separator: seq_ex.sequence.separator.clone(), }; Ok(CmdResult::ExecuteSequence { sequence }) @@ -752,21 +734,31 @@ pub trait PanelState { } } - // overloaded in stage_state fn get_verb_status( &self, verb: &Verb, invocation: &VerbInvocation, - _app_state: &AppState, + app_state: &AppState, cc: &CmdContext, ) -> Status { - let selection = self.selection(); - if let Some(err) = verb.check_args(&selection, invocation, &cc.app.other_path) { + let sel_info = self.sel_info(app_state); + if sel_info.count_paths() > 1 { + if let VerbExecution::External(external) = &verb.execution { + if external.exec_mode != ExternalExecutionMode::StayInBroot { + return Status::new( + "only verbs returning to broot on end can be executed on a multi-selection".to_owned(), + true, + ); + } + } + // right now there's no check for sequences but they're inherently dangereous + } + if let Some(err) = verb.check_args(&sel_info, invocation, &cc.app.other_path) { Status::new(err, true) } else { Status::new( verb.get_status_markdown( - selection, + sel_info, &cc.app.other_path, invocation, ), diff --git a/src/app/selection.rs b/src/app/selection.rs index 98ad2b1..07d5e1e 100644 --- a/src/app/selection.rs +++ b/src/app/selection.rs @@ -96,7 +96,7 @@ impl Selection<'_> { } } -impl SelInfo<'_> { +impl<'a> SelInfo<'a> { pub fn common_path(&self) -> Option<PathBuf> { match self { SelInfo::None => None, @@ -104,6 +104,13 @@ impl SelInfo<'_> { SelInfo::More(stage) => Some(longest_common_ancestor(&stage.paths)) } } + pub fn count_paths(&self) -> usize { + match self { + SelInfo::None => 0, + SelInfo::One(_) => 1, + SelInfo::More(stage) => stage.paths.len(), + } + } pub fn common_stype(&self) -> Option<SelectionType> { match self { SelInfo::None => None, @@ -118,6 +125,11 @@ impl SelInfo<'_> { Some(stype) } } - + } + pub fn as_one_sel(self) -> Option<Selection<'a>> { + match self { + SelInfo::One(sel) => Some(sel), + _ => None, + } } } |