feat: support default cwd (#2290)

* init commit

* add default config to kdl file

* change the field from `default_cwd` to `override_cwd`

* change back to default_cwd

* fix test

* default cwd works without `default_shell`
This commit is contained in:
Kangaxx-0 2023-04-18 07:35:51 -07:00 committed by GitHub
parent cecd7b2b7f
commit 4c87b1e6bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
24 changed files with 56 additions and 1 deletions

View file

@ -343,9 +343,11 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
let default_shell = config_options.default_shell.map(|shell| { let default_shell = config_options.default_shell.map(|shell| {
TerminalAction::RunCommand(RunCommand { TerminalAction::RunCommand(RunCommand {
command: shell, command: shell,
cwd: config_options.default_cwd.clone(),
..Default::default() ..Default::default()
}) })
}); });
let cwd = config_options.default_cwd;
let spawn_tabs = |tab_layout, floating_panes_layout, tab_name, swap_layouts| { let spawn_tabs = |tab_layout, floating_panes_layout, tab_name, swap_layouts| {
session_data session_data
@ -355,6 +357,7 @@ pub fn start_server(mut os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
.unwrap() .unwrap()
.senders .senders
.send_to_screen(ScreenInstruction::NewTab( .send_to_screen(ScreenInstruction::NewTab(
cwd.clone(),
default_shell.clone(), default_shell.clone(),
tab_layout, tab_layout,
floating_panes_layout, floating_panes_layout,

View file

@ -36,6 +36,7 @@ pub enum PluginInstruction {
AddClient(ClientId), AddClient(ClientId),
RemoveClient(ClientId), RemoveClient(ClientId),
NewTab( NewTab(
Option<PathBuf>,
Option<TerminalAction>, Option<TerminalAction>,
Option<TiledPaneLayout>, Option<TiledPaneLayout>,
Vec<FloatingPaneLayout>, Vec<FloatingPaneLayout>,
@ -112,6 +113,7 @@ pub(crate) fn plugin_thread_main(
wasm_bridge.remove_client(client_id); wasm_bridge.remove_client(client_id);
}, },
PluginInstruction::NewTab( PluginInstruction::NewTab(
cwd,
terminal_action, terminal_action,
tab_layout, tab_layout,
floating_panes_layout, floating_panes_layout,
@ -142,6 +144,7 @@ pub(crate) fn plugin_thread_main(
} }
} }
drop(bus.senders.send_to_pty(PtyInstruction::NewTab( drop(bus.senders.send_to_pty(PtyInstruction::NewTab(
cwd,
terminal_action, terminal_action,
tab_layout, tab_layout,
floating_panes_layout, floating_panes_layout,

View file

@ -48,6 +48,7 @@ pub enum PtyInstruction {
UpdateActivePane(Option<PaneId>, ClientId), UpdateActivePane(Option<PaneId>, ClientId),
GoToTab(TabIndex, ClientId), GoToTab(TabIndex, ClientId),
NewTab( NewTab(
Option<PathBuf>,
Option<TerminalAction>, Option<TerminalAction>,
Option<TiledPaneLayout>, Option<TiledPaneLayout>,
Vec<FloatingPaneLayout>, Vec<FloatingPaneLayout>,
@ -336,6 +337,7 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
})?; })?;
}, },
PtyInstruction::NewTab( PtyInstruction::NewTab(
cwd,
terminal_action, terminal_action,
tab_layout, tab_layout,
floating_panes_layout, floating_panes_layout,
@ -351,6 +353,7 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
floating_panes_layout floating_panes_layout
}; };
pty.spawn_terminals_for_layout( pty.spawn_terminals_for_layout(
cwd,
tab_layout.unwrap_or_else(|| layout.new_tab().0), tab_layout.unwrap_or_else(|| layout.new_tab().0),
floating_panes_layout, floating_panes_layout,
terminal_action.clone(), terminal_action.clone(),
@ -591,6 +594,7 @@ impl Pty {
} }
pub fn spawn_terminals_for_layout( pub fn spawn_terminals_for_layout(
&mut self, &mut self,
cwd: Option<PathBuf>,
layout: TiledPaneLayout, layout: TiledPaneLayout,
floating_panes_layout: Vec<FloatingPaneLayout>, floating_panes_layout: Vec<FloatingPaneLayout>,
default_shell: Option<TerminalAction>, default_shell: Option<TerminalAction>,
@ -601,7 +605,7 @@ impl Pty {
let err_context = || format!("failed to spawn terminals for layout for client {client_id}"); let err_context = || format!("failed to spawn terminals for layout for client {client_id}");
let mut default_shell = let mut default_shell =
default_shell.unwrap_or_else(|| self.get_default_terminal(None, None)); default_shell.unwrap_or_else(|| self.get_default_terminal(cwd, None));
self.fill_cwd(&mut default_shell, client_id); self.fill_cwd(&mut default_shell, client_id);
let extracted_run_instructions = layout.extract_run_instructions(); let extracted_run_instructions = layout.extract_run_instructions();
let extracted_floating_run_instructions = let extracted_floating_run_instructions =

View file

@ -464,6 +464,7 @@ pub(crate) fn route_action(
session session
.senders .senders
.send_to_screen(ScreenInstruction::NewTab( .send_to_screen(ScreenInstruction::NewTab(
None,
shell, shell,
tab_layout, tab_layout,
floating_panes_layout, floating_panes_layout,

View file

@ -2,6 +2,7 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::{BTreeMap, HashMap, HashSet}; use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::PathBuf;
use std::rc::Rc; use std::rc::Rc;
use std::str; use std::str;
@ -190,6 +191,7 @@ pub enum ScreenInstruction {
UpdatePaneName(Vec<u8>, ClientId), UpdatePaneName(Vec<u8>, ClientId),
UndoRenamePane(ClientId), UndoRenamePane(ClientId),
NewTab( NewTab(
Option<PathBuf>,
Option<TerminalAction>, Option<TerminalAction>,
Option<TiledPaneLayout>, Option<TiledPaneLayout>,
Vec<FloatingPaneLayout>, Vec<FloatingPaneLayout>,
@ -2097,6 +2099,7 @@ pub(crate) fn screen_thread_main(
screen.render()?; screen.render()?;
}, },
ScreenInstruction::NewTab( ScreenInstruction::NewTab(
cwd,
default_shell, default_shell,
layout, layout,
floating_panes_layout, floating_panes_layout,
@ -2111,6 +2114,7 @@ pub(crate) fn screen_thread_main(
.bus .bus
.senders .senders
.send_to_plugin(PluginInstruction::NewTab( .send_to_plugin(PluginInstruction::NewTab(
cwd,
default_shell, default_shell,
layout, layout,
floating_panes_layout, floating_panes_layout,
@ -2202,6 +2206,7 @@ pub(crate) fn screen_thread_main(
.bus .bus
.senders .senders
.send_to_plugin(PluginInstruction::NewTab( .send_to_plugin(PluginInstruction::NewTab(
None,
default_shell, default_shell,
None, None,
vec![], vec![],

View file

@ -299,6 +299,7 @@ impl MockScreen {
let tab_name = None; let tab_name = None;
let tab_index = self.last_opened_tab_index.map(|l| l + 1).unwrap_or(0); let tab_index = self.last_opened_tab_index.map(|l| l + 1).unwrap_or(0);
let _ = self.to_screen.send(ScreenInstruction::NewTab( let _ = self.to_screen.send(ScreenInstruction::NewTab(
None,
default_shell, default_shell,
Some(pane_layout.clone()), Some(pane_layout.clone()),
vec![], // floating_panes_layout vec![], // floating_panes_layout
@ -329,6 +330,7 @@ impl MockScreen {
pane_ids.push((i as u32, None)); pane_ids.push((i as u32, None));
} }
let _ = self.to_screen.send(ScreenInstruction::NewTab( let _ = self.to_screen.send(ScreenInstruction::NewTab(
None,
default_shell, default_shell,
Some(tab_layout.clone()), Some(tab_layout.clone()),
vec![], // floating_panes_layout vec![], // floating_panes_layout

View file

@ -4,6 +4,7 @@ expression: "format!(\"{:#?}\", new_tab_action)"
--- ---
Some( Some(
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {

View file

@ -4,6 +4,7 @@ assertion_line: 2448
expression: "format!(\"{:#?}\", new_tab_instruction)" expression: "format!(\"{:#?}\", new_tab_instruction)"
--- ---
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {

View file

@ -27,6 +27,7 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())"
], ],
), ),
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {
@ -187,6 +188,7 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())"
], ],
), ),
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {

View file

@ -27,6 +27,7 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())"
], ],
), ),
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {
@ -187,6 +188,7 @@ expression: "format!(\"{:#?}\", * received_plugin_instructions.lock().unwrap())"
], ],
), ),
NewTab( NewTab(
None,
None, None,
Some( Some(
TiledPaneLayout { TiledPaneLayout {

View file

@ -200,6 +200,10 @@ plugins {
// //
// default_shell "fish" // default_shell "fish"
// Choose the path to override cwd that zellij will use for opening new panes
//
// default_cwd ""
// Toggle between having pane frames around the panes // Toggle between having pane frames around the panes
// Options: // Options:
// - true (default) // - true (default)

View file

@ -303,6 +303,7 @@ mod config_test {
theme "my cool theme" theme "my cool theme"
default_mode "locked" default_mode "locked"
default_shell "/path/to/my/shell" default_shell "/path/to/my/shell"
default_cwd "/path"
default_layout "/path/to/my/layout.kdl" default_layout "/path/to/my/layout.kdl"
layout_dir "/path/to/my/layout-dir" layout_dir "/path/to/my/layout-dir"
theme_dir "/path/to/my/theme-dir" theme_dir "/path/to/my/theme-dir"
@ -339,6 +340,11 @@ mod config_test {
Some(PathBuf::from("/path/to/my/shell")), Some(PathBuf::from("/path/to/my/shell")),
"Option set in config" "Option set in config"
); );
assert_eq!(
config.options.default_cwd,
Some(PathBuf::from("/path")),
"Option set in config"
);
assert_eq!( assert_eq!(
config.options.default_layout, config.options.default_layout,
Some(PathBuf::from("/path/to/my/layout.kdl")), Some(PathBuf::from("/path/to/my/layout.kdl")),

View file

@ -52,6 +52,9 @@ pub struct Options {
/// Set the default shell /// Set the default shell
#[clap(long, value_parser)] #[clap(long, value_parser)]
pub default_shell: Option<PathBuf>, pub default_shell: Option<PathBuf>,
/// Set the default cwd
#[clap(long, value_parser)]
pub default_cwd: Option<PathBuf>,
/// Set the default layout /// Set the default layout
#[clap(long, value_parser)] #[clap(long, value_parser)]
pub default_layout: Option<PathBuf>, pub default_layout: Option<PathBuf>,
@ -167,6 +170,7 @@ impl Options {
let simplified_ui = other.simplified_ui.or(self.simplified_ui); let simplified_ui = other.simplified_ui.or(self.simplified_ui);
let default_mode = other.default_mode.or(self.default_mode); let default_mode = other.default_mode.or(self.default_mode);
let default_shell = other.default_shell.or_else(|| self.default_shell.clone()); let default_shell = other.default_shell.or_else(|| self.default_shell.clone());
let default_cwd = other.default_cwd.or_else(|| self.default_cwd.clone());
let default_layout = other.default_layout.or_else(|| self.default_layout.clone()); let default_layout = other.default_layout.or_else(|| self.default_layout.clone());
let layout_dir = other.layout_dir.or_else(|| self.layout_dir.clone()); let layout_dir = other.layout_dir.or_else(|| self.layout_dir.clone());
let theme_dir = other.theme_dir.or_else(|| self.theme_dir.clone()); let theme_dir = other.theme_dir.or_else(|| self.theme_dir.clone());
@ -189,6 +193,7 @@ impl Options {
theme, theme,
default_mode, default_mode,
default_shell, default_shell,
default_cwd,
default_layout, default_layout,
layout_dir, layout_dir,
theme_dir, theme_dir,
@ -230,6 +235,7 @@ impl Options {
let default_mode = other.default_mode.or(self.default_mode); let default_mode = other.default_mode.or(self.default_mode);
let default_shell = other.default_shell.or_else(|| self.default_shell.clone()); let default_shell = other.default_shell.or_else(|| self.default_shell.clone());
let default_cwd = other.default_cwd.or_else(|| self.default_cwd.clone());
let default_layout = other.default_layout.or_else(|| self.default_layout.clone()); let default_layout = other.default_layout.or_else(|| self.default_layout.clone());
let layout_dir = other.layout_dir.or_else(|| self.layout_dir.clone()); let layout_dir = other.layout_dir.or_else(|| self.layout_dir.clone());
let theme_dir = other.theme_dir.or_else(|| self.theme_dir.clone()); let theme_dir = other.theme_dir.or_else(|| self.theme_dir.clone());
@ -252,6 +258,7 @@ impl Options {
theme, theme,
default_mode, default_mode,
default_shell, default_shell,
default_cwd,
default_layout, default_layout,
layout_dir, layout_dir,
theme_dir, theme_dir,
@ -310,6 +317,7 @@ impl From<CliOptions> for Options {
theme: opts.theme, theme: opts.theme,
default_mode: opts.default_mode, default_mode: opts.default_mode,
default_shell: opts.default_shell, default_shell: opts.default_shell,
default_cwd: opts.default_cwd,
default_layout: opts.default_layout, default_layout: opts.default_layout,
layout_dir: opts.layout_dir, layout_dir: opts.layout_dir,
theme_dir: opts.theme_dir, theme_dir: opts.theme_dir,

View file

@ -1304,6 +1304,8 @@ impl Options {
let default_shell = let default_shell =
kdl_property_first_arg_as_string_or_error!(kdl_options, "default_shell") kdl_property_first_arg_as_string_or_error!(kdl_options, "default_shell")
.map(|(string, _entry)| PathBuf::from(string)); .map(|(string, _entry)| PathBuf::from(string));
let default_cwd = kdl_property_first_arg_as_string_or_error!(kdl_options, "default_cwd")
.map(|(string, _entry)| PathBuf::from(string));
let pane_frames = let pane_frames =
kdl_property_first_arg_as_bool_or_error!(kdl_options, "pane_frames").map(|(v, _)| v); kdl_property_first_arg_as_bool_or_error!(kdl_options, "pane_frames").map(|(v, _)| v);
let auto_layout = let auto_layout =
@ -1358,6 +1360,7 @@ impl Options {
theme, theme,
default_mode, default_mode,
default_shell, default_shell,
default_cwd,
default_layout, default_layout,
layout_dir, layout_dir,
theme_dir, theme_dir,

View file

@ -10,6 +10,7 @@ Options {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -8,6 +8,7 @@ Options {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -8,6 +8,7 @@ Options {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -3525,6 +3525,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -3525,6 +3525,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -65,6 +65,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -8,6 +8,7 @@ Options {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -3525,6 +3525,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -3525,6 +3525,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,

View file

@ -3525,6 +3525,7 @@ Config {
theme: None, theme: None,
default_mode: None, default_mode: None,
default_shell: None, default_shell: None,
default_cwd: None,
default_layout: None, default_layout: None,
layout_dir: None, layout_dir: None,
theme_dir: None, theme_dir: None,