From 330a126c750c1c6ee0939b7eb86e8cb2aa307a16 Mon Sep 17 00:00:00 2001 From: "Dr. Matthias Ratajczak" Date: Wed, 1 Feb 2023 16:57:34 +0100 Subject: [PATCH] refactoring --- src/review.rs | 24 ++---------------------- src/ui.rs | 23 ++++++++++++++++++++++- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/review.rs b/src/review.rs index 8d040c7..615af0b 100644 --- a/src/review.rs +++ b/src/review.rs @@ -1,12 +1,11 @@ -use std::io::{self, stdin, stdout, Read, Write}; +use std::io::{stdin, stdout, Write}; use std::rc::Rc; use anyhow::Result; -use termios::*; use crate::backend::{Backend, ToDoPerBackend}; use crate::grouping::{Group, Package}; -use crate::ui::get_user_confirmation; +use crate::ui::{get_user_confirmation, read_single_char_from_terminal}; #[derive(Debug)] struct ReviewsPerBackend(Vec<(Box, Vec)>); @@ -137,25 +136,6 @@ fn ask_user_action_for_package() -> Result { _ => Ok(ReviewIntention::Invalid), } } - -fn read_single_char_from_terminal() -> Result { - let fd = 0; // 0 is the file descriptor for stdin - let termios = Termios::from_fd(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(); - - let mut input = [0u8; 1]; - io::stdin().read_exact(&mut input[..]).unwrap(); - let result = input[0] as char; - println!("{result}"); - - tcsetattr(fd, TCSANOW, &termios).unwrap(); // restore previous settings - Ok(result) -} - fn print_enumerated_groups(groups: &[Rc]) { for (i, group) in groups.iter().enumerate() { println!("{i}: {}", group.name); diff --git a/src/ui.rs b/src/ui.rs index 766a531..7a298fe 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,4 +1,7 @@ -use std::io::{BufRead, Write}; +use std::io::{self, BufRead, Read, Write}; + +use anyhow::Result; +use termios::*; #[must_use] pub(crate) fn get_user_confirmation() -> bool { @@ -7,3 +10,21 @@ pub(crate) fn get_user_confirmation() -> bool { let reply = std::io::stdin().lock().lines().next().unwrap().unwrap(); 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)?; + 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(); + + let mut input = [0u8; 1]; + io::stdin().read_exact(&mut input[..]).unwrap(); + let result = input[0] as char; + println!("{result}"); + + tcsetattr(fd, TCSANOW, &termios).unwrap(); // restore previous settings + Ok(result) +}