feat: should make crate usable on Windows (#35)

Co-authored-by: LIAUD Corentin <corentin.liaud@orange.fr>
This commit is contained in:
cocool97
2024-10-01 19:39:25 +02:00
committed by GitHub
parent 2f39c13355
commit ed884b0d27
6 changed files with 42 additions and 66 deletions

View File

@@ -13,4 +13,7 @@ adb_client = { version = "1.0.6" }
anyhow = { version = "1.0.89" }
clap = { version = "4.5.18", features = ["derive"] }
env_logger = { version = "0.11.5" }
log = "0.4.22"
log = { version = "0.4.22" }
[target.'cfg(unix)'.dependencies]
termios = { version = "0.3.3" }

View File

@@ -0,0 +1,40 @@
#![cfg(any(target_os = "linux", target_os = "macos"))]
use std::os::unix::prelude::{AsRawFd, RawFd};
use termios::{tcsetattr, Termios, TCSANOW, VMIN, VTIME};
use crate::Result;
pub struct ADBTermios {
fd: RawFd,
old_termios: Termios,
new_termios: Termios,
}
impl ADBTermios {
pub fn new(fd: impl AsRawFd) -> Result<Self> {
let mut new_termios = Termios::from_fd(fd.as_raw_fd())?;
let old_termios = new_termios; // Saves previous state
new_termios.c_lflag = 0;
new_termios.c_cc[VTIME] = 0;
new_termios.c_cc[VMIN] = 1;
Ok(Self {
fd: fd.as_raw_fd(),
old_termios,
new_termios,
})
}
pub fn set_adb_termios(&mut self) -> Result<()> {
Ok(tcsetattr(self.fd, TCSANOW, &self.new_termios)?)
}
}
impl Drop for ADBTermios {
fn drop(&mut self) {
// Custom drop implementation, restores previous termios structure.
tcsetattr(self.fd, TCSANOW, &self.old_termios).unwrap();
}
}

View File

@@ -1,3 +1,5 @@
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod adb_termios;
mod commands;
mod models;
@@ -53,7 +55,19 @@ fn main() -> Result<()> {
}
LocalCommand::Shell { command } => {
if command.is_empty() {
device.shell()?;
// Need to duplicate some code here as ADBTermios [Drop] implementation resets terminal state.
// Using a scope here would call drop() too early..
#[cfg(any(target_os = "linux", target_os = "macos"))]
{
let mut adb_termios = adb_termios::ADBTermios::new(std::io::stdin())?;
adb_termios.set_adb_termios()?;
device.shell(std::io::stdin(), std::io::stdout())?;
}
#[cfg(not(any(target_os = "linux", target_os = "macos")))]
{
device.shell(std::io::stdin(), std::io::stdout())?;
}
} else {
device.shell_command(command, std::io::stdout())?;
}