This commit is contained in:
Valeria Fadeeva 2023-07-04 17:34:01 +05:00
parent d6f42c98dd
commit 5c359c2bd6
1 changed files with 62 additions and 45 deletions

View File

@ -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<BufReader<tokio::process::ChildStdout>>) -> 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<tokio::process::ChildStdout> {
let buffer: BufReader<tokio::process::ChildStdout> = BufReader::new(get_stdout().await);
buffer
}
async fn get_lines() -> io::Lines<BufReader<tokio::process::ChildStdout>> {
let lines: io::Lines<BufReader<tokio::process::ChildStdout>> = get_buffer().await.lines();
lines
}
async fn get_line(lines: &mut io::Lines<BufReader<tokio::process::ChildStdout>>) -> io::Result<Option<String>> {
let line: Result<Option<String>, 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<BufReader<tokio::process::ChildStdout>>) ->
let mut printed_close_msg = false;
let mut lines: io::Lines<BufReader<tokio::process::ChildStdout>> = 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<BufReader<tokio::process::ChildStdout>>) ->
printed_close_msg = true;
}
}
// app::sleep(0.05);
}
Ok(())
}
async fn get_lines(reader: &mut io::Lines<BufReader<tokio::process::ChildStdout>>) -> io::Result<Option<String>> {
reader.next_line().await
}
async fn run_process() -> io::Lines<BufReader<tokio::process::ChildStdout>>{
// 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<tokio::process::ChildStdout>> = BufReader::new(stdout).lines();
reader
}
#[tokio::main]
async fn main() -> io::Result<()> {
let mut reader: io::Lines<BufReader<tokio::process::ChildStdout>> = run_process().await;
gui(&mut reader).await
gui().await
}