Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a --size parameter to zellij run #3748

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn main() {
y,
width,
height,
size,
})) = opts.command
{
let cwd = cwd.or_else(|| std::env::current_dir().ok());
Expand All @@ -57,6 +58,7 @@ fn main() {
y,
width,
height,
size,
};
commands::send_action_to_session(command_cli_action, opts.session, config);
std::process::exit(0);
Expand All @@ -71,6 +73,7 @@ fn main() {
y,
width,
height,
size,
})) = opts.command
{
let cwd = None;
Expand All @@ -90,6 +93,7 @@ fn main() {
y,
width,
height,
size,
};
commands::send_action_to_session(command_cli_action, opts.session, config);
std::process::exit(0);
Expand Down
22 changes: 14 additions & 8 deletions zellij-server/src/panes/tiled_panes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use zellij_utils::{
errors::prelude::*,
input::{
command::RunCommand,
layout::{Run, RunPluginOrAlias, SplitDirection},
layout::{Run, RunPluginOrAlias, SplitDirection, SplitSize},
},
pane_size::{Offset, PaneGeom, Size, SizeInPixels, Viewport},
};
Expand Down Expand Up @@ -202,7 +202,8 @@ impl TiledPanes {
// this unwrap is safe because floating panes should not be visible if there are no floating panes
let pane_to_split = self.panes.get_mut(&pane_id_to_split).unwrap();
let size_of_both_panes = pane_to_split.position_and_size();
if let Some((first_geom, second_geom)) = split(split_direction, &size_of_both_panes)
if let Some((first_geom, second_geom)) =
split(split_direction, &size_of_both_panes, None, false)
{
pane_to_split.set_geom(first_geom);
pane.set_geom(second_geom);
Expand Down Expand Up @@ -349,7 +350,8 @@ impl TiledPanes {
{
return false;
} else {
return split(SplitDirection::Horizontal, &full_pane_size).is_some();
return split(SplitDirection::Horizontal, &full_pane_size, None, false)
.is_some();
}
}
}
Expand All @@ -364,7 +366,7 @@ impl TiledPanes {
{
return false;
}
return split(SplitDirection::Vertical, &full_pane_size).is_some();
return split(SplitDirection::Vertical, &full_pane_size, None, false).is_some();
}
}
false
Expand All @@ -376,7 +378,7 @@ impl TiledPanes {
if full_pane_size.rows.is_fixed() || full_pane_size.is_stacked {
return false;
}
if split(SplitDirection::Horizontal, &full_pane_size).is_some() {
if split(SplitDirection::Horizontal, &full_pane_size, None, false).is_some() {
true
} else {
false
Expand All @@ -387,12 +389,14 @@ impl TiledPanes {
pid: PaneId,
mut new_pane: Box<dyn Pane>,
client_id: ClientId,
size: Option<SplitSize>,
up: bool,
) {
let active_pane_id = &self.active_panes.get(&client_id).unwrap();
let active_pane = self.panes.get_mut(active_pane_id).unwrap();
let full_pane_size = active_pane.position_and_size();
if let Some((top_winsize, bottom_winsize)) =
split(SplitDirection::Horizontal, &full_pane_size)
split(SplitDirection::Horizontal, &full_pane_size, size, up)
{
active_pane.set_geom(top_winsize);
new_pane.set_geom(bottom_winsize);
Expand All @@ -407,7 +411,7 @@ impl TiledPanes {
if full_pane_size.cols.is_fixed() || full_pane_size.is_stacked {
return false;
}
if split(SplitDirection::Vertical, &full_pane_size).is_some() {
if split(SplitDirection::Vertical, &full_pane_size, None, false).is_some() {
true
} else {
false
Expand All @@ -418,12 +422,14 @@ impl TiledPanes {
pid: PaneId,
mut new_pane: Box<dyn Pane>,
client_id: ClientId,
size: Option<SplitSize>,
left: bool,
) {
let active_pane_id = &self.active_panes.get(&client_id).unwrap();
let active_pane = self.panes.get_mut(active_pane_id).unwrap();
let full_pane_size = active_pane.position_and_size();
if let Some((left_winsize, right_winsize)) =
split(SplitDirection::Vertical, &full_pane_size)
split(SplitDirection::Vertical, &full_pane_size, size, left)
{
active_pane.set_geom(left_winsize);
new_pane.set_geom(right_winsize);
Expand Down
30 changes: 24 additions & 6 deletions zellij-server/src/panes/tiled_panes/tiled_pane_grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{panes::PaneId, tab::Pane};
use std::cmp::{Ordering, Reverse};
use std::collections::{HashMap, HashSet};
use zellij_utils::data::{Direction, Resize, ResizeStrategy};
use zellij_utils::input::layout::SplitSize;
use zellij_utils::{
errors::prelude::*,
input::layout::SplitDirection,
Expand Down Expand Up @@ -1383,35 +1384,52 @@ impl<'a> TiledPaneGrid<'a> {
}
}

pub fn split(direction: SplitDirection, rect: &PaneGeom) -> Option<(PaneGeom, PaneGeom)> {
pub fn split(
direction: SplitDirection,
rect: &PaneGeom,
size: Option<SplitSize>,
swap: bool,
) -> Option<(PaneGeom, PaneGeom)> {
let space = match direction {
SplitDirection::Vertical => rect.cols,
SplitDirection::Horizontal => rect.rows,
};
let size = size.unwrap_or(SplitSize::Percent(50));
let size = match size {
SplitSize::Percent(x) => x as f64 / 100.0,
SplitSize::Fixed(x) => (x as f64 / space.as_usize() as f64).clamp(0.0, 1.0),
};
let size = if swap { 1.0 - size } else { size };
if let Some(p) = space.as_percent() {
let dim1 = Dimension::percent(p * (1.0 - size));
let dim2 = Dimension::percent(p * size);
let first_rect = match direction {
SplitDirection::Vertical => PaneGeom {
cols: Dimension::percent(p / 2.0),
cols: dim1,
..*rect
},
SplitDirection::Horizontal => PaneGeom {
rows: Dimension::percent(p / 2.0),
rows: dim1,
..*rect
},
};
let second_rect = match direction {
SplitDirection::Vertical => PaneGeom {
x: first_rect.x + 1,
cols: first_rect.cols,
cols: dim2,
..*rect
},
SplitDirection::Horizontal => PaneGeom {
y: first_rect.y + 1,
rows: first_rect.rows,
rows: dim2,
..*rect
},
};
Some((first_rect, second_rect))
if swap {
Some((second_rect, first_rect))
} else {
Some((first_rect, second_rect))
}
} else {
None
}
Expand Down
4 changes: 2 additions & 2 deletions zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ fn open_terminal(env: &PluginEnv, cwd: PathBuf) {
TerminalAction::RunCommand(run_command) => Some(run_command.into()),
_ => None,
};
let action = Action::NewTiledPane(None, run_command_action, None);
let action = Action::NewTiledPane(None, run_command_action, None, None);
apply_action!(action, error_msg, env);
}

Expand Down Expand Up @@ -661,7 +661,7 @@ fn open_command_pane(
context,
)),
};
let action = Action::NewTiledPane(direction, Some(run_command_action), name);
let action = Action::NewTiledPane(direction, Some(run_command_action), name, None);
apply_action!(action, error_msg, env);
}

Expand Down
46 changes: 37 additions & 9 deletions zellij-server/src/pty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
use async_std::task::{self, JoinHandle};
use std::sync::Arc;
use std::{collections::HashMap, os::unix::io::RawFd, path::PathBuf};
use zellij_utils::input::layout::SplitSize;
use zellij_utils::nix::unistd::Pid;
use zellij_utils::{
async_std,
Expand Down Expand Up @@ -48,13 +49,20 @@ pub enum PtyInstruction {
), // bool (if Some) is
// should_float, String is an optional pane name
OpenInPlaceEditor(PathBuf, Option<usize>, ClientTabIndexOrPaneId), // Option<usize> is the optional line number
SpawnTerminalVertically(Option<TerminalAction>, Option<String>, ClientId), // String is an
// optional pane
// name
// bool is start_suppressed
SpawnTerminalHorizontally(Option<TerminalAction>, Option<String>, ClientId), // String is an
// optional pane
// name
SpawnTerminalVertically(
Option<TerminalAction>,
Option<String>,
ClientId,
Option<SplitSize>,
bool,
), // String is an optional pane name, bool is left
SpawnTerminalHorizontally(
Option<TerminalAction>,
Option<String>,
ClientId,
Option<SplitSize>,
bool,
), // String is an optional pane name, bool is up
UpdateActivePane(Option<PaneId>, ClientId),
GoToTab(TabIndex, ClientId),
NewTab(
Expand Down Expand Up @@ -385,7 +393,13 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
},
}
},
PtyInstruction::SpawnTerminalVertically(terminal_action, name, client_id) => {
PtyInstruction::SpawnTerminalVertically(
terminal_action,
name,
client_id,
size,
left,
) => {
let err_context =
|| format!("failed to spawn terminal vertically for client {client_id}");

Expand All @@ -410,6 +424,8 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
pane_title,
hold_for_command,
client_id,
size,
left,
))
.with_context(err_context)?;
},
Expand All @@ -424,6 +440,8 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
pane_title,
hold_for_command,
client_id,
size,
left,
))
.with_context(err_context)?;
if let Some(run_command) = run_command {
Expand Down Expand Up @@ -456,7 +474,13 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
},
}
},
PtyInstruction::SpawnTerminalHorizontally(terminal_action, name, client_id) => {
PtyInstruction::SpawnTerminalHorizontally(
terminal_action,
name,
client_id,
size,
up,
) => {
let err_context =
|| format!("failed to spawn terminal horizontally for client {client_id}");

Expand All @@ -481,6 +505,8 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
pane_title,
hold_for_command,
client_id,
size,
up,
))
.with_context(err_context)?;
},
Expand All @@ -495,6 +521,8 @@ pub(crate) fn pty_thread_main(mut pty: Pty, layout: Box<Layout>) -> Result<()> {
pane_title,
hold_for_command,
client_id,
size,
up
))
.with_context(err_context)?;
if let Some(run_command) = run_command {
Expand Down
Loading