Struct broot::verb::ExecutionStringBuilder
source · pub struct ExecutionStringBuilder<'b> {
pub sel_info: SelInfo<'b>,
/* private fields */
}
Expand description
a temporary structure gathering selection and invocation parameters and able to generate an executable string from a verb’s execution pattern
Fields§
§sel_info: SelInfo<'b>
the current file selection
Implementations§
source§impl<'b> ExecutionStringBuilder<'b>
impl<'b> ExecutionStringBuilder<'b>
sourcepub fn without_invocation(sel_info: SelInfo<'b>, app_state: &'b AppState) -> Self
pub fn without_invocation(sel_info: SelInfo<'b>, app_state: &'b AppState) -> Self
constructor to use when there’s no invocation string (because we’re in the process of building one, for example when a verb is triggered from a key shortcut)
Examples found in repository?
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
fn get_command(
&mut self,
timed_event: TimedEvent,
con: &AppContext,
sel_info: SelInfo<'_>,
app_state: &AppState,
mode: Mode,
panel_state_type: PanelStateType,
) -> Command {
match timed_event.event {
Event::Mouse(MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }) => {
if self.input_field.apply_timed_event(timed_event) {
Command::empty()
} else {
match kind {
MouseEventKind::Up(MouseButton::Left) => {
if timed_event.double_click {
Command::DoubleClick(column, row)
} else {
Command::Click(column, row)
}
}
MouseEventKind::ScrollDown => {
Command::Internal {
internal: Internal::line_down,
input_invocation: None,
}
}
MouseEventKind::ScrollUp => {
Command::Internal {
internal: Internal::line_up,
input_invocation: None,
}
}
_ => Command::None,
}
}
}
Event::Key(key) => {
// value of raw and parts before any key related change
let raw = self.input_field.get_content();
let mut parts = CommandParts::from(raw.clone());
// we first handle the cases that MUST absolutely
// not be overridden by configuration
if key == key!(esc) {
// tab cycling
self.tab_cycle_count = 0;
if let Some(raw) = self.input_before_cycle.take() {
// we cancel the tab cycling
self.input_field.set_str(&raw);
self.input_before_cycle = None;
return Command::from_raw(raw, false);
} else if con.modal && mode == Mode::Input {
// leave insertion mode
return Command::Internal {
internal: Internal::mode_command,
input_invocation: None,
};
} else {
// general back command
self.input_field.clear();
let internal = Internal::back;
return Command::Internal {
internal,
input_invocation: parts.verb_invocation,
};
}
}
// tab completion
if key == key!(tab) {
if parts.verb_invocation.is_some() {
let parts_before_cycle;
let completable_parts = if let Some(s) = &self.input_before_cycle {
parts_before_cycle = CommandParts::from(s.clone());
&parts_before_cycle
} else {
&parts
};
let completions = Completions::for_input(completable_parts, con, sel_info);
info!(" -> completions: {:?}", &completions);
let added = match completions {
Completions::None => {
debug!("nothing to complete!");
self.tab_cycle_count = 0;
self.input_before_cycle = None;
None
}
Completions::Common(completion) => {
self.tab_cycle_count = 0;
Some(completion)
}
Completions::List(mut completions) => {
let idx = self.tab_cycle_count % completions.len();
if self.tab_cycle_count == 0 {
self.input_before_cycle = Some(raw.to_string());
}
self.tab_cycle_count += 1;
Some(completions.swap_remove(idx))
}
};
if let Some(added) = added {
let mut raw = self
.input_before_cycle
.as_ref()
.map_or(raw, |s| s.to_string());
raw.push_str(&added);
self.input_field.set_str(&raw);
return Command::from_raw(raw, false);
} else {
return Command::None;
}
}
} else {
self.tab_cycle_count = 0;
self.input_before_cycle = None;
}
if key == key!(enter) && parts.has_not_empty_verb_invocation() {
return Command::from_parts(parts, true);
}
if (key == key!('?') || key == key!(shift-'?'))
&& (raw.is_empty() || parts.verb_invocation.is_some()) {
// a '?' opens the help when it's the first char
// or when it's part of the verb invocation
return Command::Internal {
internal: Internal::help,
input_invocation: parts.verb_invocation,
};
}
// we now check if the key is the trigger key of one of the verbs
if keys::is_key_allowed_for_verb(key, mode, raw.is_empty()) {
for (index, verb) in con.verb_store.verbs.iter().enumerate() {
for verb_key in &verb.keys {
if *verb_key != key {
continue;
}
if self.handle_input_related_verb(verb, con) {
return Command::from_raw(self.input_field.get_content(), false);
}
if !verb.selection_condition.is_respected_by(sel_info.common_stype()) {
continue;
}
if !verb.can_be_called_in_panel(panel_state_type) {
continue;
}
if mode != Mode::Input && verb.is_internal(Internal::mode_input) {
self.enter_input_mode_with_key(key, &parts);
}
if !verb.file_extensions.is_empty() {
let extension = sel_info.extension();
if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
continue;
}
}
if verb.auto_exec {
return Command::VerbTrigger {
index,
input_invocation: parts.verb_invocation,
};
}
if let Some(invocation_parser) = &verb.invocation_parser {
let exec_builder = ExecutionStringBuilder::without_invocation(
sel_info,
app_state,
);
let verb_invocation = exec_builder.invocation_with_default(
&invocation_parser.invocation_pattern
);
parts.verb_invocation = Some(verb_invocation);
self.set_content(&parts.to_string());
return Command::VerbEdit(parts.verb_invocation.unwrap());
}
}
}
}
// input field management
if mode == Mode::Input {
if self.input_field.apply_timed_event(timed_event) {
return Command::from_raw(self.input_field.get_content(), false);
}
}
Command::None
}
_ => Command::None,
}
}
sourcepub fn with_invocation(
invocation_parser: &Option<InvocationParser>,
sel_info: SelInfo<'b>,
app_state: &'b AppState,
invocation_args: Option<&String>
) -> Self
pub fn with_invocation(
invocation_parser: &Option<InvocationParser>,
sel_info: SelInfo<'b>,
app_state: &'b AppState,
invocation_args: Option<&String>
) -> Self
Examples found in repository?
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
fn execute_external(
&mut self,
w: &mut W,
verb: &Verb,
external_execution: &ExternalExecution,
invocation: Option<&VerbInvocation>,
app_state: &mut AppState,
cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let sel_info = self.sel_info(app_state);
if let Some(invocation) = &invocation {
if let Some(error) = verb.check_args(sel_info, invocation, &app_state.other_panel_path) {
debug!("verb.check_args prevented execution: {:?}", &error);
return Ok(CmdResult::error(error));
}
}
let exec_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
sel_info,
app_state,
if let Some(inv) = invocation {
inv.args.as_ref()
} else {
None
},
);
external_execution.to_cmd_result(w, exec_builder, cc.app.con)
}
fn execute_sequence(
&mut self,
_w: &mut W,
verb: &Verb,
seq_ex: &SequenceExecution,
invocation: Option<&VerbInvocation>,
app_state: &mut AppState,
_cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let sel_info = self.sel_info(app_state);
if matches!(sel_info, SelInfo::More(_)) {
// sequences would be hard to execute as the execution on a file can change the
// state in too many ways (changing selection, focused panel, parent, unstage or
// stage files, removing the staged paths, etc.)
return Ok(CmdResult::error("sequences can't be executed on multiple selections"));
}
let exec_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
sel_info,
app_state,
if let Some(inv) = invocation {
inv.args.as_ref()
} else {
None
},
);
// TODO what follows is dangerous: if an inserted group value contains the separator,
// the parsing will cut on this separator
let sequence = Sequence {
raw: exec_builder.shell_exec_string(&ExecPattern::from_string(&seq_ex.sequence.raw)),
separator: seq_ex.sequence.separator.clone(),
};
Ok(CmdResult::ExecuteSequence { sequence })
}
More examples
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
pub fn get_status_markdown(
&self,
sel_info: SelInfo<'_>,
app_state: &AppState,
invocation: &VerbInvocation,
) -> String {
let name = self.names.get(0).unwrap_or(&invocation.name);
// there's one special case: the ̀ :focus` internal. As long
// as no other internal takes args, and no other verb can
// have an optional argument, I don't try to build a
// generic behavior for internal optionally taking args and
// thus I hardcode the test here.
if let VerbExecution::Internal(internal_exec) = &self.execution {
if internal_exec.internal == Internal::focus {
return internal_focus::get_status_markdown(
self,
internal_exec,
sel_info,
invocation,
app_state,
);
}
}
let builder = || {
ExecutionStringBuilder::with_invocation(
&self.invocation_parser,
sel_info,
app_state,
invocation.args.as_ref(),
)
};
if let VerbExecution::Sequence(seq_ex) = &self.execution {
let exec_desc = builder().shell_exec_string(
&ExecPattern::from_string(&seq_ex.sequence.raw)
);
format!("Hit *enter* to **{}**: `{}`", name, &exec_desc)
} else if let VerbExecution::External(external_exec) = &self.execution {
let exec_desc = builder().shell_exec_string(&external_exec.exec_pattern);
format!("Hit *enter* to **{}**: `{}`", name, &exec_desc)
} else if self.description.code {
format!("Hit *enter* to **{}**: `{}`", name, &self.description.content)
} else {
format!("Hit *enter* to **{}**: {}", name, &self.description.content)
}
}
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
fn path_from_input(
verb: &Verb,
internal_exec: &InternalExecution,
base_path: &Path, // either the selected path or the root path
input_arg: Option<&String>,
app_state: &AppState,
) -> PathBuf {
match (input_arg, internal_exec.arg.as_ref()) {
(Some(input_arg), Some(verb_arg)) => {
// The verb probably defines some patttern which uses the input.
// For example:
// {
// invocation: "gotar {path}"
// execution: ":select {path}/target"
// }
// (or that input is useless)
let path_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
SelInfo::from_path(base_path),
app_state,
Some(input_arg),
);
path_builder.path(verb_arg)
}
(Some(input_arg), None) => {
// the verb defines nothing
// The :select internal execution was triggered from the
// input (which must be a kind of alias for :select)
// so we do exactly what the input asks for
path::path_from(base_path, PathAnchor::Unspecified, input_arg)
}
(None, Some(verb_arg)) => {
// the verb defines the path where to go..
// the internal_execution specifies the path to use
// (it may come from a configured verb whose execution is
// `:select some/path`).
// The given path may be relative hence the need for the
// state's selection
// (we assume a check before ensured it doesn't need an input)
path::path_from(base_path, PathAnchor::Unspecified, verb_arg)
}
(None, None) => {
// This doesn't really make sense: we're selecting the currently
// selected path
base_path.to_path_buf()
}
}
}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
fn path_from_input(
verb: &Verb,
internal_exec: &InternalExecution,
base_path: &Path, // either the selected path or the root path
input_arg: Option<&String>,
app_state: &AppState,
) -> PathBuf {
match (input_arg, internal_exec.arg.as_ref()) {
(Some(input_arg), Some(verb_arg)) => {
// The verb probably defines some patttern which uses the input.
// For example:
// {
// invocation: "gotar {path}"
// execution: ":focus {path}/target"
// }
// (or that input is useless)
let path_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
SelInfo::from_path(base_path),
app_state,
Some(input_arg),
);
path_builder.path(verb_arg)
}
(Some(input_arg), None) => {
// the verb defines nothing
// The :focus internal execution was triggered from the
// input (which must be a kind of alias for :focus)
// so we do exactly what the input asks for
path::path_from(base_path, PathAnchor::Unspecified, input_arg)
}
(None, Some(verb_arg)) => {
// the verb defines the path where to go..
// the internal_execution specifies the path to use
// (it may come from a configured verb whose execution is
// `:focus some/path`).
// The given path may be relative hence the need for the
// state's selection
// (we assume a check before ensured it doesn't need an input)
path::path_from(base_path, PathAnchor::Unspecified, verb_arg)
}
(None, None) => {
// user only wants to open the selected path, either in the same panel or
// in a new one
base_path.to_path_buf()
}
}
}
sourcepub fn invocation_with_default(
&self,
verb_invocation: &VerbInvocation
) -> VerbInvocation
pub fn invocation_with_default(
&self,
verb_invocation: &VerbInvocation
) -> VerbInvocation
fills groups having a default value (after the colon)
This is used to fill the input in case on non auto_exec verb triggered with a key
Examples found in repository?
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
fn get_command(
&mut self,
timed_event: TimedEvent,
con: &AppContext,
sel_info: SelInfo<'_>,
app_state: &AppState,
mode: Mode,
panel_state_type: PanelStateType,
) -> Command {
match timed_event.event {
Event::Mouse(MouseEvent { kind, column, row, modifiers: KeyModifiers::NONE }) => {
if self.input_field.apply_timed_event(timed_event) {
Command::empty()
} else {
match kind {
MouseEventKind::Up(MouseButton::Left) => {
if timed_event.double_click {
Command::DoubleClick(column, row)
} else {
Command::Click(column, row)
}
}
MouseEventKind::ScrollDown => {
Command::Internal {
internal: Internal::line_down,
input_invocation: None,
}
}
MouseEventKind::ScrollUp => {
Command::Internal {
internal: Internal::line_up,
input_invocation: None,
}
}
_ => Command::None,
}
}
}
Event::Key(key) => {
// value of raw and parts before any key related change
let raw = self.input_field.get_content();
let mut parts = CommandParts::from(raw.clone());
// we first handle the cases that MUST absolutely
// not be overridden by configuration
if key == key!(esc) {
// tab cycling
self.tab_cycle_count = 0;
if let Some(raw) = self.input_before_cycle.take() {
// we cancel the tab cycling
self.input_field.set_str(&raw);
self.input_before_cycle = None;
return Command::from_raw(raw, false);
} else if con.modal && mode == Mode::Input {
// leave insertion mode
return Command::Internal {
internal: Internal::mode_command,
input_invocation: None,
};
} else {
// general back command
self.input_field.clear();
let internal = Internal::back;
return Command::Internal {
internal,
input_invocation: parts.verb_invocation,
};
}
}
// tab completion
if key == key!(tab) {
if parts.verb_invocation.is_some() {
let parts_before_cycle;
let completable_parts = if let Some(s) = &self.input_before_cycle {
parts_before_cycle = CommandParts::from(s.clone());
&parts_before_cycle
} else {
&parts
};
let completions = Completions::for_input(completable_parts, con, sel_info);
info!(" -> completions: {:?}", &completions);
let added = match completions {
Completions::None => {
debug!("nothing to complete!");
self.tab_cycle_count = 0;
self.input_before_cycle = None;
None
}
Completions::Common(completion) => {
self.tab_cycle_count = 0;
Some(completion)
}
Completions::List(mut completions) => {
let idx = self.tab_cycle_count % completions.len();
if self.tab_cycle_count == 0 {
self.input_before_cycle = Some(raw.to_string());
}
self.tab_cycle_count += 1;
Some(completions.swap_remove(idx))
}
};
if let Some(added) = added {
let mut raw = self
.input_before_cycle
.as_ref()
.map_or(raw, |s| s.to_string());
raw.push_str(&added);
self.input_field.set_str(&raw);
return Command::from_raw(raw, false);
} else {
return Command::None;
}
}
} else {
self.tab_cycle_count = 0;
self.input_before_cycle = None;
}
if key == key!(enter) && parts.has_not_empty_verb_invocation() {
return Command::from_parts(parts, true);
}
if (key == key!('?') || key == key!(shift-'?'))
&& (raw.is_empty() || parts.verb_invocation.is_some()) {
// a '?' opens the help when it's the first char
// or when it's part of the verb invocation
return Command::Internal {
internal: Internal::help,
input_invocation: parts.verb_invocation,
};
}
// we now check if the key is the trigger key of one of the verbs
if keys::is_key_allowed_for_verb(key, mode, raw.is_empty()) {
for (index, verb) in con.verb_store.verbs.iter().enumerate() {
for verb_key in &verb.keys {
if *verb_key != key {
continue;
}
if self.handle_input_related_verb(verb, con) {
return Command::from_raw(self.input_field.get_content(), false);
}
if !verb.selection_condition.is_respected_by(sel_info.common_stype()) {
continue;
}
if !verb.can_be_called_in_panel(panel_state_type) {
continue;
}
if mode != Mode::Input && verb.is_internal(Internal::mode_input) {
self.enter_input_mode_with_key(key, &parts);
}
if !verb.file_extensions.is_empty() {
let extension = sel_info.extension();
if !extension.map_or(false, |ext| verb.file_extensions.iter().any(|ve| ve == ext)) {
continue;
}
}
if verb.auto_exec {
return Command::VerbTrigger {
index,
input_invocation: parts.verb_invocation,
};
}
if let Some(invocation_parser) = &verb.invocation_parser {
let exec_builder = ExecutionStringBuilder::without_invocation(
sel_info,
app_state,
);
let verb_invocation = exec_builder.invocation_with_default(
&invocation_parser.invocation_pattern
);
parts.verb_invocation = Some(verb_invocation);
self.set_content(&parts.to_string());
return Command::VerbEdit(parts.verb_invocation.unwrap());
}
}
}
}
// input field management
if mode == Mode::Input {
if self.input_field.apply_timed_event(timed_event) {
return Command::from_raw(self.input_field.get_content(), false);
}
}
Command::None
}
_ => Command::None,
}
}
sourcepub fn path(&self, pattern: &str) -> PathBuf
pub fn path(&self, pattern: &str) -> PathBuf
build a path
Examples found in repository?
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
fn working_dir_path(
&self,
builder: &ExecutionStringBuilder<'_>,
) -> Option<PathBuf> {
self.working_dir
.as_ref()
.map(|pattern| builder.path(pattern))
.filter(|pb| {
if pb.exists() {
true
} else {
warn!("workding dir doesn't exist: {:?}", pb);
false
}
})
}
More examples
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
fn path_from_input(
verb: &Verb,
internal_exec: &InternalExecution,
base_path: &Path, // either the selected path or the root path
input_arg: Option<&String>,
app_state: &AppState,
) -> PathBuf {
match (input_arg, internal_exec.arg.as_ref()) {
(Some(input_arg), Some(verb_arg)) => {
// The verb probably defines some patttern which uses the input.
// For example:
// {
// invocation: "gotar {path}"
// execution: ":select {path}/target"
// }
// (or that input is useless)
let path_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
SelInfo::from_path(base_path),
app_state,
Some(input_arg),
);
path_builder.path(verb_arg)
}
(Some(input_arg), None) => {
// the verb defines nothing
// The :select internal execution was triggered from the
// input (which must be a kind of alias for :select)
// so we do exactly what the input asks for
path::path_from(base_path, PathAnchor::Unspecified, input_arg)
}
(None, Some(verb_arg)) => {
// the verb defines the path where to go..
// the internal_execution specifies the path to use
// (it may come from a configured verb whose execution is
// `:select some/path`).
// The given path may be relative hence the need for the
// state's selection
// (we assume a check before ensured it doesn't need an input)
path::path_from(base_path, PathAnchor::Unspecified, verb_arg)
}
(None, None) => {
// This doesn't really make sense: we're selecting the currently
// selected path
base_path.to_path_buf()
}
}
}
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
fn path_from_input(
verb: &Verb,
internal_exec: &InternalExecution,
base_path: &Path, // either the selected path or the root path
input_arg: Option<&String>,
app_state: &AppState,
) -> PathBuf {
match (input_arg, internal_exec.arg.as_ref()) {
(Some(input_arg), Some(verb_arg)) => {
// The verb probably defines some patttern which uses the input.
// For example:
// {
// invocation: "gotar {path}"
// execution: ":focus {path}/target"
// }
// (or that input is useless)
let path_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
SelInfo::from_path(base_path),
app_state,
Some(input_arg),
);
path_builder.path(verb_arg)
}
(Some(input_arg), None) => {
// the verb defines nothing
// The :focus internal execution was triggered from the
// input (which must be a kind of alias for :focus)
// so we do exactly what the input asks for
path::path_from(base_path, PathAnchor::Unspecified, input_arg)
}
(None, Some(verb_arg)) => {
// the verb defines the path where to go..
// the internal_execution specifies the path to use
// (it may come from a configured verb whose execution is
// `:focus some/path`).
// The given path may be relative hence the need for the
// state's selection
// (we assume a check before ensured it doesn't need an input)
path::path_from(base_path, PathAnchor::Unspecified, verb_arg)
}
(None, None) => {
// user only wants to open the selected path, either in the same panel or
// in a new one
base_path.to_path_buf()
}
}
}
sourcepub fn shell_exec_string(&self, exec_pattern: &ExecPattern) -> String
pub fn shell_exec_string(&self, exec_pattern: &ExecPattern) -> String
build a shell compatible command, with escapings
Examples found in repository?
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
fn cmd_result_exec_from_parent_shell(
&self,
builder: ExecutionStringBuilder<'_>,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if builder.sel_info.count_paths() > 1 {
return Ok(CmdResult::error(
"only verbs returning to broot on end can be executed on a multi-selection"
));
}
if let Some(ref export_path) = con.launch_args.outcmd {
// Broot was probably launched as br.
// the whole command is exported in the passed file
let f = OpenOptions::new().append(true).open(export_path)?;
writeln!(&f, "{}", builder.shell_exec_string(&self.exec_pattern))?;
Ok(CmdResult::Quit)
} else {
Ok(CmdResult::error(
"this verb needs broot to be launched as `br`. Try `broot --install` if necessary."
))
}
}
More examples
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
fn execute_sequence(
&mut self,
_w: &mut W,
verb: &Verb,
seq_ex: &SequenceExecution,
invocation: Option<&VerbInvocation>,
app_state: &mut AppState,
_cc: &CmdContext,
) -> Result<CmdResult, ProgramError> {
let sel_info = self.sel_info(app_state);
if matches!(sel_info, SelInfo::More(_)) {
// sequences would be hard to execute as the execution on a file can change the
// state in too many ways (changing selection, focused panel, parent, unstage or
// stage files, removing the staged paths, etc.)
return Ok(CmdResult::error("sequences can't be executed on multiple selections"));
}
let exec_builder = ExecutionStringBuilder::with_invocation(
&verb.invocation_parser,
sel_info,
app_state,
if let Some(inv) = invocation {
inv.args.as_ref()
} else {
None
},
);
// TODO what follows is dangerous: if an inserted group value contains the separator,
// the parsing will cut on this separator
let sequence = Sequence {
raw: exec_builder.shell_exec_string(&ExecPattern::from_string(&seq_ex.sequence.raw)),
separator: seq_ex.sequence.separator.clone(),
};
Ok(CmdResult::ExecuteSequence { sequence })
}
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
pub fn get_status_markdown(
&self,
sel_info: SelInfo<'_>,
app_state: &AppState,
invocation: &VerbInvocation,
) -> String {
let name = self.names.get(0).unwrap_or(&invocation.name);
// there's one special case: the ̀ :focus` internal. As long
// as no other internal takes args, and no other verb can
// have an optional argument, I don't try to build a
// generic behavior for internal optionally taking args and
// thus I hardcode the test here.
if let VerbExecution::Internal(internal_exec) = &self.execution {
if internal_exec.internal == Internal::focus {
return internal_focus::get_status_markdown(
self,
internal_exec,
sel_info,
invocation,
app_state,
);
}
}
let builder = || {
ExecutionStringBuilder::with_invocation(
&self.invocation_parser,
sel_info,
app_state,
invocation.args.as_ref(),
)
};
if let VerbExecution::Sequence(seq_ex) = &self.execution {
let exec_desc = builder().shell_exec_string(
&ExecPattern::from_string(&seq_ex.sequence.raw)
);
format!("Hit *enter* to **{}**: `{}`", name, &exec_desc)
} else if let VerbExecution::External(external_exec) = &self.execution {
let exec_desc = builder().shell_exec_string(&external_exec.exec_pattern);
format!("Hit *enter* to **{}**: `{}`", name, &exec_desc)
} else if self.description.code {
format!("Hit *enter* to **{}**: `{}`", name, &self.description.content)
} else {
format!("Hit *enter* to **{}**: {}", name, &self.description.content)
}
}
sourcepub fn sel_shell_exec_string(
&self,
exec_pattern: &ExecPattern,
sel: Option<Selection<'_>>
) -> String
pub fn sel_shell_exec_string(
&self,
exec_pattern: &ExecPattern,
sel: Option<Selection<'_>>
) -> String
build a shell compatible command, with escapings, for a specific selection (this is intended for execution on all selections of a stage)
sourcepub fn exec_token(&self, exec_pattern: &ExecPattern) -> Vec<String> ⓘ
pub fn exec_token(&self, exec_pattern: &ExecPattern) -> Vec<String> ⓘ
build a vec of tokens which can be passed to Command to launch an executable
Examples found in repository?
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
fn cmd_result_exec_leave_broot(
&self,
builder: ExecutionStringBuilder<'_>,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
if builder.sel_info.count_paths() > 1 {
return Ok(CmdResult::error(
"only verbs returning to broot on end can be executed on a multi-selection"
));
}
let launchable = Launchable::program(
builder.exec_token(&self.exec_pattern),
self.working_dir_path(&builder),
con,
)?;
Ok(CmdResult::from(launchable))
}
/// build the cmd result as an executable which will be called in a process
/// launched by broot
fn cmd_result_exec_stay_in_broot(
&self,
w: &mut W,
builder: ExecutionStringBuilder<'_>,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
let working_dir_path = self.working_dir_path(&builder);
match &builder.sel_info {
SelInfo::None | SelInfo::One(_) => {
// zero or one selection -> only one execution
let launchable = Launchable::program(
builder.exec_token(&self.exec_pattern),
working_dir_path,
con,
)?;
info!("Executing not leaving, launchable {:?}", launchable);
if let Err(e) = launchable.execute(Some(w)) {
warn!("launchable failed : {:?}", e);
return Ok(CmdResult::error(e.to_string()));
}
}
SelInfo::More(stage) => {
// multiselection -> we must execute on all paths
let sels = stage.paths().iter()
.map(|path| Selection {
path,
line: 0,
stype: SelectionType::from(path),
is_exe: false,
});
for sel in sels {
let launchable = Launchable::program(
builder.sel_exec_token(&self.exec_pattern, Some(sel)),
working_dir_path.clone(),
con,
)?;
if let Err(e) = launchable.execute(Some(w)) {
warn!("launchable failed : {:?}", e);
return Ok(CmdResult::error(e.to_string()));
}
}
}
}
Ok(CmdResult::RefreshState { clear_cache: true })
}
sourcepub fn sel_exec_token(
&self,
exec_pattern: &ExecPattern,
sel: Option<Selection<'_>>
) -> Vec<String> ⓘ
pub fn sel_exec_token(
&self,
exec_pattern: &ExecPattern,
sel: Option<Selection<'_>>
) -> Vec<String> ⓘ
build a vec of tokens which can be passed to Command to launch an executable
Examples found in repository?
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
fn cmd_result_exec_stay_in_broot(
&self,
w: &mut W,
builder: ExecutionStringBuilder<'_>,
con: &AppContext,
) -> Result<CmdResult, ProgramError> {
let working_dir_path = self.working_dir_path(&builder);
match &builder.sel_info {
SelInfo::None | SelInfo::One(_) => {
// zero or one selection -> only one execution
let launchable = Launchable::program(
builder.exec_token(&self.exec_pattern),
working_dir_path,
con,
)?;
info!("Executing not leaving, launchable {:?}", launchable);
if let Err(e) = launchable.execute(Some(w)) {
warn!("launchable failed : {:?}", e);
return Ok(CmdResult::error(e.to_string()));
}
}
SelInfo::More(stage) => {
// multiselection -> we must execute on all paths
let sels = stage.paths().iter()
.map(|path| Selection {
path,
line: 0,
stype: SelectionType::from(path),
is_exe: false,
});
for sel in sels {
let launchable = Launchable::program(
builder.sel_exec_token(&self.exec_pattern, Some(sel)),
working_dir_path.clone(),
con,
)?;
if let Err(e) = launchable.execute(Some(w)) {
warn!("launchable failed : {:?}", e);
return Ok(CmdResult::error(e.to_string()));
}
}
}
}
Ok(CmdResult::RefreshState { clear_cache: true })
}