melawy-linux-updater/src/main.rs

182 lines
4.7 KiB
Rust

// #![allow(dead_code, unused)]
extern crate dotenv;
use fltk::{
app,
app::Screen,
enums::Color,
frame, /*button,*/
image,
prelude::*,
terminal::{/*RedrawStyle,*/ Terminal},
window,
};
use fltk_theme::{
SchemeType, ThemeType, WidgetScheme,
/*ColorTheme,
color_themes,
widget_themes,*/
WidgetTheme,
};
use tokio::io::{self, AsyncBufReadExt, BufReader};
use tokio::process::Command;
use std::env;
use std::path::Path;
use std::process::Stdio;
async fn run_process() -> tokio::process::Child {
#[cfg(not(debug_assertions))]
let env_path = Path::new("/etc/melawy-linux-updater/.env");
#[cfg(debug_assertions)]
let env_path = Path::new(".env_debug");
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 padding: i32 = 10;
let title_and_panel_h: i32 = 36 * 2;
// let (w, h) = app::screen_size();
// println!("{} {}", w, h);
// let width = 1280;
// let height = 720;
let screens = Screen::all_screens();
let mut width = 100000;
let mut height = 100000;
for s in screens {
if s.work_area().w < width {
width = s.work_area().w;
}
if s.work_area().h < height {
height = s.work_area().h;
}
}
width = width / 3 * 2 as i32;
height = height - title_and_panel_h;
let app = app::App::default();
// let app = app::App::default().with_scheme(app::Scheme::Gtk); // Base, Gleam, Gtk, Oxy, Plastic
let widget_theme = WidgetTheme::new(ThemeType::Dark);
widget_theme.apply();
let widget_scheme = WidgetScheme::new(SchemeType::Fluent);
widget_scheme.apply();
let mut win: fltk::window::DoubleWindow = window::Window::default()
.with_size(width, height)
.center_screen()
.with_label("Melawy Linux Updater");
let image =
image::SvgImage::load("/usr/share/melawy-linux-updater/assets/icons/Melawy.svg").unwrap();
win.set_icon(Some(image));
let frame = frame::Frame::default_fill().center_of(&win);
let mut term: Terminal = Terminal::default_fill().center_of(&frame);
term.set_color(Color::Background2);
term.set_text_color(Color::Light2);
term.set_history_rows(5000);
// term.set_redraw_style(RedrawStyle::PerWrite);
win.make_resizable(true);
win.end();
win.show();
let mut printed_close_msg = false;
let mut lines: io::Lines<BufReader<tokio::process::ChildStdout>> = get_lines().await;
// app.run().unwrap();
while app.wait() {
if let Some(line) = get_line(&mut lines).await? {
term.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"
};
term.append(format!("\n{}\n", text_to_close).as_str());
printed_close_msg = true;
}
}
}
Ok(())
}
#[tokio::main]
async fn main() -> io::Result<()> {
gui().await
}