melawy-arch-linux-updater/src/main.rs

129 lines
3.3 KiB
Rust

// #![allow(dead_code, unused)]
extern crate dotenv;
use fltk::{prelude::*, app, text::SimpleTerminal, frame::Frame, window::Window};
use tokio::io::{self, BufReader, AsyncBufReadExt};
use tokio::process::Command;
use std::env;
use std::process::Stdio;
use std::path::Path;
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();
let mut wind: fltk::window::DoubleWindow = Window::default()
.with_size(width, height)
.center_screen()
.with_label("Arch Linux Updater");
let frame = Frame::default_fill()
.with_size(width, height)
.center_of(&wind);
let mut terminal: SimpleTerminal = SimpleTerminal::default_fill()
.with_size(width, height)
.center_of(&frame);
wind.make_resizable(true);
wind.end();
wind.show();
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_line(&mut lines).await? {
terminal.append(format!("{}\n", line).as_str());
} else {
if printed_close_msg == false {
let lang_key = "LANG";
let get_lang = match env::var(lang_key) {
Ok(val) => val,
Err(_) => "".to_string(),
};
let text_to_close = if get_lang == "ru_RU.UTF-8" {
"Теперь это окно можно закрыть"
} else {
"Now this window may close"
};
terminal.append(format!("\n{}\n", text_to_close).as_str());
printed_close_msg = true;
}
}
}
Ok(())
}
#[tokio::main]
async fn main() -> io::Result<()> {
gui().await
}