Skip to content

Commit 730cd4a

Browse files
authored
Merge pull request #95 from a-kenji/examples/improve-cross-platform-support
examples: improve cross platform support
2 parents 8f91af0 + e99a20b commit 730cd4a

File tree

4 files changed

+16
-22
lines changed

4 files changed

+16
-22
lines changed

examples/README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ The RWLock ensures that multiple threads can read from the pseudoterminal simult
2626

2727
## `nested_shell`
2828

29-
- Required: `SHELL` environment variable
3029
- Description: Demonstrates nested shell functionality.
3130
- Uses a RWLock to manage shared read/write access.
3231

3332
## `nested_shell_async`
3433

35-
- Required: SHELL environment variable
3634
- Description: Demonstrates nested shell functionality with asynchronous I/O using Tokio.
3735
- Uses an RWLock to manage shared read/write access.
3836

@@ -43,6 +41,5 @@ The RWLock ensures that multiple threads can read from the pseudoterminal simult
4341

4442
## `smux`
4543

46-
- Required: SHELL environment variable
4744
- Description: This example demonstrates a simple terminal multiplexer.
4845
- Uses: asynchronous I/O using Tokio

examples/nested_shell.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use bytes::Bytes;
88
use crossterm::{
9-
event::{self, DisableMouseCapture, Event, KeyCode, KeyEventKind},
9+
event::{self, Event, KeyCode, KeyEventKind},
1010
execute,
1111
style::ResetColor,
1212
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
@@ -39,8 +39,7 @@ fn main() -> std::io::Result<()> {
3939

4040
let pty_system = NativePtySystem::default();
4141
let cwd = std::env::current_dir().unwrap();
42-
let shell = std::env::var("SHELL").unwrap();
43-
let mut cmd = CommandBuilder::new(shell);
42+
let mut cmd = CommandBuilder::new_default_prog();
4443
cmd.cwd(cwd);
4544

4645
let size = Size {
@@ -105,11 +104,7 @@ fn main() -> std::io::Result<()> {
105104

106105
// restore terminal
107106
disable_raw_mode()?;
108-
execute!(
109-
terminal.backend_mut(),
110-
LeaveAlternateScreen,
111-
DisableMouseCapture
112-
)?;
107+
execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
113108
terminal.show_cursor()?;
114109
println!("{size:?}");
115110
Ok(())
@@ -138,7 +133,12 @@ fn run<B: Backend>(
138133
KeyCode::Backspace => {
139134
sender.send(Bytes::from(vec![8])).unwrap();
140135
}
141-
KeyCode::Enter => sender.send(Bytes::from(vec![b'\n'])).unwrap(),
136+
KeyCode::Enter => {
137+
#[cfg(unix)]
138+
sender.send(Bytes::from(vec![b'\n'])).unwrap();
139+
#[cfg(windows)]
140+
sender.send(Bytes::from(vec![b'\r', b'\n'])).unwrap();
141+
}
142142
KeyCode::Left => sender.send(Bytes::from(vec![27, 91, 68])).unwrap(),
143143
KeyCode::Right => sender.send(Bytes::from(vec![27, 91, 67])).unwrap(),
144144
KeyCode::Up => sender.send(Bytes::from(vec![27, 91, 65])).unwrap(),

examples/nested_shell_async.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66

77
use bytes::Bytes;
88
use crossterm::{
9-
event::{self, DisableMouseCapture, Event, KeyCode, KeyEventKind},
9+
event::{self, Event, KeyCode, KeyEventKind},
1010
execute,
1111
style::ResetColor,
1212
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
@@ -44,8 +44,7 @@ async fn main() -> io::Result<()> {
4444

4545
let pty_system = NativePtySystem::default();
4646
let cwd = std::env::current_dir().unwrap();
47-
let shell = std::env::var("SHELL").unwrap();
48-
let mut cmd = CommandBuilder::new(shell);
47+
let mut cmd = CommandBuilder::new_default_prog();
4948
cmd.cwd(cwd);
5049

5150
let size = Size {
@@ -112,11 +111,7 @@ async fn main() -> io::Result<()> {
112111

113112
// restore terminal
114113
disable_raw_mode()?;
115-
execute!(
116-
terminal.backend_mut(),
117-
LeaveAlternateScreen,
118-
DisableMouseCapture
119-
)?;
114+
execute!(terminal.backend_mut(), LeaveAlternateScreen,)?;
120115
terminal.show_cursor()?;
121116
println!("{size:?}");
122117
Ok(())

examples/smux.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ async fn main() -> io::Result<()> {
4040
let (mut terminal, mut size) = setup_terminal().unwrap();
4141

4242
let cwd = std::env::current_dir().unwrap();
43-
let shell = std::env::var("SHELL").unwrap();
44-
let mut cmd = CommandBuilder::new(shell);
43+
let mut cmd = CommandBuilder::new_default_prog();
4544
cmd.cwd(cwd);
4645

4746
let mut panes: Vec<PtyPane> = Vec::new();
@@ -245,7 +244,10 @@ async fn handle_pane_key_event(pane: &mut PtyPane, key: &KeyEvent) -> bool {
245244
}
246245
send
247246
}
247+
#[cfg(unix)]
248248
KeyCode::Enter => vec![b'\n'],
249+
#[cfg(windows)]
250+
KeyCode::Enter => vec![b'\r', b'\n'],
249251
KeyCode::Backspace => vec![8],
250252
KeyCode::Left => vec![27, 91, 68],
251253
KeyCode::Right => vec![27, 91, 67],

0 commit comments

Comments
 (0)