1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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
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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
|
//! Definition of the actions that can be bound to keys.
use super::command::{OpenFilePayload, RunCommandAction};
use super::layout::{
FloatingPaneLayout, Layout, PluginAlias, RunPlugin, RunPluginLocation, RunPluginOrAlias,
SwapFloatingLayout, SwapTiledLayout, TiledPaneLayout,
};
use crate::cli::CliAction;
use crate::data::{Direction, KeyWithModifier, Resize};
use crate::data::{FloatingPaneCoordinates, InputMode};
use crate::home::{find_default_config_dir, get_layout_dir};
use crate::input::config::{Config, ConfigError, KdlError};
use crate::input::options::OnForceClose;
use miette::{NamedSource, Report};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use uuid::Uuid;
use std::path::PathBuf;
use std::str::FromStr;
use crate::position::Position;
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum ResizeDirection {
Left,
Right,
Up,
Down,
Increase,
Decrease,
}
impl FromStr for ResizeDirection {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Left" | "left" => Ok(ResizeDirection::Left),
"Right" | "right" => Ok(ResizeDirection::Right),
"Up" | "up" => Ok(ResizeDirection::Up),
"Down" | "down" => Ok(ResizeDirection::Down),
"Increase" | "increase" | "+" => Ok(ResizeDirection::Increase),
"Decrease" | "decrease" | "-" => Ok(ResizeDirection::Decrease),
_ => Err(format!(
"Failed to parse ResizeDirection. Unknown ResizeDirection: {}",
s
)),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum SearchDirection {
Down,
Up,
}
impl FromStr for SearchDirection {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Down" | "down" => Ok(SearchDirection::Down),
"Up" | "up" => Ok(SearchDirection::Up),
_ => Err(format!(
"Failed to parse SearchDirection. Unknown SearchDirection: {}",
s
)),
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum SearchOption {
CaseSensitivity,
WholeWord,
Wrap,
}
impl FromStr for SearchOption {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"CaseSensitivity" | "casesensitivity" | "Casesensitivity" => {
Ok(SearchOption::CaseSensitivity)
},
"WholeWord" | "wholeword" | "Wholeword" => Ok(SearchOption::WholeWord),
"Wrap" | "wrap" => Ok(SearchOption::Wrap),
_ => Err(format!(
"Failed to parse SearchOption. Unknown SearchOption: {}",
s
)),
}
}
}
// As these actions are bound to the default config, please
// do take care when refactoring - or renaming.
// They might need to be adjusted in the default config
// as well `../../assets/config/default.yaml`
/// Actions that can be bound to keys.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub enum Action {
/// Quit Zellij.
Quit,
/// Write to the terminal.
Write(Option<KeyWithModifier>, Vec<u8>, bool), // bool -> is_kitty_keyboard_protocol
/// Write Characters to the terminal.
WriteChars(String),
/// Switch to the specified input mode.
SwitchToMode(InputMode),
/// Switch all connected clients to the specified input mode.
SwitchModeForAllClients(InputMode),
/// Shrink/enlarge focused pane at specified border
Resize(Resize, Option<Direction>),
/// Switch focus to next pane in specified direction.
FocusNextPane,
FocusPreviousPane,
/// Move the focus pane in specified direction.
SwitchFocus,
MoveFocus(Direction),
/// Tries to move the focus pane in specified direction.
/// If there is no pane in the direction, move to previous/next Tab.
MoveFocusOrTab(Direction),
MovePane(Option<Direction>),
MovePaneBackwards,
/// Clear all buffers of a current screen
ClearScreen,
/// Dumps the screen to a file
DumpScreen(String, bool),
/// Dumps
DumpLayout,
/// Scroll up in focus pane.
EditScrollback,
ScrollUp,
/// Scroll up at point
ScrollUpAt(Position),
/// Scroll down in focus pane.
ScrollDown,
/// Scroll down at point
ScrollDownAt(Position),
/// Scroll down to bottom in focus pane.
ScrollToBottom,
/// Scroll up to top in focus pane.
ScrollToTop,
/// Scroll up one page in focus pane.
PageScrollUp,
/// Scroll down one page in focus pane.
PageScrollDown,
/// Scroll up half page in focus pane.
HalfPageScrollUp,
/// Scroll down half page in focus pane.
HalfPageScrollDown,
/// Toggle between fullscreen focus pane and normal layout.
ToggleFocusFullscreen,
/// Toggle frames around panes in the UI
TogglePaneFrames,
/// Toggle between sending text commands to all panes on the current tab and normal mode.
ToggleActiveSyncTab,
/// Open a new pane in the specified direction (relative to focus).
/// If no direction is specified, will try to use the biggest available space.
NewPane(Option<Direction>, Option<String>, bool), // String is an optional pane name
/// Open the file in a new pane using the default editor, bool -> start suppressed
EditFile(
OpenFilePayload,
Option<Direction>,
bool,
bool,
bool,
Option<FloatingPaneCoordinates>,
), // bool is floating true/false, second bool is in_place
// third bool is start_suppressed
/// Open a new floating pane
NewFloatingPane(
Option<RunCommandAction>,
Option<String>,
Option<FloatingPaneCoordinates>,
), // String is an optional pane name
/// Open a new tiled (embedded, non-floating) pane
NewTiledPane(Option<Direction>, Option<RunCommandAction>, Option<String>), // String is an
/// Open a new pane in place of the focused one, suppressing it instead
NewInPlacePane(Option<RunCommandAction>, Option<String>), // String is an
// optional pane
// name
/// Embed focused pane in tab if floating or float focused pane if embedded
TogglePaneEmbedOrFloating,
/// Toggle the visibility of all floating panes (if any) in the current Tab
ToggleFloatingPanes,
/// Close the focus pane.
CloseFocus,
PaneNameInput(Vec<u8>),
UndoRenamePane,
/// Create a new tab, optionally with a specified tab layout.
NewTab(
Option<TiledPaneLayout>,
Vec<FloatingPaneLayout>,
Option<Vec<SwapTiledLayout>>,
Option<Vec<SwapFloatingLayout>>,
Option<String>,
bool, // should_change_focus_to_new_tab
), // the String is the tab name
/// Do nothing.
NoOp,
/// Go to the next tab.
GoToNextTab,
/// Go to the previous tab.
GoToPreviousTab,
/// Close the current tab.
CloseTab,
GoToTab(u32),
GoToTabName(String, bool),
ToggleTab,
TabNameInput(Vec<u8>),
UndoRenameTab,
MoveTab(Direction),
/// Run specified command in new pane.
Run(RunCommandAction),
/// Detach session and exit
Detach,
LeftClick(Position),
RightClick(Position),
MiddleClick(Position),
LaunchOrFocusPlugin(RunPluginOrAlias, bool, bool, bool, bool), // bools => should float,
// move_to_focused_tab, should_open_in_place, skip_cache
LaunchPlugin(RunPluginOrAlias, bool, bool, bool, Option<PathBuf>), // bools => should float,
// should_open_in_place, skip_cache, Option<PathBuf> is cwd
LeftMouseRelease(Position),
RightMouseRelease(Position),
MiddleMouseRelease(Position),
MouseHoldLeft(Position),
MouseHoldRight(Position),
MouseHoldMiddle(Position),
Copy,
/// Confirm a prompt
Confirm,
/// Deny a prompt
Deny,
/// Confirm an action that invokes a prompt automatically
SkipConfirm(Box<Action>),
/// Search for String
SearchInput(Vec<u8>),
/// Search for something
Search(SearchDirection),
/// Toggle case sensitivity of search
SearchToggleOption(SearchOption),
ToggleMouseMode,
PreviousSwapLayout,
NextSwapLayout,
/// Query all tab names
QueryTabNames,
/// Open a new tiled (embedded, non-floating) plugin pane
NewTiledPluginPane(RunPluginOrAlias, Option<String>, bool, Option<PathBuf>), // String is an optional name, bool is
// skip_cache, Option<PathBuf> is cwd
NewFloatingPluginPane(
RunPluginOrAlias,
Option<String>,
bool,
Option<PathBuf>,
Option<FloatingPaneCoordinates>,
), // String is an optional name, bool is
// skip_cache, Option<PathBuf> is cwd
NewInPlacePluginPane(RunPluginOrAlias, Option<String>, bool), // String is an optional name, bool is
// skip_cache
StartOrReloadPlugin(RunPluginOrAlias),
CloseTerminalPane(u32),
ClosePluginPane(u32),
FocusTerminalPaneWithId(u32, bool), // bool is should_float_if_hidden
FocusPluginPaneWithId(u32, bool), // bool is should_float_if_hidden
RenameTerminalPane(u32, Vec<u8>),
RenamePluginPane(u32, Vec<u8>),
RenameTab(u32, Vec<u8>),
BreakPane,
BreakPaneRight,
BreakPaneLeft,
RenameSession(String),
CliPipe {
pipe_id: String,
name: Option<String>,
payload: Option<String>,
args: Option<BTreeMap<String, String>>,
plugin: Option<String>,
configuration: Option<BTreeMap<String, String>>,
launch_new: bool,
skip_cache: bool,
floating: Option<bool>,
in_place: Option<bool>,
cwd: Option<PathBuf>,
pane_title: Option<String>,
},
KeybindPipe {
name: Option<String>,
payload: Option<String>,
args: Option<BTreeMap<String, String>>,
plugin: Option<String>,
plugin_id: Option<u32>, // supercedes plugin if present
configuration: Option<BTreeMap<String, String>>,
launch_new: bool,
skip_cache: bool,
floating: Option<bool>,
in_place: Option<bool>,
cwd: Option<PathBuf>,
pane_title: Option<String>,
},
ListClients,
}
impl Action {
/// Checks that two Action are match except their mutable attributes.
pub fn shallow_eq(&self, other_action: &Action) -> bool {
match (self, other_action) {
(Action::NewTab(..), Action::NewTab(..)) => true,
(Action::LaunchOrFocusPlugin(..), Action::LaunchOrFocusPlugin(..)) => true,
(Action::LaunchPlugin(..), Action::LaunchPlugin(..)) => true,
_ => self == other_action,
}
}
pub fn actions_from_cli(
cli_action: CliAction,
get_current_dir: Box<dyn Fn() -> PathBuf>,
config: Option<Config>,
) -> Result<Vec<Action>, String> {
match cli_action {
CliAction::Write { bytes } => Ok(vec![Action::Write(None, bytes, false)]),
CliAction::WriteChars { chars } => Ok(vec![Action::WriteChars(chars)]),
CliAction::Resize { resize, direction } => Ok(vec![Action::Resize(resize, direction)]),
CliAction::FocusNextPane => Ok(vec![Action::FocusNextPane]),
CliAction::FocusPreviousPane => Ok(vec![Action::FocusPreviousPane]),
CliAction::MoveFocus { direction } => Ok(vec![Action::MoveFocus(direction)]),
CliAction::MoveFocusOrTab { direction } => Ok(vec![Action::MoveFocusOrTab(direction)]),
CliAction::MovePane { direction } => Ok(vec![Action::MovePane(direction)]),
CliAction::MovePaneBackwards => Ok(vec![Action::MovePaneBackwards]),
CliAction::MoveTab { direction } => Ok(vec![Action::MoveTab(direction)]),
CliAction::Clear => Ok(vec![Action::ClearScreen]),
CliAction::DumpScreen { path, full } => Ok(vec![Action::DumpScreen(
path.as_os_str().to_string_lossy().into(),
full,
)]),
CliAction::DumpLayout => Ok(vec![Action::DumpLayout]),
CliAction::EditScrollback => Ok(vec![Action::EditScrollback]),
CliAction::ScrollUp => Ok(vec![Action::ScrollUp]),
CliAction::ScrollDown => Ok(vec![Action::ScrollDown]),
CliAction::ScrollToBottom => Ok(vec![Action::ScrollToBottom]),
CliAction::ScrollToTop => Ok(vec![Action::ScrollToTop]),
CliAction::PageScrollUp => Ok(vec![Action::PageScrollUp]),
CliAction::PageScrollDown => Ok(vec![Action::PageScrollDown]),
CliAction::HalfPageScrollUp => Ok(vec![Action::HalfPageScrollUp]),
CliAction::HalfPageScrollDown => Ok(vec![Action::HalfPageScrollDown]),
CliAction::ToggleFullscreen => Ok(vec![Action::ToggleFocusFullscreen]),
CliAction::TogglePaneFrames => Ok(vec![Action::TogglePaneFrames]),
CliAction::ToggleActiveSyncTab => Ok(vec![Action::ToggleActiveSyncTab]),
CliAction::NewPane {
direction,
command,
plugin,
cwd,
floating,
in_place,
name,
close_on_exit,
start_suspended,
configuration,
skip_plugin_cache,
x,
y,
width,
height,
} => {
let current_dir = get_current_dir();
// cwd should only be specified in a plugin alias if it was explicitly given to us,
// otherwise the current_dir might override a cwd defined in the alias itself
let alias_cwd = cwd.clone().map(|cwd| current_dir.join(cwd));
let cwd = cwd
.map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir.clone()));
if let Some(plugin) = plugin {
let plugin = match RunPluginLocation::parse(&plugin, cwd.clone()) {
Ok(location) => {
let user_configuration = configuration.unwrap_or_default();
RunPluginOrAlias::RunPlugin(RunPlugin {
_allow_exec_host_cmd: false,
location,
configuration: user_configuration,
initial_cwd: cwd.clone(),
})
},
Err(_) => {
let mut plugin_alias = PluginAlias::new(
&plugin,
&configuration.map(|c| c.inner().clone()),
alias_cwd,
);
plugin_alias.set_caller_cwd_if_not_set(Some(current_dir));
RunPluginOrAlias::Alias(plugin_alias)
},
};
if floating {
Ok(vec![Action::NewFloatingPluginPane(
plugin,
name,
skip_plugin_cache,
cwd,
FloatingPaneCoordinates::new(x, y, width, height),
)])
} else if in_place {
Ok(vec![Action::NewInPlacePluginPane(
plugin,
name,
skip_plugin_cache,
)])
} else {
// it is intentional that a new tiled plugin pane cannot include a
// direction
// this is because the cli client opening a tiled plugin pane is a
// different client than the one opening the pane, and this can potentially
// create very confusing races if the client changes focus while the plugin
// is being loaded
// this is not the case with terminal panes for historical reasons of
// backwards compatibility to a time before we had auto layouts
Ok(vec![Action::NewTiledPluginPane(
plugin,
name,
skip_plugin_cache,
cwd,
)])
}
} else if !command.is_empty() {
let mut command = command.clone();
let (command, args) = (PathBuf::from(command.remove(0)), command);
let hold_on_start = start_suspended;
let hold_on_close = !close_on_exit;
let run_command_action = RunCommandAction {
command,
args,
cwd,
direction,
hold_on_close,
hold_on_start,
..Default::default()
};
if floating {
Ok(vec![Action::NewFloatingPane(
Some(run_command_action),
name,
FloatingPaneCoordinates::new(x, y, width, height),
)])
} else if in_place {
Ok(vec![Action::NewInPlacePane(Some(run_command_action), name)])
} else {
Ok(vec![Action::NewTiledPane(
direction,
Some(run_command_action),
name,
)])
}
} else {
if floating {
Ok(vec![Action::NewFloatingPane(
None,
name,
FloatingPaneCoordinates::new(x, y, width, height),
)])
} else if in_place {
Ok(vec![Action::NewInPlacePane(None, name)])
} else {
Ok(vec![Action::NewTiledPane(direction, None, name)])
}
}
},
CliAction::Edit {
direction,
file,
line_number,
floating,
in_place,
cwd,
x,
y,
width,
height,
} => {
let mut file = file;
let current_dir = get_current_dir();
let cwd = cwd
.map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir));
if file.is_relative() {
if let Some(cwd) = cwd.as_ref() {
file = cwd.join(file);
}
}
let start_suppressed = false;
Ok(vec![Action::EditFile(
OpenFilePayload::new(file, line_number, cwd),
direction,
floating,
in_place,
start_suppressed,
FloatingPaneCoordinates::new(x, y, width, height),
)])
},
CliAction::SwitchMode { input_mode } => {
Ok(vec![Action::SwitchModeForAllClients(input_mode)])
},
CliAction::TogglePaneEmbedOrFloating => Ok(vec![Action::TogglePaneEmbedOrFloating]),
CliAction::ToggleFloatingPanes => Ok(vec![Action::ToggleFloatingPanes]),
CliAction::ClosePane => Ok(vec![Action::CloseFocus]),
CliAction::RenamePane { name } => Ok(vec![
Action::UndoRenamePane,
Action::PaneNameInput(name.as_bytes().to_vec()),
]),
CliAction::UndoRenamePane => Ok(vec![Action::UndoRenamePane]),
CliAction::GoToNextTab => Ok(vec![Action::GoToNextTab]),
CliAction::GoToPreviousTab => Ok(vec![Action::GoToPreviousTab]),
CliAction::CloseTab => Ok(vec![Action::CloseTab]),
CliAction::GoToTab { index } => Ok(vec![Action::GoToTab(index)]),
CliAction::GoToTabName { name, create } => Ok(vec![Action::GoToTabName(name, create)]),
CliAction::RenameTab { name } => Ok(vec![
Action::TabNameInput(vec![0]),
Action::TabNameInput(name.as_bytes().to_vec()),
]),
CliAction::UndoRenameTab => Ok(vec![Action::UndoRenameTab]),
CliAction::NewTab {
name,
layout,
layout_dir,
cwd,
} => {
let current_dir = get_current_dir();
let cwd = cwd
.map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir));
if let Some(layout_path) = layout {
let layout_dir = layout_dir
.or_else(|| config.and_then(|c| c.options.layout_dir))
.or_else(|| get_layout_dir(find_default_config_dir()));
let mut should_start_layout_commands_suspended = false;
let (path_to_raw_layout, raw_layout, swap_layouts) = if let Some(layout_url) =
layout_path.to_str().and_then(|l| {
if l.starts_with("http://") || l.starts_with("https://") {
Some(l)
} else {
None
}
}) {
should_start_layout_commands_suspended = true;
(
layout_url.to_owned(),
Layout::stringified_from_url(layout_url)
.map_err(|e| format!("Failed to load layout: {}", e))?,
None,
)
} else {
Layout::stringified_from_path_or_default(Some(&layout_path), layout_dir)
.map_err(|e| format!("Failed to load layout: {}", e))?
};
let mut layout = Layout::from_str(&raw_layout, path_to_raw_layout, swap_layouts.as_ref().map(|(f, p)| (f.as_str(), p.as_str())), cwd).map_err(|e| {
let stringified_error = match e {
ConfigError::KdlError(kdl_error) => {
let error = kdl_error.add_src(layout_path.as_path().as_os_str().to_string_lossy().to_string(), String::from(raw_layout));
let report: Report = error.into();
format!("{:?}", report)
}
ConfigError::KdlDeserializationError(kdl_error) => {
let error_message = match kdl_error.kind {
kdl::KdlErrorKind::Context("valid node terminator") => {
format!("Failed to deserialize KDL node. \nPossible reasons:\n{}\n{}\n{}\n{}",
"- Missing `;` after a node name, eg. { node; another_node; }",
"- Missing quotations (\") around an argument node eg. { first_node \"argument_node\"; }",
"- Missing an equal sign (=) between node arguments on a title line. eg. argument=\"value\"",
"- Found an extraneous equal sign (=) between node child arguments and their values. eg. { argument=\"value\" }")
},
_ => String::from(kdl_error.help.unwrap_or("Kdl Deserialization Error")),
};
let kdl_error = KdlError {
error_message,
src: Some(NamedSource::new(layout_path.as_path().as_os_str().to_string_lossy().to_string(), String::from(raw_layout))),
offset: Some(kdl_error.span.offset()),
len: Some(kdl_error.span.len()),
help_message: None,
};
let report: Report = kdl_error.into();
format!("{:?}", report)
},
e => format!("{}", e)
};
stringified_error
})?;
if should_start_layout_commands_suspended {
layout.recursively_add_start_suspended_including_template(Some(true));
}
let mut tabs = layout.tabs();
if !tabs.is_empty() {
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
let swap_floating_layouts = Some(layout.swap_floating_layouts.clone());
let mut new_tab_actions = vec![];
let mut has_focused_tab = tabs
.iter()
.any(|(_, layout, _)| layout.focus.unwrap_or(false));
for (tab_name, layout, floating_panes_layout) in tabs.drain(..) {
let name = tab_name.or_else(|| name.clone());
let should_change_focus_to_new_tab =
layout.focus.unwrap_or_else(|| {
if !has_focused_tab {
has_focused_tab = true;
true
} else {
false
}
});
new_tab_actions.push(Action::NewTab(
Some(layout),
floating_panes_layout,
swap_tiled_layouts.clone(),
swap_floating_layouts.clone(),
name,
should_change_focus_to_new_tab,
));
}
Ok(new_tab_actions)
} else {
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
let swap_floating_layouts = Some(layout.swap_floating_layouts.clone());
let (layout, floating_panes_layout) = layout.new_tab();
let should_change_focus_to_new_tab = true;
Ok(vec![Action::NewTab(
Some(layout),
floating_panes_layout,
swap_tiled_layouts,
swap_floating_layouts,
name,
should_change_focus_to_new_tab,
)])
}
} else {
let should_change_focus_to_new_tab = true;
Ok(vec![Action::NewTab(
None,
vec![],
None,
None,
name,
should_change_focus_to_new_tab,
)])
}
},
CliAction::PreviousSwapLayout => Ok(vec![Action::PreviousSwapLayout]),
CliAction::NextSwapLayout => Ok(vec![Action::NextSwapLayout]),
CliAction::QueryTabNames => Ok(vec![Action::QueryTabNames]),
CliAction::StartOrReloadPlugin { url, configuration } => {
let current_dir = get_current_dir();
let run_plugin_or_alias = RunPluginOrAlias::from_url(
&url,
&configuration.map(|c| c.inner().clone()),
None,
Some(current_dir),
)?;
Ok(vec![Action::StartOrReloadPlugin(run_plugin_or_alias)])
},
CliAction::LaunchOrFocusPlugin {
url,
floating,
in_place,
move_to_focused_tab,
configuration,
skip_plugin_cache,
} => {
let current_dir = get_current_dir();
let run_plugin_or_alias = RunPluginOrAlias::from_url(
url.as_str(),
&configuration.map(|c| c.inner().clone()),
None,
Some(current_dir),
)?;
Ok(vec![Action::LaunchOrFocusPlugin(
run_plugin_or_alias,
floating,
move_to_focused_tab,
in_place,
skip_plugin_cache,
)])
},
CliAction::LaunchPlugin {
url,
floating,
in_place,
configuration,
skip_plugin_cache,
} => {
let current_dir = get_current_dir();
let run_plugin_or_alias = RunPluginOrAlias::from_url(
&url.as_str(),
&configuration.map(|c| c.inner().clone()),
None,
Some(current_dir.clone()),
)?;
Ok(vec![Action::LaunchPlugin(
run_plugin_or_alias,
floating,
in_place,
skip_plugin_cache,
Some(current_dir),
)])
},
CliAction::RenameSession { name } => Ok(vec![Action::RenameSession(name)]),
CliAction::Pipe {
name,
payload,
args,
plugin,
plugin_configuration,
force_launch_plugin,
skip_plugin_cache,
floating_plugin,
in_place_plugin,
plugin_cwd,
plugin_title,
} => {
let current_dir = get_current_dir();
let cwd = plugin_cwd
.map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir));
let skip_cache = skip_plugin_cache;
let pipe_id = Uuid::new_v4().to_string();
Ok(vec![Action::CliPipe {
pipe_id,
name,
payload,
args: args.map(|a| a.inner().clone()), // TODO: no clone somehow
plugin,
configuration: plugin_configuration.map(|a| a.inner().clone()), // TODO: no clone
// somehow
launch_new: force_launch_plugin,
floating: floating_plugin,
in_place: in_place_plugin,
cwd,
pane_title: plugin_title,
skip_cache,
}])
},
CliAction::ListClients => Ok(vec![Action::ListClients]),
}
}
pub fn launches_plugin(&self, plugin_url: &str) -> bool {
match self {
Action::LaunchPlugin(run_plugin_or_alias, ..) => {
&run_plugin_or_alias.location_string() == plugin_url
},
Action::LaunchOrFocusPlugin(run_plugin_or_alias, ..) => {
&run_plugin_or_alias.location_string() == plugin_url
},
_ => false,
}
}
}
impl From<OnForceClose> for Action {
fn from(ofc: OnForceClose) -> Action {
match ofc {
OnForceClose::Quit => Action::Quit,
OnForceClose::Detach => Action::Detach,
}
}
}
|