feat: should make crate usable on Windows (#35)
Co-authored-by: LIAUD Corentin <corentin.liaud@orange.fr>
This commit is contained in:
@@ -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" }
|
||||
|
||||
40
adb_cli/src/adb_termios.rs
Normal file
40
adb_cli/src/adb_termios.rs
Normal 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();
|
||||
}
|
||||
}
|
||||
@@ -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())?;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user