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

Improve action new-tab #3196

Open
wants to merge 6 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ target/
.vim
.DS_Store
/assets/man/zellij.1
.idea/
docker-compose.override.yml
4 changes: 2 additions & 2 deletions default-plugins/status-bar/src/one_line_ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
};

vec![
(s("New"), s("New"), single_action_key(&km, &[A::NewTab(None, vec![], None, None, None, true), TO_NORMAL])),
(s("New"), s("New"), single_action_key(&km, &[A::NewTab(None, None, None, vec![], None, None, None, true), TO_NORMAL])),
(s("Change focus"), s("Move"), focus_keys),
(s("Close"), s("Close"), single_action_key(&km, &[A::CloseTab, TO_NORMAL])),
(s("Rename"), s("Rename"),
Expand Down Expand Up @@ -1284,7 +1284,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None, false), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None, false), TO_NORMAL])),
(s("Fullscreen"), s("Fullscreen"), action_key(&km, &[A::ToggleFocusFullscreen, TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, vec![], None, None, None, true), TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, None, None, vec![], None, None, None, true), TO_NORMAL])),
(s("Rename tab"), s("Rename"),
action_key(&km, &[A::SwitchToMode(IM::RenameTab), A::TabNameInput(vec![0])])),
(s("Previous Tab"), s("Previous"), action_key(&km, &[A::GoToPreviousTab, TO_NORMAL])),
Expand Down
4 changes: 2 additions & 2 deletions default-plugins/status-bar/src/second_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
};

