diff --git a/keyfork-prompt/src/alternate_screen.rs b/keyfork-prompt/src/alternate_screen.rs index eb4e486..ed22e50 100644 --- a/keyfork-prompt/src/alternate_screen.rs +++ b/keyfork-prompt/src/alternate_screen.rs @@ -1,7 +1,6 @@ use std::{ io::Write, os::fd::AsRawFd, - sync::{Arc, Mutex}, }; use crossterm::{ @@ -12,50 +11,44 @@ use crossterm::{ use crate::Result; -pub(crate) struct AlternateScreen +pub(crate) struct AlternateScreen<'a, W> where W: Write + AsRawFd + Sized, { - write: Arc>, + write: &'a mut W, } -impl AlternateScreen +impl<'a, W> AlternateScreen<'a, W> where W: Write + AsRawFd + Sized, { - pub(crate) fn new(write_handle: Arc>) -> Result { - let mut write = write_handle.lock().unwrap(); - write.execute(EnterAlternateScreen)?.execute(MoveTo(0, 0))?; - drop(write); + pub(crate) fn new(write_handle: &'a mut W) -> Result { + write_handle.execute(EnterAlternateScreen)?.execute(MoveTo(0, 0))?; Ok(Self { write: write_handle, }) } - - pub(crate) fn arc_mutex(self) -> Arc> { - Arc::new(Mutex::new(self)) - } } -impl Write for AlternateScreen +impl Write for AlternateScreen<'_, W> where W: Write + AsRawFd + Sized, { fn write(&mut self, buf: &[u8]) -> std::io::Result { - self.write.lock().unwrap().write(buf) + self.write.write(buf) } fn flush(&mut self) -> std::io::Result<()> { - self.write.lock().unwrap().flush() + self.write.flush() } } -impl AsRawFd for AlternateScreen +impl AsRawFd for AlternateScreen<'_, W> where W: Write + AsRawFd + Sized, { fn as_raw_fd(&self) -> std::os::fd::RawFd { - self.write.lock().unwrap().as_raw_fd() + self.write.as_raw_fd() } } @@ -77,12 +70,11 @@ where } */ -impl Drop for AlternateScreen +impl Drop for AlternateScreen<'_, W> where W: Write + AsRawFd + Sized, { fn drop(&mut self) { - let mut write_handle = self.write.lock().unwrap(); - write_handle.execute(LeaveAlternateScreen).unwrap(); + self.write.execute(LeaveAlternateScreen).unwrap(); } } diff --git a/keyfork-prompt/src/lib.rs b/keyfork-prompt/src/lib.rs index 124bdda..34a5627 100644 --- a/keyfork-prompt/src/lib.rs +++ b/keyfork-prompt/src/lib.rs @@ -1,7 +1,6 @@ use std::{ io::{BufRead, BufReader, Read, Write}, os::fd::AsRawFd, - sync::{Arc, Mutex}, }; use crossterm::{ @@ -29,8 +28,8 @@ pub enum Error { pub type Result = std::result::Result; pub struct PromptManager { - read: Arc>>, - write: Arc>, + read: BufReader, + write: W, } impl PromptManager @@ -43,31 +42,27 @@ where return Err(Error::NotATTY); } Ok(Self { - read: Arc::new(Mutex::new(BufReader::new(read_handle))), - write: Arc::new(Mutex::new(write_handle)), + read: BufReader::new(read_handle), + write: write_handle, }) } - fn alt_screen(&self) -> Result> { - AlternateScreen::new(self.write.clone()) - } - pub fn prompt_input(&mut self, prompt: &str) -> Result { - let mut alt_screen = self.alt_screen()?; - alt_screen + let mut terminal = AlternateScreen::new(&mut self.write)?; + terminal .execute(terminal::Clear(terminal::ClearType::All))? .execute(Print(prompt))?; let mut line = String::new(); - self.read.lock().unwrap().read_line(&mut line)?; + self.read.read_line(&mut line)?; Ok(line) } // TODO: return secrecy::Secret // TODO: write a guard drop system for raw mode pub fn prompt_passphrase(&mut self, prompt: &str) -> Result { - let write = AlternateScreen::new(self.write.clone())?; - let mut write = RawMode::new(write.arc_mutex())?; - write + let mut terminal = AlternateScreen::new(&mut self.write)?; + let mut terminal = RawMode::new(&mut terminal)?; + terminal .execute(terminal::Clear(terminal::ClearType::All))? .execute(Print(prompt))?; let mut passphrase = String::new(); @@ -79,7 +74,7 @@ where break; } KeyCode::Char(c) => { - write.execute(Print("*"))?; + terminal.execute(Print("*"))?; passphrase.push(c); } _ => (), diff --git a/keyfork-prompt/src/raw_mode.rs b/keyfork-prompt/src/raw_mode.rs index 7cfdde9..60be05c 100644 --- a/keyfork-prompt/src/raw_mode.rs +++ b/keyfork-prompt/src/raw_mode.rs @@ -1,56 +1,51 @@ use std::{ io::Write, os::fd::AsRawFd, - sync::{Arc, Mutex}, }; -use crossterm::{terminal, ExecutableCommand}; +use crossterm::terminal; use crate::Result; -pub(crate) struct RawMode +pub(crate) struct RawMode<'a, W> where W: Write + AsRawFd + Sized, { - write: Arc>, + write: &'a mut W, } // TODO: fork crossterm to allow using FD from as_raw_fd() -impl RawMode +impl<'a, W> RawMode<'a, W> where W: Write + AsRawFd + Sized, { - pub(crate) fn new(write_handle: Arc>) -> Result { + pub(crate) fn new(write_handle: &'a mut W) -> Result { terminal::enable_raw_mode()?; Ok(Self { write: write_handle, }) } - - pub(crate) fn arc_mutex(self) -> Arc> { - Arc::new(Mutex::new(self)) - } } -impl Write for RawMode +impl Write for RawMode<'_, W> where W: Write + AsRawFd + Sized, { fn write(&mut self, buf: &[u8]) -> std::io::Result { - self.write.lock().unwrap().write(buf) + self.write.write(buf) } fn flush(&mut self) -> std::io::Result<()> { - self.write.lock().unwrap().flush() + self.write.flush() } } -impl AsRawFd for RawMode +impl AsRawFd for RawMode<'_, W> where W: Write + AsRawFd + Sized, { fn as_raw_fd(&self) -> std::os::fd::RawFd { - self.write.lock().unwrap().as_raw_fd() + self.write.as_raw_fd() } } @@ -72,7 +67,7 @@ where } */ -impl Drop for RawMode +impl Drop for RawMode<'_, W> where W: Write + AsRawFd + Sized, {