From 2f7742cc9d8045ff198e7828aa7c23c9eabafa3f Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Thu, 2 Feb 2023 15:32:23 +0100 Subject: [PATCH] refactoring --- src/core.rs | 4 ++-- src/grouping/group.rs | 3 +-- src/review/mod.rs | 2 +- src/ui.rs | 31 +++++++++++++++++++++---------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/core.rs b/src/core.rs index 9f1c859..527caa0 100644 --- a/src/core.rs +++ b/src/core.rs @@ -98,7 +98,7 @@ impl Pacdef { to_install.show("install".into()); - if !get_user_confirmation() { + if !get_user_confirmation()? { return Ok(()); }; @@ -178,7 +178,7 @@ impl Pacdef { to_remove.show("remove".into()); - if !get_user_confirmation() { + if !get_user_confirmation()? { return Ok(()); }; diff --git a/src/grouping/group.rs b/src/grouping/group.rs index b7b40fb..cfbae6f 100644 --- a/src/grouping/group.rs +++ b/src/grouping/group.rs @@ -1,7 +1,6 @@ -use std::fmt::Write as FmtWrite; use std::fs::{read_to_string, File}; use std::hash::Hash; -use std::io::Write as IoWrite; +use std::io::Write; use std::path::{Path, PathBuf}; use std::{collections::HashSet, fmt::Display}; diff --git a/src/review/mod.rs b/src/review/mod.rs index 410ace6..5242746 100644 --- a/src/review/mod.rs +++ b/src/review/mod.rs @@ -50,7 +50,7 @@ pub(crate) fn review( strat.show(); } - if !get_user_confirmation() { + if !get_user_confirmation()? { return Ok(()); } diff --git a/src/ui.rs b/src/ui.rs index 7a298fe..466c417 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,30 +1,41 @@ -use std::io::{self, BufRead, Read, Write}; +use std::io::{self, Read, Write}; -use anyhow::Result; +use anyhow::{Context, Result}; use termios::*; #[must_use] -pub(crate) fn get_user_confirmation() -> bool { +pub(crate) fn get_user_confirmation() -> Result { print!("Continue? [Y/n] "); std::io::stdout().flush().unwrap(); - let reply = std::io::stdin().lock().lines().next().unwrap().unwrap(); - reply.trim().is_empty() || reply.to_lowercase().contains('y') + + let mut reply = String::new(); + std::io::stdin() + .read_line(&mut reply) + .context("reading stdin")?; + + Ok(reply.trim().is_empty() || reply.to_lowercase().contains('y')) } pub(crate) fn read_single_char_from_terminal() -> Result { - let fd = 0; // 0 is the file descriptor for stdin - let termios = Termios::from_fd(fd)?; + // 0 is the file descriptor for stdin + let fd = 0; + let termios = Termios::from_fd(fd).context("getting stdin fd")?; let mut new_termios = termios; new_termios.c_lflag &= !(ICANON | ECHO); new_termios.c_cc[VMIN] = 1; new_termios.c_cc[VTIME] = 0; - tcsetattr(fd, TCSANOW, &new_termios).unwrap(); + tcsetattr(fd, TCSANOW, &new_termios).context("setting terminal mode")?; let mut input = [0u8; 1]; - io::stdin().read_exact(&mut input[..]).unwrap(); + io::stdin() + .read_exact(&mut input[..]) + .context("reading one byte from stdin")?; let result = input[0] as char; + // stdin is not echoed automatically in this terminal mode println!("{result}"); - tcsetattr(fd, TCSANOW, &termios).unwrap(); // restore previous settings + // restore previous settings + tcsetattr(fd, TCSANOW, &termios).context("restoring terminal mode")?; + Ok(result) }