refactoring

This commit is contained in:
steven-omaha
2023-02-01 16:57:34 +01:00
parent 537529cb77
commit 70bb6100bc
2 changed files with 24 additions and 23 deletions
+2 -22
View File
@@ -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<dyn Backend>, Vec<ReviewAction>)>);
@@ -137,25 +136,6 @@ fn ask_user_action_for_package() -> Result<ReviewIntention> {
_ => Ok(ReviewIntention::Invalid),
}
}
fn read_single_char_from_terminal() -> Result<char> {
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<Group>]) {
for (i, group) in groups.iter().enumerate() {
println!("{i}: {}", group.name);
+22 -1
View File
@@ -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<char> {
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)
}