Skip to content

Commit

Permalink
pr: requested refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
on3iro committed Mar 30, 2023
1 parent b59b577 commit 44dfdb7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
63 changes: 33 additions & 30 deletions zellij-server/src/tab/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ impl Tab {
let messages_to_pty = terminal_output.drain_messages_to_pty();
let clipboard_update = terminal_output.drain_clipboard_update();
for message in messages_to_pty {
self.write_to_pane_id(message, PaneId::Terminal(pid), false)
self.write_to_pane_id(message, PaneId::Terminal(pid))
.with_context(err_context)?;
}
if let Some(string) = clipboard_update {
Expand All @@ -1457,7 +1457,7 @@ impl Tab {
let pane_ids = self.get_static_and_floating_pane_ids();
for pane_id in pane_ids {
let ui_change_triggered = self
.write_to_pane_id(input_bytes.clone(), pane_id, true)
.write_to_pane_id(input_bytes.clone(), pane_id)
.context("failed to write to terminals on current tab")?;
if ui_change_triggered {
should_trigger_ui_change = true;
Expand Down Expand Up @@ -1496,7 +1496,7 @@ impl Tab {
.with_context(err_context)?
};
// Can't use 'err_context' here since it borrows 'input_bytes'
self.write_to_pane_id(input_bytes, pane_id, false)
self.write_to_pane_id(input_bytes, pane_id)
.with_context(|| format!("failed to write to active terminal for client {client_id}"))
}

Expand All @@ -1513,7 +1513,7 @@ impl Tab {
.get_pane_id_at(position, false)
.with_context(err_context)?;
if let Some(pane_id) = pane_id {
self.write_to_pane_id(input_bytes, pane_id, false)
self.write_to_pane_id(input_bytes, pane_id)
.with_context(err_context)?;
return Ok(());
}
Expand All @@ -1523,46 +1523,49 @@ impl Tab {
.get_pane_id_at(position, false)
.with_context(err_context)?;
if let Some(pane_id) = pane_id {
self.write_to_pane_id(input_bytes, pane_id, false)
self.write_to_pane_id(input_bytes, pane_id)
.with_context(err_context)?;
return Ok(());
}
Ok(())
}

pub fn write_to_pane_id(
&mut self,
input_bytes: Vec<u8>,
pane_id: PaneId,
is_synced: bool,
) -> Result<bool> {
pub fn write_to_pane_id(&mut self, input_bytes: Vec<u8>, pane_id: PaneId) -> Result<bool> {
// returns true if we need to update the UI (eg. when a command pane is closed with ctrl-c)
let err_context = || format!("failed to write to pane with id {pane_id:?}");

let mut should_update_ui = false;
let is_sync_panes_active = self.is_sync_panes_active();

let active_terminal = self
.floating_panes
.get_mut(&pane_id)
.or_else(|| self.tiled_panes.get_pane_mut(pane_id))
.or_else(|| self.suppressed_panes.get_mut(&pane_id))
.ok_or_else(|| anyhow!(format!("failed to find pane with id {pane_id:?}")))
.with_context(err_context)?;

// We always write for non-synced terminals.
// However if the terminal is part of a tab-sync, we need to
// check if the terminal should receive input or not (depending on its
// 'exclude_from_sync' configuration).
let should_not_write_to_terminal =
is_sync_panes_active && active_terminal.exclude_from_sync();

if should_not_write_to_terminal {
return Ok(should_update_ui);
}

match pane_id {
PaneId::Terminal(active_terminal_id) => {
let active_terminal = self
.floating_panes
.get_mut(&pane_id)
.or_else(|| self.tiled_panes.get_pane_mut(pane_id))
.or_else(|| self.suppressed_panes.get_mut(&pane_id))
.ok_or_else(|| anyhow!(format!("failed to find pane with id {pane_id:?}")))
.with_context(err_context)?;
match active_terminal.adjust_input_to_terminal(input_bytes) {
Some(AdjustedInput::WriteBytesToTerminal(adjusted_input)) => {
// We always write for non-synced terminals.
// However if the terminal is part of a tab-sync, we need to
// check if the terminal should receive input or not (depending on its
// 'exclude_from_sync' configuration).
if !is_synced || !active_terminal.exclude_from_sync() {
self.senders
.send_to_pty_writer(PtyWriteInstruction::Write(
adjusted_input,
active_terminal_id,
))
.with_context(err_context)?;
}
self.senders
.send_to_pty_writer(PtyWriteInstruction::Write(
adjusted_input,
active_terminal_id,
))
.with_context(err_context)?;
},
Some(AdjustedInput::ReRunCommandInThisPane(command)) => {
self.pids_waiting_resize.insert(active_terminal_id);
Expand Down
2 changes: 1 addition & 1 deletion zellij-server/src/tab/unit/tab_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn write_to_suppressed_pane() {
// Make sure it's suppressed now
tab.suppressed_panes.get(&PaneId::Terminal(2)).unwrap();
// Write content to it
tab.write_to_pane_id(vec![34, 127, 31, 82, 17, 182], PaneId::Terminal(2), false)
tab.write_to_pane_id(vec![34, 127, 31, 82, 17, 182], PaneId::Terminal(2))
.unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion zellij-utils/src/input/unit/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ fn layout_with_pane_excluded_from_sync() {
}
"#;
let layout = Layout::from_kdl(kdl_layout, "layout_file_name".into(), None, None).unwrap();
assert_snapshot!(format!("{:?}", layout));
assert_snapshot!(format!("{:#?}", layout));
}

#[test]
Expand Down

0 comments on commit 44dfdb7

Please sign in to comment.