diff --git a/src/main.rs b/src/main.rs index 92a73f7..41f0240 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,8 +2,6 @@ extern crate dotenv; -// use dotenv::dotenv; - use fltk::{prelude::*, app, text::SimpleTerminal, frame::Frame, window::Window}; use tokio::io::{self, BufReader, AsyncBufReadExt}; @@ -14,11 +12,67 @@ use std::process::Stdio; use std::path::Path; -async fn gui(reader: &mut io::Lines>) -> io::Result<()> { +async fn run_process() -> tokio::process::Child { + let env_path = Path::new("/etc/arch-linux-updater/.env"); + + dotenv::from_path(env_path).unwrap(); + + let cmd_line = env::var("RUN").unwrap(); + + let par = env::var("PARAM").unwrap(); + + let v = par.split_whitespace(); + + println!("{cmd_line} {par}"); + + let child: tokio::process::Child = Command::new(cmd_line) + .args(v) + .stderr(Stdio::piped()) // don't care about stderr + .stdout(Stdio::piped()) // set up stdout so we can read it + .stdin(Stdio::piped()) // set up stdin so we can write on it + .spawn() + .expect("Could not run the command"); // finally run the command + + child +} + + +async fn get_stdout() -> tokio::process::ChildStdout { + let mut child: tokio::process::Child = run_process().await; + + let stdout: tokio::process::ChildStdout = child.stdout.take() + .expect("child did not have a handle to stdout"); + + stdout +} + + +async fn get_buffer() -> BufReader { + let buffer: BufReader = BufReader::new(get_stdout().await); + + buffer +} + + +async fn get_lines() -> io::Lines> { + let lines: io::Lines> = get_buffer().await.lines(); + + lines +} + + +async fn get_line(lines: &mut io::Lines>) -> io::Result> { + let line: Result, io::Error> = lines.next_line().await; + + line +} + + +async fn gui() -> io::Result<()> { let width = 800; let height = 600; - let app = app::App::default().load_system_fonts(); + let app = app::App::default(); let mut wind: fltk::window::DoubleWindow = Window::default() .with_size(width, height) .center_screen() @@ -38,8 +92,10 @@ async fn gui(reader: &mut io::Lines>) -> let mut printed_close_msg = false; + let mut lines: io::Lines> = get_lines().await; + while app.wait() { - if let Some(line) = get_lines(reader).await? { + if let Some(line) = get_line(&mut lines).await? { terminal.append(format!("{}\n", line).as_str()); } else { if printed_close_msg == false { @@ -60,52 +116,13 @@ async fn gui(reader: &mut io::Lines>) -> printed_close_msg = true; } } - // app::sleep(0.05); } Ok(()) } -async fn get_lines(reader: &mut io::Lines>) -> io::Result> { - - reader.next_line().await -} - - -async fn run_process() -> io::Lines>{ - // dotenv().ok(); - let env_path = Path::new("/etc/arch-linux-updater/.env"); - - dotenv::from_path(env_path).unwrap(); - - let cmd_line = env::var("RUN").unwrap(); - - let par = env::var("PARAM").unwrap(); - - let v = par.split_whitespace(); - - println!("{cmd_line} {par}"); - - let mut child = Command::new(cmd_line) - .args(v) - .stderr(Stdio::piped()) // don't care about stderr - .stdout(Stdio::piped()) // set up stdout so we can read it - .stdin(Stdio::piped()) // set up stdin so we can write on it - .spawn() - .expect("Could not run the command"); // finally run the command - - let stdout = child.stdout.take() - .expect("child did not have a handle to stdout"); - - let reader: io::Lines> = BufReader::new(stdout).lines(); - - reader -} - #[tokio::main] async fn main() -> io::Result<()> { - - let mut reader: io::Lines> = run_process().await; - gui(&mut reader).await + gui().await }