Skip to content

Commit

Permalink
switch to connection-oriented interface for Config, since Windows nee…
Browse files Browse the repository at this point in the history
…ds to read from and write to both handles
  • Loading branch information
apparebit committed Dec 23, 2024
1 parent 47eee96 commit fd030e0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
10 changes: 6 additions & 4 deletions crates/prettytty/src/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl Connection {
let connection = RawConnection::open(&options)
.map_err(|e| Error::new(ErrorKind::ConnectionRefused, e))?;

let config = Config::read(connection.input())?;
let config = Config::read(&connection)?;
let verbose = options.verbose();
if verbose {
println!("terminal::config {:?}", &config);
Expand All @@ -55,9 +55,11 @@ impl Connection {
if verbose {
println!("terminal::reconfig {:?}", &reconfig);
}
reconfig.write(connection.output())?;
reconfig.write(&connection)?;
if verbose {
println!("terminal::reconfigured")
// We need explicit carriage-return and line-feed characters
// because the reconfiguratio just took effect.
print!("terminal::reconfigured\r\n")
}
Ok(Some(config))
},
Expand Down Expand Up @@ -147,7 +149,7 @@ impl Drop for Connection {
let _ = w.flush();
});
if let Some(cfg) = &self.config {
let _ = cfg.write(self.connection.output());
let _ = cfg.write(&self.connection);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions crates/prettytty/src/sys/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ pub(crate) struct Config {

impl Config {
/// Read the configuration.
pub fn read(input: RawInput) -> Result<Self> {
pub fn read(connection: &RawConnection) -> Result<Self> {
let mut state = std::mem::MaybeUninit::uninit();
unsafe { libc::tcgetattr(input.handle(), state.as_mut_ptr()) }.into_result()?;
unsafe { libc::tcgetattr(connection.input().handle(), state.as_mut_ptr()) }.into_result()?;
Ok(Self {
state: unsafe { state.assume_init() },
})
Expand All @@ -175,8 +175,8 @@ impl Config {
}

/// Write the configuration.
pub fn write(&self, output: RawOutput) -> Result<()> {
unsafe { libc::tcsetattr(output.handle(), libc::TCSAFLUSH, from_ref(&self.state)) }
pub fn write(&self, connection: &RawConnection) -> Result<()> {
unsafe { libc::tcsetattr(connection.input().handle(), libc::TCSAFLUSH, from_ref(&self.state)) }
.into_result()?;
Ok(())
}
Expand Down Expand Up @@ -327,6 +327,7 @@ impl RawOutput {
Self { handle }
}

#[allow(dead_code)]
#[inline]
fn handle(&self) -> RawHandle {
self.handle
Expand Down
34 changes: 24 additions & 10 deletions crates/prettytty/src/sys/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ pub(crate) struct Config {
}

impl Config {
pub fn read(input: RawInput) -> Result<Self> {
let input_modes = Self::read_mode(&input)?;
pub fn read(connection: &RawConnection) -> Result<Self> {
let input_modes = Self::read_mode(connection.input())?;
let input_encoding = unsafe { Console::GetConsoleCP() }.into_result()?;
let output_modes = Self::read_mode(&input)?;
let output_modes = Self::read_mode(connection.output())?;
let output_encoding = unsafe { Console::GetConsoleOutputCP() }.into_result()?;

Ok(Self {
Expand All @@ -113,9 +113,9 @@ impl Config {
})
}

fn read_mode(input: &RawInput) -> Result<ConsoleMode> {
fn read_mode(handle: impl Into<RawHandle>) -> Result<ConsoleMode> {
let mut mode = 0;
unsafe { Console::GetConsoleMode(input.handle(), from_mut(&mut mode)) }.into_result()?;
unsafe { Console::GetConsoleMode(handle.into(), from_mut(&mut mode)) }.into_result()?;
Ok(mode)
}

Expand Down Expand Up @@ -156,18 +156,18 @@ impl Config {
})
}

pub fn write(&self, output: RawOutput) -> Result<()> {
let result1 = Self::write_mode(&output, self.input_modes);
pub fn write(&self, connection: &RawConnection) -> Result<()> {
let result1 = Self::write_mode(connection.input(), self.input_modes);
let result2 = unsafe { Console::SetConsoleCP(self.input_encoding) }.into_result();
let result3 = Self::write_mode(&output, self.output_modes);
let result3 = Self::write_mode(connection.output(), self.output_modes);
let result4 = unsafe { Console::SetConsoleOutputCP(self.output_encoding) }.into_result();

result1.and(result2).and(result3).and(result4)?;
Ok(())
}

fn write_mode(output: &RawOutput, mode: ConsoleMode) -> Result<()> {
unsafe { Console::SetConsoleMode(output.handle(), mode) }.into_result()?;
fn write_mode(handle: impl Into<RawHandle>, mode: ConsoleMode) -> Result<()> {
unsafe { Console::SetConsoleMode(handle.into(), mode) }.into_result()?;
Ok(())
}

Expand Down Expand Up @@ -243,6 +243,7 @@ impl RawInput {
Self { handle, timeout }
}

#[allow(dead_code)]
#[inline]
fn handle(&self) -> RawHandle {
self.handle
Expand All @@ -255,6 +256,12 @@ impl RawInput {
// for wrapped handles, too. Also, access to raw input is gated by a mutex.
unsafe impl Send for RawInput {}

impl From<RawInput> for RawHandle {
fn from(value: RawInput) -> Self {
value.handle
}
}

impl Read for RawInput {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
let status = unsafe { Threading::WaitForSingleObject(self.handle, self.timeout) };
Expand Down Expand Up @@ -296,6 +303,7 @@ impl RawOutput {
Self { handle }
}

#[allow(dead_code)]
#[inline]
pub fn handle(&self) -> RawHandle {
self.handle
Expand All @@ -308,6 +316,12 @@ impl RawOutput {
// for wrapped handles, too. Also, access to raw input is gated by a mutex.
unsafe impl Send for RawOutput {}

impl From<RawOutput> for RawHandle {
fn from(value: RawOutput) -> Self {
value.handle
}
}

impl Write for RawOutput {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
let mut did_write: u32 = 0;
Expand Down

0 comments on commit fd030e0

Please sign in to comment.