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 action to clear all buffers for a specific pane #2239

Merged
merged 9 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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: 1 addition & 1 deletion src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ pub(crate) fn assert_session_ne(name: &str) {
process::exit(1);
}
if name.contains('/') {
eprintln!("Session name cannot contains '/'.");
eprintln!("Session name cannot contain '/'.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<3

process::exit(1);
}

Expand Down
1 change: 1 addition & 0 deletions zellij-client/src/input_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ impl InputHandler {
.send_to_server(ClientToServerMsg::Action(action, None));
},
Action::CloseFocus
| Action::ClearScreen
| Action::NewPane(..)
| Action::Run(_)
| Action::ToggleFloatingPanes
Expand Down
8 changes: 7 additions & 1 deletion zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,13 @@ impl Grid {
Some((self.cursor.x, self.cursor.y))
}
}

/// Clears all buffers with text
pub fn clear_screen(&mut self) {
self.lines_above = Default::default();
self.lines_below = Default::default();
self.viewport = Default::default();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a conditional here so that we don't do this if alternate_screen is Some. This happens when eg. programs running in the terminal switch to full screen (eg. htop and friends - also Zellij itself). I think in such cases we really don't want to do this. We can add a log if that happens.

}
/// Dumps all lines above terminal vieport and the viewport itself to a string
pub fn dump_screen(&mut self, full: bool) -> String {
let viewport: String = dump_screen!(self.viewport);
if !full {
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/panes/terminal_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ impl Pane for TerminalPane {
fn dump_screen(&mut self, _client_id: ClientId, full: bool) -> String {
self.grid.dump_screen(full)
}
fn clear_screen(&mut self) {
self.grid.clear_screen()
}
fn scroll_up(&mut self, count: usize, _client_id: ClientId) {
self.grid.move_viewport_up(count);
self.set_should_render(true);
Expand Down
6 changes: 6 additions & 0 deletions zellij-server/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ pub(crate) fn route_action(
.send_to_screen(ScreenInstruction::MovePaneBackwards(client_id))
.with_context(err_context)?;
},
Action::ClearScreen => {
session
.senders
.send_to_screen(ScreenInstruction::ClearScreen(client_id))
.with_context(err_context)?;
},
Action::DumpScreen(val, full) => {
session
.senders
Expand Down
14 changes: 14 additions & 0 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ pub enum ScreenInstruction {
MovePaneRight(ClientId),
MovePaneLeft(ClientId),
Exit,
ClearScreen(ClientId),
DumpScreen(String, ClientId, bool),
EditScrollback(ClientId),
ScrollUp(ClientId),
Expand Down Expand Up @@ -310,6 +311,7 @@ impl From<&ScreenInstruction> for ScreenContext {
ScreenInstruction::MovePaneRight(..) => ScreenContext::MovePaneRight,
ScreenInstruction::MovePaneLeft(..) => ScreenContext::MovePaneLeft,
ScreenInstruction::Exit => ScreenContext::Exit,
ScreenInstruction::ClearScreen(..) => ScreenContext::ClearScreen,
ScreenInstruction::DumpScreen(..) => ScreenContext::DumpScreen,
ScreenInstruction::EditScrollback(..) => ScreenContext::EditScrollback,
ScreenInstruction::ScrollUp(..) => ScreenContext::ScrollUp,
Expand Down Expand Up @@ -1690,6 +1692,18 @@ pub(crate) fn screen_thread_main(
screen.render()?;
screen.unblock_input()?;
},
ScreenInstruction::ClearScreen(client_id) => {
active_tab_and_connected_client_id!(
screen,
client_id,
|tab: &mut Tab, client_id: ClientId| tab.clear_active_terminal_screen(
client_id,
),
?
);
screen.render()?;
screen.unblock_input()?;
},
ScreenInstruction::DumpScreen(file, client_id, full) => {
active_tab_and_connected_client_id!(
screen,
Expand Down
9 changes: 9 additions & 0 deletions zellij-server/src/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ pub trait Pane {
fn push_right(&mut self, count: usize);
fn pull_left(&mut self, count: usize);
fn pull_up(&mut self, count: usize);
fn clear_screen(&mut self) {
todo!();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not forgo the default implementation here? If you're concerned about plugins (our only other type of Pane) then I think we can leave it and remove the todo!() - I don't think this action is relevant for plugins...

Copy link
Contributor Author

@olekspickle olekspickle Mar 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, when I was doing this specifically I was trying to understand the whole architecture of zellij, so, I'll gladly just remove it

fn dump_screen(&mut self, _client_id: ClientId, _full: bool) -> String {
"".to_owned()
}
Expand Down Expand Up @@ -2308,6 +2311,12 @@ impl Tab {
}
Ok(())
}
pub fn clear_active_terminal_screen(&mut self, client_id: ClientId) -> Result<()> {
if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
active_pane.clear_screen();
}
Ok(())
}
pub fn dump_active_terminal_screen(
&mut self,
file: Option<String>,
Expand Down
29 changes: 29 additions & 0 deletions zellij-server/src/tab/unit/tab_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,35 @@ fn dump_screen() {
);
}

#[test]
fn clear_screen() {
let size = Size {
cols: 121,
rows: 20,
};
let client_id = 1;
let mut tab = create_new_tab(size, ModeInfo::default());
let map = Arc::new(Mutex::new(HashMap::new()));
tab.os_api = Box::new(FakeInputOutput {
file_dumps: map.clone(),
..Default::default()
});
let new_pane_id = PaneId::Terminal(2);
tab.new_pane(new_pane_id, None, None, Some(client_id))
.unwrap();
tab.handle_pty_bytes(2, Vec::from("scratch".as_bytes()))
.unwrap();
let file = "/tmp/log.sh";
tab.clear_active_terminal_screen(client_id).unwrap();
tab.dump_active_terminal_screen(Some(file.to_string()), client_id, false)
.unwrap();
assert_eq!(
map.lock().unwrap().get(file).unwrap(),
"",
"screen was cleared properly"
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!


#[test]
fn new_floating_pane() {
let size = Size {
Expand Down
2 changes: 2 additions & 0 deletions zellij-utils/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ pub enum CliAction {
},
/// Rotate the location of the previous pane backwards
MovePaneBackwards,
/// Clear all buffers for a focused pane
Clear,
/// Dump the focused pane to a file
DumpScreen {
path: PathBuf,
Expand Down
1 change: 1 addition & 0 deletions zellij-utils/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ pub enum ScreenContext {
MovePaneRight,
MovePaneLeft,
Exit,
ClearScreen,
DumpScreen,
EditScrollback,
ScrollUp,
Expand Down
3 changes: 3 additions & 0 deletions zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ pub enum Action {
MoveFocusOrTab(Direction),
MovePane(Option<Direction>),
MovePaneBackwards,
/// Clear all buffers of a current screen
ClearScreen,
/// Dumps the screen to a file
DumpScreen(String, bool),
/// Scroll up in focus pane.
Expand Down Expand Up @@ -251,6 +253,7 @@ impl Action {
CliAction::MoveFocusOrTab { direction } => Ok(vec![Action::MoveFocusOrTab(direction)]),
CliAction::MovePane { direction } => Ok(vec![Action::MovePane(direction)]),
CliAction::MovePaneBackwards => Ok(vec![Action::MovePaneBackwards]),
CliAction::Clear => Ok(vec![Action::ClearScreen]),
CliAction::DumpScreen { path, full } => Ok(vec![Action::DumpScreen(
path.as_os_str().to_string_lossy().into(),
full,
Expand Down