vec![
(s("New"), s("New"), action_key(&km, &[A::NewTab(None, vec![], None, None, None, true), TO_NORMAL])),
(s("New"), s("New"), action_key(&km, &[A::NewTab(None, None, None, vec![], None, None, None, true), TO_NORMAL])),
(s("Change focus"), s("Move"), focus_keys),
(s("Close"), s("Close"), action_key(&km, &[A::CloseTab, TO_NORMAL])),
(s("Rename"), s("Rename"),
Expand Down Expand Up @@ -259,7 +259,7 @@ fn get_keys_and_hints(mi: &ModeInfo) -> Vec<(String, String, Vec<KeyWithModifier
(s("Split down"), s("Down"), action_key(&km, &[A::NewPane(Some(Dir::Down), None, false), TO_NORMAL])),
(s("Split right"), s("Right"), action_key(&km, &[A::NewPane(Some(Dir::Right), None, false), TO_NORMAL])),
(s("Fullscreen"), s("Fullscreen"), action_key(&km, &[A::ToggleFocusFullscreen, TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, vec![], None, None, None, true), TO_NORMAL])),
(s("New tab"), s("New"), action_key(&km, &[A::NewTab(None, None, None, vec![], None, None, None, true), TO_NORMAL])),
(s("Rename tab"), s("Rename"),
action_key(&km, &[A::SwitchToMode(IM::RenameTab), A::TabNameInput(vec![0])])),
(s("Previous Tab"), s("Previous"), action_key(&km, &[A::GoToPreviousTab, TO_NORMAL])),
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ services:
PUID: 1000
PGID: 1000
TZ: Europe/Vienna
PASSWORD_ACCESS: true
PASSWORD_ACCESS: "true"
USER_PASSWORD: test
USER_NAME: test
volumes:
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ fn main() {
layout_dir: options.as_ref().and_then(|o| o.layout_dir.clone()),
name: None,
cwd: options.as_ref().and_then(|o| o.default_cwd.clone()),
run: None,
args: None,
edit: None,
};
commands::send_action_to_session(new_layout_cli_action, Some(session_name), config);
} else {
Expand Down
6 changes: 5 additions & 1 deletion zellij-server/src/plugins/zellij_exports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,8 @@ fn apply_layout(env: &PluginEnv, layout: Layout) {
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
let swap_floating_layouts = Some(layout.swap_floating_layouts.clone());
let action = Action::NewTab(
None,
None,
layout.template.as_ref().map(|t| t.0.clone()),
layout.template.map(|t| t.1).unwrap_or_default(),
swap_tiled_layouts,
Expand All @@ -1046,6 +1048,8 @@ fn apply_layout(env: &PluginEnv, layout: Layout) {
let swap_tiled_layouts = Some(layout.swap_tiled_layouts.clone());
let swap_floating_layouts = Some(layout.swap_floating_layouts.clone());
let action = Action::NewTab(
None,
None,
Some(tiled_pane_layout),
floating_pane_layout,
swap_tiled_layouts,
Expand All @@ -1063,7 +1067,7 @@ fn apply_layout(env: &PluginEnv, layout: Layout) {
}

fn new_tab(env: &PluginEnv) {
let action = Action::NewTab(None, vec![], None, None, None, true);
let action = Action::NewTab(None, None, None, vec![], None, None, None, true);
let error_msg = || format!("Failed to open new tab");
apply_action!(action, error_msg, env);
}
Expand Down
6 changes: 4 additions & 2 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,21 +496,23 @@ pub(crate) fn route_action(
.with_context(err_context)?;
},
Action::NewTab(
cwd,
run,
tab_layout,
floating_panes_layout,
swap_tiled_layouts,
swap_floating_layouts,
tab_name,
should_change_focus_to_new_tab,
) => {
let shell = default_shell.clone();
let shell = run.or_else(|| default_shell.clone());
let swap_tiled_layouts =
swap_tiled_layouts.unwrap_or_else(|| default_layout.swap_tiled_layouts.clone());
let swap_floating_layouts = swap_floating_layouts
.unwrap_or_else(|| default_layout.swap_floating_layouts.clone());
senders
.send_to_screen(ScreenInstruction::NewTab(
None,
cwd,
shell,
tab_layout,
floating_panes_layout,
Expand Down
6 changes: 6 additions & 0 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2656,6 +2656,9 @@ pub fn send_cli_new_tab_action_default_params() {
layout: None,
layout_dir: None,
cwd: None,
run: None,
args: None,
edit: None,
};
send_cli_action_to_server(&session_metadata, new_tab_action, client_id);
std::thread::sleep(std::time::Duration::from_millis(100));
Expand Down Expand Up @@ -2696,6 +2699,9 @@ pub fn send_cli_new_tab_action_with_name_and_layout() {
))),
layout_dir: None,
cwd: None,
run: None,
args: None,
edit: None,
};
send_cli_action_to_server(&session_metadata, new_tab_action, client_id);
std::thread::sleep(std::time::Duration::from_millis(100));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ assertion_line: 2711
expression: "format!(\"{:#?}\", new_tab_instruction)"
---
NewTab(
None,
Some(
".",
),
None,
Some(
TiledPaneLayout {
Expand Down
14 changes: 13 additions & 1 deletion zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,8 +625,20 @@ pub enum CliAction {
name: Option<String>,

/// Change the working directory of the new tab
#[clap(short, long, value_parser, requires("layout"))]
#[clap(short, long, value_parser)]
cwd: Option<PathBuf>,

/// Run command in the new tab
#[clap(short, long, value_parser, conflicts_with("edit"))]
run: Option<PathBuf>,

/// Command line arguments when running a command
#[clap(short, long, value_parser, requires("run"))]
args: Option<Vec<String>>,

/// Edit file in the new tab
#[clap(short, long, value_parser)]
edit: Option<PathBuf>,
},
/// Move the focused tab in the specified direction. [right|left]
MoveTab {
Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/src/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ mod not_wasm {
// Zellij asset map
pub static ref ASSET_MAP: HashMap<PathBuf, Vec<u8>> = {
let mut assets = std::collections::HashMap::new();
add_plugin!(assets, "compact-bar.wasm");
add_plugin!(assets, "compact-bar.wasm");
add_plugin!(assets, "status-bar.wasm");
add_plugin!(assets, "tab-bar.wasm");
add_plugin!(assets, "strider.wasm");
Expand Down
31 changes: 27 additions & 4 deletions zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Definition of the actions that can be bound to keys.

use super::command::{OpenFilePayload, RunCommandAction};
use super::command::{OpenFilePayload, RunCommandAction, TerminalAction};
use super::layout::{
FloatingPaneLayout, Layout, PluginAlias, RunPlugin, RunPluginLocation, RunPluginOrAlias,
SwapFloatingLayout, SwapTiledLayout, TiledPaneLayout,
Expand All @@ -18,6 +18,7 @@ use uuid::Uuid;

use std::path::PathBuf;
use std::str::FromStr;
use crate::input::command::RunCommand;

use crate::position::Position;

Expand Down Expand Up @@ -190,13 +191,15 @@ pub enum Action {
UndoRenamePane,
/// Create a new tab, optionally with a specified tab layout.
NewTab(
Option<PathBuf>, // cwd
Option<TerminalAction>, // initial command or file to edit
Option<TiledPaneLayout>,
Vec<FloatingPaneLayout>,
Option<Vec<SwapTiledLayout>>,
Option<Vec<SwapFloatingLayout>>,
Option<String>,
Option<String>, // tab name
bool, // should_change_focus_to_new_tab
), // the String is the tab name
),
/// Do nothing.
NoOp,
/// Go to the next tab.
Expand Down Expand Up @@ -523,11 +526,25 @@ impl Action {
layout,
layout_dir,
cwd,
run,
args,
edit,
} => {
let current_dir = get_current_dir();
let cwd = cwd
.map(|cwd| current_dir.join(cwd))
.or_else(|| Some(current_dir));


let action = run.map(|command| TerminalAction::RunCommand(RunCommand {
command,
args: args.unwrap_or_else(|| vec![]),
cwd: cwd.clone(),
hold_on_close: false,
hold_on_start: false,
originating_plugin: None,
})).or_else(|| edit.map(|path| TerminalAction::OpenFile(OpenFilePayload::new(path, None, cwd.clone()))));

if let Some(layout_path) = layout {
let layout_dir = layout_dir
.or_else(|| config.and_then(|c| c.options.layout_dir))
Expand All @@ -553,7 +570,7 @@ impl Action {
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 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.clone()).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));
Expand Down Expand Up @@ -608,6 +625,8 @@ impl Action {
}
});
new_tab_actions.push(Action::NewTab(
cwd.clone(),
action.clone(),
Some(layout),
floating_panes_layout,
swap_tiled_layouts.clone(),
Expand All @@ -623,6 +642,8 @@ impl Action {
let (layout, floating_panes_layout) = layout.new_tab();
let should_change_focus_to_new_tab = true;
Ok(vec![Action::NewTab(
cwd,
action,
Some(layout),
floating_panes_layout,
swap_tiled_layouts,
Expand All @@ -634,6 +655,8 @@ impl Action {
} else {
let should_change_focus_to_new_tab = true;
Ok(vec![Action::NewTab(
cwd,
action,
None,
vec![],
None,
Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/src/input/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::data::{Direction, OriginatingPlugin};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum TerminalAction {
OpenFile(OpenFilePayload),
RunCommand(RunCommand),
Expand Down
10 changes: 7 additions & 3 deletions zellij-utils/src/kdl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ impl Action {
Some(node)
},
Action::UndoRenamePane => Some(KdlNode::new("UndoRenamePane")),
Action::NewTab(_, _, _, _, name, should_change_focus_to_new_tab) => {
Action::NewTab(_, _, _, _, _, _, name, should_change_focus_to_new_tab) => {
log::warn!("Converting new tab action without arguments, original action saved to .bak.kdl file");
let mut node = KdlNode::new("NewTab");
if let Some(name) = name {
Expand Down Expand Up @@ -1387,7 +1387,7 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
"NewTab" => {
let command_metadata = action_children.iter().next();
if command_metadata.is_none() {
return Ok(Action::NewTab(None, vec![], None, None, None, true));
return Ok(Action::NewTab(None, None, None, vec![], None, None, None, true));
}

let current_dir = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
Expand Down Expand Up @@ -1423,7 +1423,7 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
&raw_layout,
path_to_raw_layout,
swap_layouts.as_ref().map(|(f, p)| (f.as_str(), p.as_str())),
cwd,
cwd.clone(),
)
.map_err(|e| {
ConfigError::new_kdl_error(
Expand All @@ -1449,6 +1449,8 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
let should_change_focus_to_new_tab = layout.focus.unwrap_or(true);

Ok(Action::NewTab(
cwd,
None,
Some(layout),
floating_panes_layout,
swap_tiled_layouts,
Expand All @@ -1461,6 +1463,8 @@ impl TryFrom<(&KdlNode, &Options)> for Action {
let should_change_focus_to_new_tab = layout.focus.unwrap_or(true);

Ok(Action::NewTab(
cwd,
None,
Some(layout),
floating_panes_layout,
swap_tiled_layouts,
Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/src/plugin_api/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl TryFrom<ProtobufAction> for Action {
Some(_) => Err("NewTab should not have a payload"),
None => {
// we do not serialize the layouts of this action
Ok(Action::NewTab(None, vec![], None, None, None, true))
Ok(Action::NewTab(None, None, None, vec![], None, None, None, true))
},
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down Expand Up @@ -5240,6 +5242,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down Expand Up @@ -5240,6 +5242,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down Expand Up @@ -5240,6 +5242,8 @@ Config {
key_modifiers: {},
}: [
NewTab(
None,
None,
None,
[],
None,
Expand Down
Loading