Skip to content

Commit

Permalink
Initial commit for fixing zellij-org#1353
Browse files Browse the repository at this point in the history
  • Loading branch information
cosminadrianpopescu committed May 3, 2022
1 parent 667ee8b commit 9e32246
Show file tree
Hide file tree
Showing 17 changed files with 88 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/MANPAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ ACTIONS
next ID.
* __MoveFocus: <Direction\>__ - moves focus in the specified direction (Left,
Right, Up, Down).
* __DumpScreen: <File\>__ - dumps the screen in the specified file.
* __ScrollUp__ - scrolls up 1 line in the focused pane.
* __ScrollDown__ - scrolls down 1 line in the focused pane.
* __PageScrollUp__ - scrolls up 1 page in the focused pane.
Expand Down
2 changes: 2 additions & 0 deletions example/alt-centered-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ keybinds:
key: [ Char: 'j', Down,]
- action: [MoveFocus: Up,]
key: [ Char: 'k', Up,]
- action: [DumpScreen,]
key: [Char: '/']
- action: [SwitchFocus,]
key: [Char: 'p']
- action: [NewPane: ,]
Expand Down
2 changes: 2 additions & 0 deletions example/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ keybinds:
key: [Ctrl: 'b',]
- action: [Quit,]
key: [Ctrl: 'q',]
- action: [DumpScreen,]
key: [Char: '/']
- action: [MoveFocus: Left,]
key: [ Char: 'h', Left,]
- action: [MoveFocus: Right,]
Expand Down
1 change: 1 addition & 0 deletions zellij-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typetag = "0.1.7"
chrono = "0.4.19"
close_fds = "0.3.2"
sysinfo = "0.22.5"
tempfile = "3.2.0"

[dev-dependencies]
insta = "1.6.0"
Expand Down
14 changes: 14 additions & 0 deletions zellij-server/src/os_input_output.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::collections::HashMap;
use std::{fs::File, io::Write};

use crate::panes::PaneId;
use tempfile::tempfile;

use std::env;
use std::os::unix::io::RawFd;
Expand Down Expand Up @@ -268,6 +270,8 @@ pub trait ServerOsApi: Send + Sync {
fn load_palette(&self) -> Palette;
/// Returns the current working directory for a given pid
fn get_cwd(&self, pid: Pid) -> Option<PathBuf>;
/// Writes the given buffer to a string
fn write_to_file(&self, buf: String, file: Option<String>);
}

impl ServerOsApi for ServerOsInputOutput {
Expand Down Expand Up @@ -345,6 +349,16 @@ impl ServerOsApi for ServerOsInputOutput {
}
None
}
fn write_to_file(&self, buf: String, name: Option<String>) {
let mut f: File;
match name {
Some(x) => f = File::create(x).unwrap(),
None => f = tempfile().unwrap(),
}
if let Err(e) = write!(f, "{}", buf) {
log::error!("could not write to file: {}", e);
}
}
}

impl Clone for Box<dyn ServerOsApi> {
Expand Down
24 changes: 24 additions & 0 deletions zellij-server/src/panes/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,23 @@ fn subtract_isize_from_usize(u: usize, i: isize) -> usize {
}
}

macro_rules! dump_screen {
($lines:expr) => {{
let mut is_first = true;
let mut buf = "".to_owned();

for line in &$lines {
if line.is_canonical && !is_first {
buf.push_str("\n");
}
let s: String = (&line.columns).into_iter().map(|x| x.character).collect();
buf.push_str(&(s.replace("[ ]*$", "")));
is_first = false;
}
buf
}};
}

#[derive(Clone)]
pub struct Grid {
lines_above: VecDeque<Row>,
Expand Down Expand Up @@ -796,6 +813,13 @@ impl Grid {
Some((self.cursor.x, self.cursor.y))
}
}

