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

flake.lock: Update #180

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ We are always happy to receive contributions and attempt to process them in a ti

## Branches
Releases will be cut on the `release` branch.
Most PR`s and features should land on the `development` branch.
Most PRs and features should land on the `development` branch.

## Issues
To get an overview of what can be worked on, please take a look at the [issues](https://github.com/a-kenji/tui-term/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc).
Expand Down
2 changes: 1 addition & 1 deletion examples/nested_shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn run<B: Backend>(
Event::FocusLost => {}
Event::Mouse(_) => {}
Event::Paste(_) => todo!(),
Event::Resize(rows, cols) => {
Event::Resize(cols, rows) => {
parser.write().unwrap().set_size(rows, cols);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/nested_shell_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ async fn run<B: Backend>(
Event::FocusLost => {}
Event::Mouse(_) => {}
Event::Paste(_) => todo!(),
Event::Resize(rows, cols) => {
Event::Resize(cols, rows) => {
parser.write().unwrap().set_size(rows, cols);
}
}
Expand Down
91 changes: 62 additions & 29 deletions examples/smux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crossterm::{
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use portable_pty::{native_pty_system, CommandBuilder, PtySize};
use portable_pty::{native_pty_system, CommandBuilder, MasterPty, PtySize};
use ratatui::{
backend::CrosstermBackend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
Expand All @@ -26,9 +26,9 @@ use tokio::{
};
use tracing::Level;
use tracing_subscriber::FmtSubscriber;
use tui_term::widget::PseudoTerminal;
use tui_term::widget::{Cursor, PseudoTerminal};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Copy)]
struct Size {
cols: u16,
rows: u16,
Expand All @@ -47,8 +47,7 @@ async fn main() -> io::Result<()> {
let mut active_pane: Option<usize> = None;

// Add a default pane
let mut pane_size = size.clone();
pane_size.rows -= 2;
let pane_size = calc_pane_size(size, 1);
open_new_pane(&mut panes, &mut active_pane, &cmd, pane_size)?;

loop {
Expand All @@ -69,18 +68,20 @@ async fn main() -> io::Result<()> {
let block = Block::default()
.borders(Borders::ALL)
.style(Style::default().add_modifier(Modifier::BOLD));
let mut cursor = Cursor::default();
let block = if Some(index) == active_pane {
block.style(
Style::default()
.add_modifier(Modifier::BOLD)
.fg(Color::LightMagenta),
)
} else {
cursor.hide();
block
};
let parser = pane.parser.read().unwrap();
let screen = parser.screen();
let pseudo_term = PseudoTerminal::new(screen).block(block);
let pseudo_term = PseudoTerminal::new(screen).block(block).cursor(cursor);
let pane_chunk = Rect {
x: chunks[0].x,
y: chunks[0].y + (index as u16 * pane_height), /* Adjust the y coordinate for
Expand Down Expand Up @@ -108,14 +109,14 @@ async fn main() -> io::Result<()> {
return Ok(());
}
KeyCode::Char('n') if key.modifiers.contains(KeyModifiers::CONTROL) => {
let amount = panes.len() + 1;
let mut pane_size = size.clone();
pane_size.rows /= amount as u16;
let pane_size = calc_pane_size(size, panes.len() + 1);
tracing::info!("Opened new pane with size: {size:?}");
resize_all_panes(&mut panes, pane_size);
open_new_pane(&mut panes, &mut active_pane, &cmd, pane_size)?;
}
KeyCode::Char('x') if key.modifiers.contains(KeyModifiers::CONTROL) => {
close_active_pane(&mut panes, &mut active_pane).await?;
resize_all_panes(&mut panes, pane_size);
}
KeyCode::Char('k') if key.modifiers.contains(KeyModifiers::CONTROL) => {
if let Some(pane) = active_pane {
Expand All @@ -137,23 +138,35 @@ async fn main() -> io::Result<()> {
}
}
},
Event::Resize(rows, cols) => {
Event::Resize(cols, rows) => {
tracing::info!("Resized to: rows: {} cols: {}", rows, cols);
for pane in panes.iter_mut() {
pane.parser.write().unwrap().set_size(rows, cols);
}
size.rows = rows;
size.cols = cols;
let pane_size = calc_pane_size(size, panes.len());
resize_all_panes(&mut panes, pane_size);
}
_ => {}
}
}
}
}

fn calc_pane_size(mut size: Size, nr_panes: usize) -> Size {
size.rows -= 2;
size.rows /= nr_panes as u16;
size
}

fn resize_all_panes(panes: &mut Vec<PtyPane>, size: Size) {
for pane in panes.iter() {
pane.resize(size);
}
}

struct PtyPane {
parser: Arc<RwLock<vt100::Parser>>,
sender: Sender<Bytes>,
master_pty: Box<dyn MasterPty>,
}

impl PtyPane {
Expand Down Expand Up @@ -205,19 +218,35 @@ impl PtyPane {

let (tx, mut rx) = channel::<Bytes>(32);

{
let mut writer = BufWriter::new(pty_pair.master.take_writer().unwrap());
// Drop writer on purpose
tokio::spawn(async move {
while let Some(bytes) = rx.recv().await {
writer.write_all(&bytes).unwrap();
writer.flush().unwrap();
}
drop(pty_pair.master);
});
}
let mut writer = BufWriter::new(pty_pair.master.take_writer().unwrap());
// writer is moved into the tokio task below
tokio::spawn(async move {
while let Some(bytes) = rx.recv().await {
writer.write_all(&bytes).unwrap();
writer.flush().unwrap();
}
});

Ok(Self { parser, sender: tx })
Ok(Self {
parser,
sender: tx,
master_pty: pty_pair.master,
})
}

fn resize(&self, size: Size) {
self.parser
.write()
.unwrap()
.set_size(size.rows - 4, size.cols - 4);
self.master_pty
.resize(PtySize {
rows: size.rows - 4,
cols: size.cols - 4,
pixel_width: 0,
pixel_height: 0,
})
.unwrap();
}
}

Expand All @@ -235,11 +264,15 @@ async fn handle_pane_key_event(pane: &mut PtyPane, key: &KeyEvent) -> bool {
// Close the pane
return false;
}
'l' => {
send = vec![27, 91, 50, 74];
_ => {
let char = ch.to_ascii_uppercase();
let ascii_val = char as u8;
// Since char is guaranteed to be an ASCII character,
// we can safely subtract 64 to get
// the corresponding control character
let ascii_to_send = ascii_val - 64;
send = vec![ascii_to_send];
}

_ => {}
}
}
send
Expand Down
Loading