Skip to content

Commit

Permalink
Merge pull request #594 from a-kenji/default-shell
Browse files Browse the repository at this point in the history
Add an option to specify a default-shell
  • Loading branch information
a-kenji authored Jul 1, 2021
2 parents 71f980a + 6f1c103 commit 8524f52
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 16 deletions.
26 changes: 24 additions & 2 deletions zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ use zellij_utils::{
channels::{self, ChannelWithContext, SenderWithContext},
cli::CliArgs,
errors::{ContextType, ErrorInstruction, ServerContext},
input::{get_mode_info, layout::Layout, options::Options},
input::{
command::{RunCommand, TerminalAction},
get_mode_info,
layout::Layout,
options::Options,
},
ipc::{ClientAttributes, ClientToServerMsg, ExitReason, ServerToClientMsg},
setup::get_default_data_dir,
};
Expand Down Expand Up @@ -84,6 +89,7 @@ pub(crate) struct SessionMetaData {
pub senders: ThreadSenders,
pub capabilities: PluginCapabilities,
pub palette: Palette,
pub default_shell: Option<TerminalAction>,
screen_thread: Option<thread::JoinHandle<()>>,
pty_thread: Option<thread::JoinHandle<()>>,
wasm_thread: Option<thread::JoinHandle<()>>,
Expand Down Expand Up @@ -219,13 +225,21 @@ pub fn start_server(os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) {
);
*session_data.write().unwrap() = Some(session);
*session_state.write().unwrap() = SessionState::Attached;

let default_shell = session_data
.read()
.unwrap()
.as_ref()
.map(|session| session.default_shell.clone())
.flatten();

session_data
.read()
.unwrap()
.as_ref()
.unwrap()
.senders
.send_to_pty(PtyInstruction::NewTab)
.send_to_pty(PtyInstruction::NewTab(default_shell.clone()))
.unwrap();
}
ServerInstruction::AttachClient(attrs, _, options) => {
Expand Down Expand Up @@ -324,6 +338,13 @@ fn init_session(
arrow_fonts: config_options.simplified_ui,
};

let default_shell = config_options.default_shell.clone().map(|command| {
TerminalAction::RunCommand(RunCommand {
command,
..Default::default()
})
});

let pty_thread = thread::Builder::new()
.name("pty".to_string())
.spawn({
Expand Down Expand Up @@ -393,6 +414,7 @@ fn init_session(
should_silently_fail: false,
},
capabilities,
default_shell,
palette: client_attributes.palette,
screen_thread: Some(screen_thread),
pty_thread: Some(pty_thread),
Expand Down
24 changes: 16 additions & 8 deletions zellij-server/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub(crate) enum PtyInstruction {
SpawnTerminal(Option<TerminalAction>),
SpawnTerminalVertically(Option<TerminalAction>),
SpawnTerminalHorizontally(Option<TerminalAction>),
NewTab,
NewTab(Option<TerminalAction>),
ClosePane(PaneId),
CloseTab(Vec<PaneId>),
Exit,
Expand All @@ -42,7 +42,7 @@ impl From<&PtyInstruction> for PtyContext {
PtyInstruction::SpawnTerminalHorizontally(_) => PtyContext::SpawnTerminalHorizontally,
PtyInstruction::ClosePane(_) => PtyContext::ClosePane,
PtyInstruction::CloseTab(_) => PtyContext::CloseTab,
PtyInstruction::NewTab => PtyContext::NewTab,
PtyInstruction::NewTab(_) => PtyContext::NewTab,
PtyInstruction::Exit => PtyContext::Exit,
}
}
Expand Down Expand Up @@ -81,11 +81,11 @@ pub(crate) fn pty_thread_main(mut pty: Pty, maybe_layout: Option<Layout>) {
.send_to_screen(ScreenInstruction::HorizontalSplit(PaneId::Terminal(pid)))
.unwrap();
}
PtyInstruction::NewTab => {
PtyInstruction::NewTab(terminal_action) => {
if let Some(layout) = maybe_layout.clone() {
pty.spawn_terminals_for_layout(layout);
pty.spawn_terminals_for_layout(layout, terminal_action);
} else {
let pid = pty.spawn_terminal(None);
let pid = pty.spawn_terminal(terminal_action);
pty.bus
.senders
.send_to_screen(ScreenInstruction::NewTab(pid))
Expand Down Expand Up @@ -234,12 +234,20 @@ impl Pty {
self.id_to_child_pid.insert(pid_primary, pid_secondary);
pid_primary
}
pub fn spawn_terminals_for_layout(&mut self, layout: Layout) {
pub fn spawn_terminals_for_layout(
&mut self,
layout: Layout,
terminal_action: Option<TerminalAction>,
) {
let total_panes = layout.total_terminal_panes();
let mut new_pane_pids = vec![];
for _ in 0..total_panes {
let (pid_primary, pid_secondary): (RawFd, Pid) =
self.bus.os_input.as_mut().unwrap().spawn_terminal(None);
let (pid_primary, pid_secondary): (RawFd, Pid) = self
.bus
.os_input
.as_mut()
.unwrap()
.spawn_terminal(terminal_action.clone());
self.id_to_child_pid.insert(pid_primary, pid_secondary);
new_pane_pids.push(pid_primary);
}
Expand Down
17 changes: 11 additions & 6 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,14 @@ fn route_action(
.unwrap();
}
Action::NewPane(direction) => {
let shell = session.default_shell.clone();
let pty_instr = match direction {
Some(Direction::Left) => PtyInstruction::SpawnTerminalVertically(None),
Some(Direction::Right) => PtyInstruction::SpawnTerminalVertically(None),
Some(Direction::Up) => PtyInstruction::SpawnTerminalHorizontally(None),
Some(Direction::Down) => PtyInstruction::SpawnTerminalHorizontally(None),
Some(Direction::Left) => PtyInstruction::SpawnTerminalVertically(shell),
Some(Direction::Right) => PtyInstruction::SpawnTerminalVertically(shell),
Some(Direction::Up) => PtyInstruction::SpawnTerminalHorizontally(shell),
Some(Direction::Down) => PtyInstruction::SpawnTerminalHorizontally(shell),
// No direction specified - try to put it in the biggest available spot
None => PtyInstruction::SpawnTerminal(None),
None => PtyInstruction::SpawnTerminal(shell),
};
session.senders.send_to_pty(pty_instr).unwrap();
}
Expand All @@ -150,7 +151,11 @@ fn route_action(
.unwrap();
}
Action::NewTab => {
session.senders.send_to_pty(PtyInstruction::NewTab).unwrap();
let shell = session.default_shell.clone();
session
.senders
.send_to_pty(PtyInstruction::NewTab(shell))
.unwrap();
}
Action::GoToNextTab => {
session
Expand Down
9 changes: 9 additions & 0 deletions zellij-utils/src/input/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub struct Options {
/// Set the default mode
#[structopt(long)]
pub default_mode: Option<InputMode>,
/// Set the default shell
#[structopt(long, parse(from_os_str))]
pub default_shell: Option<PathBuf>,
/// Set the layout_dir, defaults to
/// subdirectory of config dir
#[structopt(long, parse(from_os_str))]
Expand Down Expand Up @@ -50,6 +53,11 @@ impl Options {
other => other,
};

let default_shell = match other.default_shell {
None => self.default_shell.clone(),
other => other,
};

let layout_dir = match other.layout_dir {
None => self.layout_dir.clone(),
other => other,
Expand All @@ -64,6 +72,7 @@ impl Options {
simplified_ui,
theme,
default_mode,
default_shell,
layout_dir,
}
}
Expand Down

0 comments on commit 8524f52

Please sign in to comment.