pub fn dump_screen(&mut self) -> String {
let mut lines: String = dump_screen!(self.lines_above);
let viewport: String = dump_screen!(self.viewport);
lines.push_str(&viewport);
return lines;
}
pub fn move_viewport_up(&mut self, count: usize) {
for _ in 0..count {
self.scroll_up_one_line();
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/panes/plugin_pane.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,9 @@ impl Pane for PluginPane {
self.geom.y -= count;
self.should_render = true;
}
fn dump_screen(&mut self, _client_id: ClientId) -> String {
return "".to_owned();
}
fn scroll_up(&mut self, count: usize, client_id: ClientId) {
self.send_plugin_instructions
.send(PluginInstruction::Update(
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 @@ -386,6 +386,9 @@ impl Pane for TerminalPane {
self.geom.y -= count;
self.reflow_lines();
}
fn dump_screen(&mut self, _client_id: ClientId) -> String {
return self.grid.dump_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 @@ -146,6 +146,12 @@ fn route_action(
};
session.senders.send_to_screen(screen_instr).unwrap();
}
Action::DumpScreen(val) => {
session
.senders
.send_to_screen(ScreenInstruction::DumpScreen(val, client_id))
.unwrap();
}
Action::ScrollUp => {
session
.senders
Expand Down
11 changes: 11 additions & 0 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub enum ScreenInstruction {
MovePaneRight(ClientId),
MovePaneLeft(ClientId),
Exit,
DumpScreen(String, ClientId),
ScrollUp(ClientId),
ScrollUpAt(Position, ClientId),
ScrollDown(ClientId),
Expand Down Expand Up @@ -140,6 +141,7 @@ impl From<&ScreenInstruction> for ScreenContext {
ScreenInstruction::MovePaneRight(..) => ScreenContext::MovePaneRight,
ScreenInstruction::MovePaneLeft(..) => ScreenContext::MovePaneLeft,
ScreenInstruction::Exit => ScreenContext::Exit,
ScreenInstruction::DumpScreen(..) => ScreenContext::DumpScreen,
ScreenInstruction::ScrollUp(..) => ScreenContext::ScrollUp,
ScreenInstruction::ScrollDown(..) => ScreenContext::ScrollDown,
ScreenInstruction::ScrollToBottom(..) => ScreenContext::ScrollToBottom,
Expand Down Expand Up @@ -1037,6 +1039,15 @@ pub(crate) fn screen_thread_main(

screen.render();
}
ScreenInstruction::DumpScreen(file, client_id) => {
if let Some(active_tab) = screen.get_active_tab_mut(client_id) {
active_tab.dump_active_terminal(Some(file.to_string()), client_id);
} else {
log::error!("Active tab not found for client id: {:?}", client_id);
}

screen.render();
}
ScreenInstruction::ScrollUp(client_id) => {
if let Some(active_tab) = screen.get_active_tab_mut(client_id) {
active_tab.scroll_active_terminal_up(client_id);
Expand Down
7 changes: 7 additions & 0 deletions zellij-server/src/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ 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 dump_screen(&mut self, client_id: ClientId) -> String;
fn scroll_up(&mut self, count: usize, client_id: ClientId);
fn scroll_down(&mut self, count: usize, client_id: ClientId);
fn clear_scroll(&mut self);
Expand Down Expand Up @@ -1370,6 +1371,12 @@ impl Tab {
.unwrap();
}
}
pub fn dump_active_terminal(&mut self, file: Option<String>, client_id: ClientId) {
if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
let dump = active_pane.dump_screen(client_id);
self.os_api.write_to_file(dump, file);
}
}
pub fn scroll_active_terminal_up(&mut self, client_id: ClientId) {
if let Some(active_pane) = self.get_active_pane_or_floating_pane_mut(client_id) {
active_pane.scroll_up(1, client_id);
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/tab/unit/tab_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ impl ServerOsApi for FakeInputOutput {
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
unimplemented!()
}
fn write_to_file(&self, buf: String, name: Option<String>) {
unimplemented!()
}
}

// TODO: move to shared thingy with other test file
Expand Down
4 changes: 4 additions & 0 deletions zellij-server/src/tab/unit/tab_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ impl ServerOsApi for FakeInputOutput {
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
unimplemented!()
}

fn write_to_file(&self, buf: String, name: Option<String>) {
unimplemented!()
}
}

fn create_new_tab(size: Size) -> Tab {
Expand Down
3 changes: 3 additions & 0 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ impl ServerOsApi for FakeInputOutput {
fn get_cwd(&self, _pid: Pid) -> Option<PathBuf> {
unimplemented!()
}
fn write_to_file(&self, _: std::string::String, _: std::option::Option<std::string::String>) {
unimplemented!()
}
}

fn create_new_screen(size: Size) -> Screen {
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 @@ -240,6 +240,7 @@ pub enum ScreenContext {
MovePaneRight,
MovePaneLeft,
Exit,
DumpScreen,
ScrollUp,
ScrollUpAt,
ScrollDown,
Expand Down
2 changes: 2 additions & 0 deletions zellij-utils/src/input/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ pub enum Action {
/// If there is no pane in the direction, move to previous/next Tab.
MoveFocusOrTab(Direction),
MovePane(Option<Direction>),
/// Dumps the screen to a file
DumpScreen(String),
/// Scroll up in focus pane.
ScrollUp,
/// Scroll up at point
Expand Down

0 comments on commit 9e32246

Please sign in to comment.