aboutsummaryrefslogtreecommitdiff
path: root/vendor/portable-pty/examples/bash.rs
diff options
context:
space:
mode:
authorOgulcan Celik <ogulcancelik@gmail.com>2026-06-23 21:43:23 +0300
committerOgulcan Celik <ogulcancelik@gmail.com>2026-06-23 21:43:23 +0300
commita66f4c64e9d1591f042a94d6f3bef3b4298a4d1c (patch)
tree747de08b6ada4300afb4124da9feff0cc66b96a7 /vendor/portable-pty/examples/bash.rs
parent5981ba426e8ed8f6785195a03cc0d8a190173e61 (diff)
fix: force system conpty on windows
refs #761
Diffstat (limited to 'vendor/portable-pty/examples/bash.rs')
-rw-r--r--vendor/portable-pty/examples/bash.rs85
1 files changed, 85 insertions, 0 deletions
diff --git a/vendor/portable-pty/examples/bash.rs b/vendor/portable-pty/examples/bash.rs
new file mode 100644
index 00000000..2066b7ec
--- /dev/null
+++ b/vendor/portable-pty/examples/bash.rs
@@ -0,0 +1,85 @@
+//! This example demonstrates how to spawn a Bash shell using the `portable_pty` crate.
+//! based on pty/examples/whoami.rs
+
+use portable_pty::{CommandBuilder, NativePtySystem, PtySize, PtySystem};
+use std::io::{Read, Write};
+use std::sync::mpsc::channel;
+use std::thread;
+
+fn main() {
+ let pty_system = NativePtySystem::default();
+
+ // Open the PTY with specified size.
+ let pair = pty_system
+ .openpty(PtySize {
+ rows: 24,
+ cols: 80,
+ pixel_width: 0,
+ pixel_height: 0,
+ })
+ .unwrap();
+
+ // Set up the command to launch Bash.
+ let cmd = CommandBuilder::new("bash");
+ let mut child = pair.slave.spawn_command(cmd).unwrap();
+
+ drop(pair.slave);
+
+ // Set up channels for reading and writing.
+ let (tx, rx) = channel::<String>();
+ let mut reader = pair.master.try_clone_reader().unwrap();
+ let master_writer = pair.master.take_writer().unwrap();
+
+ // Thread to read from the PTY and send data to the main thread.
+ thread::spawn(move || {
+ let mut buffer = [0u8; 1024];
+ loop {
+ match reader.read(&mut buffer) {
+ Ok(0) => break, // EOF
+ Ok(n) => {
+ let output = String::from_utf8_lossy(&buffer[..n]);
+ println!("{}", output); // Print to stdout for visibility.
+ }
+ Err(e) => {
+ eprintln!("Error reading from PTY: {}", e);
+ break;
+ }
+ }
+ }
+ });
+
+ // Thread to write input into the PTY.
+ let tx_writer = thread::spawn(move || {
+ handle_input_stream(rx, master_writer);
+ });
+
+ println!("You can now type commands for Bash (type 'exit' to quit):");
+
+ // Main thread sends user input to the writer thread.
+ loop {
+ let mut input = String::new();
+ std::io::stdin().read_line(&mut input).unwrap();
+
+ if input.trim() == "exit" {
+ break;
+ }
+
+ tx.send(input).unwrap();
+ }
+
+ drop(tx);
+ tx_writer.join().unwrap();
+
+ println!("Waiting for Bash to exit...");
+ let status = child.wait().unwrap();
+ println!("Bash exited with status: {:?}", status);
+}
+
+fn handle_input_stream(rx: std::sync::mpsc::Receiver<String>, mut writer: Box<dyn Write + Send>) {
+ for input in rx.iter() {
+ if writer.write_all(input.as_bytes()).is_err() {
+ eprintln!("Error writing to PTY");
+ break;
+ }
+ }
+}