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

108 lines
2.9 KiB
Rust

// #![allow(dead_code, unused)]
extern crate dotenv;
use dotenv::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;
async fn gui(reader: &mut io::Lines<BufReader<tokio::process::ChildStdout>>) -> io::Result<()> {
let width = 800;
let height = 600;
let app = app::App::default().load_system_fonts();
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;
while app.wait() {
if let Some(line) = get_lines(reader).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;
}
}
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 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
}