Skip to content

Commit

Permalink
feat(plugins): send info about $EDITOR and $SHELL
Browse files Browse the repository at this point in the history
  • Loading branch information
imsnif committed Feb 2, 2025
1 parent 1dd6850 commit 5cb214a
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 25 deletions.
1 change: 1 addition & 0 deletions zellij-server/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ impl SessionMetaData {
rounded_corners: new_config.ui.pane_frames.rounded_corners,
hide_session_name: new_config.ui.pane_frames.hide_session_name,
stacked_resize: new_config.options.stacked_resize.unwrap_or(true),
default_editor: new_config.options.scrollback_editor.clone(),
})
.unwrap();
self.senders
Expand Down
53 changes: 45 additions & 8 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use crate::{
panes::sixel::SixelImageStore,
panes::PaneId,
plugins::{PluginId, PluginInstruction, PluginRenderAsset},
pty::{ClientTabIndexOrPaneId, PtyInstruction, VteBytes},
pty::{get_default_shell, ClientTabIndexOrPaneId, PtyInstruction, VteBytes},
tab::{SuppressedPanes, Tab},
thread_bus::Bus,
ui::{
Expand Down Expand Up @@ -368,6 +368,7 @@ pub enum ScreenInstruction {
rounded_corners: bool,
hide_session_name: bool,
stacked_resize: bool,
default_editor: Option<PathBuf>,
},
RerunCommandPane(u32), // u32 - terminal pane id
ResizePaneWithId(ResizeStrategy, PaneId),
Expand Down Expand Up @@ -687,12 +688,13 @@ pub(crate) struct Screen {
resurrectable_sessions: BTreeMap<String, Duration>, // String is the session name, duration is
// its creation time
default_layout: Box<Layout>,
default_shell: Option<PathBuf>,
default_shell: PathBuf,
styled_underlines: bool,
arrow_fonts: bool,
layout_dir: Option<PathBuf>,
default_layout_name: Option<String>,
explicitly_disable_kitty_keyboard_protocol: bool,
default_editor: Option<PathBuf>,
}

impl Screen {
Expand All @@ -709,7 +711,7 @@ impl Screen {
debug: bool,
default_layout: Box<Layout>,
default_layout_name: Option<String>,
default_shell: Option<PathBuf>,
default_shell: PathBuf,
session_serialization: bool,
serialize_pane_viewport: bool,
scrollback_lines_to_serialize: Option<usize>,
Expand All @@ -718,6 +720,7 @@ impl Screen {
layout_dir: Option<PathBuf>,
explicitly_disable_kitty_keyboard_protocol: bool,
stacked_resize: bool,
default_editor: Option<PathBuf>,
) -> Self {
let session_name = mode_info.session_name.clone().unwrap_or_default();
let session_info = SessionInfo::new(session_name.clone());
Expand Down Expand Up @@ -760,6 +763,7 @@ impl Screen {
resurrectable_sessions,
layout_dir,
explicitly_disable_kitty_keyboard_protocol,
default_editor,
}
}

Expand Down Expand Up @@ -1359,6 +1363,7 @@ impl Screen {
self.arrow_fonts,
self.styled_underlines,
self.explicitly_disable_kitty_keyboard_protocol,
self.default_editor.clone(),
);
for (client_id, mode_info) in &self.mode_info {
tab.change_mode_info(mode_info.clone(), *client_id);
Expand Down Expand Up @@ -1650,7 +1655,7 @@ impl Screen {
}
fn dump_layout_to_hd(&mut self) -> Result<()> {
let err_context = || format!("Failed to log and report session state");
let session_layout_metadata = self.get_layout_metadata(self.default_shell.clone());
let session_layout_metadata = self.get_layout_metadata(Some(self.default_shell.clone()));
self.bus
.senders
.send_to_plugin(PluginInstruction::LogLayoutToHd(session_layout_metadata))
Expand Down Expand Up @@ -2450,6 +2455,7 @@ impl Screen {
rounded_corners: bool,
hide_session_name: bool,
stacked_resize: bool,
default_editor: Option<PathBuf>,
client_id: ClientId,
) -> Result<()> {
let should_support_arrow_fonts = !simplified_ui;
Expand All @@ -2458,7 +2464,8 @@ impl Screen {
self.default_mode_info.update_theme(theme);
self.default_mode_info
.update_rounded_corners(rounded_corners);
self.default_shell = default_shell.clone();
self.default_shell = default_shell.clone().unwrap_or_else(|| get_default_shell());
self.default_editor = default_editor.clone().or_else(|| get_default_editor());
self.auto_layout = auto_layout;
self.copy_options.command = copy_command.clone();
self.copy_options.copy_on_select = copy_on_select;
Expand All @@ -2477,6 +2484,7 @@ impl Screen {
tab.update_theme(theme);
tab.update_rounded_corners(rounded_corners);
tab.update_default_shell(default_shell.clone());
tab.update_default_editor(self.default_editor.clone());
tab.update_auto_layout(auto_layout);
tab.update_copy_options(&self.copy_options);
tab.set_pane_frames(pane_frames);
Expand Down Expand Up @@ -2749,6 +2757,19 @@ impl Screen {
}
}

#[cfg(not(test))]
fn get_default_editor() -> Option<PathBuf> {
std::env::var("EDITOR")
.or_else(|_| std::env::var("VISUAL"))
.map(|e| PathBuf::from(e))
.ok()
}

#[cfg(test)]
fn get_default_editor() -> Option<PathBuf> {
None
}

// The box is here in order to make the
// NewClient enum smaller
#[allow(clippy::boxed_local)]
Expand All @@ -2769,7 +2790,20 @@ pub(crate) fn screen_thread_main(
let scrollback_lines_to_serialize = config_options.scrollback_lines_to_serialize;
let session_is_mirrored = config_options.mirror_session.unwrap_or(false);
let layout_dir = config_options.layout_dir;
let default_shell = config_options.default_shell;
#[cfg(test)]
let default_shell = config_options
.default_shell
.clone()
.unwrap_or(PathBuf::from("/bin/sh"));
#[cfg(not(test))]
let default_shell = config_options
.default_shell
.clone()
.unwrap_or_else(|| get_default_shell());
let default_editor = config_options
.scrollback_editor
.clone()
.or_else(|| get_default_editor());
let default_layout_name = config_options
.default_layout
.map(|l| format!("{}", l.display()));
Expand Down Expand Up @@ -2819,6 +2853,7 @@ pub(crate) fn screen_thread_main(
layout_dir,
explicitly_disable_kitty_keyboard_protocol,
stacked_resize,
default_editor,
);

let mut pending_tab_ids: HashSet<usize> = HashSet::new();
Expand Down Expand Up @@ -3240,7 +3275,7 @@ pub(crate) fn screen_thread_main(
ScreenInstruction::DumpLayoutToPlugin(plugin_id) => {
let err_context = || format!("Failed to dump layout");
let session_layout_metadata =
screen.get_layout_metadata(screen.default_shell.clone());
screen.get_layout_metadata(Some(screen.default_shell.clone()));
screen
.bus
.senders
Expand All @@ -3254,7 +3289,7 @@ pub(crate) fn screen_thread_main(
ScreenInstruction::ListClientsToPlugin(plugin_id, client_id) => {
let err_context = || format!("Failed to dump layout");
let session_layout_metadata =
screen.get_layout_metadata(screen.default_shell.clone());
screen.get_layout_metadata(Some(screen.default_shell.clone()));
screen
.bus
.senders
Expand Down Expand Up @@ -4561,6 +4596,7 @@ pub(crate) fn screen_thread_main(
rounded_corners,
hide_session_name,
stacked_resize,
default_editor,
} => {
screen
.reconfigure(
Expand All @@ -4577,6 +4613,7 @@ pub(crate) fn screen_thread_main(
rounded_corners,
hide_session_name,
stacked_resize,
default_editor,
client_id,
)
.non_fatal();
Expand Down
29 changes: 22 additions & 7 deletions zellij-server/src/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ pub(crate) struct Tab {
pending_instructions: Vec<BufferedTabInstruction>, // instructions that came while the tab was
// pending and need to be re-applied
swap_layouts: SwapLayouts,
default_shell: Option<PathBuf>,
default_shell: PathBuf,
default_editor: Option<PathBuf>,
debug: bool,
arrow_fonts: bool,
styled_underlines: bool,
Expand Down Expand Up @@ -582,11 +583,12 @@ impl Tab {
terminal_emulator_colors: Rc<RefCell<Palette>>,
terminal_emulator_color_codes: Rc<RefCell<HashMap<usize, String>>>,
swap_layouts: (Vec<SwapTiledLayout>, Vec<SwapFloatingLayout>),
default_shell: Option<PathBuf>,
default_shell: PathBuf,
debug: bool,
arrow_fonts: bool,
styled_underlines: bool,
explicitly_disable_kitty_keyboard_protocol: bool,
default_editor: Option<PathBuf>,
) -> Self {
let name = if name.is_empty() {
format!("Tab #{}", index + 1)
Expand Down Expand Up @@ -682,6 +684,7 @@ impl Tab {
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
default_editor,
}
}

Expand Down Expand Up @@ -886,8 +889,13 @@ impl Tab {
let mode_infos = self.mode_info.borrow();
let mut plugin_updates = vec![];
for client_id in self.connected_clients.borrow().iter() {
let mode_info = mode_infos.get(client_id).unwrap_or(&self.default_mode_info);
plugin_updates.push((None, Some(*client_id), Event::ModeUpdate(mode_info.clone())));
let mut mode_info = mode_infos
.get(client_id)
.unwrap_or(&self.default_mode_info)
.clone();
mode_info.shell = Some(self.default_shell.clone());
mode_info.editor = self.default_editor.clone();
plugin_updates.push((None, Some(*client_id), Event::ModeUpdate(mode_info)));
}
self.senders
.send_to_plugin(PluginInstruction::Update(plugin_updates))
Expand Down Expand Up @@ -1906,7 +1914,7 @@ impl Tab {
self.senders
.send_to_pty(PtyInstruction::DropToShellInPane {
pane_id: PaneId::Terminal(active_terminal_id),
shell: self.default_shell.clone(),
shell: Some(self.default_shell.clone()),
working_dir,
})
.with_context(err_context)?;
Expand Down Expand Up @@ -4418,8 +4426,15 @@ impl Tab {
pane.update_arrow_fonts(should_support_arrow_fonts);
}
}
pub fn update_default_shell(&mut self, default_shell: Option<PathBuf>) {
self.default_shell = default_shell;
pub fn update_default_shell(&mut self, mut default_shell: Option<PathBuf>) {
if let Some(default_shell) = default_shell.take() {
self.default_shell = default_shell;
}
}
pub fn update_default_editor(&mut self, mut default_editor: Option<PathBuf>) {
if let Some(default_editor) = default_editor.take() {
self.default_editor = Some(default_editor);
}
}
pub fn update_copy_options(&mut self, copy_options: &CopyOptions) {
self.clipboard_provider = match &copy_options.command {
Expand Down
18 changes: 12 additions & 6 deletions zellij-server/src/tab/unit/tab_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ fn create_new_tab(size: Size, default_mode: ModeInfo) -> Tab {
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]),
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down Expand Up @@ -333,11 +334,12 @@ fn create_new_tab_with_swap_layouts(
terminal_emulator_colors,
terminal_emulator_color_codes,
swap_layouts,
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
let (
base_layout,
Expand Down Expand Up @@ -416,11 +418,12 @@ fn create_new_tab_with_os_api(
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down Expand Up @@ -485,11 +488,12 @@ fn create_new_tab_with_layout(size: Size, default_mode: ModeInfo, layout: &str)
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
let pane_ids = tab_layout
.extract_run_instructions()
Expand Down Expand Up @@ -568,11 +572,12 @@ fn create_new_tab_with_mock_pty_writer(
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down Expand Up @@ -642,11 +647,12 @@ fn create_new_tab_with_sixel_support(
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down
9 changes: 6 additions & 3 deletions zellij-server/src/tab/unit/tab_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,12 @@ fn create_new_tab(size: Size, stacked_resize: bool) -> Tab {
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down Expand Up @@ -257,11 +258,12 @@ fn create_new_tab_with_layout(size: Size, layout: TiledPaneLayout) -> Tab {
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
let mut new_terminal_ids = vec![];
for i in 0..layout.extract_run_instructions().len() {
Expand Down Expand Up @@ -329,11 +331,12 @@ fn create_new_tab_with_cell_size(
terminal_emulator_colors,
terminal_emulator_color_codes,
(vec![], vec![]), // swap layouts
None,
PathBuf::from("my_default_shell"),
debug,
arrow_fonts,
styled_underlines,
explicitly_disable_kitty_keyboard_protocol,
None,
);
tab.apply_layout(
TiledPaneLayout::default(),
Expand Down
Loading

0 comments on commit 5cb214a

Please sign in to comment.