Compare commits
83 commits
move-core-
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e12223344 | |||
| ee7de8f355 | |||
| 25b6fe4b7e | |||
| b777a94c98 | |||
| c789f51bac | |||
| 827451322d | |||
| 46e53ef105 | |||
| 3340114522 | |||
| 3fafcc4292 | |||
| 1d0b692028 | |||
| 185ddf3103 | |||
| 561d57d9a2 | |||
| fe12b3c4e2 | |||
| 710b9d7c16 | |||
| e55a254be9 | |||
| b63927d3ef | |||
| 9f0ecb9baf | |||
| 5ba30e626f | |||
| ee7147a187 | |||
| 4fca7d6696 | |||
| 0084b5968d | |||
| 92dcfc2592 | |||
| 92f7874584 | |||
| a9e929585f | |||
| dc49006af8 | |||
| 88f1234dd0 | |||
| 718e4061f8 | |||
| dfc861cc09 | |||
| 52ccc154cc | |||
| e0823293e2 | |||
| 2296b8f274 | |||
| 081fd22de1 | |||
| b06c0db27d | |||
| a2f61e310f | |||
| 7b0deb4cc7 | |||
| 8f8f78a161 | |||
| b9ade066e7 | |||
| 713dc8c2de | |||
| 2aede33db6 | |||
| 15b5d5e131 | |||
| 0a00e970aa | |||
| f556703baf | |||
| d0eecc36c5 | |||
| 59b9ddbee6 | |||
| 7e08ca5f72 | |||
| 31d1391925 | |||
| 3c4603dc7b | |||
| 5028e16c5b | |||
| 6970f2db3e | |||
| 367a290b6d | |||
| f22501ce7d | |||
| f1c3743584 | |||
| c5a9dc80cb | |||
| 2b2a5160fd | |||
| f272b0c482 | |||
| 121beeaa78 | |||
| 2829fbcac1 | |||
| 72130109cb | |||
| 1fab68500b | |||
| 1c9ed12d49 | |||
| cd38712ea9 | |||
| 167ec52d6f | |||
| 1336c20dc2 | |||
| 14b40410d4 | |||
| acccdccb38 | |||
| dd7c2fd7ab | |||
| 46311a3ede | |||
| de7520324b | |||
| b2b90bb946 | |||
| 1ce7f76229 | |||
| aa4ac8d5e8 | |||
| 9addea0676 | |||
| 8be96d21b9 | |||
| 5c0c47cc0f | |||
| b8403d6f52 | |||
| 833223b7ad | |||
| 1373d33f42 | |||
| 51610fdc23 | |||
| 6b2eb8155f | |||
| 76a27cd179 | |||
| 3674fbdee7 | |||
| 783e31a582 | |||
| 6938960f3e |
64 changed files with 4535 additions and 1851 deletions
614
Cargo.lock
generated
614
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
12
Cargo.toml
12
Cargo.toml
|
|
@ -1,19 +1,19 @@
|
||||||
# This file is part of Deja-vu.
|
# This file is part of Deja-Vu.
|
||||||
#
|
#
|
||||||
# Deja-vu is free software: you can redistribute it and/or modify it
|
# Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
# under the terms of the GNU General Public License as published by
|
# under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# Deja-vu is distributed in the hope that it will be useful, but
|
# Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
# See the GNU General Public License for more details.
|
# See the GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
# along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["core", "deja_vu", "pe_menu"]
|
members = ["core", "boot_diags", "deja_vu", "pe_menu"]
|
||||||
default-members = ["deja_vu", "pe_menu"]
|
default-members = ["core", "boot_diags", "deja_vu", "pe_menu"]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
|
||||||
48
boot_diags/Cargo.toml
Normal file
48
boot_diags/Cargo.toml
Normal file
|
|
@ -0,0 +1,48 @@
|
||||||
|
# This file is part of Deja-Vu.
|
||||||
|
#
|
||||||
|
# Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
# under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
# See the GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
[package]
|
||||||
|
name = "boot-diags"
|
||||||
|
authors = ["2Shirt <2xShirt@gmail.com>"]
|
||||||
|
edition = "2024"
|
||||||
|
license = "GPL"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
core = { path = "../core" }
|
||||||
|
clap = { version = "4.4.5", features = [
|
||||||
|
"derive",
|
||||||
|
"cargo",
|
||||||
|
"wrap_help",
|
||||||
|
"unicode",
|
||||||
|
"string",
|
||||||
|
"unstable-styles",
|
||||||
|
] }
|
||||||
|
color-eyre = "0.6.3"
|
||||||
|
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
||||||
|
futures = "0.3.30"
|
||||||
|
ratatui = "0.29.0"
|
||||||
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
|
tokio = { version = "1.43.0", features = ["full"] }
|
||||||
|
toml = "0.8.13"
|
||||||
|
tracing = "0.1.41"
|
||||||
|
tracing-error = "0.2.0"
|
||||||
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
||||||
|
check_elevation = "0.2.4"
|
||||||
|
regex = "1.11.1"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
anyhow = "1.0.86"
|
||||||
|
vergen-gix = { version = "1.0.0", features = ["build", "cargo"] }
|
||||||
28
boot_diags/build.rs
Normal file
28
boot_diags/build.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use anyhow::Result;
|
||||||
|
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let build = BuildBuilder::all_build()?;
|
||||||
|
let gix = GixBuilder::all_git()?;
|
||||||
|
let cargo = CargoBuilder::all_cargo()?;
|
||||||
|
Emitter::default()
|
||||||
|
.add_instructions(&build)?
|
||||||
|
.add_instructions(&gix)?
|
||||||
|
.add_instructions(&cargo)?
|
||||||
|
.emit()
|
||||||
|
}
|
||||||
958
boot_diags/src/app.rs
Normal file
958
boot_diags/src/app.rs
Normal file
|
|
@ -0,0 +1,958 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use core::{
|
||||||
|
action::{Action, DiagResult},
|
||||||
|
components::{
|
||||||
|
Component,
|
||||||
|
footer::Footer,
|
||||||
|
left::{Left, SelectionType},
|
||||||
|
popup,
|
||||||
|
right::Right,
|
||||||
|
state::StatefulList,
|
||||||
|
title::Title,
|
||||||
|
},
|
||||||
|
config::Config,
|
||||||
|
line::{DVLine, get_disk_description_right, get_part_description},
|
||||||
|
state::Mode,
|
||||||
|
system::{
|
||||||
|
boot::{self, SafeMode, configure_disk},
|
||||||
|
cpu::get_cpu_name,
|
||||||
|
drivers,
|
||||||
|
},
|
||||||
|
tasks::{Task, TaskResult, TaskType, Tasks},
|
||||||
|
tui::{Event, Tui},
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
env,
|
||||||
|
iter::zip,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
|
use color_eyre::Result;
|
||||||
|
use ratatui::{
|
||||||
|
crossterm::event::KeyEvent,
|
||||||
|
layout::{Constraint, Direction, Layout},
|
||||||
|
prelude::Rect,
|
||||||
|
style::Color,
|
||||||
|
};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
diags::{
|
||||||
|
DiagGroup, Type as DiagType, get_diag_type, parse_bcd, parse_bitlocker, parse_chkdsk,
|
||||||
|
parse_dism, parse_registry_hives, parse_system_files,
|
||||||
|
},
|
||||||
|
scan,
|
||||||
|
state::State,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct App {
|
||||||
|
// TUI
|
||||||
|
action_rx: mpsc::UnboundedReceiver<Action>,
|
||||||
|
action_tx: mpsc::UnboundedSender<Action>,
|
||||||
|
components: Vec<Box<dyn Component>>,
|
||||||
|
config: Config,
|
||||||
|
frame_rate: f64,
|
||||||
|
last_tick_key_events: Vec<KeyEvent>,
|
||||||
|
should_quit: bool,
|
||||||
|
should_suspend: bool,
|
||||||
|
tick_rate: f64,
|
||||||
|
// App
|
||||||
|
cur_mode: Mode,
|
||||||
|
diag_groups: Arc<Mutex<Vec<DiagGroup>>>,
|
||||||
|
list: StatefulList<Mode>,
|
||||||
|
boot_modes: Vec<SafeMode>,
|
||||||
|
setup_modes: Vec<SafeMode>,
|
||||||
|
selections: Vec<Option<usize>>,
|
||||||
|
state: State,
|
||||||
|
system32: String,
|
||||||
|
tasks: Tasks,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
|
||||||
|
let (action_tx, action_rx) = mpsc::unbounded_channel();
|
||||||
|
let diag_groups_arc = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
||||||
|
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
||||||
|
let mut list = StatefulList::default();
|
||||||
|
list.set_items(vec![
|
||||||
|
Mode::BootScan,
|
||||||
|
Mode::BootSetup,
|
||||||
|
Mode::InjectDrivers,
|
||||||
|
Mode::SetBootMode,
|
||||||
|
]);
|
||||||
|
Ok(Self {
|
||||||
|
// TUI
|
||||||
|
action_rx,
|
||||||
|
action_tx,
|
||||||
|
components: vec![
|
||||||
|
Box::new(Title::new("Boot Diagnostics")),
|
||||||
|
Box::new(Left::new()),
|
||||||
|
Box::new(Right::new()),
|
||||||
|
Box::new(Footer::new()),
|
||||||
|
Box::new(popup::Popup::new()),
|
||||||
|
Box::new(crate::components::progress::Progress::new()),
|
||||||
|
Box::new(crate::components::logview::LogView::new(
|
||||||
|
diag_groups_arc.clone(),
|
||||||
|
)),
|
||||||
|
],
|
||||||
|
config: Config::new()?,
|
||||||
|
frame_rate,
|
||||||
|
last_tick_key_events: Vec::new(),
|
||||||
|
should_quit: false,
|
||||||
|
should_suspend: false,
|
||||||
|
tick_rate,
|
||||||
|
// App
|
||||||
|
state: State::new(disk_list_arc),
|
||||||
|
cur_mode: Mode::Home,
|
||||||
|
diag_groups: diag_groups_arc,
|
||||||
|
list,
|
||||||
|
boot_modes: vec![SafeMode::Enable, SafeMode::Disable],
|
||||||
|
setup_modes: vec![SafeMode::Disable, SafeMode::Enable],
|
||||||
|
system32: String::new(),
|
||||||
|
selections: vec![None, None],
|
||||||
|
tasks,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn inject_driver(&mut self, index: usize) {
|
||||||
|
if let Some(driver) = self.state.driver_list.get(index)
|
||||||
|
&& let Some(disk_index) = self.state.disk_index_dest
|
||||||
|
{
|
||||||
|
let disk_list = self.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk) = disk_list.get(disk_index)
|
||||||
|
&& let Some(os_index) = self.state.part_index_os
|
||||||
|
&& let Ok(task) = boot::inject_driver(
|
||||||
|
driver,
|
||||||
|
disk.get_part_letter(os_index).as_str(),
|
||||||
|
&self.system32,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
self.tasks.add(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn next_mode(&mut self) -> Mode {
|
||||||
|
match self.cur_mode {
|
||||||
|
Mode::Home => Mode::ScanDisks,
|
||||||
|
Mode::InstallDrivers => Mode::ScanDisks,
|
||||||
|
Mode::ScanDisks => Mode::SelectDisks,
|
||||||
|
Mode::SelectDisks => Mode::SelectParts,
|
||||||
|
Mode::SelectParts => Mode::DiagMenu,
|
||||||
|
Mode::BootDiags => Mode::DiagMenu,
|
||||||
|
Mode::BootScan => Mode::BootDiags,
|
||||||
|
Mode::BootSetup | Mode::InjectDrivers | Mode::SetBootMode => Mode::Process,
|
||||||
|
Mode::Process => Mode::DiagMenu,
|
||||||
|
Mode::Done => Mode::DiagMenu,
|
||||||
|
Mode::Failed => Mode::Failed,
|
||||||
|
// Default to current mode
|
||||||
|
_ => self.cur_mode,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_boot_mode(&mut self, boot_mode: SafeMode) {
|
||||||
|
let new_mode = match boot_mode {
|
||||||
|
SafeMode::Disable => "Normal",
|
||||||
|
SafeMode::Enable => "Safe Mode (minimal)",
|
||||||
|
};
|
||||||
|
info!("Setting boot mode to: {new_mode}");
|
||||||
|
let disk_list = self.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk_index) = self.state.disk_index_dest
|
||||||
|
&& let Some(disk) = disk_list.get(disk_index)
|
||||||
|
&& let Some(boot_index) = self.state.part_index_boot
|
||||||
|
&& let Ok(task) = boot::set_mode(
|
||||||
|
disk.get_part_letter(boot_index).as_str(),
|
||||||
|
&boot_mode,
|
||||||
|
&self.system32,
|
||||||
|
&disk.part_type,
|
||||||
|
)
|
||||||
|
{
|
||||||
|
self.tasks.add(task);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_mode(&mut self, new_mode: Mode) -> Result<()> {
|
||||||
|
info!("Setting mode to {new_mode:?}");
|
||||||
|
self.cur_mode = new_mode;
|
||||||
|
match new_mode {
|
||||||
|
Mode::DiagMenu => {
|
||||||
|
self.selections[0] = None;
|
||||||
|
self.selections[1] = None;
|
||||||
|
self.list.select_first_item();
|
||||||
|
}
|
||||||
|
Mode::BootScan => {
|
||||||
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
|
popup::Type::Info,
|
||||||
|
String::from("Gathering info..."),
|
||||||
|
))?;
|
||||||
|
scan::queue_boot_scan_tasks(
|
||||||
|
self.action_tx.clone(),
|
||||||
|
self.diag_groups.clone(),
|
||||||
|
&self.state,
|
||||||
|
self.system32.clone(),
|
||||||
|
&mut self.tasks,
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
Mode::InjectDrivers | Mode::InstallDrivers => self.state.scan_drivers(),
|
||||||
|
Mode::Process => {
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::DisplayPopup(popup::Type::Info, String::from("...")))?;
|
||||||
|
}
|
||||||
|
Mode::ScanDisks => {
|
||||||
|
if self.tasks.idle() {
|
||||||
|
self.tasks.add(TaskType::ScanDisks);
|
||||||
|
}
|
||||||
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
|
popup::Type::Info,
|
||||||
|
String::from("Scanning Disks..."),
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn run(&mut self) -> Result<()> {
|
||||||
|
let mut tui = Tui::new()?
|
||||||
|
// .mouse(true) // uncomment this line to enable mouse support
|
||||||
|
.tick_rate(self.tick_rate)
|
||||||
|
.frame_rate(self.frame_rate);
|
||||||
|
tui.enter()?;
|
||||||
|
|
||||||
|
for component in &mut self.components {
|
||||||
|
component.register_action_handler(self.action_tx.clone())?;
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
component.register_config_handler(self.config.clone())?;
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
component.init(tui.size()?)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let action_tx = self.action_tx.clone();
|
||||||
|
|
||||||
|
// Late init
|
||||||
|
self.system32 = if cfg!(windows) {
|
||||||
|
if let Ok(path) = env::var("SYSTEMROOT") {
|
||||||
|
format!("{path}\\System32")
|
||||||
|
} else {
|
||||||
|
self.action_tx.send(Action::Error(String::from(
|
||||||
|
"ERROR\n\n\nFailed to find SYSTEMROOT",
|
||||||
|
)))?;
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
String::from(".")
|
||||||
|
};
|
||||||
|
action_tx.send(Action::SetMode(Mode::ScanDisks))?;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
self.handle_events(&mut tui).await?;
|
||||||
|
self.handle_actions(&mut tui)?;
|
||||||
|
if self.should_suspend {
|
||||||
|
tui.suspend()?;
|
||||||
|
action_tx.send(Action::Resume)?;
|
||||||
|
action_tx.send(Action::ClearScreen)?;
|
||||||
|
// tui.mouse(true);
|
||||||
|
tui.enter()?;
|
||||||
|
} else if self.should_quit {
|
||||||
|
tui.stop()?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tui.exit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_events(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
let Some(event) = tui.next_event().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let action_tx = self.action_tx.clone();
|
||||||
|
match event {
|
||||||
|
Event::Quit => action_tx.send(Action::Quit)?,
|
||||||
|
Event::Tick => action_tx.send(Action::Tick)?,
|
||||||
|
Event::Render => action_tx.send(Action::Render)?,
|
||||||
|
Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
|
||||||
|
Event::Key(key) => self.handle_key_event(key)?,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
if let Some(action) = component.handle_events(Some(event.clone()))? {
|
||||||
|
action_tx.send(action)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_key_event(&mut self, key: KeyEvent) -> Result<()> {
|
||||||
|
let action_tx = self.action_tx.clone();
|
||||||
|
let Some(keymap) = self.config.keybindings.get(&self.cur_mode) else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
if let Some(action) = keymap.get(&vec![key]) {
|
||||||
|
info!("Got action: {action:?}");
|
||||||
|
action_tx.send(action.clone())?;
|
||||||
|
} else {
|
||||||
|
// If the key was not handled as a single key action,
|
||||||
|
// then consider it for multi-key combinations.
|
||||||
|
self.last_tick_key_events.push(key);
|
||||||
|
|
||||||
|
// Check for multi-key combinations
|
||||||
|
if let Some(action) = keymap.get(&self.last_tick_key_events) {
|
||||||
|
info!("Got action: {action:?}");
|
||||||
|
action_tx.send(action.clone())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_actions(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
while let Ok(action) = self.action_rx.try_recv() {
|
||||||
|
if action != Action::Tick && action != Action::Render {
|
||||||
|
debug!("{action:?}");
|
||||||
|
}
|
||||||
|
match action {
|
||||||
|
Action::Tick => {
|
||||||
|
self.last_tick_key_events.drain(..);
|
||||||
|
// Check background task(s)
|
||||||
|
if let Some(task) = self.tasks.poll()? {
|
||||||
|
self.handle_task(&task)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Quit => self.should_quit = true,
|
||||||
|
Action::Suspend => self.should_suspend = true,
|
||||||
|
Action::Resume => self.should_suspend = false,
|
||||||
|
Action::ClearScreen => tui.terminal.clear()?,
|
||||||
|
Action::KeyUp => self.list.previous(),
|
||||||
|
Action::KeyDown => self.list.next(),
|
||||||
|
Action::Error(ref msg) => {
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::DisplayPopup(popup::Type::Error, msg.clone()))?;
|
||||||
|
self.action_tx.send(Action::SetMode(Mode::Failed))?;
|
||||||
|
}
|
||||||
|
Action::BootScan => self.action_tx.send(Action::SetMode(Mode::BootScan))?,
|
||||||
|
Action::DiagMainMenu => self.action_tx.send(Action::SetMode(Mode::DiagMenu))?,
|
||||||
|
Action::InstallDriver => {
|
||||||
|
self.action_tx.send(Action::SetMode(Mode::InstallDrivers))?;
|
||||||
|
}
|
||||||
|
Action::NextScreen => {
|
||||||
|
let next_mode = self.next_mode();
|
||||||
|
self.cur_mode = next_mode;
|
||||||
|
self.action_tx.send(Action::DismissPopup)?;
|
||||||
|
self.action_tx.send(Action::SetMode(next_mode))?;
|
||||||
|
}
|
||||||
|
Action::Process => match self.cur_mode {
|
||||||
|
Mode::BootDiags => {
|
||||||
|
self.action_tx.send(Action::SetMode(Mode::LogView))?;
|
||||||
|
}
|
||||||
|
Mode::DiagMenu => {
|
||||||
|
// Use highlighted entry
|
||||||
|
if let Some(new_mode) = self.list.get_selected() {
|
||||||
|
self.action_tx.send(Action::SetMode(new_mode))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::Done => {
|
||||||
|
self.action_tx.send(Action::NextScreen)?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
|
||||||
|
Action::Render => self.render(tui)?,
|
||||||
|
Action::ScanDisks => self.action_tx.send(Action::SetMode(Mode::ScanDisks))?,
|
||||||
|
Action::Select(one, two) => match self.cur_mode {
|
||||||
|
Mode::InjectDrivers => {
|
||||||
|
if let Some(index) = one {
|
||||||
|
self.inject_driver(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::InstallDrivers => {
|
||||||
|
if let Some(index) = one
|
||||||
|
&& let Some(driver) = self.state.driver_list.get(index).cloned()
|
||||||
|
{
|
||||||
|
drivers::load(&driver.inf_paths);
|
||||||
|
self.state.driver = Some(driver);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::BootSetup => {
|
||||||
|
if let Some(index) = one
|
||||||
|
&& let Some(boot_mode) = self.setup_modes.get(index)
|
||||||
|
{
|
||||||
|
info!("create_boot_files?");
|
||||||
|
create_boot_files(self, boot_mode.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::SelectDisks => {
|
||||||
|
self.state.disk_index_dest = one;
|
||||||
|
}
|
||||||
|
Mode::SelectParts => {
|
||||||
|
self.state.part_index_boot = one;
|
||||||
|
self.state.part_index_os = two;
|
||||||
|
}
|
||||||
|
Mode::SetBootMode => {
|
||||||
|
if let Some(index) = one
|
||||||
|
&& let Some(boot_mode) = self.boot_modes.get(index)
|
||||||
|
{
|
||||||
|
self.set_boot_mode(boot_mode.to_owned());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
},
|
||||||
|
Action::SetMode(new_mode) => {
|
||||||
|
self.set_mode(new_mode)?;
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::UpdateFooter(build_footer_string(self.cur_mode)))?;
|
||||||
|
self.action_tx.send(build_left_items(self))?;
|
||||||
|
self.action_tx.send(build_right_items(self))?;
|
||||||
|
self.action_tx.send(Action::Select(None, None))?;
|
||||||
|
}
|
||||||
|
Action::TaskGroupStart(ref title) => {
|
||||||
|
// TODO: Verify this isn't broken and/or unused
|
||||||
|
if self.cur_mode == Mode::BootScan {
|
||||||
|
self.action_tx.send(Action::DiagLineStart {
|
||||||
|
text: title.clone(),
|
||||||
|
})?;
|
||||||
|
if let Ok(mut diag_groups) = self.diag_groups.lock() {
|
||||||
|
diag_groups.push(DiagGroup::new(get_diag_type(title)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::TasksComplete => {
|
||||||
|
if self.cur_mode == Mode::BootDiags {
|
||||||
|
self.action_tx.send(Action::DismissPopup)?;
|
||||||
|
} else {
|
||||||
|
self.action_tx.send(Action::NextScreen)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
if let Some(action) = component.update(action.clone())? {
|
||||||
|
self.action_tx.send(action)?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_resize(&mut self, tui: &mut Tui, w: u16, h: u16) -> Result<()> {
|
||||||
|
tui.resize(Rect::new(0, 0, w, h))?;
|
||||||
|
self.render(tui)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_task(&mut self, task: &Task) -> Result<()> {
|
||||||
|
info!("Handling Task: {task:?}");
|
||||||
|
if self.cur_mode == Mode::BootScan {
|
||||||
|
if let Ok(mut diag_groups) = self.diag_groups.lock()
|
||||||
|
&& let Some(current_group) = diag_groups.last_mut()
|
||||||
|
{
|
||||||
|
match current_group.diag_type {
|
||||||
|
DiagType::Bitlocker => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
parse_bitlocker(current_group, task_result.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::BootConfigData => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
parse_bcd(current_group, task_result.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::CheckDisk => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
parse_chkdsk(current_group, task_result.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::ComponentStore => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
parse_dism(current_group, task_result.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::SystemFiles => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
parse_system_files(current_group, task_result.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::Registry => {
|
||||||
|
if let Some(task_result) = &task.result {
|
||||||
|
match &task.task_type {
|
||||||
|
TaskType::CommandWait(_, cmd_args) => {
|
||||||
|
parse_registry_hives(
|
||||||
|
current_group,
|
||||||
|
task_result.clone(),
|
||||||
|
cmd_args.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
DiagType::Unknown => {
|
||||||
|
panic!("This shouldn't happen?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.action_tx.send(Action::DiagLineUpdate {
|
||||||
|
result: current_group.get_pass_fail_warn(),
|
||||||
|
text: current_group.result.clone(),
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
match task.task_type {
|
||||||
|
TaskType::CommandNoWait(_, _) | TaskType::CommandWait(_, _) | TaskType::Diskpart(_) => {
|
||||||
|
// Check result
|
||||||
|
if let Some(result) = &task.result {
|
||||||
|
match result {
|
||||||
|
TaskResult::Error(msg) => {
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::Error(format!("{task:?} Failed: {msg}")))?;
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, success) => {
|
||||||
|
if !success {
|
||||||
|
let msg = if !stdout.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else if !stderr.is_empty() {
|
||||||
|
stderr.clone()
|
||||||
|
} else {
|
||||||
|
String::from("Unknown Error")
|
||||||
|
};
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::Error(format!("{task:?} Failed: {msg}")))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TaskType::GroupEnd { ref label } => {
|
||||||
|
self.action_tx.send(Action::DiagLineEnd {
|
||||||
|
text: label.clone(),
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
tui.draw(|frame| {
|
||||||
|
if let [header, _body, footer, left, right, popup, progress, results] =
|
||||||
|
get_chunks(frame.area())[..]
|
||||||
|
{
|
||||||
|
let component_areas = vec![
|
||||||
|
header, left, right, footer, popup, // core
|
||||||
|
progress, results, // boot-diags
|
||||||
|
];
|
||||||
|
for (component, area) in zip(self.components.iter_mut(), component_areas) {
|
||||||
|
if let Err(err) = component.draw(frame, area) {
|
||||||
|
let _ = self
|
||||||
|
.action_tx
|
||||||
|
.send(Action::Error(format!("Failed to draw: {err:?}")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||||
|
// Cut the given rectangle into three vertical pieces
|
||||||
|
let popup_layout = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
Constraint::Percentage(percent_y),
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
])
|
||||||
|
.split(r);
|
||||||
|
|
||||||
|
// Then cut the middle vertical piece into three width-wise pieces
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
Constraint::Percentage(percent_x),
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
])
|
||||||
|
.split(popup_layout[1])[1] // Return the middle chunk
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_chunks(r: Rect) -> Vec<Rect> {
|
||||||
|
let mut chunks: Vec<Rect> = Vec::with_capacity(6);
|
||||||
|
|
||||||
|
// Main sections
|
||||||
|
chunks.extend(
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(3),
|
||||||
|
Constraint::Min(1),
|
||||||
|
Constraint::Length(3),
|
||||||
|
])
|
||||||
|
.split(r)
|
||||||
|
.to_vec(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Left/Right
|
||||||
|
chunks.extend(
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
|
.split(centered_rect(90, 90, chunks[1]))
|
||||||
|
.to_vec(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Popup
|
||||||
|
chunks.push(centered_rect(60, 25, r));
|
||||||
|
|
||||||
|
// Progress
|
||||||
|
chunks.push(centered_rect(60, 70, r));
|
||||||
|
|
||||||
|
// Results
|
||||||
|
chunks.push(centered_rect(60, 70, r));
|
||||||
|
|
||||||
|
// Done
|
||||||
|
chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_footer_string(cur_mode: Mode) -> String {
|
||||||
|
match cur_mode {
|
||||||
|
Mode::BootDiags | Mode::BootSetup | Mode::InjectDrivers | Mode::SetBootMode => {
|
||||||
|
String::from("(Enter) to select / (m) for menu / (s) to start over / (q) to quit")
|
||||||
|
}
|
||||||
|
Mode::BootScan | Mode::Home | Mode::Process | Mode::ScanDisks => {
|
||||||
|
String::from("(q) to quit")
|
||||||
|
}
|
||||||
|
Mode::DiagMenu | Mode::SelectParts => {
|
||||||
|
String::from("(Enter) to select / (s) to start over / (q) to quit")
|
||||||
|
}
|
||||||
|
Mode::Done => String::from("(Enter) to continue / (q) to quit"),
|
||||||
|
Mode::InstallDrivers => String::from("(Enter) to select / (q) to quit"),
|
||||||
|
Mode::LogView => {
|
||||||
|
String::from("(Enter | Esc) to close log / (up | down) to scroll / (q) to quit")
|
||||||
|
}
|
||||||
|
Mode::SelectDisks => String::from(
|
||||||
|
"(Enter) to select / / (i) to install driver / (r) to rescan / (q) to quit",
|
||||||
|
),
|
||||||
|
Mode::Failed => String::from("(Enter) or (q) to quit"),
|
||||||
|
// Invalid states
|
||||||
|
Mode::Confirm
|
||||||
|
| Mode::Clone
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::PreClone
|
||||||
|
| Mode::PostClone
|
||||||
|
| Mode::SelectTableType => {
|
||||||
|
panic!("This shouldn't happen?")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_left_items(app: &App) -> Action {
|
||||||
|
let mut items = Vec::new();
|
||||||
|
let mut labels = vec![String::new(), String::new()];
|
||||||
|
let select_type: SelectionType;
|
||||||
|
let title: String;
|
||||||
|
match app.cur_mode {
|
||||||
|
Mode::Home => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Home");
|
||||||
|
}
|
||||||
|
Mode::DiagMenu => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Troubleshooting");
|
||||||
|
app.list.items.iter().for_each(|mode| {
|
||||||
|
let (name, _) = get_mode_strings(*mode);
|
||||||
|
items.push(name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Mode::InstallDrivers => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
|
title = String::from("Install Drivers");
|
||||||
|
app.state
|
||||||
|
.driver_list
|
||||||
|
.iter()
|
||||||
|
.for_each(|driver| items.push(driver.to_string()));
|
||||||
|
}
|
||||||
|
Mode::InjectDrivers => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
|
title = String::from("Select Drivers");
|
||||||
|
app.state
|
||||||
|
.driver_list
|
||||||
|
.iter()
|
||||||
|
.for_each(|driver| items.push(driver.to_string()));
|
||||||
|
}
|
||||||
|
Mode::SelectDisks => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
|
title = String::from("Select Disk");
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
disk_list
|
||||||
|
.iter()
|
||||||
|
.for_each(|disk| items.push(disk.description.to_string()));
|
||||||
|
}
|
||||||
|
Mode::BootScan | Mode::Process | Mode::ScanDisks => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Processing");
|
||||||
|
}
|
||||||
|
Mode::SelectParts => {
|
||||||
|
select_type = SelectionType::Two;
|
||||||
|
title = String::from("Select Boot and OS Partitions");
|
||||||
|
labels[0] = String::from("boot");
|
||||||
|
labels[1] = String::from("os");
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(index) = app.state.disk_index_dest
|
||||||
|
&& let Some(disk) = disk_list.get(index)
|
||||||
|
{
|
||||||
|
disk.get_parts().iter().for_each(|part| {
|
||||||
|
items.push(part.to_string());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::BootDiags | Mode::LogView => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
let (new_title, _) = get_mode_strings(app.cur_mode);
|
||||||
|
title = new_title;
|
||||||
|
if let Ok(diag_groups) = app.diag_groups.lock() {
|
||||||
|
let labels: Vec<String> = diag_groups
|
||||||
|
.iter()
|
||||||
|
.map(|group| {
|
||||||
|
let label = group.diag_type.to_string();
|
||||||
|
let status = if group.get_pass_fail_warn() == DiagResult::Pass {
|
||||||
|
"" // Leave blank if OK
|
||||||
|
} else {
|
||||||
|
" -- Issue(s) detected"
|
||||||
|
};
|
||||||
|
format!("{label}{status}")
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
items.extend(labels);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::BootSetup => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
|
let (new_title, _) = get_mode_strings(app.cur_mode);
|
||||||
|
title = new_title;
|
||||||
|
app.boot_modes.iter().rev().for_each(|entry| match entry {
|
||||||
|
SafeMode::Disable => items.push(String::from("Normal Mode")),
|
||||||
|
SafeMode::Enable => items.push(String::from("Locked in Safe Mode")),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Mode::SetBootMode => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
|
let (new_title, _) = get_mode_strings(app.cur_mode);
|
||||||
|
title = new_title;
|
||||||
|
app.boot_modes.iter().for_each(|entry| {
|
||||||
|
items.push(format!("{:?} Safe Mode", entry));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Mode::Done | Mode::Failed => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Done");
|
||||||
|
}
|
||||||
|
// Invalid states
|
||||||
|
Mode::SelectTableType
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::Confirm
|
||||||
|
| Mode::PreClone
|
||||||
|
| Mode::Clone
|
||||||
|
| Mode::PostClone => {
|
||||||
|
panic!("This shouldn't happen?")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Action::UpdateLeft(title, labels, items, select_type)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_right_items(app: &App) -> Action {
|
||||||
|
let mut items: Vec<Vec<DVLine>> = Vec::new();
|
||||||
|
let mut labels: Vec<Vec<DVLine>> = Vec::new();
|
||||||
|
let mut start_index = 0;
|
||||||
|
let disk_header = get_disk_header(app);
|
||||||
|
match app.cur_mode {
|
||||||
|
Mode::BootDiags
|
||||||
|
| Mode::BootSetup
|
||||||
|
| Mode::DiagMenu
|
||||||
|
| Mode::InjectDrivers
|
||||||
|
| Mode::InstallDrivers
|
||||||
|
| Mode::Process
|
||||||
|
| Mode::SetBootMode => {
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from("CPU")],
|
||||||
|
line_colors: vec![Color::Cyan],
|
||||||
|
}]);
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![get_cpu_name()],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::new()],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
start_index += 3;
|
||||||
|
if !disk_header.is_empty() {
|
||||||
|
items.push(disk_header);
|
||||||
|
start_index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
match app.cur_mode {
|
||||||
|
Mode::DiagMenu => {
|
||||||
|
app.list.items.iter().for_each(|mode| {
|
||||||
|
let (name, description) = get_mode_strings(*mode);
|
||||||
|
items.push(vec![
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![name],
|
||||||
|
line_colors: vec![Color::Cyan],
|
||||||
|
},
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![String::new()],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
},
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![description],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Mode::BootDiags => {
|
||||||
|
if let Ok(diag_groups) = app.diag_groups.lock() {
|
||||||
|
let mut summary: Vec<DVLine> = Vec::new();
|
||||||
|
diag_groups
|
||||||
|
.iter()
|
||||||
|
.for_each(|group| summary.extend(group.get_logs_summary()));
|
||||||
|
items.push(summary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::BootSetup => {
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Normal Mode")],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Safe Mode")],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
Mode::SelectDisks => {
|
||||||
|
let dest_dv_line = DVLine {
|
||||||
|
line_parts: vec![String::from("Disk")],
|
||||||
|
line_colors: vec![Color::Cyan],
|
||||||
|
};
|
||||||
|
labels.push(vec![dest_dv_line]);
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
disk_list
|
||||||
|
.iter()
|
||||||
|
.for_each(|disk| items.push(get_disk_description_right(disk, &None)));
|
||||||
|
}
|
||||||
|
Mode::SelectParts => {
|
||||||
|
["Boot", "OS"].iter().for_each(|s| {
|
||||||
|
labels.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from(*s)],
|
||||||
|
line_colors: vec![Color::Cyan],
|
||||||
|
}])
|
||||||
|
});
|
||||||
|
if let Some(index) = app.state.disk_index_dest {
|
||||||
|
start_index += 1;
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk) = disk_list.get(index) {
|
||||||
|
// Disk Details
|
||||||
|
items.push(get_disk_description_right(disk, &None));
|
||||||
|
|
||||||
|
// Partition Details
|
||||||
|
disk.parts
|
||||||
|
.iter()
|
||||||
|
.for_each(|part| items.push(get_part_description(part)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Mode::SetBootMode => {
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Enable Safe Mode (minimal)")],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
items.push(vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Disable Safe Mode")],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
}]);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Action::UpdateRight(labels, start_index, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn create_boot_files(app: &mut App, safe_mode: SafeMode) {
|
||||||
|
let mut tasks: Vec<TaskType> = Vec::new();
|
||||||
|
if let Some(index) = app.state.disk_index_dest {
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk) = disk_list.get(index) {
|
||||||
|
let letter_boot = disk.get_part_letter(app.state.part_index_boot.unwrap());
|
||||||
|
let letter_os = disk.get_part_letter(app.state.part_index_os.unwrap());
|
||||||
|
tasks = configure_disk(
|
||||||
|
&letter_boot,
|
||||||
|
&letter_os,
|
||||||
|
safe_mode,
|
||||||
|
&app.system32,
|
||||||
|
&disk.part_type,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tasks
|
||||||
|
.into_iter()
|
||||||
|
.for_each(|task_type| app.tasks.add(task_type));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_disk_header(app: &App) -> Vec<DVLine> {
|
||||||
|
let mut header_lines: Vec<DVLine> = Vec::new();
|
||||||
|
if let Some(index) = app.state.disk_index_dest {
|
||||||
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk) = disk_list.get(index) {
|
||||||
|
let mut parts: Vec<usize> = Vec::new();
|
||||||
|
if let Some(index) = app.state.part_index_boot {
|
||||||
|
parts.push(index);
|
||||||
|
}
|
||||||
|
if let Some(index) = app.state.part_index_os {
|
||||||
|
parts.push(index);
|
||||||
|
}
|
||||||
|
header_lines.append(&mut get_disk_description_right(disk, &Some(parts)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
header_lines
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_mode_strings(mode: Mode) -> (String, String) {
|
||||||
|
match mode {
|
||||||
|
Mode::BootScan | Mode::BootDiags | Mode::LogView => (
|
||||||
|
String::from("Boot Diagnostics"),
|
||||||
|
String::from("Check for common Windows boot issues"),
|
||||||
|
),
|
||||||
|
Mode::BootSetup => (
|
||||||
|
String::from("Boot Setup"),
|
||||||
|
String::from("Create or recreate boot files"),
|
||||||
|
),
|
||||||
|
Mode::InjectDrivers => (
|
||||||
|
String::from("Inject Drivers"),
|
||||||
|
String::from("Inject drivers into existing Windows environment"),
|
||||||
|
),
|
||||||
|
Mode::SetBootMode => (
|
||||||
|
String::from("Toggle Safe Mode"),
|
||||||
|
String::from("Enable or disable safe mode"),
|
||||||
|
),
|
||||||
|
_ => panic!("This shouldn't happen"),
|
||||||
|
}
|
||||||
|
}
|
||||||
2
boot_diags/src/components.rs
Normal file
2
boot_diags/src/components.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
pub mod logview;
|
||||||
|
pub mod progress;
|
||||||
168
boot_diags/src/components/logview.rs
Normal file
168
boot_diags/src/components/logview.rs
Normal file
|
|
@ -0,0 +1,168 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use color_eyre::Result;
|
||||||
|
use ratatui::{
|
||||||
|
Frame,
|
||||||
|
layout::Rect,
|
||||||
|
widgets::{Block, Clear, Padding, Paragraph, Wrap},
|
||||||
|
};
|
||||||
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
|
use core::{
|
||||||
|
action::Action,
|
||||||
|
components::{Component, state::StatefulList},
|
||||||
|
config::Config,
|
||||||
|
state::Mode,
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
cmp::min,
|
||||||
|
sync::{Arc, Mutex},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::diags::DiagGroup;
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct LogView {
|
||||||
|
command_tx: Option<UnboundedSender<Action>>,
|
||||||
|
config: Config,
|
||||||
|
line_index: u16,
|
||||||
|
list: StatefulList<String>,
|
||||||
|
mode: Mode,
|
||||||
|
diag_groups: Arc<Mutex<Vec<DiagGroup>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl LogView {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(diag_groups: Arc<Mutex<Vec<DiagGroup>>>) -> Self {
|
||||||
|
LogView {
|
||||||
|
diag_groups,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for LogView {
|
||||||
|
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
|
||||||
|
self.command_tx = Some(tx);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn register_config_handler(&mut self, config: Config) -> Result<()> {
|
||||||
|
self.config = config;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
|
match action {
|
||||||
|
Action::KeyUp => {
|
||||||
|
if self.mode == Mode::LogView {
|
||||||
|
if self.line_index > 0 {
|
||||||
|
self.line_index -= 1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.list.previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyDown => {
|
||||||
|
if self.mode == Mode::LogView {
|
||||||
|
let new_index = self.line_index + 1;
|
||||||
|
if let Some(log_text) = self.list.get_selected() {
|
||||||
|
let lines: Vec<&str> = log_text.split('\n').collect();
|
||||||
|
if new_index as usize > lines.len() {
|
||||||
|
self.line_index = lines.len() as u16;
|
||||||
|
} else {
|
||||||
|
self.line_index = new_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
self.list.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyPageUp => {
|
||||||
|
if self.mode == Mode::LogView {
|
||||||
|
if self.line_index > 10 {
|
||||||
|
self.line_index -= 10;
|
||||||
|
} else {
|
||||||
|
self.line_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyPageDown => {
|
||||||
|
if self.mode == Mode::LogView {
|
||||||
|
let new_index = self.line_index + 10;
|
||||||
|
if let Some(log_text) = self.list.get_selected() {
|
||||||
|
let lines: Vec<&str> = log_text.split('\n').collect();
|
||||||
|
let new_index: u16 = min((lines.len() - 3) as u16, new_index);
|
||||||
|
self.line_index = new_index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyHome => {
|
||||||
|
if self.mode == Mode::LogView {
|
||||||
|
self.line_index = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyEnd => {
|
||||||
|
if self.mode == Mode::LogView
|
||||||
|
&& let Some(log_text) = self.list.get_selected()
|
||||||
|
{
|
||||||
|
let lines: Vec<&str> = log_text.split('\n').collect();
|
||||||
|
self.line_index = (lines.len() - 3) as u16;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Process => {
|
||||||
|
if self.mode == Mode::LogView
|
||||||
|
&& let Some(command_tx) = self.command_tx.clone()
|
||||||
|
{
|
||||||
|
command_tx.send(Action::SetMode(Mode::BootDiags))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::SetMode(new_mode) => {
|
||||||
|
self.line_index = 0;
|
||||||
|
self.mode = new_mode;
|
||||||
|
if self.mode == Mode::BootDiags {
|
||||||
|
self.list.clear_items();
|
||||||
|
if let Ok(diag_groups) = self.diag_groups.lock() {
|
||||||
|
let raw_logs = diag_groups
|
||||||
|
.iter()
|
||||||
|
.map(|group| group.get_logs_raw())
|
||||||
|
.collect();
|
||||||
|
self.list.set_items(raw_logs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(&mut self, frame: &mut Frame, rect: Rect) -> Result<()> {
|
||||||
|
if self.mode != Mode::LogView {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(log_text) = self.list.get_selected() {
|
||||||
|
let paragraph = Paragraph::new(log_text)
|
||||||
|
.wrap(Wrap { trim: true })
|
||||||
|
.scroll((self.line_index, 0))
|
||||||
|
.block(Block::bordered().padding(Padding::horizontal(1)));
|
||||||
|
frame.render_widget(Clear, rect);
|
||||||
|
frame.render_widget(paragraph, rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
172
boot_diags/src/components/progress.rs
Normal file
172
boot_diags/src/components/progress.rs
Normal file
|
|
@ -0,0 +1,172 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use color_eyre::Result;
|
||||||
|
use ratatui::{
|
||||||
|
Frame,
|
||||||
|
layout::{Alignment, Rect},
|
||||||
|
style::{Color, Style, Stylize},
|
||||||
|
text::{Line, Span},
|
||||||
|
widgets::{Block, Borders, Clear, Paragraph},
|
||||||
|
};
|
||||||
|
use tracing::info;
|
||||||
|
|
||||||
|
use core::{
|
||||||
|
action::{Action, DiagResult},
|
||||||
|
components::Component,
|
||||||
|
state::Mode,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
struct ProgressLine {
|
||||||
|
name: String,
|
||||||
|
text: String,
|
||||||
|
result: DiagResult,
|
||||||
|
running: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ProgressLine {
|
||||||
|
pub fn len_name(&self) -> usize {
|
||||||
|
self.name.chars().count()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default, Debug, Clone)]
|
||||||
|
pub struct Progress {
|
||||||
|
// command_tx: Option<UnboundedSender<Action>>,
|
||||||
|
// config: Config,
|
||||||
|
lines: Vec<ProgressLine>,
|
||||||
|
mode: Mode,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Progress {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self::default()
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
|
||||||
|
// self.command_tx = Some(tx);
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn register_config_handler(&mut self, config: Config) -> Result<()> {
|
||||||
|
// self.config = config;
|
||||||
|
// Ok(())
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Component for Progress {
|
||||||
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
|
match action {
|
||||||
|
Action::DiagLineStart { text } => {
|
||||||
|
info!("Caught Action::DiagLineStart {{ \"{}\" }}", &text);
|
||||||
|
self.lines.push(ProgressLine {
|
||||||
|
name: text,
|
||||||
|
text: String::from("OK"),
|
||||||
|
result: DiagResult::Pass,
|
||||||
|
running: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Action::DiagLineUpdate { result, text } => {
|
||||||
|
info!(
|
||||||
|
"Caught Action::DiagLineUpdate {{ {}, \"{}\" }}",
|
||||||
|
&result, &text
|
||||||
|
);
|
||||||
|
if let Some(line) = self.lines.last_mut() {
|
||||||
|
let old_result = line.result.clone();
|
||||||
|
match (old_result, result.clone()) {
|
||||||
|
(DiagResult::Pass, _) => {
|
||||||
|
line.result = result;
|
||||||
|
line.text = text;
|
||||||
|
}
|
||||||
|
(DiagResult::Warn, DiagResult::Pass | DiagResult::Warn) => {
|
||||||
|
line.text = String::from("Unknown");
|
||||||
|
}
|
||||||
|
(DiagResult::Warn, DiagResult::Fail) => {
|
||||||
|
line.result = DiagResult::Fail;
|
||||||
|
line.text = String::from("Unknown");
|
||||||
|
}
|
||||||
|
(DiagResult::Fail, _) => {
|
||||||
|
line.text = String::from("Unknown");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::DiagLineEnd { text } => {
|
||||||
|
info!("Caught Action::DiagLineEnd {{ \"{}\" }}", &text);
|
||||||
|
if let Some(line) = self.lines.last_mut() {
|
||||||
|
line.running = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::SetMode(mode) => {
|
||||||
|
self.mode = mode;
|
||||||
|
self.lines.clear();
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
};
|
||||||
|
Ok(None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
||||||
|
if self.mode != Mode::BootScan {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut body_text = Vec::with_capacity(20); // TODO: Needs fine tuning?
|
||||||
|
body_text.push(Line::default());
|
||||||
|
|
||||||
|
// Add line(s)
|
||||||
|
self.lines.iter().for_each(|line| {
|
||||||
|
//.for_each(|(part, color)| spans.push(Span::styled(part, Style::default().fg(color))));
|
||||||
|
//Line::from(spans)
|
||||||
|
let color = match line.result {
|
||||||
|
DiagResult::Pass => Color::Green,
|
||||||
|
DiagResult::Fail => Color::Red,
|
||||||
|
DiagResult::Warn => Color::Yellow,
|
||||||
|
};
|
||||||
|
let text = if line.running || line.text.is_empty() {
|
||||||
|
String::from("..")
|
||||||
|
} else {
|
||||||
|
line.text.clone()
|
||||||
|
};
|
||||||
|
let text = format!(" [ {text} ] ");
|
||||||
|
let width = area.width as usize - line.len_name() - 1 - text.chars().count() - 7; // ' [ ]'
|
||||||
|
let spans = vec![
|
||||||
|
Span::raw(format!("{} {:.<width$}", &line.name, "")),
|
||||||
|
Span::styled(text, Style::default().fg(color)),
|
||||||
|
];
|
||||||
|
body_text.push(Line::from(spans).centered());
|
||||||
|
body_text.push(Line::default());
|
||||||
|
body_text.push(Line::default());
|
||||||
|
});
|
||||||
|
|
||||||
|
// Build block
|
||||||
|
let outer_block = Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.style(Style::default().bold());
|
||||||
|
let inner_block = Block::default()
|
||||||
|
.borders(Borders::NONE)
|
||||||
|
.style(Style::default().bold())
|
||||||
|
.title("Progress")
|
||||||
|
.title_alignment(Alignment::Center);
|
||||||
|
let inner = outer_block.inner(area);
|
||||||
|
let body = Paragraph::new(body_text).block(inner_block);
|
||||||
|
frame.render_widget(Clear, area);
|
||||||
|
frame.render_widget(outer_block, area);
|
||||||
|
frame.render_widget(body, inner);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
526
boot_diags/src/diags.rs
Normal file
526
boot_diags/src/diags.rs
Normal file
|
|
@ -0,0 +1,526 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use core::{action::DiagResult, line::DVLine, tasks::TaskResult};
|
||||||
|
use std::{fmt, sync::OnceLock, thread::sleep, time::Duration};
|
||||||
|
|
||||||
|
use ratatui::style::Color;
|
||||||
|
use regex::Regex;
|
||||||
|
use tracing::{info, warn};
|
||||||
|
|
||||||
|
pub struct RegexList {
|
||||||
|
bitlocker_locked: OnceLock<Regex>,
|
||||||
|
bitlocker_off: OnceLock<Regex>,
|
||||||
|
carriage_returns: OnceLock<Regex>,
|
||||||
|
chkdsk_splits: OnceLock<Regex>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RegexList {
|
||||||
|
fn bitlocker_locked(&self) -> &Regex {
|
||||||
|
self.bitlocker_locked
|
||||||
|
.get_or_init(|| Regex::new(r"Lock Status:\s+Locked").unwrap())
|
||||||
|
}
|
||||||
|
fn bitlocker_off(&self) -> &Regex {
|
||||||
|
self.bitlocker_off
|
||||||
|
.get_or_init(|| Regex::new(r"Protection Status:\s+Protection Off").unwrap())
|
||||||
|
}
|
||||||
|
fn carriage_returns(&self) -> &Regex {
|
||||||
|
self.carriage_returns
|
||||||
|
.get_or_init(|| Regex::new(r"^.*\r").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn chkdsk_splits(&self) -> &Regex {
|
||||||
|
self.chkdsk_splits.get_or_init(|| {
|
||||||
|
Regex::new(
|
||||||
|
r"(?m)^(WARNING|Stage |Windows |\d+.* total disk space|.*each allocation unit|Total duration)",
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static REGEXES: RegexList = RegexList {
|
||||||
|
bitlocker_locked: OnceLock::new(),
|
||||||
|
bitlocker_off: OnceLock::new(),
|
||||||
|
carriage_returns: OnceLock::new(),
|
||||||
|
chkdsk_splits: OnceLock::new(),
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum Type {
|
||||||
|
Bitlocker,
|
||||||
|
BootConfigData,
|
||||||
|
CheckDisk,
|
||||||
|
ComponentStore,
|
||||||
|
Registry,
|
||||||
|
SystemFiles,
|
||||||
|
Unknown,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for Type {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
Type::Bitlocker => write!(f, "Bitlocker"),
|
||||||
|
Type::BootConfigData => write!(f, "Boot Files"),
|
||||||
|
Type::CheckDisk => write!(f, "CHKDSK"),
|
||||||
|
Type::ComponentStore => write!(f, "DISM ScanHealth"),
|
||||||
|
Type::Registry => write!(f, "Registry"),
|
||||||
|
Type::SystemFiles => write!(f, "System Files"),
|
||||||
|
Type::Unknown => write!(f, "Unknown Type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Log {
|
||||||
|
pub label: String,
|
||||||
|
pub summary: Vec<DVLine>,
|
||||||
|
pub raw: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct DiagGroup {
|
||||||
|
pub diag_type: Type,
|
||||||
|
pub passed: Vec<bool>,
|
||||||
|
pub logs: Vec<Log>,
|
||||||
|
pub result: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DiagGroup {
|
||||||
|
pub fn new(diag_type: Type) -> Self {
|
||||||
|
DiagGroup {
|
||||||
|
diag_type,
|
||||||
|
passed: Vec::new(),
|
||||||
|
logs: Vec::new(),
|
||||||
|
result: String::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_logs_raw(&self) -> String {
|
||||||
|
let raw_logs: Vec<String> = self
|
||||||
|
.logs
|
||||||
|
.iter()
|
||||||
|
.map(|log| format!("-- {} --\n{}", &log.label, &log.raw))
|
||||||
|
.collect();
|
||||||
|
raw_logs.join("\n\n\n")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_logs_summary(&self) -> Vec<DVLine> {
|
||||||
|
let mut summaries: Vec<DVLine> = Vec::new();
|
||||||
|
self.logs
|
||||||
|
.iter()
|
||||||
|
.for_each(|log| summaries.extend(log.summary.clone()));
|
||||||
|
summaries
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pass_fail_warn(&self) -> DiagResult {
|
||||||
|
let all_passed = self.passed.iter().all(|result| *result);
|
||||||
|
let all_failed = self.passed.iter().all(|result| !*result);
|
||||||
|
if all_passed {
|
||||||
|
DiagResult::Pass
|
||||||
|
} else if all_failed {
|
||||||
|
DiagResult::Fail
|
||||||
|
} else {
|
||||||
|
DiagResult::Warn
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_diag_type(label: &str) -> Type {
|
||||||
|
info!("Getting Diag type for {label}");
|
||||||
|
match label {
|
||||||
|
"Bitlocker" => Type::Bitlocker,
|
||||||
|
"Boot Files" => Type::BootConfigData,
|
||||||
|
"DISM ScanHealth" => Type::ComponentStore,
|
||||||
|
"CHKDSK" => Type::CheckDisk,
|
||||||
|
"Registry" => Type::Registry,
|
||||||
|
"System Files" => Type::SystemFiles,
|
||||||
|
_ => {
|
||||||
|
warn!("Failed to determine type");
|
||||||
|
Type::Unknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_bcd(diag_group: &mut DiagGroup, task_result: TaskResult) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("BCD"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("BCD: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, passed) => {
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n-------\n\n{stderr}")
|
||||||
|
};
|
||||||
|
|
||||||
|
if passed {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
diag_group.result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("BCD"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("BCD: "), String::from("OK")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Unknown");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("BCD"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("BCD: "), String::from("Unknown")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Yellow],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_bitlocker(diag_group: &mut DiagGroup, task_result: TaskResult) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let re_bitlocker_locked = REGEXES.bitlocker_locked();
|
||||||
|
let re_bitlocker_off = REGEXES.bitlocker_off();
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Bitlocker"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Bitlocker: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, _) => {
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n-------\n\n{stderr}")
|
||||||
|
};
|
||||||
|
let result: String;
|
||||||
|
let set_result_text = !output_text.contains("All Key Protectors");
|
||||||
|
|
||||||
|
if re_bitlocker_off.is_match(&output_text) {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Bitlocker"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Bitlocker: "), String::from("OFF")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else if re_bitlocker_locked.is_match(&output_text) {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
result = String::from("Locked");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Bitlocker"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("Bitlocker: "), String::from("Locked")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
result = String::from("Unknown");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Bitlocker"),
|
||||||
|
summary: Vec::new(),
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if set_result_text {
|
||||||
|
diag_group.result = result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_chkdsk(diag_group: &mut DiagGroup, task_result: TaskResult) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("CHKDSK"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("CHKDSK: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, _) => {
|
||||||
|
let re_chkdsk_splits = REGEXES.chkdsk_splits();
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n-------\n\n{stderr}")
|
||||||
|
};
|
||||||
|
let output_text = remove_carriage_returns(&output_text);
|
||||||
|
let parsed_output = re_chkdsk_splits
|
||||||
|
.replace_all(output_text.as_str(), "\n $1")
|
||||||
|
.to_string();
|
||||||
|
|
||||||
|
if parsed_output.contains("Windows has scanned the file system and found no problems.")
|
||||||
|
{
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
diag_group.result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("CHKDSK"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("CHKDSK: "), String::from("OK")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}],
|
||||||
|
raw: parsed_output,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("CHKDSK"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("CHKDSK: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: parsed_output,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_dism(diag_group: &mut DiagGroup, task_result: TaskResult) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("DISM"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("DISM: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, _) => {
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n-------\n\n{stderr}")
|
||||||
|
};
|
||||||
|
|
||||||
|
if output_text.contains("No component store corruption detected") {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
diag_group.result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("DISM"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("DISM: "), String::from("OK")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Unknown");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("DISM"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("DISM: "), String::from("Unknown")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Yellow],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_registry_hives(
|
||||||
|
diag_group: &mut DiagGroup,
|
||||||
|
task_result: TaskResult,
|
||||||
|
cmd_args: Vec<String>,
|
||||||
|
) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
let hive = cmd_args.get(2);
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Registry"),
|
||||||
|
summary: if hive.is_some() {
|
||||||
|
vec![DVLine {
|
||||||
|
line_parts: vec![
|
||||||
|
format!("Registry ({}): ", hive.unwrap()),
|
||||||
|
String::from("Error"),
|
||||||
|
],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}]
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
},
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, passed) => {
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n{stderr}")
|
||||||
|
};
|
||||||
|
|
||||||
|
if passed {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
diag_group.result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Registry"),
|
||||||
|
summary: if hive.is_some() {
|
||||||
|
vec![DVLine {
|
||||||
|
line_parts: vec![
|
||||||
|
format!("Registry ({}): ", hive.unwrap()),
|
||||||
|
String::from("OK"),
|
||||||
|
],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}]
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
},
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("Registry"),
|
||||||
|
summary: if hive.is_some() {
|
||||||
|
vec![DVLine {
|
||||||
|
line_parts: vec![
|
||||||
|
format!("Registry ({}): ", hive.unwrap()),
|
||||||
|
String::from("Error"),
|
||||||
|
],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}]
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
},
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_system_files(diag_group: &mut DiagGroup, task_result: TaskResult) {
|
||||||
|
if !cfg!(windows) {
|
||||||
|
sleep(Duration::from_millis(500));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
match task_result {
|
||||||
|
TaskResult::Error(err) => {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("System Files"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("System Files: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: err,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, passed) => {
|
||||||
|
let output_text = if stderr.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else {
|
||||||
|
format!("{stdout}\n\n{stderr}")
|
||||||
|
};
|
||||||
|
|
||||||
|
if passed {
|
||||||
|
diag_group.passed.push(true);
|
||||||
|
diag_group.result = String::from("OK");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("System Files"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("System Files: "), String::from("OK")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Green],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
diag_group.passed.push(false);
|
||||||
|
diag_group.result = String::from("Error");
|
||||||
|
diag_group.logs.push(Log {
|
||||||
|
label: String::from("System Files"),
|
||||||
|
summary: vec![DVLine {
|
||||||
|
line_parts: vec![String::from("System Files: "), String::from("Error")],
|
||||||
|
line_colors: vec![Color::Reset, Color::Red],
|
||||||
|
}],
|
||||||
|
raw: output_text,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove_carriage_returns(text: &str) -> String {
|
||||||
|
let re_carriage_returns = REGEXES.carriage_returns();
|
||||||
|
let parsed_lines: Vec<String> = text
|
||||||
|
.split("\r\n")
|
||||||
|
.filter_map(|line| {
|
||||||
|
let line = re_carriage_returns.replace_all(line, "").trim().to_string();
|
||||||
|
if line.is_empty() { None } else { Some(line) }
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
parsed_lines.join("\n")
|
||||||
|
}
|
||||||
50
boot_diags/src/main.rs
Normal file
50
boot_diags/src/main.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use clap::Parser;
|
||||||
|
use color_eyre::Result;
|
||||||
|
|
||||||
|
use crate::app::App;
|
||||||
|
|
||||||
|
mod app;
|
||||||
|
mod components;
|
||||||
|
mod diags;
|
||||||
|
mod scan;
|
||||||
|
mod state;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<()> {
|
||||||
|
let mut msg = None;
|
||||||
|
if cfg!(windows) {
|
||||||
|
use check_elevation::is_elevated;
|
||||||
|
if !is_elevated().expect("Failed to get elevation status.") {
|
||||||
|
msg.replace("Administrator privedges required for Deja-Vu.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
match msg {
|
||||||
|
Some(text) => {
|
||||||
|
println!("{text}");
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
core::errors::init()?;
|
||||||
|
core::logging::init()?;
|
||||||
|
|
||||||
|
let args = core::cli::Cli::parse();
|
||||||
|
let mut app = App::new(args.tick_rate, args.frame_rate)?;
|
||||||
|
app.run().await?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
169
boot_diags/src/scan.rs
Normal file
169
boot_diags/src/scan.rs
Normal file
|
|
@ -0,0 +1,169 @@
|
||||||
|
use color_eyre::Result;
|
||||||
|
use core::system::disk::PartitionTableType;
|
||||||
|
use core::tasks::Tasks;
|
||||||
|
use core::{action::Action, tasks::TaskType};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
|
||||||
|
use crate::diags::{DiagGroup, Type as DiagType};
|
||||||
|
use crate::state::State;
|
||||||
|
|
||||||
|
pub fn queue_boot_scan_tasks(
|
||||||
|
action_tx: mpsc::UnboundedSender<Action>,
|
||||||
|
diag_groups: Arc<Mutex<Vec<DiagGroup>>>,
|
||||||
|
state: &State,
|
||||||
|
system32: String,
|
||||||
|
tasks: &mut Tasks,
|
||||||
|
) -> Result<()> {
|
||||||
|
if let Ok(mut diag_groups) = diag_groups.lock() {
|
||||||
|
diag_groups.clear();
|
||||||
|
}
|
||||||
|
let disk_list = state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk_index) = state.disk_index_dest
|
||||||
|
&& let Some(disk) = disk_list.get(disk_index)
|
||||||
|
{
|
||||||
|
let table_type = disk.part_type.clone();
|
||||||
|
let letter_boot = disk.get_part_letter(state.part_index_boot.unwrap());
|
||||||
|
let letter_os = disk.get_part_letter(state.part_index_os.unwrap());
|
||||||
|
|
||||||
|
// Safety check
|
||||||
|
if letter_os.is_empty() {
|
||||||
|
if letter_boot.is_empty() {
|
||||||
|
action_tx.send(Action::Error(String::from(
|
||||||
|
"ERROR\n\n\nFailed to get drive letters for the boot and OS volumes",
|
||||||
|
)))?;
|
||||||
|
} else {
|
||||||
|
action_tx.send(Action::Error(String::from(
|
||||||
|
"ERROR\n\n\nFailed to get drive letter for the OS volume",
|
||||||
|
)))?;
|
||||||
|
}
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
|
||||||
|
// BCD
|
||||||
|
if !letter_boot.is_empty() {
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::BootConfigData.to_string().as_str(),
|
||||||
|
vec![TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\bcdedit.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
String::from("/store"),
|
||||||
|
format!(
|
||||||
|
"{letter_boot}:{}\\Boot\\BCD",
|
||||||
|
if table_type == PartitionTableType::Guid {
|
||||||
|
"\\EFI\\Microsoft"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
),
|
||||||
|
String::from("/enum"),
|
||||||
|
],
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bitlocker
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::Bitlocker.to_string().as_str(),
|
||||||
|
vec![
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\manage-bde.exe", &system32)),
|
||||||
|
vec![String::from("-status"), format!("{letter_os}:")],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\manage-bde.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
String::from("-protectors"),
|
||||||
|
String::from("-get"),
|
||||||
|
format!("{letter_os}:"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Filesystem Health
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::CheckDisk.to_string().as_str(),
|
||||||
|
vec![TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\chkdsk.exe", &system32)),
|
||||||
|
vec![format!("{letter_os}:")],
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
|
||||||
|
// DISM Health
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::ComponentStore.to_string().as_str(),
|
||||||
|
vec![TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\dism.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
format!("/Image:{letter_os}:"),
|
||||||
|
String::from("/Cleanup-Image"),
|
||||||
|
String::from("/ScanHealth"),
|
||||||
|
],
|
||||||
|
)],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Critical Files/Folders
|
||||||
|
let paths: Vec<PathBuf> = [
|
||||||
|
// Files/Folders
|
||||||
|
"Users",
|
||||||
|
"Program Files",
|
||||||
|
"Program Files (x86)",
|
||||||
|
"ProgramData",
|
||||||
|
"Windows\\System32\\config",
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.map(|s| PathBuf::from(format!("{letter_os}:\\{s}")))
|
||||||
|
.collect();
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::SystemFiles.to_string().as_str(),
|
||||||
|
vec![TaskType::TestPaths(paths)],
|
||||||
|
);
|
||||||
|
|
||||||
|
// Registry
|
||||||
|
tasks.add_group(
|
||||||
|
DiagType::Registry.to_string().as_str(),
|
||||||
|
vec![
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
String::from("load"),
|
||||||
|
String::from("HKLM\\TmpSoftware"),
|
||||||
|
format!("{letter_os}:\\Windows\\System32\\config\\SOFTWARE"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
String::from("load"),
|
||||||
|
String::from("HKLM\\TmpSystem"),
|
||||||
|
format!("{letter_os}:\\Windows\\System32\\config\\SYSTEM"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![
|
||||||
|
String::from("load"),
|
||||||
|
String::from("HKU\\TmpDefault"),
|
||||||
|
format!("{letter_os}:\\Windows\\System32\\config\\DEFAULT"),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![String::from("unload"), String::from("HKLM\\TmpSoftware")],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![String::from("unload"), String::from("HKLM\\TmpSystem")],
|
||||||
|
),
|
||||||
|
TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{}\\reg.exe", &system32)),
|
||||||
|
vec![String::from("unload"), String::from("HKU\\TmpDefault")],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
tasks.add(TaskType::Sleep); // NOTE: DELETEME
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
42
boot_diags/src/state.rs
Normal file
42
boot_diags/src/state.rs
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use core::system::{disk::Disk, drivers};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct State {
|
||||||
|
pub disk_index_dest: Option<usize>,
|
||||||
|
pub disk_list: Arc<Mutex<Vec<Disk>>>,
|
||||||
|
pub driver_list: Vec<drivers::Driver>,
|
||||||
|
pub part_index_boot: Option<usize>,
|
||||||
|
pub part_index_os: Option<usize>,
|
||||||
|
pub driver: Option<drivers::Driver>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn new(disk_list: Arc<Mutex<Vec<Disk>>>) -> Self {
|
||||||
|
State {
|
||||||
|
disk_list,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scan_drivers(&mut self) {
|
||||||
|
self.driver_list = drivers::scan();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,21 +1,28 @@
|
||||||
{
|
{
|
||||||
"app_title": "Deja-vu: Clone Tool",
|
"app_title": "Deja-Vu",
|
||||||
"clone_app_path": "C:/Program Files/Some Clone Tool/app.exe",
|
"clone_app_path": "C:/Program Files/Some Clone Tool/app.exe",
|
||||||
|
"conemu_path": "C:/Program Files/ConEmu/ConEmu64.exe",
|
||||||
"keybindings": {
|
"keybindings": {
|
||||||
|
"Home": {
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
"ScanDisks": {
|
"ScanDisks": {
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"InstallDrivers": {
|
"InstallDrivers": {
|
||||||
"<Enter>": "Process",
|
"<Enter>": "Process",
|
||||||
"<Up>": "KeyUp",
|
"<Up>": "KeyUp",
|
||||||
"<Down>": "KeyDown",
|
"<Down>": "KeyDown",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"SelectDisks": {
|
"SelectDisks": {
|
||||||
"<i>": "InstallDriver",
|
"<i>": "InstallDriver",
|
||||||
|
|
@ -23,70 +30,163 @@
|
||||||
"<Enter>": "Process",
|
"<Enter>": "Process",
|
||||||
"<Up>": "KeyUp",
|
"<Up>": "KeyUp",
|
||||||
"<Down>": "KeyDown",
|
"<Down>": "KeyDown",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"SelectTableType": {
|
"SelectTableType": {
|
||||||
"<b>": "PrevScreen",
|
"<b>": "PrevScreen",
|
||||||
"<Enter>": "Process",
|
"<Enter>": "Process",
|
||||||
"<Up>": "KeyUp",
|
"<Up>": "KeyUp",
|
||||||
"<Down>": "KeyDown",
|
"<Down>": "KeyDown",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"Confirm": {
|
"Confirm": {
|
||||||
"<b>": "PrevScreen",
|
"<b>": "PrevScreen",
|
||||||
"<Enter>": "Process",
|
"<Enter>": "Process",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"PreClone": {
|
"PreClone": {
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"Clone": {
|
"Clone": {
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"SelectParts": {
|
"SelectParts": {
|
||||||
"<Enter>": "Process",
|
"<Enter>": "Process",
|
||||||
"<Up>": "KeyUp",
|
"<Up>": "KeyUp",
|
||||||
"<Down>": "KeyDown",
|
"<Down>": "KeyDown",
|
||||||
"<s>": "ScanDisks", // Start over
|
"<s>": "ScanDisks",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"PostClone": {
|
"PostClone": {
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"Done": {
|
"Done": {
|
||||||
"<Enter>": "Quit",
|
"<Enter>": "Process",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
"Failed": {
|
"Failed": {
|
||||||
"<Enter>": "Quit",
|
"<Enter>": "Quit",
|
||||||
"<q>": "Quit", // Quit the application
|
"<q>": "Quit",
|
||||||
"<Ctrl-d>": "Quit", // Another way to quit
|
"<Ctrl-d>": "Quit",
|
||||||
"<Ctrl-c>": "Quit", // Yet another way to quit
|
"<Ctrl-c>": "Quit",
|
||||||
"<Ctrl-z>": "Suspend" // Suspend the application
|
"<Ctrl-z>": "Suspend"
|
||||||
},
|
},
|
||||||
}
|
"DiagMenu": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<s>": "ScanDisks",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"BootDiags": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<m>": "DiagMainMenu",
|
||||||
|
"<r>": "BootScan",
|
||||||
|
"<s>": "ScanDisks",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"BootScan": {
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"BootSetup": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<m>": "DiagMainMenu",
|
||||||
|
"<s>": "ScanDisks",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"LogView": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Esc>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<PageUp>": "KeyPageUp",
|
||||||
|
"<PageDown>": "KeyPageDown",
|
||||||
|
"<Home>": "KeyHome",
|
||||||
|
"<End>": "KeyEnd",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"InjectDrivers": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<m>": "DiagMainMenu",
|
||||||
|
"<s>": "ScanDisks",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"Process": {
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"SetBootMode": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<m>": "DiagMainMenu",
|
||||||
|
"<s>": "ScanDisks",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
"PEMenu": {
|
||||||
|
"<Enter>": "Process",
|
||||||
|
"<Up>": "KeyUp",
|
||||||
|
"<Down>": "KeyDown",
|
||||||
|
"<q>": "Quit",
|
||||||
|
"<r>": "Restart",
|
||||||
|
"<p>": "Shutdown",
|
||||||
|
"<t>": "OpenTerminal",
|
||||||
|
"<Ctrl-d>": "Quit",
|
||||||
|
"<Ctrl-c>": "Quit",
|
||||||
|
"<Ctrl-z>": "Suspend"
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
# This file is part of Deja-vu.
|
# This file is part of Deja-Vu.
|
||||||
#
|
#
|
||||||
# Deja-vu is free software: you can redistribute it and/or modify it
|
# Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
# under the terms of the GNU General Public License as published by
|
# under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# Deja-vu is distributed in the hope that it will be useful, but
|
# Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
# See the GNU General Public License for more details.
|
# See the GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
# along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "core"
|
name = "core"
|
||||||
authors = ["2Shirt <2xShirt@gmail.com>"]
|
authors = ["2Shirt <2xShirt@gmail.com>"]
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
license = "GPL"
|
license = "GPL"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
|
|
@ -40,8 +40,8 @@ human-panic = "2.0.1"
|
||||||
json5 = "0.4.1"
|
json5 = "0.4.1"
|
||||||
lazy_static = "1.5.0"
|
lazy_static = "1.5.0"
|
||||||
libc = "0.2.158"
|
libc = "0.2.158"
|
||||||
once_cell = "1.20.2"
|
|
||||||
pretty_assertions = "1.4.0"
|
pretty_assertions = "1.4.0"
|
||||||
|
rand = "0.9.0"
|
||||||
ratatui = { version = "0.29.0", features = ["serde", "macros"] }
|
ratatui = { version = "0.29.0", features = ["serde", "macros"] }
|
||||||
raw-cpuid = "11.2.0"
|
raw-cpuid = "11.2.0"
|
||||||
regex = "1.11.1"
|
regex = "1.11.1"
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,63 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use strum::Display;
|
use strum::Display;
|
||||||
|
|
||||||
use crate::{components::popup::Type, line::DVLine, state::Mode, system::disk::Disk};
|
use crate::{
|
||||||
|
components::{left::SelectionType, popup::Type as PopupType},
|
||||||
|
line::DVLine,
|
||||||
|
state::Mode,
|
||||||
|
system::disk::Disk,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
|
||||||
|
pub enum DiagResult {
|
||||||
|
Pass,
|
||||||
|
Fail,
|
||||||
|
Warn,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
|
#[derive(Debug, Clone, PartialEq, Eq, Display, Serialize, Deserialize)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
// App
|
// App (Boot-Diags)
|
||||||
|
BootScan,
|
||||||
|
DiagLineStart { text: String },
|
||||||
|
DiagLineUpdate { result: DiagResult, text: String },
|
||||||
|
DiagLineEnd { text: String },
|
||||||
|
DiagMainMenu,
|
||||||
|
// App (Clone)
|
||||||
Highlight(usize),
|
Highlight(usize),
|
||||||
InstallDriver,
|
InstallDriver,
|
||||||
Process,
|
Process,
|
||||||
ScanDisks,
|
ScanDisks,
|
||||||
Select(Option<usize>, Option<usize>), // indicies for (source, dest) etc
|
Select(Option<usize>, Option<usize>), // indicies for (source, dest) etc
|
||||||
SelectRight(Option<usize>, Option<usize>), // indicies for right info pane
|
SelectRight(Option<usize>, Option<usize>), // indicies for right info pane
|
||||||
|
TaskGroupStart(String),
|
||||||
|
TasksComplete,
|
||||||
UpdateDiskList(Vec<Disk>),
|
UpdateDiskList(Vec<Disk>),
|
||||||
UpdateFooter(String),
|
UpdateFooter(String),
|
||||||
UpdateLeft(String, Vec<String>, Vec<String>, bool), // (title, labels, items, select_one)
|
UpdateLeft(String, Vec<String>, Vec<String>, SelectionType), // (title, labels, items, select_type)
|
||||||
UpdateRight(Vec<Vec<DVLine>>, usize, Vec<Vec<DVLine>>), // (labels, start_index, items) - items before start are always shown
|
UpdateRight(Vec<Vec<DVLine>>, usize, Vec<Vec<DVLine>>), // (labels, start_index, items) - items before start are always shown
|
||||||
|
// App (PE-Menu)
|
||||||
|
OpenTerminal,
|
||||||
|
Restart,
|
||||||
|
Shutdown,
|
||||||
// Screens
|
// Screens
|
||||||
DismissPopup,
|
DismissPopup,
|
||||||
DisplayPopup(Type, String),
|
DisplayPopup(PopupType, String),
|
||||||
NextScreen,
|
NextScreen,
|
||||||
PrevScreen,
|
PrevScreen,
|
||||||
SetMode(Mode),
|
SetMode(Mode),
|
||||||
|
|
@ -43,6 +67,12 @@ pub enum Action {
|
||||||
Help,
|
Help,
|
||||||
KeyDown,
|
KeyDown,
|
||||||
KeyUp,
|
KeyUp,
|
||||||
|
KeyLeft,
|
||||||
|
KeyRight,
|
||||||
|
KeyPageUp,
|
||||||
|
KeyPageDown,
|
||||||
|
KeyHome,
|
||||||
|
KeyEnd,
|
||||||
Quit,
|
Quit,
|
||||||
Render,
|
Render,
|
||||||
Resize(u16, u16),
|
Resize(u16, u16),
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
|
|
@ -38,6 +38,7 @@ const VERSION_MESSAGE: &str = concat!(
|
||||||
")"
|
")"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn version() -> String {
|
pub fn version() -> String {
|
||||||
let author = clap::crate_authors!();
|
let author = clap::crate_authors!();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,30 +1,30 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use crossterm::event::{KeyEvent, MouseEvent};
|
use crossterm::event::{KeyEvent, MouseEvent};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Rect, Size},
|
|
||||||
Frame,
|
Frame,
|
||||||
|
layout::{Rect, Size},
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
use crate::{action::Action, config::Config, tui::Event};
|
use crate::{action::Action, config::Config, tui::Event};
|
||||||
|
|
||||||
pub mod footer;
|
pub mod footer;
|
||||||
pub mod fps;
|
|
||||||
pub mod left;
|
pub mod left;
|
||||||
pub mod popup;
|
pub mod popup;
|
||||||
pub mod right;
|
pub mod right;
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
|
|
@ -31,6 +31,7 @@ pub struct Footer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Footer {
|
impl Footer {
|
||||||
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
text: String::from("(q) to quit"),
|
text: String::from("(q) to quit"),
|
||||||
|
|
@ -51,6 +52,7 @@ impl Component for Footer {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
match action {
|
match action {
|
||||||
Action::UpdateFooter(text) => self.text = text,
|
Action::UpdateFooter(text) => self.text = text,
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|
|
||||||
|
|
@ -1,113 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
use color_eyre::Result;
|
|
||||||
use ratatui::{
|
|
||||||
layout::{Constraint, Layout, Rect},
|
|
||||||
style::{Style, Stylize},
|
|
||||||
text::Span,
|
|
||||||
widgets::Paragraph,
|
|
||||||
Frame,
|
|
||||||
};
|
|
||||||
|
|
||||||
use super::Component;
|
|
||||||
|
|
||||||
use crate::action::Action;
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct FpsCounter {
|
|
||||||
last_tick_update: Instant,
|
|
||||||
tick_count: u32,
|
|
||||||
ticks_per_second: f64,
|
|
||||||
|
|
||||||
last_frame_update: Instant,
|
|
||||||
frame_count: u32,
|
|
||||||
frames_per_second: f64,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for FpsCounter {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self::new()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FpsCounter {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
Self {
|
|
||||||
last_tick_update: Instant::now(),
|
|
||||||
tick_count: 0,
|
|
||||||
ticks_per_second: 0.0,
|
|
||||||
last_frame_update: Instant::now(),
|
|
||||||
frame_count: 0,
|
|
||||||
frames_per_second: 0.0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn app_tick(&mut self) -> Result<()> {
|
|
||||||
self.tick_count += 1;
|
|
||||||
let now = Instant::now();
|
|
||||||
let elapsed = (now - self.last_tick_update).as_secs_f64();
|
|
||||||
if elapsed >= 1.0 {
|
|
||||||
self.ticks_per_second = self.tick_count as f64 / elapsed;
|
|
||||||
self.last_tick_update = now;
|
|
||||||
self.tick_count = 0;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_tick(&mut self) -> Result<()> {
|
|
||||||
self.frame_count += 1;
|
|
||||||
let now = Instant::now();
|
|
||||||
let elapsed = (now - self.last_frame_update).as_secs_f64();
|
|
||||||
if elapsed >= 1.0 {
|
|
||||||
self.frames_per_second = self.frame_count as f64 / elapsed;
|
|
||||||
self.last_frame_update = now;
|
|
||||||
self.frame_count = 0;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Component for FpsCounter {
|
|
||||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
|
||||||
match action {
|
|
||||||
Action::Tick => self.app_tick()?,
|
|
||||||
Action::Render => self.render_tick()?,
|
|
||||||
_ => {}
|
|
||||||
};
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
|
||||||
let [column, _] =
|
|
||||||
Layout::horizontal([Constraint::Min(1), Constraint::Length(2)]).areas(area);
|
|
||||||
let [_, row, _] = Layout::vertical([
|
|
||||||
Constraint::Length(1),
|
|
||||||
Constraint::Length(1),
|
|
||||||
Constraint::Min(0),
|
|
||||||
])
|
|
||||||
.areas(column);
|
|
||||||
let message = format!(
|
|
||||||
"{:.2} ticks/sec, {:.2} FPS",
|
|
||||||
self.ticks_per_second, self.frames_per_second
|
|
||||||
);
|
|
||||||
let span = Span::styled(message, Style::new().dim());
|
|
||||||
let paragraph = Paragraph::new(span).right_aligned();
|
|
||||||
frame.render_widget(paragraph, row);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
|
|
@ -19,10 +19,19 @@ use ratatui::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
widgets::{Block, Borders, HighlightSpacing, List, ListItem, Padding, Paragraph},
|
widgets::{Block, Borders, HighlightSpacing, List, ListItem, Padding, Paragraph},
|
||||||
};
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
use super::{state::StatefulList, Component};
|
use super::{Component, state::StatefulList};
|
||||||
use crate::{action::Action, config::Config};
|
use crate::{action::Action, config::Config, state::Mode};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub enum SelectionType {
|
||||||
|
#[default]
|
||||||
|
Loop,
|
||||||
|
One,
|
||||||
|
Two,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Left {
|
pub struct Left {
|
||||||
|
|
@ -30,17 +39,19 @@ pub struct Left {
|
||||||
config: Config,
|
config: Config,
|
||||||
labels: Vec<String>,
|
labels: Vec<String>,
|
||||||
list: StatefulList<String>,
|
list: StatefulList<String>,
|
||||||
select_one: bool,
|
mode: Mode,
|
||||||
|
select_type: SelectionType,
|
||||||
selections: Vec<Option<usize>>,
|
selections: Vec<Option<usize>>,
|
||||||
selections_saved: Vec<Option<usize>>,
|
selections_saved: Vec<Option<usize>>,
|
||||||
title_text: String,
|
title_text: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Left {
|
impl Left {
|
||||||
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
select_one: false,
|
|
||||||
labels: vec![String::from("one"), String::from("two")],
|
labels: vec![String::from("one"), String::from("two")],
|
||||||
|
select_type: SelectionType::Loop,
|
||||||
selections: vec![None, None],
|
selections: vec![None, None],
|
||||||
selections_saved: vec![None, None],
|
selections_saved: vec![None, None],
|
||||||
title_text: String::from("Home"),
|
title_text: String::from("Home"),
|
||||||
|
|
@ -72,15 +83,29 @@ impl Component for Left {
|
||||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
match action {
|
match action {
|
||||||
Action::Highlight(index) => self.set_highlight(index),
|
Action::Highlight(index) => self.set_highlight(index),
|
||||||
Action::KeyUp => self.list.previous(),
|
Action::KeyUp => {
|
||||||
Action::KeyDown => self.list.next(),
|
if self.mode != Mode::LogView {
|
||||||
|
self.list.previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyDown => {
|
||||||
|
if self.mode != Mode::LogView {
|
||||||
|
self.list.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
Action::Process => {
|
Action::Process => {
|
||||||
if let Some(command_tx) = self.command_tx.clone() {
|
if self.mode == Mode::LogView {
|
||||||
|
// Avoid updating selections/etc while log is open
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
if self.select_type == SelectionType::Loop {
|
||||||
|
// Selections aren't being used so this is a no-op
|
||||||
|
} else if let Some(command_tx) = self.command_tx.clone() {
|
||||||
match (self.selections[0], self.selections[1]) {
|
match (self.selections[0], self.selections[1]) {
|
||||||
(None, None) => {
|
(None, None) => {
|
||||||
// Making first selection
|
// Making first selection
|
||||||
command_tx.send(Action::Select(self.list.selected(), None))?;
|
command_tx.send(Action::Select(self.list.selected(), None))?;
|
||||||
if self.select_one {
|
if self.select_type == SelectionType::One {
|
||||||
// Confirm selection
|
// Confirm selection
|
||||||
command_tx.send(Action::NextScreen)?;
|
command_tx.send(Action::NextScreen)?;
|
||||||
}
|
}
|
||||||
|
|
@ -111,18 +136,19 @@ impl Component for Left {
|
||||||
self.selections_saved[0] = one;
|
self.selections_saved[0] = one;
|
||||||
self.selections_saved[1] = two;
|
self.selections_saved[1] = two;
|
||||||
}
|
}
|
||||||
Action::SetMode(_) => {
|
Action::SetMode(new_mode) => {
|
||||||
|
self.mode = new_mode;
|
||||||
self.selections[0] = None;
|
self.selections[0] = None;
|
||||||
self.selections[1] = None;
|
self.selections[1] = None;
|
||||||
}
|
}
|
||||||
Action::UpdateLeft(title, labels, items, select_one) => {
|
Action::UpdateLeft(title, labels, items, select_type) => {
|
||||||
self.title_text = title;
|
self.title_text = title;
|
||||||
self.labels = labels
|
self.labels = labels
|
||||||
.iter()
|
.iter()
|
||||||
.map(|label| format!(" ~{}~", label.to_lowercase()))
|
.map(|label| format!(" ~{}~", label.to_lowercase()))
|
||||||
.collect();
|
.collect();
|
||||||
self.list.set_items(items);
|
self.list.set_items(items);
|
||||||
self.select_one = select_one;
|
self.select_type = select_type;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -136,14 +162,10 @@ impl Component for Left {
|
||||||
.areas(area);
|
.areas(area);
|
||||||
|
|
||||||
// Title
|
// Title
|
||||||
let title_text = if self.selections[1].is_some() || self.select_one {
|
let title = Paragraph::new(
|
||||||
"Confirm Selections"
|
Line::from(Span::styled(self.title_text.as_str(), Style::default())).centered(),
|
||||||
} else {
|
)
|
||||||
self.title_text.as_str()
|
.block(Block::default().borders(Borders::NONE));
|
||||||
};
|
|
||||||
let title =
|
|
||||||
Paragraph::new(Line::from(Span::styled(title_text, Style::default())).centered())
|
|
||||||
.block(Block::default().borders(Borders::NONE));
|
|
||||||
frame.render_widget(title, title_area);
|
frame.render_widget(title, title_area);
|
||||||
|
|
||||||
// Body (Blank)
|
// Body (Blank)
|
||||||
|
|
@ -169,7 +191,7 @@ impl Component for Left {
|
||||||
.map(|(index, item)| {
|
.map(|(index, item)| {
|
||||||
let mut style = Style::default();
|
let mut style = Style::default();
|
||||||
let text = if self.selections[0].is_some_and(|first_index| first_index == index) {
|
let text = if self.selections[0].is_some_and(|first_index| first_index == index) {
|
||||||
if let Some(label) = self.labels.get(0) {
|
if let Some(label) = self.labels.first() {
|
||||||
style = style.yellow();
|
style = style.yellow();
|
||||||
label.as_str()
|
label.as_str()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,20 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
use rand::random;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
prelude::*,
|
prelude::*,
|
||||||
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
widgets::{Block, Borders, Clear, Paragraph, Wrap},
|
||||||
|
|
@ -42,11 +43,9 @@ pub struct Popup {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Popup {
|
impl Popup {
|
||||||
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self::default()
|
||||||
popup_text: String::from("Scanning Disks..."),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -66,7 +65,7 @@ impl Component for Popup {
|
||||||
Action::DismissPopup => self.popup_text.clear(),
|
Action::DismissPopup => self.popup_text.clear(),
|
||||||
Action::DisplayPopup(new_type, new_text) => {
|
Action::DisplayPopup(new_type, new_text) => {
|
||||||
self.popup_type = new_type;
|
self.popup_type = new_type;
|
||||||
self.popup_text = new_text;
|
self.popup_text = format!("\n{new_text}");
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -98,3 +97,20 @@ impl Component for Popup {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
|
pub fn fortune() -> String {
|
||||||
|
String::from(match random::<u8>() / 4 {
|
||||||
|
0 => "FUN FACT\n\n\nComputers barely work.",
|
||||||
|
1 => "CRASH OVERRIDE\n\n\n\"Hack the planet!\"",
|
||||||
|
2 => "CATS\n\n\n\"All your base are belong to us!\"",
|
||||||
|
3 => "HMM\n\n\nThis has all happened before...\n\nThis will all happen again.",
|
||||||
|
4 => "CYPHER\n\n\n\"I don’t even see the code. All I see is blonde, brunette, red-head.\"",
|
||||||
|
5 => "CONGRATULATIONS\n\n\nYour did it!",
|
||||||
|
6 => "DID YOU KNOW?\n\n\nmacOS includes a built-in screen reader!",
|
||||||
|
7 => "TIP OF THE DAY\n\n\nNever go full Snappy!",
|
||||||
|
8 => "WORDS OF WISDOM\n\n\n\nIt’s not DNS,\n\nThere’s no way it’s DNS,\n\nIt was DNS.",
|
||||||
|
9 => "HAL 9000\n\n\n\"I'm sorry Dave, I'm afraid I can't do that.\"",
|
||||||
|
_ => "COMPLETE\n\n\nThank you for using this tool!",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
|
|
@ -21,8 +21,8 @@ use ratatui::{
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use tokio::sync::mpsc::UnboundedSender;
|
||||||
|
|
||||||
use super::{state::StatefulList, Component};
|
use super::{Component, state::StatefulList};
|
||||||
use crate::{action::Action, config::Config, line::DVLine};
|
use crate::{action::Action, config::Config, line::DVLine, state::Mode};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Right {
|
pub struct Right {
|
||||||
|
|
@ -31,12 +31,14 @@ pub struct Right {
|
||||||
list_header: Vec<DVLine>,
|
list_header: Vec<DVLine>,
|
||||||
list_labels: Vec<Vec<DVLine>>,
|
list_labels: Vec<Vec<DVLine>>,
|
||||||
list: StatefulList<Vec<DVLine>>,
|
list: StatefulList<Vec<DVLine>>,
|
||||||
|
mode: Mode,
|
||||||
selections: Vec<Option<usize>>,
|
selections: Vec<Option<usize>>,
|
||||||
selections_saved: Vec<Option<usize>>,
|
selections_saved: Vec<Option<usize>>,
|
||||||
title: String,
|
title: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Right {
|
impl Right {
|
||||||
|
#[must_use]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
selections: vec![None, None],
|
selections: vec![None, None],
|
||||||
|
|
@ -92,8 +94,16 @@ impl Component for Right {
|
||||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
match action {
|
match action {
|
||||||
Action::Highlight(index) => self.set_highlight(index),
|
Action::Highlight(index) => self.set_highlight(index),
|
||||||
Action::KeyUp => self.list.previous(),
|
Action::KeyUp => {
|
||||||
Action::KeyDown => self.list.next(),
|
if self.mode != Mode::LogView {
|
||||||
|
self.list.previous();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyDown => {
|
||||||
|
if self.mode != Mode::LogView {
|
||||||
|
self.list.next();
|
||||||
|
}
|
||||||
|
}
|
||||||
Action::Select(one, two) => {
|
Action::Select(one, two) => {
|
||||||
self.selections[0] = one;
|
self.selections[0] = one;
|
||||||
self.selections[1] = two;
|
self.selections[1] = two;
|
||||||
|
|
@ -108,7 +118,8 @@ impl Component for Right {
|
||||||
self.selections_saved[0] = one;
|
self.selections_saved[0] = one;
|
||||||
self.selections_saved[1] = two;
|
self.selections_saved[1] = two;
|
||||||
}
|
}
|
||||||
Action::SetMode(_) => {
|
Action::SetMode(new_mode) => {
|
||||||
|
self.mode = new_mode;
|
||||||
self.selections[0] = None;
|
self.selections[0] = None;
|
||||||
self.selections[1] = None;
|
self.selections[1] = None;
|
||||||
self.selections_saved[0] = None;
|
self.selections_saved[0] = None;
|
||||||
|
|
@ -148,37 +159,37 @@ impl Component for Right {
|
||||||
}
|
}
|
||||||
|
|
||||||
// First selection
|
// First selection
|
||||||
if let Some(first_index) = self.get_first() {
|
if let Some(first_index) = self.get_first()
|
||||||
if let Some(first_desc) = self.list.get(first_index) {
|
&& let Some(first_desc) = self.list.get(first_index)
|
||||||
if let Some(label) = self.list_labels.get(0) {
|
{
|
||||||
label
|
if let Some(label) = self.list_labels.first() {
|
||||||
.iter()
|
label
|
||||||
.for_each(|dv| body_text.push(dv.as_line().bold()));
|
|
||||||
}
|
|
||||||
body_text.push(Line::from(""));
|
|
||||||
first_desc
|
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|dv| body_text.push(dv.as_line()));
|
.for_each(|dv| body_text.push(dv.as_line().bold()));
|
||||||
|
body_text.push(Line::from(""));
|
||||||
}
|
}
|
||||||
|
first_desc
|
||||||
|
.iter()
|
||||||
|
.for_each(|dv| body_text.push(dv.as_line()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Second selection
|
// Second selection
|
||||||
if let Some(second_index) = self.get_second() {
|
if let Some(second_index) = self.get_second()
|
||||||
if let Some(second_desc) = self.list.get(second_index) {
|
&& let Some(second_desc) = self.list.get(second_index)
|
||||||
// Divider
|
{
|
||||||
body_text.push(Line::from(""));
|
// Divider
|
||||||
body_text.push(Line::from(str::repeat("─", (body_area.width - 4) as usize)));
|
body_text.push(Line::from(""));
|
||||||
body_text.push(Line::from(""));
|
body_text.push(Line::from(str::repeat("─", (body_area.width - 4) as usize)));
|
||||||
if let Some(label) = self.list_labels.get(1) {
|
body_text.push(Line::from(""));
|
||||||
label
|
if let Some(label) = self.list_labels.get(1) {
|
||||||
.iter()
|
label
|
||||||
.for_each(|dv| body_text.push(dv.as_line().bold()));
|
|
||||||
}
|
|
||||||
body_text.push(Line::from(""));
|
|
||||||
second_desc
|
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|dv| body_text.push(dv.as_line()));
|
.for_each(|dv| body_text.push(dv.as_line().bold()));
|
||||||
}
|
}
|
||||||
|
body_text.push(Line::from(""));
|
||||||
|
second_desc
|
||||||
|
.iter()
|
||||||
|
.for_each(|dv| body_text.push(dv.as_line()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build Paragraph
|
// Build Paragraph
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
|
@ -31,14 +31,17 @@ impl<T: Clone> StatefulList<T> {
|
||||||
self.items.clear();
|
self.items.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get(&self, index: usize) -> Option<&T> {
|
pub fn get(&self, index: usize) -> Option<&T> {
|
||||||
self.items.get(index)
|
self.items.get(index)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_selected(&self) -> Option<T> {
|
pub fn get_selected(&self) -> Option<T> {
|
||||||
self.state.selected().map(|i| self.items[i].clone())
|
self.state.selected().map(|i| self.items[i].clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.items.is_empty()
|
self.items.is_empty()
|
||||||
}
|
}
|
||||||
|
|
@ -51,11 +54,12 @@ impl<T: Clone> StatefulList<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn selected(&self) -> Option<usize> {
|
pub fn selected(&self) -> Option<usize> {
|
||||||
self.state.selected()
|
self.state.selected()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select_first_item(&mut self) {
|
pub fn select_first_item(&mut self) {
|
||||||
if self.items.is_empty() {
|
if self.items.is_empty() {
|
||||||
self.state.select(None);
|
self.state.select(None);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
|
|
@ -1,67 +1,140 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
|
Frame,
|
||||||
|
layout::{Constraint, Layout, Rect},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
|
style::{Style, Stylize},
|
||||||
|
text::Span,
|
||||||
widgets::{Block, Borders, Paragraph},
|
widgets::{Block, Borders, Paragraph},
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc::UnboundedSender;
|
use std::time::Instant;
|
||||||
|
|
||||||
use super::Component;
|
use super::Component;
|
||||||
use crate::{action::Action, config::Config};
|
use crate::action::Action;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Title {
|
pub struct Title {
|
||||||
command_tx: Option<UnboundedSender<Action>>,
|
mode: String,
|
||||||
config: Config,
|
title: String,
|
||||||
|
|
||||||
|
last_tick_update: Instant,
|
||||||
|
tick_count: u32,
|
||||||
|
ticks_per_second: f64,
|
||||||
|
|
||||||
|
last_frame_update: Instant,
|
||||||
|
frame_count: u32,
|
||||||
|
frames_per_second: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Title {
|
impl Title {
|
||||||
pub fn new() -> Self {
|
#[must_use]
|
||||||
Self::default()
|
pub fn new(title: &str) -> Self {
|
||||||
|
Self {
|
||||||
|
mode: String::new(),
|
||||||
|
title: String::from(title),
|
||||||
|
last_tick_update: Instant::now(),
|
||||||
|
tick_count: 0,
|
||||||
|
ticks_per_second: 0.0,
|
||||||
|
last_frame_update: Instant::now(),
|
||||||
|
frame_count: 0,
|
||||||
|
frames_per_second: 0.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
|
fn app_tick(&mut self) -> Result<()> {
|
||||||
|
self.tick_count += 1;
|
||||||
|
let now = Instant::now();
|
||||||
|
let elapsed = (now - self.last_tick_update).as_secs_f64();
|
||||||
|
if elapsed >= 1.0 {
|
||||||
|
self.ticks_per_second = f64::from(self.tick_count) / elapsed;
|
||||||
|
self.last_tick_update = now;
|
||||||
|
self.tick_count = 0;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::unnecessary_wraps)]
|
||||||
|
fn render_tick(&mut self) -> Result<()> {
|
||||||
|
self.frame_count += 1;
|
||||||
|
let now = Instant::now();
|
||||||
|
let elapsed = (now - self.last_frame_update).as_secs_f64();
|
||||||
|
if elapsed >= 1.0 {
|
||||||
|
self.frames_per_second = f64::from(self.frame_count) / elapsed;
|
||||||
|
self.last_frame_update = now;
|
||||||
|
self.frame_count = 0;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component for Title {
|
impl Component for Title {
|
||||||
fn register_action_handler(&mut self, tx: UnboundedSender<Action>) -> Result<()> {
|
|
||||||
self.command_tx = Some(tx);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn register_config_handler(&mut self, config: Config) -> Result<()> {
|
|
||||||
self.config = config;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
fn update(&mut self, action: Action) -> Result<Option<Action>> {
|
||||||
|
#[allow(clippy::match_single_binding)]
|
||||||
match action {
|
match action {
|
||||||
|
Action::SetMode(mode) => {
|
||||||
|
self.mode = format!("{mode:?}");
|
||||||
|
}
|
||||||
|
Action::Render => self.render_tick()?,
|
||||||
|
Action::Tick => self.app_tick()?,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
fn draw(&mut self, frame: &mut Frame, area: Rect) -> Result<()> {
|
||||||
|
let [_, row, _] = Layout::vertical([
|
||||||
|
Constraint::Length(1),
|
||||||
|
Constraint::Length(1),
|
||||||
|
Constraint::Min(0),
|
||||||
|
])
|
||||||
|
.areas(area);
|
||||||
|
let [_, left, right, _] = Layout::horizontal([
|
||||||
|
Constraint::Length(2),
|
||||||
|
Constraint::Min(1),
|
||||||
|
Constraint::Min(1),
|
||||||
|
Constraint::Length(2),
|
||||||
|
])
|
||||||
|
.areas(row);
|
||||||
|
|
||||||
// Title Block
|
// Title Block
|
||||||
let title_text = Span::styled(
|
let title_text = Span::styled(
|
||||||
self.config.app_title.as_str(),
|
format!("Deja-Vu: {}", self.title),
|
||||||
Style::default().fg(Color::LightCyan),
|
Style::default().fg(Color::LightCyan),
|
||||||
);
|
);
|
||||||
let title = Paragraph::new(Line::from(title_text).centered())
|
let title = Paragraph::new(Line::from(title_text).centered())
|
||||||
.block(Block::default().borders(Borders::ALL));
|
.block(Block::default().borders(Borders::ALL));
|
||||||
frame.render_widget(title, area);
|
frame.render_widget(title, area);
|
||||||
|
|
||||||
|
// Mode
|
||||||
|
let span = Span::styled(format!("Mode: {}", &self.mode), Style::new().dim());
|
||||||
|
let paragraph = Paragraph::new(span).left_aligned();
|
||||||
|
frame.render_widget(paragraph, left);
|
||||||
|
|
||||||
|
// FPS
|
||||||
|
let message = format!(
|
||||||
|
"{:.2} ticks/sec, {:.2} FPS",
|
||||||
|
self.ticks_per_second, self.frames_per_second
|
||||||
|
);
|
||||||
|
let span = Span::styled(message, Style::new().dim());
|
||||||
|
let paragraph = Paragraph::new(span).right_aligned();
|
||||||
|
frame.render_widget(paragraph, right);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,20 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
#![allow(clippy::ref_option)]
|
||||||
#![allow(dead_code)] // Remove this once you start using the code
|
#![allow(dead_code)] // Remove this once you start using the code
|
||||||
|
|
||||||
use std::{collections::HashMap, env, path::PathBuf};
|
use std::{collections::HashMap, env, path::PathBuf};
|
||||||
|
|
@ -23,7 +25,7 @@ use derive_deref::{Deref, DerefMut};
|
||||||
use directories::ProjectDirs;
|
use directories::ProjectDirs;
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use ratatui::style::{Color, Modifier, Style};
|
use ratatui::style::{Color, Modifier, Style};
|
||||||
use serde::{de::Deserializer, Deserialize};
|
use serde::{Deserialize, de::Deserializer};
|
||||||
use tracing::error;
|
use tracing::error;
|
||||||
|
|
||||||
use crate::{action::Action, state::Mode};
|
use crate::{action::Action, state::Mode};
|
||||||
|
|
@ -44,6 +46,8 @@ pub struct Config {
|
||||||
pub app_title: String,
|
pub app_title: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub clone_app_path: PathBuf,
|
pub clone_app_path: PathBuf,
|
||||||
|
#[serde(default)]
|
||||||
|
pub conemu_path: PathBuf,
|
||||||
#[serde(default, flatten)]
|
#[serde(default, flatten)]
|
||||||
pub config: AppConfig,
|
pub config: AppConfig,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
|
@ -52,15 +56,15 @@ pub struct Config {
|
||||||
pub styles: Styles,
|
pub styles: Styles,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub static PROJECT_NAME: &'static str = "DEJA-VU";
|
pub static PROJECT_NAME: &str = "DEJA-VU";
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
//pub static ref PROJECT_NAME: String = env!("CARGO_PKG_NAME").to_uppercase().to_string();
|
//pub static ref PROJECT_NAME: String = env!("CARGO_PKG_NAME").to_uppercase().to_string();
|
||||||
pub static ref DATA_FOLDER: Option<PathBuf> =
|
pub static ref DATA_FOLDER: Option<PathBuf> =
|
||||||
env::var(format!("{}_DATA", PROJECT_NAME))
|
env::var(format!("{PROJECT_NAME}_DATA"))
|
||||||
.ok()
|
.ok()
|
||||||
.map(PathBuf::from);
|
.map(PathBuf::from);
|
||||||
pub static ref CONFIG_FOLDER: Option<PathBuf> =
|
pub static ref CONFIG_FOLDER: Option<PathBuf> =
|
||||||
env::var(format!("{}_CONFIG", PROJECT_NAME))
|
env::var(format!("{PROJECT_NAME}_CONFIG"))
|
||||||
.ok()
|
.ok()
|
||||||
.map(PathBuf::from);
|
.map(PathBuf::from);
|
||||||
}
|
}
|
||||||
|
|
@ -76,6 +80,10 @@ impl Config {
|
||||||
"clone_app_path",
|
"clone_app_path",
|
||||||
String::from("C:\\Program Files\\Some Clone Tool\\app.exe"),
|
String::from("C:\\Program Files\\Some Clone Tool\\app.exe"),
|
||||||
)?
|
)?
|
||||||
|
.set_default(
|
||||||
|
"conemu_path",
|
||||||
|
String::from("C:\\Program Files\\ConEmu\\ConEmu64.exe"),
|
||||||
|
)?
|
||||||
.set_default("config_dir", config_dir.to_str().unwrap())?
|
.set_default("config_dir", config_dir.to_str().unwrap())?
|
||||||
.set_default("data_dir", data_dir.to_str().unwrap())?;
|
.set_default("data_dir", data_dir.to_str().unwrap())?;
|
||||||
|
|
||||||
|
|
@ -120,31 +128,31 @@ impl Config {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_data_dir() -> PathBuf {
|
pub fn get_data_dir() -> PathBuf {
|
||||||
let directory = if let Some(s) = DATA_FOLDER.clone() {
|
if let Some(s) = DATA_FOLDER.clone() {
|
||||||
s
|
s
|
||||||
} else if let Some(proj_dirs) = project_directory() {
|
} else if let Some(proj_dirs) = project_directory() {
|
||||||
proj_dirs.data_local_dir().to_path_buf()
|
proj_dirs.data_local_dir().to_path_buf()
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from(".").join(".data")
|
PathBuf::from(".").join(".data")
|
||||||
};
|
}
|
||||||
directory
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_config_dir() -> PathBuf {
|
pub fn get_config_dir() -> PathBuf {
|
||||||
let directory = if let Some(s) = CONFIG_FOLDER.clone() {
|
if let Some(s) = CONFIG_FOLDER.clone() {
|
||||||
s
|
s
|
||||||
} else if let Some(proj_dirs) = project_directory() {
|
} else if let Some(proj_dirs) = project_directory() {
|
||||||
proj_dirs.config_local_dir().to_path_buf()
|
proj_dirs.config_local_dir().to_path_buf()
|
||||||
} else {
|
} else {
|
||||||
PathBuf::from(".").join(".config")
|
PathBuf::from(".").join(".config")
|
||||||
};
|
}
|
||||||
directory
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn project_directory() -> Option<ProjectDirs> {
|
fn project_directory() -> Option<ProjectDirs> {
|
||||||
ProjectDirs::from("com", "Deja-vu", "deja-vu")
|
ProjectDirs::from("com", "Deja-Vu", "deja-vu")
|
||||||
//ProjectDirs::from("com", "Deja-vu", env!("CARGO_PKG_NAME"))
|
//ProjectDirs::from("com", "Deja-Vu", env!("CARGO_PKG_NAME"))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deref, DerefMut)]
|
#[derive(Clone, Debug, Default, Deref, DerefMut)]
|
||||||
|
|
@ -252,6 +260,7 @@ fn parse_key_code_with_modifiers(
|
||||||
Ok(KeyEvent::new(c, modifiers))
|
Ok(KeyEvent::new(c, modifiers))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn key_event_to_string(key_event: &KeyEvent) -> String {
|
pub fn key_event_to_string(key_event: &KeyEvent) -> String {
|
||||||
let char;
|
let char;
|
||||||
let key_code = match key_event.code {
|
let key_code = match key_event.code {
|
||||||
|
|
@ -322,8 +331,8 @@ pub fn parse_key_sequence(raw: &str) -> Result<Vec<KeyEvent>, String> {
|
||||||
let raw = if raw.contains("><") {
|
let raw = if raw.contains("><") {
|
||||||
raw
|
raw
|
||||||
} else {
|
} else {
|
||||||
let raw = raw.strip_prefix('<').unwrap_or(raw);
|
let mut raw = raw.strip_prefix('<').unwrap_or(raw);
|
||||||
let raw = raw.strip_prefix('>').unwrap_or(raw);
|
raw = raw.strip_prefix('>').unwrap_or(raw);
|
||||||
raw
|
raw
|
||||||
};
|
};
|
||||||
let sequences = raw
|
let sequences = raw
|
||||||
|
|
@ -367,6 +376,7 @@ impl<'de> Deserialize<'de> for Styles {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn parse_style(line: &str) -> Style {
|
pub fn parse_style(line: &str) -> Style {
|
||||||
let (foreground, background) =
|
let (foreground, background) =
|
||||||
line.split_at(line.to_lowercase().find("on ").unwrap_or(line.len()));
|
line.split_at(line.to_lowercase().find("on ").unwrap_or(line.len()));
|
||||||
|
|
@ -429,8 +439,11 @@ fn parse_color(s: &str) -> Option<Color> {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
Some(Color::Indexed(c))
|
Some(Color::Indexed(c))
|
||||||
} else if s.contains("rgb") {
|
} else if s.contains("rgb") {
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
let red = (s.as_bytes()[3] as char).to_digit(10).unwrap_or_default() as u8;
|
let red = (s.as_bytes()[3] as char).to_digit(10).unwrap_or_default() as u8;
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
let green = (s.as_bytes()[4] as char).to_digit(10).unwrap_or_default() as u8;
|
let green = (s.as_bytes()[4] as char).to_digit(10).unwrap_or_default() as u8;
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
let blue = (s.as_bytes()[5] as char).to_digit(10).unwrap_or_default() as u8;
|
let blue = (s.as_bytes()[5] as char).to_digit(10).unwrap_or_default() as u8;
|
||||||
let c = 16 + red * 36 + green * 6 + blue;
|
let c = 16 + red * 36 + green * 6 + blue;
|
||||||
Some(Color::Indexed(c))
|
Some(Color::Indexed(c))
|
||||||
|
|
@ -529,7 +542,7 @@ mod tests {
|
||||||
let c = Config::new()?;
|
let c = Config::new()?;
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
c.keybindings
|
c.keybindings
|
||||||
.get(&Mode::ScanDisks) // i.e. Home
|
.get(&Mode::Home) // i.e. Home
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.get(&parse_key_sequence("<q>").unwrap_or_default())
|
.get(&parse_key_sequence("<q>").unwrap_or_default())
|
||||||
.unwrap(),
|
.unwrap(),
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,19 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
|
@ -30,10 +31,10 @@ pub fn init() -> Result<()> {
|
||||||
.into_hooks();
|
.into_hooks();
|
||||||
eyre_hook.install()?;
|
eyre_hook.install()?;
|
||||||
std::panic::set_hook(Box::new(move |panic_info| {
|
std::panic::set_hook(Box::new(move |panic_info| {
|
||||||
if let Ok(mut t) = crate::tui::Tui::new() {
|
if let Ok(mut t) = crate::tui::Tui::new()
|
||||||
if let Err(r) = t.exit() {
|
&& let Err(r) = t.exit()
|
||||||
error!("Unable to exit Terminal: {:?}", r);
|
{
|
||||||
}
|
error!("Unable to exit Terminal: {:?}", r);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
pub mod action;
|
pub mod action;
|
||||||
pub mod cli;
|
pub mod cli;
|
||||||
|
|
@ -23,4 +23,5 @@ pub mod logging;
|
||||||
pub mod state;
|
pub mod state;
|
||||||
pub mod system;
|
pub mod system;
|
||||||
pub mod tasks;
|
pub mod tasks;
|
||||||
|
pub mod tests;
|
||||||
pub mod tui;
|
pub mod tui;
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
style::{Color, Style},
|
style::{Color, Style},
|
||||||
|
|
@ -30,13 +30,15 @@ pub struct DVLine {
|
||||||
|
|
||||||
impl DVLine {
|
impl DVLine {
|
||||||
/// Convert to Line with colored span(s)
|
/// Convert to Line with colored span(s)
|
||||||
pub fn as_line(&self) -> Line {
|
#[must_use]
|
||||||
|
pub fn as_line(&self) -> Line<'_> {
|
||||||
let mut spans = Vec::new();
|
let mut spans = Vec::new();
|
||||||
zip(self.line_parts.clone(), self.line_colors.clone())
|
zip(self.line_parts.clone(), self.line_colors.clone())
|
||||||
.for_each(|(part, color)| spans.push(Span::styled(part, Style::default().fg(color))));
|
.for_each(|(part, color)| spans.push(Span::styled(part, Style::default().fg(color))));
|
||||||
Line::from(spans)
|
Line::from(spans)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn blank() -> Self {
|
pub fn blank() -> Self {
|
||||||
Self {
|
Self {
|
||||||
line_parts: vec![String::new()],
|
line_parts: vec![String::new()],
|
||||||
|
|
@ -45,7 +47,11 @@ impl DVLine {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_disk_description_right(disk: &Disk) -> Vec<DVLine> {
|
#[must_use]
|
||||||
|
pub fn get_disk_description_right(
|
||||||
|
disk: &Disk,
|
||||||
|
boot_os_indicies: &Option<Vec<usize>>,
|
||||||
|
) -> Vec<DVLine> {
|
||||||
let mut description: Vec<DVLine> = vec![
|
let mut description: Vec<DVLine> = vec![
|
||||||
DVLine {
|
DVLine {
|
||||||
line_parts: vec![format!(
|
line_parts: vec![format!(
|
||||||
|
|
@ -67,15 +73,33 @@ pub fn get_disk_description_right(disk: &Disk) -> Vec<DVLine> {
|
||||||
line_colors: vec![Color::Blue],
|
line_colors: vec![Color::Blue],
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
for line in &disk.parts_description {
|
disk.parts_description
|
||||||
description.push(DVLine {
|
.iter()
|
||||||
line_parts: vec![line.clone()],
|
.enumerate()
|
||||||
line_colors: vec![Color::Reset],
|
.for_each(|(index, line)| {
|
||||||
|
let mut line_parts = vec![line.clone()];
|
||||||
|
let mut line_colors = vec![Color::Reset];
|
||||||
|
if let Some(indicies) = boot_os_indicies {
|
||||||
|
let boot_index = indicies.first();
|
||||||
|
if boot_index.is_some_and(|i| i == &index) {
|
||||||
|
line_parts.push(String::from(" <-- Boot Partition"));
|
||||||
|
line_colors.push(Color::Cyan);
|
||||||
|
}
|
||||||
|
let boot_index = indicies.get(1);
|
||||||
|
if boot_index.is_some_and(|i| i == &index) {
|
||||||
|
line_parts.push(String::from(" <-- OS Partition"));
|
||||||
|
line_colors.push(Color::Cyan);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
description.push(DVLine {
|
||||||
|
line_parts,
|
||||||
|
line_colors,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
description
|
description
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_part_description(part: &Partition) -> Vec<DVLine> {
|
pub fn get_part_description(part: &Partition) -> Vec<DVLine> {
|
||||||
let description: Vec<DVLine> = vec![
|
let description: Vec<DVLine> = vec![
|
||||||
DVLine {
|
DVLine {
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,28 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
|
||||||
|
|
||||||
use crate::config;
|
use crate::config;
|
||||||
|
|
||||||
lazy_static::lazy_static! {
|
lazy_static::lazy_static! {
|
||||||
pub static ref LOG_ENV: String = format!("{}_LOG_LEVEL", config::PROJECT_NAME);
|
pub static ref LOG_ENV: String = format!("{}_LOG_LEVEL", config::PROJECT_NAME);
|
||||||
pub static ref LOG_FILE: String = format!("{}.log", config::PROJECT_NAME);
|
pub static ref LOG_FILE: String = format!("{}.log", config::PROJECT_NAME.to_lowercase());
|
||||||
//pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME"));
|
//pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,38 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
|
||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
use crate::system::{
|
|
||||||
disk::{Disk, PartitionTableType},
|
|
||||||
drivers,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||||
pub enum Mode {
|
pub enum Mode {
|
||||||
|
// Core
|
||||||
#[default]
|
#[default]
|
||||||
|
Home,
|
||||||
|
Done,
|
||||||
|
Failed,
|
||||||
|
// Boot Diags
|
||||||
|
DiagMenu,
|
||||||
|
BootDiags,
|
||||||
|
BootScan,
|
||||||
|
BootSetup,
|
||||||
|
LogView,
|
||||||
|
Process,
|
||||||
|
InjectDrivers,
|
||||||
|
SetBootMode,
|
||||||
|
// Clone
|
||||||
ScanDisks,
|
ScanDisks,
|
||||||
InstallDrivers,
|
InstallDrivers,
|
||||||
SelectDisks,
|
SelectDisks,
|
||||||
|
|
@ -35,31 +42,6 @@ pub enum Mode {
|
||||||
Clone,
|
Clone,
|
||||||
SelectParts,
|
SelectParts,
|
||||||
PostClone,
|
PostClone,
|
||||||
Done,
|
// WinPE
|
||||||
Failed,
|
PEMenu,
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub struct CloneSettings {
|
|
||||||
pub disk_index_dest: Option<usize>,
|
|
||||||
pub disk_index_source: Option<usize>,
|
|
||||||
pub disk_list: Arc<Mutex<Vec<Disk>>>,
|
|
||||||
pub driver_list: Vec<drivers::Driver>,
|
|
||||||
pub part_index_boot: Option<usize>,
|
|
||||||
pub part_index_os: Option<usize>,
|
|
||||||
pub driver: Option<drivers::Driver>,
|
|
||||||
pub table_type: Option<PartitionTableType>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CloneSettings {
|
|
||||||
pub fn new(disk_list: Arc<Mutex<Vec<Disk>>>) -> Self {
|
|
||||||
CloneSettings {
|
|
||||||
disk_list,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn scan_drivers(&mut self) {
|
|
||||||
self.driver_list = drivers::scan();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
pub mod boot;
|
pub mod boot;
|
||||||
pub mod cpu;
|
pub mod cpu;
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,44 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
|
||||||
use super::{disk::PartitionTableType, drivers::Driver};
|
use super::{disk::PartitionTableType, drivers::Driver};
|
||||||
use crate::tasks::Task;
|
use crate::tasks::TaskType;
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, PartialEq)]
|
||||||
|
pub enum SafeMode {
|
||||||
|
#[default]
|
||||||
|
Disable,
|
||||||
|
Enable,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn configure_disk(
|
pub fn configure_disk(
|
||||||
letter_boot: &str,
|
letter_boot: &str,
|
||||||
letter_os: &str,
|
letter_os: &str,
|
||||||
|
safe_mode: SafeMode,
|
||||||
system32: &str,
|
system32: &str,
|
||||||
table_type: PartitionTableType,
|
table_type: &PartitionTableType,
|
||||||
) -> Vec<Task> {
|
) -> Vec<TaskType> {
|
||||||
let mut tasks = Vec::new();
|
let mut tasks = Vec::new();
|
||||||
|
|
||||||
// Create
|
// Create
|
||||||
tasks.push(Task::Command(
|
tasks.push(TaskType::CommandWait(
|
||||||
PathBuf::from(format!("{system32}/bcdboot.exe")),
|
PathBuf::from(format!("{system32}/bcdboot.exe")),
|
||||||
vec![
|
vec![
|
||||||
format!("{letter_os}:\\Windows"),
|
format!("{letter_os}:\\Windows"),
|
||||||
|
|
@ -42,8 +53,8 @@ pub fn configure_disk(
|
||||||
));
|
));
|
||||||
|
|
||||||
// Update boot sector (for legacy setups)
|
// Update boot sector (for legacy setups)
|
||||||
if table_type == PartitionTableType::Legacy {
|
if *table_type == PartitionTableType::Legacy {
|
||||||
tasks.push(Task::Command(
|
tasks.push(TaskType::CommandWait(
|
||||||
PathBuf::from(format!("{system32}/bootsect.exe")),
|
PathBuf::from(format!("{system32}/bootsect.exe")),
|
||||||
vec![
|
vec![
|
||||||
String::from("/nt60"),
|
String::from("/nt60"),
|
||||||
|
|
@ -54,7 +65,38 @@ pub fn configure_disk(
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lock in safe mode
|
// Lock in safe mode (if needed)
|
||||||
|
if safe_mode == SafeMode::Enable {
|
||||||
|
tasks.push(
|
||||||
|
set_mode(letter_boot, &SafeMode::Enable, system32, table_type)
|
||||||
|
.expect("Failed to create set_mode task."),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Done
|
||||||
|
tasks
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn inject_driver(driver: &Driver, letter_os: &str, system32: &str) -> Result<TaskType> {
|
||||||
|
//if let Some(driver_path_str) = driver.path.to_str() {
|
||||||
|
let driver_path = driver.path.to_str().unwrap();
|
||||||
|
Ok(TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{system32}/dism.exe")),
|
||||||
|
vec![
|
||||||
|
format!("/image:{letter_os}:\\"),
|
||||||
|
String::from("/add-driver"),
|
||||||
|
format!("/driver:{driver_path}"),
|
||||||
|
String::from("/recurse"),
|
||||||
|
],
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_mode(
|
||||||
|
letter_boot: &str,
|
||||||
|
mode: &SafeMode,
|
||||||
|
system32: &str,
|
||||||
|
table_type: &PartitionTableType,
|
||||||
|
) -> Result<TaskType> {
|
||||||
let bcd_path = match table_type {
|
let bcd_path = match table_type {
|
||||||
PartitionTableType::Guid => {
|
PartitionTableType::Guid => {
|
||||||
format!("{letter_boot}:\\EFI\\Microsoft\\Boot\\BCD")
|
format!("{letter_boot}:\\EFI\\Microsoft\\Boot\\BCD")
|
||||||
|
|
@ -63,32 +105,24 @@ pub fn configure_disk(
|
||||||
format!("{letter_boot}:\\Boot\\BCD")
|
format!("{letter_boot}:\\Boot\\BCD")
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
tasks.push(Task::Command(
|
|
||||||
|
// Build CommandWait
|
||||||
|
let mut cmd_args = vec![String::from("/store"), bcd_path];
|
||||||
|
match mode {
|
||||||
|
SafeMode::Disable => {
|
||||||
|
cmd_args.push(String::from("/deletevalue"));
|
||||||
|
cmd_args.push(String::from("{default}"));
|
||||||
|
cmd_args.push(String::from("safeboot"));
|
||||||
|
}
|
||||||
|
SafeMode::Enable => {
|
||||||
|
cmd_args.push(String::from("/set"));
|
||||||
|
cmd_args.push(String::from("{default}"));
|
||||||
|
cmd_args.push(String::from("safeboot"));
|
||||||
|
cmd_args.push(String::from("minimal"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(TaskType::CommandWait(
|
||||||
PathBuf::from(format!("{system32}/bcdedit.exe")),
|
PathBuf::from(format!("{system32}/bcdedit.exe")),
|
||||||
vec![
|
cmd_args,
|
||||||
String::from("/store"),
|
|
||||||
bcd_path,
|
|
||||||
String::from("/set"),
|
|
||||||
String::from("{default}"),
|
|
||||||
String::from("safeboot"),
|
|
||||||
String::from("minimal"),
|
|
||||||
],
|
|
||||||
));
|
|
||||||
|
|
||||||
// Done
|
|
||||||
tasks
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn inject_driver(driver: &Driver, letter_os: &str, system32: &str) -> Result<Task> {
|
|
||||||
//if let Some(driver_path_str) = driver.path.to_str() {
|
|
||||||
let driver_path = driver.path.to_str().unwrap();
|
|
||||||
Ok(Task::Command(
|
|
||||||
PathBuf::from(format!("{system32}/dism.exe")),
|
|
||||||
vec![
|
|
||||||
format!("/image:{letter_os}:\\"),
|
|
||||||
String::from("/add-driver"),
|
|
||||||
format!("/driver:\"{}\"", driver_path,),
|
|
||||||
String::from("/recurse"),
|
|
||||||
],
|
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn get_cpu_name() -> String {
|
pub fn get_cpu_name() -> String {
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,31 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
|
sync::OnceLock,
|
||||||
thread::sleep,
|
thread::sleep,
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
|
||||||
use crate::system::diskpart;
|
use crate::system::diskpart::{self, REGEXES};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
|
||||||
pub struct Disk {
|
pub struct Disk {
|
||||||
|
|
@ -36,7 +36,6 @@ pub struct Disk {
|
||||||
pub part_type: PartitionTableType,
|
pub part_type: PartitionTableType,
|
||||||
pub parts: Vec<Partition>,
|
pub parts: Vec<Partition>,
|
||||||
pub parts_description: Vec<String>,
|
pub parts_description: Vec<String>,
|
||||||
pub sector_size: usize,
|
|
||||||
pub serial: String,
|
pub serial: String,
|
||||||
pub size: u64, // In bytes
|
pub size: u64, // In bytes
|
||||||
}
|
}
|
||||||
|
|
@ -67,6 +66,7 @@ impl Disk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_part_letter(&self, part_index: usize) -> String {
|
pub fn get_part_letter(&self, part_index: usize) -> String {
|
||||||
// Used to get Boot and OS letters
|
// Used to get Boot and OS letters
|
||||||
if let Some(part) = self.parts.get(part_index) {
|
if let Some(part) = self.parts.get(part_index) {
|
||||||
|
|
@ -76,13 +76,53 @@ impl Disk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn get_parts(&self) -> Vec<Partition> {
|
pub fn get_parts(&self) -> Vec<Partition> {
|
||||||
self.parts.clone()
|
self.parts.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn num_parts(&self) -> usize {
|
pub fn num_parts(&self) -> usize {
|
||||||
self.parts.len()
|
self.parts.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn refresh_disk_info(&mut self, details_str: Option<&str>) {
|
||||||
|
let re_details = REGEXES.detail_disk();
|
||||||
|
let re_uuid = REGEXES.uuid();
|
||||||
|
if cfg!(windows) {
|
||||||
|
info!("Refresh disk info via Diskpart");
|
||||||
|
// Get details
|
||||||
|
let details: String;
|
||||||
|
if let Some(s) = details_str {
|
||||||
|
details = String::from(s);
|
||||||
|
} else {
|
||||||
|
let script = format!("select disk {}\r\ndetail disk", self.id);
|
||||||
|
details = diskpart::run_script(&script);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Parse details
|
||||||
|
for (_, [model, part_type, conn_type]) in
|
||||||
|
re_details.captures_iter(&details).map(|c| c.extract())
|
||||||
|
{
|
||||||
|
self.model = String::from(model);
|
||||||
|
self.conn_type = String::from(conn_type);
|
||||||
|
if re_uuid.is_match(part_type) {
|
||||||
|
self.part_type = PartitionTableType::Guid;
|
||||||
|
} else {
|
||||||
|
self.part_type = PartitionTableType::Legacy;
|
||||||
|
}
|
||||||
|
self.serial = get_disk_serial_number(self.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Partition details
|
||||||
|
self.parts = diskpart::get_partitions(self.id, None, None);
|
||||||
|
self.generate_descriptions();
|
||||||
|
} else {
|
||||||
|
info!("Refresh fake disk info");
|
||||||
|
self.parts = refresh_fake_disk_info();
|
||||||
|
self.generate_descriptions();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Disk {
|
impl fmt::Display for Disk {
|
||||||
|
|
@ -103,6 +143,24 @@ impl fmt::Display for Disk {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Partition {
|
||||||
|
pub fn set_details(&mut self, part_type: &str, vol_line: &str) {
|
||||||
|
let re_list_volume = REGEXES.list_volumes();
|
||||||
|
|
||||||
|
// Partition info
|
||||||
|
self.part_type = String::from(part_type.trim());
|
||||||
|
|
||||||
|
// Volume info
|
||||||
|
for (_, [_id, letter, label, fs_type]) in
|
||||||
|
re_list_volume.captures_iter(vol_line).map(|c| c.extract())
|
||||||
|
{
|
||||||
|
self.label = String::from(label.trim());
|
||||||
|
self.letter = String::from(letter.trim());
|
||||||
|
self.fs_type = String::from(fs_type.trim());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Partition {
|
impl fmt::Display for Partition {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let mut s: String;
|
let mut s: String;
|
||||||
|
|
@ -160,9 +218,9 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 1,
|
id: 1,
|
||||||
fs_type: String::from("NTFS"),
|
fs_type: String::from("NTFS"),
|
||||||
label: String::from("System Reserved"),
|
label: String::from("System Reserved"),
|
||||||
|
letter: String::from("C"),
|
||||||
part_type: String::from("7"),
|
part_type: String::from("7"),
|
||||||
size: 104_857_600,
|
size: 104_857_600,
|
||||||
..Default::default()
|
|
||||||
},
|
},
|
||||||
Partition {
|
Partition {
|
||||||
id: 2,
|
id: 2,
|
||||||
|
|
@ -174,17 +232,17 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 5,
|
id: 5,
|
||||||
fs_type: String::from("NTFS"),
|
fs_type: String::from("NTFS"),
|
||||||
label: String::from("Win7"),
|
label: String::from("Win7"),
|
||||||
|
letter: String::from("D"),
|
||||||
part_type: String::from("7"),
|
part_type: String::from("7"),
|
||||||
size: 267_701_452_800,
|
size: 267_701_452_800,
|
||||||
..Default::default()
|
|
||||||
},
|
},
|
||||||
Partition {
|
Partition {
|
||||||
id: 6,
|
id: 6,
|
||||||
fs_type: String::from("NTFS"),
|
fs_type: String::from("NTFS"),
|
||||||
label: String::from("Tools"),
|
label: String::from("Tools"),
|
||||||
|
letter: String::from("E"),
|
||||||
part_type: String::from("7"),
|
part_type: String::from("7"),
|
||||||
size: 524_288_000,
|
size: 524_288_000,
|
||||||
..Default::default()
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
serial: "MDZ1243".to_string(),
|
serial: "MDZ1243".to_string(),
|
||||||
|
|
@ -200,9 +258,9 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 1,
|
id: 1,
|
||||||
fs_type: String::from("NTFS"),
|
fs_type: String::from("NTFS"),
|
||||||
label: String::from("Scratch"),
|
label: String::from("Scratch"),
|
||||||
|
letter: String::from("G"),
|
||||||
part_type: String::from("7"),
|
part_type: String::from("7"),
|
||||||
size: 249_998_951_424,
|
size: 249_998_951_424,
|
||||||
..Default::default()
|
|
||||||
}],
|
}],
|
||||||
serial: "000010000".to_string(),
|
serial: "000010000".to_string(),
|
||||||
size: 250_000_000_000,
|
size: 250_000_000_000,
|
||||||
|
|
@ -218,7 +276,7 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 1,
|
id: 1,
|
||||||
fs_type: String::from("FAT32"),
|
fs_type: String::from("FAT32"),
|
||||||
label: String::from("ESP"),
|
label: String::from("ESP"),
|
||||||
letter: String::from("Q"),
|
letter: String::from("J"),
|
||||||
part_type: String::from("EFI"),
|
part_type: String::from("EFI"),
|
||||||
size: 272_629_760,
|
size: 272_629_760,
|
||||||
},
|
},
|
||||||
|
|
@ -232,7 +290,7 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 4,
|
id: 4,
|
||||||
fs_type: String::from("NTFS"),
|
fs_type: String::from("NTFS"),
|
||||||
label: String::from("Win10"),
|
label: String::from("Win10"),
|
||||||
letter: String::from("V"),
|
letter: String::from("K"),
|
||||||
part_type: String::from("MS Basic Data"),
|
part_type: String::from("MS Basic Data"),
|
||||||
size: 824_340_119_552,
|
size: 824_340_119_552,
|
||||||
},
|
},
|
||||||
|
|
@ -251,9 +309,9 @@ pub fn get_fake_disks() -> Vec<Disk> {
|
||||||
id: 1,
|
id: 1,
|
||||||
fs_type: String::from("FAT32"),
|
fs_type: String::from("FAT32"),
|
||||||
label: String::from("EFI Boot"),
|
label: String::from("EFI Boot"),
|
||||||
|
letter: String::from("I"),
|
||||||
part_type: String::from("EFI"),
|
part_type: String::from("EFI"),
|
||||||
size: 209_715_200,
|
size: 209_715_200,
|
||||||
..Default::default()
|
|
||||||
},
|
},
|
||||||
Partition {
|
Partition {
|
||||||
id: 2,
|
id: 2,
|
||||||
|
|
@ -312,20 +370,6 @@ pub fn get_disk_serial_number(id: usize) -> String {
|
||||||
serial
|
serial
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh_disk_info(disk: &mut Disk) -> Disk {
|
|
||||||
let mut new_disk: Disk;
|
|
||||||
if cfg!(windows) {
|
|
||||||
info!("Refresh disk via Diskpart");
|
|
||||||
new_disk = diskpart::refresh_disk_info(disk);
|
|
||||||
} else {
|
|
||||||
info!("Refresh fake disk");
|
|
||||||
new_disk = disk.clone();
|
|
||||||
new_disk.parts = refresh_fake_disk_info();
|
|
||||||
new_disk.generate_descriptions();
|
|
||||||
}
|
|
||||||
new_disk
|
|
||||||
}
|
|
||||||
|
|
||||||
fn refresh_fake_disk_info() -> Vec<Partition> {
|
fn refresh_fake_disk_info() -> Vec<Partition> {
|
||||||
vec![
|
vec![
|
||||||
Partition {
|
Partition {
|
||||||
|
|
@ -382,10 +426,11 @@ pub fn bytes_to_string(size: u64) -> String {
|
||||||
///
|
///
|
||||||
/// Will panic if s is not simliar to 32B, 64MB, etc...
|
/// Will panic if s is not simliar to 32B, 64MB, etc...
|
||||||
pub fn string_to_bytes(s: &str) -> u64 {
|
pub fn string_to_bytes(s: &str) -> u64 {
|
||||||
static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"(\d+)\s+(\w+)B").unwrap());
|
static RE_BYTES: OnceLock<Regex> = OnceLock::new();
|
||||||
|
let re_bytes = RE_BYTES.get_or_init(|| Regex::new(r"(\d+)\s+(\w+)B").unwrap());
|
||||||
let base: u64 = 1024;
|
let base: u64 = 1024;
|
||||||
let mut size: u64 = 0;
|
let mut size: u64 = 0;
|
||||||
for (_, [size_str, suffix]) in RE.captures_iter(s).map(|c| c.extract()) {
|
for (_, [size_str, suffix]) in re_bytes.captures_iter(s).map(|c| c.extract()) {
|
||||||
let x: u64 = size_str.parse().unwrap();
|
let x: u64 = size_str.parse().unwrap();
|
||||||
size += x;
|
size += x;
|
||||||
match suffix {
|
match suffix {
|
||||||
|
|
|
||||||
|
|
@ -1,43 +1,119 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::Write,
|
io::Write,
|
||||||
process::{Command, Output, Stdio},
|
process::{Command, Output, Stdio},
|
||||||
|
sync::OnceLock,
|
||||||
};
|
};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use tempfile::tempdir;
|
use tempfile::tempdir;
|
||||||
use tracing::{info, warn};
|
use tracing::{info, warn};
|
||||||
|
|
||||||
use crate::system::disk::{
|
use crate::system::disk::{
|
||||||
get_disk_serial_number, string_to_bytes, Disk, Partition, PartitionTableType,
|
Disk, Partition, PartitionTableType, get_disk_serial_number, string_to_bytes,
|
||||||
};
|
};
|
||||||
|
|
||||||
static DEFAULT_MAX_DISKS: usize = 8;
|
static DEFAULT_MAX_DISKS: usize = 8;
|
||||||
|
|
||||||
|
pub struct RegexList {
|
||||||
|
detail_all_disks: OnceLock<Regex>,
|
||||||
|
detail_disk: OnceLock<Regex>,
|
||||||
|
detail_partition: OnceLock<Regex>,
|
||||||
|
disk_numbers: OnceLock<Regex>,
|
||||||
|
list_disk: OnceLock<Regex>,
|
||||||
|
list_partition: OnceLock<Regex>,
|
||||||
|
list_volumes: OnceLock<Regex>,
|
||||||
|
split_all_disks: OnceLock<Regex>,
|
||||||
|
uuid: OnceLock<Regex>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RegexList {
|
||||||
|
pub fn detail_all_disks(&self) -> &Regex {
|
||||||
|
self.detail_all_disks.get_or_init(|| {
|
||||||
|
Regex::new(r"(?s)Disk (\d+) is now the selected disk.*?\r?\n\s*\r?\n(.*)").unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn detail_disk(&self) -> &Regex {
|
||||||
|
self.detail_disk.get_or_init(|| {
|
||||||
|
Regex::new(r"(.*?)\r?\nDisk ID\s*:\s+(.*?)\r?\nType\s*:\s+(.*?)\r?\n").unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn detail_partition(&self) -> &Regex {
|
||||||
|
self.detail_partition
|
||||||
|
.get_or_init(|| Regex::new(r"Partition (\d+)\r?\nType\s*: (\S+)(\r?\n.*){5}\s*(Volume.*\r?\n.*\r?\n|There is no volume)(.*)").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn disk_numbers(&self) -> &Regex {
|
||||||
|
self.disk_numbers
|
||||||
|
.get_or_init(|| Regex::new(r"\s+Disk\s+(\d+).*\n.*\n.*\nDisk ID:").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_disk(&self) -> &Regex {
|
||||||
|
self.list_disk
|
||||||
|
.get_or_init(|| Regex::new(r"Disk\s+(\d+)\s+(\w+)\s+(\d+\s+\w+B)").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_partition(&self) -> &Regex {
|
||||||
|
self.list_partition
|
||||||
|
.get_or_init(|| Regex::new(r"Partition\s+(\d+)\s+\w+\s+(\d+\s+\w+B)").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn split_all_disks(&self) -> &Regex {
|
||||||
|
self.split_all_disks
|
||||||
|
.get_or_init(|| Regex::new(r"Disk \d+ is now the selected disk").unwrap())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn list_volumes(&self) -> &Regex {
|
||||||
|
self.list_volumes.get_or_init(|| {
|
||||||
|
// Volume ### Ltr Label Fs Type Size Status Info
|
||||||
|
// ---------- --- ----------- ----- ---------- ------- --------- --------
|
||||||
|
// * Volume 1 S ESP FAT32 Partition 100 MB Healthy Hidden
|
||||||
|
Regex::new(r"..Volume (\d.{2}) (.{3}) (.{11}) (.{5})").unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn uuid(&self) -> &Regex {
|
||||||
|
self.uuid.get_or_init(|| {
|
||||||
|
Regex::new(r"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$")
|
||||||
|
.unwrap()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub static REGEXES: RegexList = RegexList {
|
||||||
|
detail_all_disks: OnceLock::new(),
|
||||||
|
detail_disk: OnceLock::new(),
|
||||||
|
detail_partition: OnceLock::new(),
|
||||||
|
disk_numbers: OnceLock::new(),
|
||||||
|
list_disk: OnceLock::new(),
|
||||||
|
list_partition: OnceLock::new(),
|
||||||
|
list_volumes: OnceLock::new(),
|
||||||
|
split_all_disks: OnceLock::new(),
|
||||||
|
uuid: OnceLock::new(),
|
||||||
|
};
|
||||||
|
|
||||||
pub fn get_disk_details(disk_id: usize, disk_size: u64, disk_details: Option<&str>) -> Disk {
|
pub fn get_disk_details(disk_id: usize, disk_size: u64, disk_details: Option<&str>) -> Disk {
|
||||||
static RE_DETAILS: Lazy<Regex> = Lazy::new(|| {
|
let detail_disk = REGEXES.detail_disk();
|
||||||
Regex::new(r"(.*?)\r?\nDisk ID\s*:\s+(.*?)\r?\nType\s*:\s+(.*?)\r?\n").unwrap()
|
let re_uuid = REGEXES.uuid();
|
||||||
});
|
|
||||||
static RE_UUID: Lazy<Regex> = Lazy::new(|| {
|
|
||||||
Regex::new(r"^\{[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}\}$").unwrap()
|
|
||||||
});
|
|
||||||
let mut disk = Disk {
|
let mut disk = Disk {
|
||||||
id: disk_id,
|
id: disk_id,
|
||||||
size: disk_size,
|
size: disk_size,
|
||||||
|
|
@ -55,11 +131,11 @@ pub fn get_disk_details(disk_id: usize, disk_size: u64, disk_details: Option<&st
|
||||||
|
|
||||||
// Parse details
|
// Parse details
|
||||||
for (_, [model, part_type, conn_type]) in
|
for (_, [model, part_type, conn_type]) in
|
||||||
RE_DETAILS.captures_iter(&details).map(|c| c.extract())
|
detail_disk.captures_iter(&details).map(|c| c.extract())
|
||||||
{
|
{
|
||||||
disk.model = String::from(model);
|
disk.model = String::from(model);
|
||||||
disk.conn_type = String::from(conn_type);
|
disk.conn_type = String::from(conn_type);
|
||||||
if RE_UUID.is_match(part_type) {
|
if re_uuid.is_match(part_type) {
|
||||||
disk.part_type = PartitionTableType::Guid;
|
disk.part_type = PartitionTableType::Guid;
|
||||||
} else {
|
} else {
|
||||||
disk.part_type = PartitionTableType::Legacy;
|
disk.part_type = PartitionTableType::Legacy;
|
||||||
|
|
@ -71,13 +147,12 @@ pub fn get_disk_details(disk_id: usize, disk_size: u64, disk_details: Option<&st
|
||||||
disk
|
disk
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_partition_details(
|
pub fn get_partitions(
|
||||||
disk_id: usize,
|
disk_id: usize,
|
||||||
disk_details: Option<&str>,
|
disk_details: Option<&str>,
|
||||||
part_details: Option<&str>,
|
part_details: Option<&str>,
|
||||||
) -> Vec<Partition> {
|
) -> Vec<Partition> {
|
||||||
static RE_LIS: Lazy<Regex> =
|
let list_partitions = REGEXES.list_partition();
|
||||||
Lazy::new(|| Regex::new(r"Partition\s+(\d+)\s+\w+\s+(\d+\s+\w+B)").unwrap());
|
|
||||||
let mut parts = Vec::new();
|
let mut parts = Vec::new();
|
||||||
|
|
||||||
// List partition
|
// List partition
|
||||||
|
|
@ -88,7 +163,10 @@ pub fn get_partition_details(
|
||||||
let script = format!("select disk {disk_id}\r\nlist partition");
|
let script = format!("select disk {disk_id}\r\nlist partition");
|
||||||
contents = run_script(&script);
|
contents = run_script(&script);
|
||||||
};
|
};
|
||||||
for (_, [number, size]) in RE_LIS.captures_iter(&contents).map(|c| c.extract()) {
|
for (_, [number, size]) in list_partitions
|
||||||
|
.captures_iter(&contents)
|
||||||
|
.map(|c| c.extract())
|
||||||
|
{
|
||||||
let part_num = number.parse().unwrap();
|
let part_num = number.parse().unwrap();
|
||||||
if part_num != 0 {
|
if part_num != 0 {
|
||||||
// part_num == 0 is reserved for extended partition "containers" so we can exclude them
|
// part_num == 0 is reserved for extended partition "containers" so we can exclude them
|
||||||
|
|
@ -119,7 +197,7 @@ pub fn get_partition_details(
|
||||||
part_contents = String::from(details);
|
part_contents = String::from(details);
|
||||||
} else {
|
} else {
|
||||||
part_contents = run_script(script.join("\r\n").as_str());
|
part_contents = run_script(script.join("\r\n").as_str());
|
||||||
};
|
}
|
||||||
parse_partition_details(&mut parts, &part_contents);
|
parse_partition_details(&mut parts, &part_contents);
|
||||||
|
|
||||||
// Done
|
// Done
|
||||||
|
|
@ -129,7 +207,7 @@ pub fn get_partition_details(
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn build_dest_format_script(disk_id: usize, part_type: &PartitionTableType) -> String {
|
pub fn build_dest_format_script(disk_id: usize, part_type: &PartitionTableType) -> String {
|
||||||
let disk_id = format!("{disk_id}");
|
let disk_id = format!("{disk_id}");
|
||||||
let mut script = vec!["select disk {disk_id}", "clean"];
|
let mut script = vec!["automount enable noerr", "select disk {disk_id}", "clean"];
|
||||||
match part_type {
|
match part_type {
|
||||||
PartitionTableType::Guid => {
|
PartitionTableType::Guid => {
|
||||||
script.push("convert gpt");
|
script.push("convert gpt");
|
||||||
|
|
@ -148,13 +226,12 @@ pub fn build_dest_format_script(disk_id: usize, part_type: &PartitionTableType)
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
pub fn build_get_disk_script(disk_nums: Option<Vec<&str>>) -> String {
|
pub fn build_get_disk_script(disk_nums: Option<Vec<&str>>) -> String {
|
||||||
let capacity = DEFAULT_MAX_DISKS * 3 + 1;
|
|
||||||
let script: String;
|
let script: String;
|
||||||
|
|
||||||
// Get disk and partition details
|
// Get disk and partition details
|
||||||
if let Some(disks) = disk_nums {
|
if let Some(disks) = disk_nums {
|
||||||
// (Slower case)
|
// (Slower case)
|
||||||
let mut script_parts = Vec::with_capacity(capacity);
|
let mut script_parts = Vec::with_capacity(DEFAULT_MAX_DISKS * 3 + 1);
|
||||||
|
|
||||||
// Get list of disks
|
// Get list of disks
|
||||||
script_parts.push(String::from("list disk"));
|
script_parts.push(String::from("list disk"));
|
||||||
|
|
@ -170,7 +247,7 @@ pub fn build_get_disk_script(disk_nums: Option<Vec<&str>>) -> String {
|
||||||
script = script_parts.join("\n");
|
script = script_parts.join("\n");
|
||||||
} else {
|
} else {
|
||||||
// (Best case)
|
// (Best case)
|
||||||
let mut script_parts = Vec::with_capacity(capacity);
|
let mut script_parts = Vec::with_capacity(DEFAULT_MAX_DISKS + 1);
|
||||||
|
|
||||||
// Get list of disks
|
// Get list of disks
|
||||||
script_parts.push("list disk");
|
script_parts.push("list disk");
|
||||||
|
|
@ -180,9 +257,9 @@ pub fn build_get_disk_script(disk_nums: Option<Vec<&str>>) -> String {
|
||||||
script_parts.push("detail disk");
|
script_parts.push("detail disk");
|
||||||
script_parts.push("list partition");
|
script_parts.push("list partition");
|
||||||
|
|
||||||
// Limit to 8 disks (if there's more the manual "worst" case will be used)
|
// Limit to DEFAULT_MAX_DISKS (if there's more the manual "worst" case will be used)
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
while i < 8 {
|
while i < DEFAULT_MAX_DISKS {
|
||||||
script_parts.push("select disk next");
|
script_parts.push("select disk next");
|
||||||
script_parts.push("detail disk");
|
script_parts.push("detail disk");
|
||||||
script_parts.push("list partition");
|
script_parts.push("list partition");
|
||||||
|
|
@ -197,11 +274,8 @@ pub fn build_get_disk_script(disk_nums: Option<Vec<&str>>) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_disks() -> Vec<Disk> {
|
pub fn get_disks() -> Vec<Disk> {
|
||||||
static RE_DIS_DET: Lazy<Regex> = Lazy::new(|| {
|
let detail_all_disks = REGEXES.detail_all_disks();
|
||||||
Regex::new(r"(?s)Disk (\d+) is now the selected disk.*?\r?\n\s*\r?\n(.*)").unwrap()
|
let list_disk = REGEXES.list_disk();
|
||||||
});
|
|
||||||
static RE_DIS_LIS: Lazy<Regex> =
|
|
||||||
Lazy::new(|| Regex::new(r"Disk\s+(\d+)\s+(\w+)\s+(\d+\s+\w+B)").unwrap());
|
|
||||||
let mut contents: String;
|
let mut contents: String;
|
||||||
let mut output;
|
let mut output;
|
||||||
let mut script: String;
|
let mut script: String;
|
||||||
|
|
@ -212,7 +286,7 @@ pub fn get_disks() -> Vec<Disk> {
|
||||||
contents = String::from_utf8_lossy(&output.stdout).to_string();
|
contents = String::from_utf8_lossy(&output.stdout).to_string();
|
||||||
if let Some(return_code) = output.status.code() {
|
if let Some(return_code) = output.status.code() {
|
||||||
let disk_nums = parse_disk_numbers(&contents);
|
let disk_nums = parse_disk_numbers(&contents);
|
||||||
if return_code != 0 && !disk_nums.is_empty() {
|
if return_code != 0 && !disk_nums.is_empty() && disk_nums.len() != DEFAULT_MAX_DISKS {
|
||||||
// The base assumptions were correct! skipping fallback method
|
// The base assumptions were correct! skipping fallback method
|
||||||
//
|
//
|
||||||
// Since the return_code was not zero, and at least one disk was detected, that
|
// Since the return_code was not zero, and at least one disk was detected, that
|
||||||
|
|
@ -243,7 +317,7 @@ pub fn get_disks() -> Vec<Disk> {
|
||||||
// i.e. 0, 1, 3, 4
|
// i.e. 0, 1, 3, 4
|
||||||
// For instance, this can happen if a drive is disconnected after startup
|
// For instance, this can happen if a drive is disconnected after startup
|
||||||
let mut disks_map: HashMap<&str, Disk> = HashMap::with_capacity(DEFAULT_MAX_DISKS);
|
let mut disks_map: HashMap<&str, Disk> = HashMap::with_capacity(DEFAULT_MAX_DISKS);
|
||||||
for (_, [number, _status, size]) in RE_DIS_LIS
|
for (_, [number, _status, size]) in list_disk
|
||||||
.captures_iter(dp_sections.remove(0)) // This is the "list disk" section
|
.captures_iter(dp_sections.remove(0)) // This is the "list disk" section
|
||||||
.map(|c| c.extract())
|
.map(|c| c.extract())
|
||||||
{
|
{
|
||||||
|
|
@ -260,11 +334,11 @@ pub fn get_disks() -> Vec<Disk> {
|
||||||
// Add Disk details
|
// Add Disk details
|
||||||
let mut disks_raw: Vec<Disk> = Vec::with_capacity(DEFAULT_MAX_DISKS);
|
let mut disks_raw: Vec<Disk> = Vec::with_capacity(DEFAULT_MAX_DISKS);
|
||||||
for section in dp_sections {
|
for section in dp_sections {
|
||||||
for (_, [id, details]) in RE_DIS_DET.captures_iter(section).map(|c| c.extract()) {
|
for (_, [id, details]) in detail_all_disks.captures_iter(section).map(|c| c.extract()) {
|
||||||
if let Some(disk) = disks_map.remove(id) {
|
if let Some(disk) = disks_map.remove(id) {
|
||||||
// We remove the disk from the HashMap because we're moving it to the Vec
|
// We remove the disk from the HashMap because we're moving it to the Vec
|
||||||
let mut disk = get_disk_details(disk.id, disk.size, Some(details));
|
let mut disk = get_disk_details(disk.id, disk.size, Some(details));
|
||||||
disk.parts = get_partition_details(disk.id, Some(details), None);
|
disk.parts = get_partitions(disk.id, Some(details), None);
|
||||||
disk.generate_descriptions();
|
disk.generate_descriptions();
|
||||||
disks_raw.push(disk);
|
disks_raw.push(disk);
|
||||||
}
|
}
|
||||||
|
|
@ -280,59 +354,28 @@ pub fn parse_disk_numbers(contents: &str) -> Vec<&str> {
|
||||||
//
|
//
|
||||||
//Red Hat VirtIO SCSI Disk Device
|
//Red Hat VirtIO SCSI Disk Device
|
||||||
//Disk ID: {E9CE8DFA-46B2-43C1-99BB-850C661CEE6B}
|
//Disk ID: {E9CE8DFA-46B2-43C1-99BB-850C661CEE6B}
|
||||||
static RE: Lazy<Regex> =
|
let disk_numbers = REGEXES.disk_numbers();
|
||||||
Lazy::new(|| Regex::new(r"\s+Disk\s+(\d+).*\n.*\n.*\nDisk ID:").unwrap());
|
|
||||||
|
|
||||||
let mut disk_nums = Vec::new();
|
let mut disk_nums = Vec::new();
|
||||||
for (_, [number]) in RE.captures_iter(contents).map(|c| c.extract()) {
|
for (_, [number]) in disk_numbers.captures_iter(contents).map(|c| c.extract()) {
|
||||||
disk_nums.push(number);
|
disk_nums.push(number);
|
||||||
}
|
}
|
||||||
disk_nums
|
disk_nums
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_partition_details(parts: &mut [Partition], contents: &str) {
|
pub fn parse_partition_details(parts: &mut [Partition], contents: &str) {
|
||||||
static RE_PAR: Lazy<Regex> = Lazy::new(|| {
|
let detail_partition = REGEXES.detail_partition();
|
||||||
Regex::new(
|
|
||||||
r"Partition (\d+)\r?\nType\s*: (\S+)(\r?\n.*){5}\s*(Volume.*\r?\n.*\r?\n|There is no volume)(.*)",
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
});
|
|
||||||
static RE_VOL: Lazy<Regex> = Lazy::new(|| {
|
|
||||||
// Volume ### Ltr Label Fs Type Size Status Info
|
|
||||||
// ---------- --- ----------- ----- ---------- ------- --------- --------
|
|
||||||
// * Volume 1 S ESP FAT32 Partition 100 MB Healthy Hidden
|
|
||||||
Regex::new(r"..Volume (\d.{2}) (.{3}) (.{11}) (.{5})").unwrap()
|
|
||||||
});
|
|
||||||
|
|
||||||
for (part_index, (_, [_part_id, part_type, _, _vol_header, vol_line])) in RE_PAR
|
for (part_index, (_, [_part_id, part_type, _, _vol_header, vol_line])) in detail_partition
|
||||||
.captures_iter(contents)
|
.captures_iter(contents)
|
||||||
.map(|c| c.extract())
|
.map(|c| c.extract())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
{
|
{
|
||||||
if let Some(part) = parts.get_mut(part_index) {
|
if let Some(part) = parts.get_mut(part_index) {
|
||||||
// Partition info
|
part.set_details(part_type, vol_line);
|
||||||
part.part_type = String::from(part_type.trim());
|
|
||||||
|
|
||||||
// Volume info
|
|
||||||
for (_, [_id, letter, label, fs_type]) in
|
|
||||||
RE_VOL.captures_iter(vol_line).map(|c| c.extract())
|
|
||||||
{
|
|
||||||
part.label = String::from(label.trim());
|
|
||||||
part.letter = String::from(letter.trim());
|
|
||||||
part.fs_type = String::from(fs_type.trim());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh_disk_info(disk: &Disk) -> Disk {
|
|
||||||
info!("Refresh disk info");
|
|
||||||
let mut disk = get_disk_details(disk.id, disk.size, None);
|
|
||||||
disk.parts = get_partition_details(disk.id, None, None);
|
|
||||||
disk.generate_descriptions();
|
|
||||||
disk
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Will panic if diskpart fails to run the script or if the script if malformed
|
/// Will panic if diskpart fails to run the script or if the script if malformed
|
||||||
|
|
@ -344,12 +387,11 @@ pub fn run_script_raw(script: &str) -> Output {
|
||||||
script_file
|
script_file
|
||||||
.write_all(script.as_bytes())
|
.write_all(script.as_bytes())
|
||||||
.expect("Failed to write script to disk");
|
.expect("Failed to write script to disk");
|
||||||
let output = Command::new("diskpart")
|
Command::new("diskpart")
|
||||||
.args(["/s", format!("{}", script_path.display()).as_str()])
|
.args(["/s", format!("{}", script_path.display()).as_str()])
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.output()
|
.output()
|
||||||
.expect("Failed to execute Diskpart script");
|
.expect("Failed to execute Diskpart script")
|
||||||
output
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
|
|
@ -360,12 +402,11 @@ pub fn run_script(script: &str) -> String {
|
||||||
|
|
||||||
pub fn split_diskpart_disk_output(contents: &str) -> Vec<&str> {
|
pub fn split_diskpart_disk_output(contents: &str) -> Vec<&str> {
|
||||||
// NOTE: A simple split isn't helpful since we want to include the matching lines
|
// NOTE: A simple split isn't helpful since we want to include the matching lines
|
||||||
static RE: Lazy<Regex> =
|
let split_all_disks = REGEXES.split_all_disks();
|
||||||
Lazy::new(|| Regex::new(r"Disk \d+ is now the selected disk").unwrap());
|
|
||||||
let mut sections = Vec::new();
|
let mut sections = Vec::new();
|
||||||
let mut starts: Vec<usize> = vec![0];
|
let mut starts: Vec<usize> = vec![0];
|
||||||
let mut ends: Vec<usize> = Vec::new();
|
let mut ends: Vec<usize> = Vec::new();
|
||||||
let _: Vec<_> = RE
|
let _: Vec<_> = split_all_disks
|
||||||
.find_iter(contents)
|
.find_iter(contents)
|
||||||
.map(|m| {
|
.map(|m| {
|
||||||
ends.push(m.start() - 1);
|
ends.push(m.start() - 1);
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use std::{env, fmt, fs::read_dir, path::PathBuf, process::Command};
|
use std::{env, fmt, fs::read_dir, path::PathBuf, process::Command};
|
||||||
|
|
||||||
|
|
@ -73,14 +73,14 @@ pub fn scan() -> Vec<Driver> {
|
||||||
let driver_path = exe_path.with_file_name("drivers");
|
let driver_path = exe_path.with_file_name("drivers");
|
||||||
if let Ok(dir_entry) = read_dir(driver_path) {
|
if let Ok(dir_entry) = read_dir(driver_path) {
|
||||||
for entry in dir_entry.flatten() {
|
for entry in dir_entry.flatten() {
|
||||||
if entry.path().is_dir() {
|
if entry.path().is_dir()
|
||||||
if let Ok(name) = entry.file_name().into_string() {
|
&& let Ok(name) = entry.file_name().into_string()
|
||||||
drivers.push(Driver {
|
{
|
||||||
name,
|
drivers.push(Driver {
|
||||||
path: entry.path(),
|
name,
|
||||||
inf_paths: Vec::new(),
|
path: entry.path(),
|
||||||
});
|
inf_paths: Vec::new(),
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -95,10 +95,10 @@ pub fn scan() -> Vec<Driver> {
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(Result::ok)
|
.filter_map(Result::ok)
|
||||||
{
|
{
|
||||||
if let Some(ext) = entry.path().extension() {
|
if let Some(ext) = entry.path().extension()
|
||||||
if ext == "inf" {
|
&& ext == "inf"
|
||||||
driver.inf_paths.push(entry.into_path());
|
{
|
||||||
}
|
driver.inf_paths.push(entry.into_path());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,32 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
|
#![allow(clippy::missing_panics_doc)]
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
|
fmt,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{Command, Stdio},
|
process::{Command, Stdio},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
thread::{self, sleep, JoinHandle},
|
thread::{self, JoinHandle, sleep},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tracing::info;
|
use tracing::info;
|
||||||
|
|
||||||
|
|
@ -34,23 +37,76 @@ use crate::{
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub enum Task {
|
pub enum TaskResult {
|
||||||
Command(PathBuf, Vec<String>), // (command, args)
|
Error(String),
|
||||||
Diskpart(String), // (script_as_string)
|
Output(String, String, bool), // stdout, stderr, success
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||||
|
pub enum TaskType {
|
||||||
|
CommandNoWait(PathBuf, Vec<String>), // (command, args)
|
||||||
|
CommandWait(PathBuf, Vec<String>), // (command, args)
|
||||||
|
Diskpart(String), // (script_as_string)
|
||||||
ScanDisks,
|
ScanDisks,
|
||||||
Sleep,
|
Sleep,
|
||||||
|
TestPaths(Vec<PathBuf>),
|
||||||
UpdateDestDisk(usize), // (disk_index)
|
UpdateDestDisk(usize), // (disk_index)
|
||||||
UpdateDiskList,
|
UpdateDiskList,
|
||||||
|
GroupStart { label: String },
|
||||||
|
GroupEnd { label: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for TaskType {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
match self {
|
||||||
|
TaskType::CommandNoWait(cmd_path, _) | TaskType::CommandWait(cmd_path, _) => {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
"Command(\"{}\")",
|
||||||
|
cmd_path.file_name().unwrap().to_string_lossy()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
TaskType::Diskpart(_) => {
|
||||||
|
write!(f, "Diskpart")
|
||||||
|
}
|
||||||
|
TaskType::ScanDisks => write!(f, "ScanDisks"),
|
||||||
|
TaskType::Sleep => write!(f, "Sleep"),
|
||||||
|
TaskType::TestPaths(_) => write!(f, "TestPaths"),
|
||||||
|
TaskType::UpdateDestDisk(_) => write!(f, "UpdateDestDisk"),
|
||||||
|
TaskType::UpdateDiskList => write!(f, "UpdateDiskList"),
|
||||||
|
TaskType::GroupStart { label } => write!(f, "GroupStart({})", &label),
|
||||||
|
TaskType::GroupEnd { label } => write!(f, "GroupEnd({})", &label),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Task {
|
||||||
|
pub handle: Option<JoinHandle<()>>,
|
||||||
|
pub result: Option<TaskResult>,
|
||||||
|
pub task_type: TaskType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Task {
|
||||||
|
#[must_use]
|
||||||
|
pub fn new(task_type: TaskType) -> Task {
|
||||||
|
Task {
|
||||||
|
handle: None,
|
||||||
|
result: None,
|
||||||
|
task_type,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Tasks {
|
pub struct Tasks {
|
||||||
action_tx: mpsc::UnboundedSender<Action>,
|
action_tx: mpsc::UnboundedSender<Action>,
|
||||||
disk_list: Arc<Mutex<Vec<disk::Disk>>>,
|
disk_list: Arc<Mutex<Vec<disk::Disk>>>,
|
||||||
handle: Option<JoinHandle<()>>,
|
cur_handle: Option<JoinHandle<()>>,
|
||||||
|
cur_task: Option<Task>,
|
||||||
task_list: VecDeque<Task>,
|
task_list: VecDeque<Task>,
|
||||||
task_rx: mpsc::UnboundedReceiver<Action>, // Used to forward Actions from Tasks to App
|
task_rx: mpsc::UnboundedReceiver<TaskResult>,
|
||||||
task_tx: mpsc::UnboundedSender<Action>, // Used to forward Actions from Tasks to App
|
task_tx: mpsc::UnboundedSender<TaskResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tasks {
|
impl Tasks {
|
||||||
|
|
@ -62,159 +118,239 @@ impl Tasks {
|
||||||
Tasks {
|
Tasks {
|
||||||
action_tx,
|
action_tx,
|
||||||
disk_list: disk_list_arc,
|
disk_list: disk_list_arc,
|
||||||
handle: None,
|
cur_handle: None,
|
||||||
|
cur_task: None,
|
||||||
task_list: VecDeque::new(),
|
task_list: VecDeque::new(),
|
||||||
task_rx,
|
task_rx,
|
||||||
task_tx,
|
task_tx,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&mut self, task: Task) {
|
pub fn add(&mut self, task_type: TaskType) {
|
||||||
info!("Adding task: {:?}", &task);
|
info!("Adding task: {:?}", &task_type);
|
||||||
self.task_list.push_back(task);
|
self.task_list.push_back(Task::new(task_type));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_group(&mut self, group_label: &str, group_tasks: Vec<TaskType>) {
|
||||||
|
info!("Adding task group: {group_label}");
|
||||||
|
self.task_list.push_back(Task::new(TaskType::GroupStart {
|
||||||
|
label: group_label.to_string(),
|
||||||
|
}));
|
||||||
|
for task in group_tasks {
|
||||||
|
self.task_list.push_back(Task::new(task));
|
||||||
|
}
|
||||||
|
self.task_list.push_back(Task::new(TaskType::GroupEnd {
|
||||||
|
label: group_label.to_string(),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn idle(&self) -> bool {
|
pub fn idle(&self) -> bool {
|
||||||
self.handle.is_none()
|
self.cur_handle.is_none()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn poll(&mut self) -> Result<()> {
|
pub fn poll(&mut self) -> Result<Option<Task>> {
|
||||||
// Forward any actions to main app
|
let mut return_task: Option<Task> = None;
|
||||||
if let Ok(action) = self.task_rx.try_recv() {
|
// Handle task channel item(s)
|
||||||
let result = self.action_tx.send(action.clone());
|
if let Ok(result) = self.task_rx.try_recv()
|
||||||
assert!(result.is_ok(), "Failed to send Action: {action:?}");
|
&& let Some(mut task) = self.cur_task.take()
|
||||||
|
{
|
||||||
|
task.result.replace(result);
|
||||||
|
self.cur_task.replace(task);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check status of current task (if one is running).
|
// Check status of current task (if one is running).
|
||||||
// NOTE: Action::NextScreen is sent once all tasks are complete
|
// NOTE: Action::TasksComplete is sent once all tasks are complete
|
||||||
if let Some(handle) = self.handle.take() {
|
if let Some(task_handle) = self.cur_handle.take() {
|
||||||
if handle.is_finished() {
|
if task_handle.is_finished() {
|
||||||
|
// Need to return task with handle
|
||||||
|
if let Some(mut cur_task) = self.cur_task.take() {
|
||||||
|
cur_task.handle = Some(task_handle);
|
||||||
|
return_task = Some(cur_task);
|
||||||
|
}
|
||||||
if self.task_list.is_empty() {
|
if self.task_list.is_empty() {
|
||||||
// No tasks remain
|
// No tasks remain
|
||||||
self.task_tx.send(Action::NextScreen)?;
|
self.action_tx.send(Action::TasksComplete)?;
|
||||||
} else {
|
} else {
|
||||||
// Start next task
|
// Start next task
|
||||||
self.start()?;
|
self.start()?;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Task not complete, return handle
|
// TaskType not complete, return handle
|
||||||
self.handle = Some(handle);
|
self.cur_handle.replace(task_handle);
|
||||||
}
|
}
|
||||||
} else if !self.task_list.is_empty() {
|
} else if !self.task_list.is_empty() {
|
||||||
// No current task but one is available
|
// No current task but one is available
|
||||||
self.start()?;
|
self.start()?;
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(return_task)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start(&mut self) -> Result<()> {
|
pub fn start(&mut self) -> Result<()> {
|
||||||
if let Some(task) = self.task_list.pop_front() {
|
self.cur_task = self.task_list.pop_front();
|
||||||
let task_str = format!("{task:?}");
|
if let Some(task) = self.cur_task.take() {
|
||||||
let task_tx = self.task_tx.clone();
|
let task_tx = self.task_tx.clone();
|
||||||
match task {
|
match task.task_type {
|
||||||
Task::Command(ref cmd_path, ref cmd_args) => {
|
TaskType::CommandNoWait(ref cmd_path, ref cmd_args) => {
|
||||||
let cmd_path = cmd_path.clone();
|
self.cur_handle = None;
|
||||||
let cmd_args = cmd_args.clone();
|
run_task_command(cmd_path.clone(), cmd_args.clone(), task_tx);
|
||||||
if cfg!(windows) {
|
|
||||||
self.handle = Some(thread::spawn(move || {
|
|
||||||
let result = Command::new(cmd_path)
|
|
||||||
.args(cmd_args)
|
|
||||||
.stdout(Stdio::piped())
|
|
||||||
.output();
|
|
||||||
if let Some(action) = match result {
|
|
||||||
Ok(output) => {
|
|
||||||
if output.status.success() {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
// Command returned an error status
|
|
||||||
let mut msg = String::new();
|
|
||||||
if let Ok(stdout) = String::from_utf8(output.stdout) {
|
|
||||||
msg = String::from(stdout.trim());
|
|
||||||
}
|
|
||||||
if msg.is_empty() {
|
|
||||||
msg = String::from("Generic error");
|
|
||||||
}
|
|
||||||
Some(Action::Error(format!("Command failed: {msg}",)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
Some(Action::Error(format!("Failed to run command: {err:?}")))
|
|
||||||
}
|
|
||||||
} {
|
|
||||||
let msg = format!("{:?}", &action);
|
|
||||||
let result = task_tx.send(action);
|
|
||||||
assert!(result.is_ok(), "Failed to send Action: {msg}");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
// Simulate task if not running under Windows
|
|
||||||
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Task::Diskpart(ref script) => {
|
TaskType::CommandWait(ref cmd_path, ref cmd_args) => {
|
||||||
if cfg!(windows) {
|
self.cur_handle = Some(run_task_command(
|
||||||
let script = String::from(script);
|
cmd_path.clone(),
|
||||||
self.handle = Some(thread::spawn(move || {
|
cmd_args.clone(),
|
||||||
let output = diskpart::run_script_raw(script.as_str());
|
task_tx,
|
||||||
if !output.status.success()
|
));
|
||||||
&& task_tx
|
|
||||||
.send(Action::Error(String::from(
|
|
||||||
"Diskpart script returned an error",
|
|
||||||
)))
|
|
||||||
.is_err()
|
|
||||||
{
|
|
||||||
panic!("Failed to send Action: {task_str:?}");
|
|
||||||
}
|
|
||||||
}));
|
|
||||||
} else {
|
|
||||||
// Simulate task if not running under Windows
|
|
||||||
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Task::ScanDisks => {
|
TaskType::Diskpart(ref script) => {
|
||||||
|
self.cur_handle = Some(run_task_diskpart(script, task_tx));
|
||||||
|
}
|
||||||
|
TaskType::ScanDisks => {
|
||||||
let disk_list_arc = self.disk_list.clone();
|
let disk_list_arc = self.disk_list.clone();
|
||||||
|
|
||||||
// Queue UpdateDiskList for various components
|
// Queue UpdateDiskList for various components
|
||||||
self.add(Task::UpdateDiskList);
|
self.add(TaskType::UpdateDiskList);
|
||||||
|
|
||||||
self.handle = Some(thread::spawn(move || {
|
self.cur_handle = Some(thread::spawn(move || {
|
||||||
let mut disks = disk_list_arc.lock().unwrap();
|
let mut disks = disk_list_arc.lock().unwrap();
|
||||||
*disks = disk::get_disks();
|
*disks = disk::get_disks();
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Task::Sleep => {
|
TaskType::Sleep => {
|
||||||
self.handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
|
self.cur_handle = Some(thread::spawn(|| sleep(Duration::from_millis(250))));
|
||||||
}
|
}
|
||||||
Task::UpdateDestDisk(index) => {
|
TaskType::TestPaths(ref list) => {
|
||||||
|
self.cur_handle = Some(test_paths(list.clone(), task_tx.clone()));
|
||||||
|
}
|
||||||
|
TaskType::UpdateDestDisk(index) => {
|
||||||
self.action_tx.send(Action::DisplayPopup(
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
popup::Type::Info,
|
popup::Type::Info,
|
||||||
String::from("Refreshing disk info"),
|
String::from("Refreshing disk info"),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
// Queue UpdateDiskList for various components
|
// Queue UpdateDiskList for various components
|
||||||
self.add(Task::Sleep);
|
self.add(TaskType::Sleep);
|
||||||
self.add(Task::UpdateDiskList);
|
self.add(TaskType::UpdateDiskList);
|
||||||
|
|
||||||
// Update destination disk ~in-place
|
// Update destination disk ~in-place
|
||||||
let disk_list_arc = self.disk_list.clone();
|
let disk_list_arc = self.disk_list.clone();
|
||||||
self.handle = Some(thread::spawn(move || {
|
self.cur_handle = Some(thread::spawn(move || {
|
||||||
let mut disks = disk_list_arc.lock().unwrap();
|
let mut disks = disk_list_arc.lock().unwrap();
|
||||||
let old_disk = &mut disks[index];
|
disks[index].refresh_disk_info(None);
|
||||||
disks[index] = disk::refresh_disk_info(old_disk);
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Task::UpdateDiskList => {
|
TaskType::UpdateDiskList => {
|
||||||
let disks = self.disk_list.lock().unwrap();
|
let disks = self.disk_list.lock().unwrap();
|
||||||
let disks_copy = disks.clone();
|
let disks_copy = disks.clone();
|
||||||
let action_tx = self.action_tx.clone();
|
let action_tx = self.action_tx.clone();
|
||||||
self.handle = Some(thread::spawn(move || {
|
self.cur_handle = Some(thread::spawn(move || {
|
||||||
if let Err(err) = action_tx.send(Action::UpdateDiskList(disks_copy)) {
|
if let Err(err) = action_tx.send(Action::UpdateDiskList(disks_copy)) {
|
||||||
panic!("Failed to send Action: {err:?}");
|
panic!("Failed to send Action: {err:?}");
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
TaskType::GroupStart { ref label } => {
|
||||||
|
self.action_tx.send(Action::TaskGroupStart(label.clone()))?;
|
||||||
|
}
|
||||||
|
TaskType::GroupEnd { ref label } => {
|
||||||
|
self.action_tx.send(Action::DiagLineEnd {
|
||||||
|
text: label.clone(),
|
||||||
|
})?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// Done
|
||||||
|
self.cur_task.replace(task);
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn parse_bytes_as_str(bytes: Vec<u8>) -> String {
|
||||||
|
match String::from_utf8(bytes) {
|
||||||
|
Ok(s) => s.trim().to_string(),
|
||||||
|
Err(_) => String::from("Failed to parse bytes as UTF-8 text"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_task_command(
|
||||||
|
cmd_path: PathBuf,
|
||||||
|
cmd_args: Vec<String>,
|
||||||
|
task_tx: mpsc::UnboundedSender<TaskResult>,
|
||||||
|
) -> JoinHandle<()> {
|
||||||
|
if cfg!(windows) {
|
||||||
|
thread::spawn(move || {
|
||||||
|
let result = Command::new(cmd_path)
|
||||||
|
.args(cmd_args)
|
||||||
|
.stdout(Stdio::piped())
|
||||||
|
.output();
|
||||||
|
match result {
|
||||||
|
Err(e) => {
|
||||||
|
task_tx
|
||||||
|
.send(TaskResult::Error(format!("{:?}", &e)))
|
||||||
|
.expect("Failed to propegate error?");
|
||||||
|
}
|
||||||
|
Ok(output) => {
|
||||||
|
let stderr = parse_bytes_as_str(output.stderr.clone());
|
||||||
|
let stdout = parse_bytes_as_str(output.stdout.clone());
|
||||||
|
let task_result = TaskResult::Output(stdout, stderr, output.status.success());
|
||||||
|
let err_str = format!("Failed to send TaskResult: {:?}", &task_result);
|
||||||
|
task_tx
|
||||||
|
.send(task_result)
|
||||||
|
.unwrap_or_else(|_| panic!("{}", err_str));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Simulate task if not running under Windows
|
||||||
|
thread::spawn(|| sleep(Duration::from_millis(500)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_task_diskpart(script: &str, task_tx: mpsc::UnboundedSender<TaskResult>) -> JoinHandle<()> {
|
||||||
|
if cfg!(windows) {
|
||||||
|
let script = script.to_owned();
|
||||||
|
thread::spawn(move || {
|
||||||
|
let output = diskpart::run_script_raw(&script);
|
||||||
|
let stderr = parse_bytes_as_str(output.stderr.clone());
|
||||||
|
let stdout = parse_bytes_as_str(output.stdout.clone());
|
||||||
|
let task_result = TaskResult::Output(stdout, stderr, output.status.success());
|
||||||
|
let err_str = format!("Failed to send TaskResult: {:?}", &task_result);
|
||||||
|
task_tx
|
||||||
|
.send(task_result)
|
||||||
|
.unwrap_or_else(|_| panic!("{}", err_str));
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Simulate task if not running under Windows
|
||||||
|
thread::spawn(|| sleep(Duration::from_millis(250)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_paths(
|
||||||
|
path_list: Vec<PathBuf>,
|
||||||
|
task_tx: mpsc::UnboundedSender<TaskResult>,
|
||||||
|
) -> JoinHandle<()> {
|
||||||
|
thread::spawn(move || {
|
||||||
|
let mut missing_paths = Vec::new();
|
||||||
|
for path in path_list {
|
||||||
|
if !path.exists() {
|
||||||
|
missing_paths.push(String::from(path.to_string_lossy()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let task_result = if missing_paths.is_empty() {
|
||||||
|
// No missing paths
|
||||||
|
TaskResult::Output(String::from("OK"), String::new(), true)
|
||||||
|
} else {
|
||||||
|
TaskResult::Output(
|
||||||
|
String::from("Missing item(s)"),
|
||||||
|
missing_paths.join(",\n"),
|
||||||
|
false,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
let err_str = format!("Failed to send TaskResult: {:?}", &task_result);
|
||||||
|
task_tx
|
||||||
|
.send(task_result)
|
||||||
|
.unwrap_or_else(|_| panic!("{}", err_str));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
pub mod sample_output;
|
pub mod sample_output;
|
||||||
|
|
||||||
|
|
@ -37,7 +37,7 @@ mod diskpart {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get_partition_details() {
|
fn get_partitions() {
|
||||||
// Left
|
// Left
|
||||||
let partition_1 = system::disk::Partition {
|
let partition_1 = system::disk::Partition {
|
||||||
id: 1,
|
id: 1,
|
||||||
|
|
@ -60,7 +60,6 @@ mod diskpart {
|
||||||
letter: String::from("C"),
|
letter: String::from("C"),
|
||||||
part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"),
|
part_type: String::from("ebd0a0a2-b9e5-4433-87c0-68b6b72699c7"),
|
||||||
size: 50465865728,
|
size: 50465865728,
|
||||||
..Default::default()
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Right
|
// Right
|
||||||
|
|
@ -77,7 +76,7 @@ mod diskpart {
|
||||||
id: 4,
|
id: 4,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
disk.parts = system::diskpart::get_partition_details(
|
disk.parts = system::diskpart::get_partitions(
|
||||||
2,
|
2,
|
||||||
Some(sample_output::DETAIL_DISK_GPT),
|
Some(sample_output::DETAIL_DISK_GPT),
|
||||||
Some(sample_output::SELECT_PART_DETAIL_PARTS),
|
Some(sample_output::SELECT_PART_DETAIL_PARTS),
|
||||||
|
|
@ -92,7 +91,7 @@ mod diskpart {
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_disk_numbers() {
|
fn parse_disk_numbers() {
|
||||||
let disk_numbers =
|
let disk_numbers =
|
||||||
system::diskpart::parse_disk_numbers(&sample_output::LIST_DISK_DETAIL_DISKS);
|
system::diskpart::parse_disk_numbers(sample_output::LIST_DISK_DETAIL_DISKS);
|
||||||
assert_eq!(vec!["0", "2"], disk_numbers);
|
assert_eq!(vec!["0", "2"], disk_numbers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -111,7 +110,7 @@ mod diskpart {
|
||||||
|
|
||||||
system::diskpart::parse_partition_details(
|
system::diskpart::parse_partition_details(
|
||||||
&mut disk.parts,
|
&mut disk.parts,
|
||||||
&sample_output::SELECT_PART_DETAIL_ONE_PART,
|
sample_output::SELECT_PART_DETAIL_ONE_PART,
|
||||||
);
|
);
|
||||||
assert_eq!(partition_1, disk.parts[0]);
|
assert_eq!(partition_1, disk.parts[0]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
pub static DETAIL_DISK_GPT: &str = "Disk 2 is now the selected disk.
|
pub static DETAIL_DISK_GPT: &str = "Disk 2 is now the selected disk.
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
#![allow(clippy::missing_errors_doc)]
|
||||||
#![allow(dead_code)] // Remove this once you start using the code
|
#![allow(dead_code)] // Remove this once you start using the code
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
io::{stdout, Stdout},
|
io::{Stdout, stdout},
|
||||||
ops::{Deref, DerefMut},
|
ops::{Deref, DerefMut},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
@ -85,21 +85,25 @@ impl Tui {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn tick_rate(mut self, tick_rate: f64) -> Self {
|
pub fn tick_rate(mut self, tick_rate: f64) -> Self {
|
||||||
self.tick_rate = tick_rate;
|
self.tick_rate = tick_rate;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn frame_rate(mut self, frame_rate: f64) -> Self {
|
pub fn frame_rate(mut self, frame_rate: f64) -> Self {
|
||||||
self.frame_rate = frame_rate;
|
self.frame_rate = frame_rate;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn mouse(mut self, mouse: bool) -> Self {
|
pub fn mouse(mut self, mouse: bool) -> Self {
|
||||||
self.mouse = mouse;
|
self.mouse = mouse;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[must_use]
|
||||||
pub fn paste(mut self, paste: bool) -> Self {
|
pub fn paste(mut self, paste: bool) -> Self {
|
||||||
self.paste = paste;
|
self.paste = paste;
|
||||||
self
|
self
|
||||||
|
|
@ -135,7 +139,7 @@ impl Tui {
|
||||||
.expect("failed to send init event");
|
.expect("failed to send init event");
|
||||||
loop {
|
loop {
|
||||||
let event = tokio::select! {
|
let event = tokio::select! {
|
||||||
_ = cancellation_token.cancelled() => {
|
() = cancellation_token.cancelled() => {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ = tick_interval.tick() => Event::Tick,
|
_ = tick_interval.tick() => Event::Tick,
|
||||||
|
|
@ -148,7 +152,7 @@ impl Tui {
|
||||||
CrosstermEvent::FocusLost => Event::FocusLost,
|
CrosstermEvent::FocusLost => Event::FocusLost,
|
||||||
CrosstermEvent::FocusGained => Event::FocusGained,
|
CrosstermEvent::FocusGained => Event::FocusGained,
|
||||||
CrosstermEvent::Paste(s) => Event::Paste(s),
|
CrosstermEvent::Paste(s) => Event::Paste(s),
|
||||||
_ => continue, // ignore other events
|
CrosstermEvent::Key(_) => continue, // ignore other events
|
||||||
}
|
}
|
||||||
Some(Err(_)) => Event::Error,
|
Some(Err(_)) => Event::Error,
|
||||||
None => break, // the event stream has stopped and will not produce any more events
|
None => break, // the event stream has stopped and will not produce any more events
|
||||||
|
|
|
||||||
|
|
@ -1,27 +1,25 @@
|
||||||
# This file is part of Deja-vu.
|
# This file is part of Deja-Vu.
|
||||||
#
|
#
|
||||||
# Deja-vu is free software: you can redistribute it and/or modify it
|
# Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
# under the terms of the GNU General Public License as published by
|
# under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# Deja-vu is distributed in the hope that it will be useful, but
|
# Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
# See the GNU General Public License for more details.
|
# See the GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
# along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "deja-vu"
|
name = "deja-vu"
|
||||||
authors = ["2Shirt <2xShirt@gmail.com>"]
|
authors = ["2Shirt <2xShirt@gmail.com>"]
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
license = "GPL"
|
license = "GPL"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
core = { path = "../core" }
|
core = { path = "../core" }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
|
|
@ -41,6 +39,7 @@ tokio-util = "0.7.11"
|
||||||
tracing = "0.1.41"
|
tracing = "0.1.41"
|
||||||
tracing-error = "0.2.0"
|
tracing-error = "0.2.0"
|
||||||
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
||||||
|
check_elevation = "0.2.4"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
anyhow = "1.0.86"
|
anyhow = "1.0.86"
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,17 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,43 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
|
||||||
|
|
||||||
use core::{
|
use core::{
|
||||||
action::Action,
|
action::Action,
|
||||||
components::{
|
components::{
|
||||||
footer::Footer, fps::FpsCounter, left::Left, popup, right::Right, state::StatefulList,
|
Component,
|
||||||
title::Title, Component,
|
footer::Footer,
|
||||||
|
left::{Left, SelectionType},
|
||||||
|
popup,
|
||||||
|
right::Right,
|
||||||
|
state::StatefulList,
|
||||||
|
title::Title,
|
||||||
},
|
},
|
||||||
config::Config,
|
config::Config,
|
||||||
line::{get_disk_description_right, get_part_description, DVLine},
|
line::{DVLine, get_disk_description_right, get_part_description},
|
||||||
state::{CloneSettings, Mode},
|
state::Mode,
|
||||||
system::{
|
system::{
|
||||||
boot, cpu::get_cpu_name, disk::PartitionTableType, diskpart::build_dest_format_script,
|
boot, cpu::get_cpu_name, disk::PartitionTableType, diskpart::build_dest_format_script,
|
||||||
drivers,
|
drivers,
|
||||||
},
|
},
|
||||||
tasks::{Task, Tasks},
|
tasks::{Task, TaskResult, TaskType, Tasks},
|
||||||
tui::{Event, Tui},
|
tui::{Event, Tui},
|
||||||
};
|
};
|
||||||
use std::{
|
use std::{
|
||||||
env,
|
env,
|
||||||
iter::zip,
|
iter::zip,
|
||||||
|
path::PathBuf,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -46,6 +51,8 @@ use ratatui::{
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tracing::{debug, info};
|
use tracing::{debug, info};
|
||||||
|
|
||||||
|
use crate::state::State;
|
||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
// TUI
|
// TUI
|
||||||
action_rx: mpsc::UnboundedReceiver<Action>,
|
action_rx: mpsc::UnboundedReceiver<Action>,
|
||||||
|
|
@ -58,11 +65,11 @@ pub struct App {
|
||||||
should_suspend: bool,
|
should_suspend: bool,
|
||||||
tick_rate: f64,
|
tick_rate: f64,
|
||||||
// App
|
// App
|
||||||
clone: CloneSettings,
|
|
||||||
cur_mode: Mode,
|
cur_mode: Mode,
|
||||||
list: StatefulList<usize>,
|
list: StatefulList<usize>,
|
||||||
prev_mode: Mode,
|
prev_mode: Mode,
|
||||||
selections: Vec<Option<usize>>,
|
selections: Vec<Option<usize>>,
|
||||||
|
state: State,
|
||||||
tasks: Tasks,
|
tasks: Tasks,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,15 +77,13 @@ impl App {
|
||||||
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
|
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
|
||||||
let (action_tx, action_rx) = mpsc::unbounded_channel();
|
let (action_tx, action_rx) = mpsc::unbounded_channel();
|
||||||
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
||||||
let mut tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
||||||
tasks.add(Task::ScanDisks);
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
// TUI
|
// TUI
|
||||||
action_rx,
|
action_rx,
|
||||||
action_tx,
|
action_tx,
|
||||||
components: vec![
|
components: vec![
|
||||||
Box::new(Title::new()),
|
Box::new(Title::new("Clone Tool")),
|
||||||
Box::new(FpsCounter::new()),
|
|
||||||
Box::new(Left::new()),
|
Box::new(Left::new()),
|
||||||
Box::new(Right::new()),
|
Box::new(Right::new()),
|
||||||
Box::new(Footer::new()),
|
Box::new(Footer::new()),
|
||||||
|
|
@ -91,36 +96,46 @@ impl App {
|
||||||
should_suspend: false,
|
should_suspend: false,
|
||||||
tick_rate,
|
tick_rate,
|
||||||
// App
|
// App
|
||||||
clone: CloneSettings::new(disk_list_arc),
|
state: State::new(disk_list_arc),
|
||||||
cur_mode: Mode::ScanDisks,
|
cur_mode: Mode::default(),
|
||||||
list: StatefulList::default(),
|
list: StatefulList::default(),
|
||||||
prev_mode: Mode::ScanDisks,
|
prev_mode: Mode::default(),
|
||||||
selections: vec![None, None],
|
selections: vec![None, None],
|
||||||
tasks,
|
tasks,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prev_mode(&mut self) -> Option<Mode> {
|
pub fn prev_mode(&mut self) -> Option<Mode> {
|
||||||
let new_mode = match self.cur_mode {
|
match self.cur_mode {
|
||||||
|
Mode::Home => Some(Mode::Home),
|
||||||
Mode::Failed => Some(Mode::Failed),
|
Mode::Failed => Some(Mode::Failed),
|
||||||
Mode::Done => Some(Mode::Done),
|
Mode::Done => Some(Mode::Done),
|
||||||
Mode::SelectParts => Some(Mode::SelectParts),
|
Mode::SelectParts => Some(Mode::SelectParts),
|
||||||
Mode::Confirm => Some(Mode::SelectTableType),
|
Mode::Confirm => Some(Mode::SelectTableType),
|
||||||
Mode::SelectTableType => Some(Mode::SelectDisks),
|
Mode::SelectTableType => Some(Mode::SelectDisks),
|
||||||
Mode::SelectDisks => Some(Mode::ScanDisks),
|
Mode::SelectDisks => Some(Mode::ScanDisks),
|
||||||
//
|
// Disallowed moves
|
||||||
Mode::InstallDrivers
|
Mode::InstallDrivers
|
||||||
| Mode::ScanDisks
|
| Mode::ScanDisks
|
||||||
| Mode::PreClone
|
| Mode::PreClone
|
||||||
| Mode::Clone
|
| Mode::Clone
|
||||||
| Mode::PostClone => None,
|
| Mode::PostClone => None,
|
||||||
};
|
// Invalid states
|
||||||
new_mode
|
Mode::BootDiags
|
||||||
|
| Mode::BootScan
|
||||||
|
| Mode::BootSetup
|
||||||
|
| Mode::DiagMenu
|
||||||
|
| Mode::InjectDrivers
|
||||||
|
| Mode::LogView
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::Process
|
||||||
|
| Mode::SetBootMode => panic!("This shouldn't happen?"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_mode(&mut self) -> Option<Mode> {
|
pub fn next_mode(&mut self) -> Option<Mode> {
|
||||||
let new_mode = match self.cur_mode {
|
let new_mode = match self.cur_mode {
|
||||||
Mode::InstallDrivers => Mode::ScanDisks,
|
Mode::Home | Mode::InstallDrivers => Mode::ScanDisks,
|
||||||
Mode::ScanDisks => Mode::SelectDisks,
|
Mode::ScanDisks => Mode::SelectDisks,
|
||||||
Mode::SelectDisks => Mode::SelectTableType,
|
Mode::SelectDisks => Mode::SelectTableType,
|
||||||
Mode::SelectTableType => Mode::Confirm,
|
Mode::SelectTableType => Mode::Confirm,
|
||||||
|
|
@ -130,6 +145,16 @@ impl App {
|
||||||
Mode::SelectParts => Mode::PostClone,
|
Mode::SelectParts => Mode::PostClone,
|
||||||
Mode::PostClone | Mode::Done => Mode::Done,
|
Mode::PostClone | Mode::Done => Mode::Done,
|
||||||
Mode::Failed => Mode::Failed,
|
Mode::Failed => Mode::Failed,
|
||||||
|
// Invalid states
|
||||||
|
Mode::BootDiags
|
||||||
|
| Mode::BootScan
|
||||||
|
| Mode::BootSetup
|
||||||
|
| Mode::DiagMenu
|
||||||
|
| Mode::InjectDrivers
|
||||||
|
| Mode::LogView
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::Process
|
||||||
|
| Mode::SetBootMode => panic!("This shouldn't happen?"),
|
||||||
};
|
};
|
||||||
|
|
||||||
if new_mode == self.cur_mode {
|
if new_mode == self.cur_mode {
|
||||||
|
|
@ -144,11 +169,11 @@ impl App {
|
||||||
info!("Setting mode to {new_mode:?}");
|
info!("Setting mode to {new_mode:?}");
|
||||||
self.cur_mode = new_mode;
|
self.cur_mode = new_mode;
|
||||||
match new_mode {
|
match new_mode {
|
||||||
Mode::InstallDrivers => self.clone.scan_drivers(),
|
Mode::InstallDrivers => self.state.scan_drivers(),
|
||||||
Mode::ScanDisks => {
|
Mode::ScanDisks => {
|
||||||
self.prev_mode = self.cur_mode;
|
self.prev_mode = self.cur_mode;
|
||||||
if self.tasks.idle() {
|
if self.tasks.idle() {
|
||||||
self.tasks.add(Task::ScanDisks);
|
self.tasks.add(TaskType::ScanDisks);
|
||||||
}
|
}
|
||||||
self.action_tx.send(Action::DisplayPopup(
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
popup::Type::Info,
|
popup::Type::Info,
|
||||||
|
|
@ -161,14 +186,23 @@ impl App {
|
||||||
String::from("Formatting destination disk"),
|
String::from("Formatting destination disk"),
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
|
// Get System32 path
|
||||||
|
let system32 = get_system32_path(&self.action_tx);
|
||||||
|
|
||||||
|
// (Re)Enable volume mounting
|
||||||
|
self.tasks.add(TaskType::CommandWait(
|
||||||
|
PathBuf::from(format!("{system32}/mountvol.exe")),
|
||||||
|
vec![String::from("/e")],
|
||||||
|
));
|
||||||
|
|
||||||
// Build Diskpart script to format destination disk
|
// Build Diskpart script to format destination disk
|
||||||
let disk_list = self.clone.disk_list.lock().unwrap();
|
let disk_list = self.state.disk_list.lock().unwrap();
|
||||||
if let Some(disk_index) = self.clone.disk_index_dest {
|
if let Some(disk_index) = self.state.disk_index_dest
|
||||||
if let Some(disk) = disk_list.get(disk_index) {
|
&& let Some(disk) = disk_list.get(disk_index)
|
||||||
let table_type = self.clone.table_type.clone().unwrap();
|
{
|
||||||
let diskpart_script = build_dest_format_script(disk.id, &table_type);
|
let table_type = self.state.table_type.clone().unwrap();
|
||||||
self.tasks.add(Task::Diskpart(diskpart_script));
|
let diskpart_script = build_dest_format_script(disk.id, &table_type);
|
||||||
}
|
self.tasks.add(TaskType::Diskpart(diskpart_script));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mode::Clone => {
|
Mode::Clone => {
|
||||||
|
|
@ -176,12 +210,12 @@ impl App {
|
||||||
popup::Type::Info,
|
popup::Type::Info,
|
||||||
String::from("Running Clone Tool"),
|
String::from("Running Clone Tool"),
|
||||||
))?;
|
))?;
|
||||||
self.tasks.add(Task::Command(
|
self.tasks.add(TaskType::CommandWait(
|
||||||
self.config.clone_app_path.clone(),
|
self.config.clone_app_path.clone(),
|
||||||
Vec::new(),
|
Vec::new(),
|
||||||
));
|
));
|
||||||
if let Some(dest_index) = self.clone.disk_index_dest {
|
if let Some(dest_index) = self.state.disk_index_dest {
|
||||||
self.tasks.add(Task::UpdateDestDisk(dest_index));
|
self.tasks.add(TaskType::UpdateDestDisk(dest_index));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mode::PostClone => {
|
Mode::PostClone => {
|
||||||
|
|
@ -191,61 +225,52 @@ impl App {
|
||||||
))?;
|
))?;
|
||||||
|
|
||||||
// Get System32 path
|
// Get System32 path
|
||||||
let system32 = if cfg!(windows) {
|
let system32 = get_system32_path(&self.action_tx);
|
||||||
if let Ok(path) = env::var("SYSTEMROOT") {
|
|
||||||
format!("{path}/System32")
|
// Add actions
|
||||||
} else {
|
let disk_list = self.state.disk_list.lock().unwrap();
|
||||||
|
if let Some(disk_index) = self.state.disk_index_dest
|
||||||
|
&& let Some(disk) = disk_list.get(disk_index)
|
||||||
|
{
|
||||||
|
let table_type = self.state.table_type.clone().unwrap();
|
||||||
|
let letter_boot = disk.get_part_letter(self.state.part_index_boot.unwrap());
|
||||||
|
let letter_os = disk.get_part_letter(self.state.part_index_os.unwrap());
|
||||||
|
|
||||||
|
// Safety check
|
||||||
|
if letter_boot.is_empty() || letter_os.is_empty() {
|
||||||
self.action_tx.send(Action::Error(String::from(
|
self.action_tx.send(Action::Error(String::from(
|
||||||
"ERROR\n\n\nFailed to find SYSTEMROOT",
|
"ERROR\n\n\nFailed to get drive letters for the destination",
|
||||||
)))?;
|
)))?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
String::from(".")
|
|
||||||
};
|
|
||||||
|
|
||||||
// Add actions
|
// Create boot files
|
||||||
let disk_list = self.clone.disk_list.lock().unwrap();
|
for task in boot::configure_disk(
|
||||||
if let Some(disk_index) = self.clone.disk_index_dest {
|
&letter_boot,
|
||||||
if let Some(disk) = disk_list.get(disk_index) {
|
&letter_os,
|
||||||
let table_type = self.clone.table_type.clone().unwrap();
|
boot::SafeMode::Enable,
|
||||||
let letter_boot = disk.get_part_letter(self.clone.part_index_boot.unwrap());
|
&system32,
|
||||||
let letter_os = disk.get_part_letter(self.clone.part_index_os.unwrap());
|
&table_type,
|
||||||
|
) {
|
||||||
|
self.tasks.add(task);
|
||||||
|
}
|
||||||
|
|
||||||
// Safety check
|
// Inject driver(s) (if selected)
|
||||||
if letter_boot.is_empty() || letter_os.is_empty() {
|
if let Some(driver) = &self.state.driver {
|
||||||
self.action_tx.send(Action::Error(String::from(
|
if let Ok(task) = boot::inject_driver(driver, &letter_os, &system32) {
|
||||||
"ERROR\n\n\nFailed to get drive letters for the destination",
|
|
||||||
)))?;
|
|
||||||
return Ok(());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create boot files
|
|
||||||
for task in
|
|
||||||
boot::configure_disk(&letter_boot, &letter_os, &system32, table_type)
|
|
||||||
{
|
|
||||||
self.tasks.add(task);
|
self.tasks.add(task);
|
||||||
}
|
} else {
|
||||||
|
self.action_tx.send(Action::Error(format!(
|
||||||
// Inject driver(s) (if selected)
|
"Failed to inject driver:\n{}",
|
||||||
if let Some(driver) = &self.clone.driver {
|
driver.name
|
||||||
if let Ok(task) = boot::inject_driver(driver, &letter_os, &system32) {
|
)))?;
|
||||||
self.tasks.add(task);
|
|
||||||
} else {
|
|
||||||
self.action_tx.send(Action::Error(format!(
|
|
||||||
"Failed to inject driver:\n{}",
|
|
||||||
driver.name
|
|
||||||
)))?;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mode::Done => {
|
Mode::Done => {
|
||||||
self.action_tx.send(Action::DisplayPopup(
|
self.action_tx
|
||||||
popup::Type::Success,
|
.send(Action::DisplayPopup(popup::Type::Success, popup::fortune()))?;
|
||||||
String::from("COMPLETE\n\n\nThank you for using this tool!"),
|
|
||||||
))?;
|
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
@ -270,6 +295,7 @@ impl App {
|
||||||
}
|
}
|
||||||
|
|
||||||
let action_tx = self.action_tx.clone();
|
let action_tx = self.action_tx.clone();
|
||||||
|
action_tx.send(Action::SetMode(Mode::ScanDisks))?;
|
||||||
loop {
|
loop {
|
||||||
self.handle_events(&mut tui).await?;
|
self.handle_events(&mut tui).await?;
|
||||||
self.handle_actions(&mut tui)?;
|
self.handle_actions(&mut tui)?;
|
||||||
|
|
@ -339,12 +365,9 @@ impl App {
|
||||||
match action {
|
match action {
|
||||||
Action::Tick => {
|
Action::Tick => {
|
||||||
self.last_tick_key_events.drain(..);
|
self.last_tick_key_events.drain(..);
|
||||||
match self.cur_mode {
|
// Check background task(s)
|
||||||
Mode::ScanDisks | Mode::PreClone | Mode::Clone | Mode::PostClone => {
|
if let Some(task) = self.tasks.poll()? {
|
||||||
// Check background task
|
self.handle_task(&task)?;
|
||||||
self.tasks.poll()?; // Once all are complete Action::NextScreen is sent
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Action::Quit => self.should_quit = true,
|
Action::Quit => self.should_quit = true,
|
||||||
|
|
@ -365,6 +388,9 @@ impl App {
|
||||||
Mode::Confirm => {
|
Mode::Confirm => {
|
||||||
self.action_tx.send(Action::NextScreen)?;
|
self.action_tx.send(Action::NextScreen)?;
|
||||||
}
|
}
|
||||||
|
Mode::Done => {
|
||||||
|
self.action_tx.send(Action::Quit)?;
|
||||||
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
},
|
},
|
||||||
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
|
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
|
||||||
|
|
@ -389,29 +415,29 @@ impl App {
|
||||||
Action::Select(one, two) => {
|
Action::Select(one, two) => {
|
||||||
match self.cur_mode {
|
match self.cur_mode {
|
||||||
Mode::InstallDrivers => {
|
Mode::InstallDrivers => {
|
||||||
if let Some(index) = one {
|
if let Some(index) = one
|
||||||
if let Some(driver) = self.clone.driver_list.get(index).cloned() {
|
&& let Some(driver) = self.state.driver_list.get(index).cloned()
|
||||||
drivers::load(&driver.inf_paths);
|
{
|
||||||
self.clone.driver = Some(driver);
|
drivers::load(&driver.inf_paths);
|
||||||
}
|
self.state.driver = Some(driver);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mode::SelectDisks => {
|
Mode::SelectDisks => {
|
||||||
self.clone.disk_index_source = one;
|
self.state.disk_index_source = one;
|
||||||
self.clone.disk_index_dest = two;
|
self.state.disk_index_dest = two;
|
||||||
}
|
}
|
||||||
Mode::SelectParts => {
|
Mode::SelectParts => {
|
||||||
self.clone.part_index_boot = one;
|
self.state.part_index_boot = one;
|
||||||
self.clone.part_index_os = two;
|
self.state.part_index_os = two;
|
||||||
}
|
}
|
||||||
Mode::SelectTableType => {
|
Mode::SelectTableType => {
|
||||||
self.clone.table_type = {
|
self.state.table_type = {
|
||||||
if let Some(index) = one {
|
if let Some(index) = one {
|
||||||
match index {
|
match index {
|
||||||
0 => Some(PartitionTableType::Guid),
|
0 => Some(PartitionTableType::Guid),
|
||||||
1 => Some(PartitionTableType::Legacy),
|
1 => Some(PartitionTableType::Legacy),
|
||||||
index => {
|
index => {
|
||||||
panic!("Failed to select PartitionTableType: {}", index)
|
panic!("Failed to select PartitionTableType: {index}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -425,24 +451,25 @@ impl App {
|
||||||
self.selections[1] = two;
|
self.selections[1] = two;
|
||||||
}
|
}
|
||||||
Action::SetMode(new_mode) => {
|
Action::SetMode(new_mode) => {
|
||||||
|
// Clear TableType selection
|
||||||
|
match new_mode {
|
||||||
|
Mode::SelectDisks | Mode::SelectTableType => {
|
||||||
|
self.state.table_type = None;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
self.set_mode(new_mode)?;
|
self.set_mode(new_mode)?;
|
||||||
self.action_tx
|
self.action_tx
|
||||||
.send(Action::UpdateFooter(build_footer_string(self.cur_mode)))?;
|
.send(Action::UpdateFooter(build_footer_string(self.cur_mode)))?;
|
||||||
let (title, labels, items, select_one) = build_left_items(self, self.cur_mode);
|
self.action_tx.send(build_left_items(self, self.cur_mode))?;
|
||||||
self.action_tx
|
self.action_tx
|
||||||
.send(Action::UpdateLeft(title, labels, items, select_one))?;
|
.send(build_right_items(self, self.cur_mode))?;
|
||||||
let (labels, start, items) = build_right_items(self, self.cur_mode);
|
|
||||||
self.action_tx
|
|
||||||
.send(Action::UpdateRight(labels, start, items))?;
|
|
||||||
match new_mode {
|
match new_mode {
|
||||||
// Mode::InstallDrivers | Mode::SelectDisks => {
|
|
||||||
// self.action_tx.send(Action::Select(None, None))?
|
|
||||||
// }
|
|
||||||
Mode::SelectTableType | Mode::Confirm => {
|
Mode::SelectTableType | Mode::Confirm => {
|
||||||
// Select source/dest disks
|
// Select source/dest disks
|
||||||
self.action_tx.send(Action::SelectRight(
|
self.action_tx.send(Action::SelectRight(
|
||||||
self.clone.disk_index_source,
|
self.state.disk_index_source,
|
||||||
self.clone.disk_index_dest,
|
self.state.disk_index_dest,
|
||||||
))?;
|
))?;
|
||||||
}
|
}
|
||||||
Mode::SelectParts => {
|
Mode::SelectParts => {
|
||||||
|
|
@ -450,7 +477,7 @@ impl App {
|
||||||
self.action_tx.send(Action::Select(Some(0), None))?;
|
self.action_tx.send(Action::Select(Some(0), None))?;
|
||||||
|
|
||||||
// Highlight 2nd or 3rd partition as OS partition
|
// Highlight 2nd or 3rd partition as OS partition
|
||||||
let index = if let Some(table_type) = &self.clone.table_type {
|
let index = if let Some(table_type) = &self.state.table_type {
|
||||||
match table_type {
|
match table_type {
|
||||||
PartitionTableType::Guid => 2,
|
PartitionTableType::Guid => 2,
|
||||||
PartitionTableType::Legacy => 1,
|
PartitionTableType::Legacy => 1,
|
||||||
|
|
@ -463,6 +490,7 @@ impl App {
|
||||||
_ => {}
|
_ => {}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Action::TasksComplete => self.action_tx.send(Action::NextScreen)?,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
for component in &mut self.components {
|
for component in &mut self.components {
|
||||||
|
|
@ -480,14 +508,43 @@ impl App {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn handle_task(&mut self, task: &Task) -> Result<()> {
|
||||||
|
match task.task_type {
|
||||||
|
TaskType::CommandWait(_, _) | TaskType::Diskpart(_) => {
|
||||||
|
// Check result
|
||||||
|
if let Some(result) = &task.result {
|
||||||
|
match result {
|
||||||
|
TaskResult::Error(msg) => {
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::Error(format!("{} Failed: {msg}", task.task_type)))?;
|
||||||
|
}
|
||||||
|
TaskResult::Output(stdout, stderr, success) => {
|
||||||
|
if !success {
|
||||||
|
let msg = if !stdout.is_empty() {
|
||||||
|
stdout.clone()
|
||||||
|
} else if !stderr.is_empty() {
|
||||||
|
stderr.clone()
|
||||||
|
} else {
|
||||||
|
String::from("Unknown Error")
|
||||||
|
};
|
||||||
|
self.action_tx.send(Action::Error(format!(
|
||||||
|
"{} Failed: {msg}",
|
||||||
|
task.task_type
|
||||||
|
)))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn render(&mut self, tui: &mut Tui) -> Result<()> {
|
fn render(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
tui.draw(|frame| {
|
tui.draw(|frame| {
|
||||||
if let [header, _body, footer, left, right, popup] = get_chunks(frame.area())[..] {
|
if let [header, _body, footer, left, right, popup] = get_chunks(frame.area())[..] {
|
||||||
let component_areas = vec![
|
let component_areas = vec![header, left, right, footer, popup];
|
||||||
header, // Title Bar
|
|
||||||
header, // FPS Counter
|
|
||||||
left, right, footer, popup,
|
|
||||||
];
|
|
||||||
for (component, area) in zip(self.components.iter_mut(), component_areas) {
|
for (component, area) in zip(self.components.iter_mut(), component_areas) {
|
||||||
if let Err(err) = component.draw(frame, area) {
|
if let Err(err) = component.draw(frame, area) {
|
||||||
let _ = self
|
let _ = self
|
||||||
|
|
@ -557,72 +614,105 @@ fn get_chunks(r: Rect) -> Vec<Rect> {
|
||||||
|
|
||||||
fn build_footer_string(cur_mode: Mode) -> String {
|
fn build_footer_string(cur_mode: Mode) -> String {
|
||||||
match cur_mode {
|
match cur_mode {
|
||||||
Mode::ScanDisks | Mode::PreClone | Mode::Clone | Mode::PostClone => {
|
Mode::Home | Mode::ScanDisks | Mode::PreClone | Mode::Clone | Mode::PostClone => {
|
||||||
String::from("(q) to quit")
|
String::from("(q) to quit")
|
||||||
}
|
}
|
||||||
Mode::SelectParts => String::from("(Enter) to select / (s) to start over / (q) to quit"),
|
Mode::SelectParts => String::from("(Enter) to select / (s) to start over / (q) to quit"),
|
||||||
|
Mode::InstallDrivers => String::from("(Enter) to select / (q) to quit"),
|
||||||
Mode::SelectDisks => String::from(
|
Mode::SelectDisks => String::from(
|
||||||
"(Enter) to select / / (i) to install driver / (r) to rescan / (q) to quit",
|
"(Enter) to select / / (i) to install driver / (r) to rescan / (q) to quit",
|
||||||
),
|
),
|
||||||
Mode::SelectTableType => String::from("(Enter) to select / (b) to go back / (q) to quit"),
|
Mode::SelectTableType => String::from("(Enter) to select / (b) to go back / (q) to quit"),
|
||||||
Mode::Confirm => String::from("(Enter) to confirm / (b) to go back / (q) to quit"),
|
Mode::Confirm => String::from("(Enter) to confirm / (b) to go back / (q) to quit"),
|
||||||
Mode::Done | Mode::Failed | Mode::InstallDrivers => String::from("(Enter) or (q) to quit"),
|
Mode::Done | Mode::Failed => String::from("(Enter) or (q) to quit"),
|
||||||
|
// Invalid states
|
||||||
|
Mode::BootDiags
|
||||||
|
| Mode::BootScan
|
||||||
|
| Mode::BootSetup
|
||||||
|
| Mode::DiagMenu
|
||||||
|
| Mode::InjectDrivers
|
||||||
|
| Mode::LogView
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::Process
|
||||||
|
| Mode::SetBootMode => panic!("This shouldn't happen?"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn build_left_items(app: &App, cur_mode: Mode) -> (String, Vec<String>, Vec<String>, bool) {
|
|
||||||
|
fn build_left_items(app: &App, cur_mode: Mode) -> Action {
|
||||||
|
let select_type: SelectionType;
|
||||||
let title: String;
|
let title: String;
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
let mut labels: Vec<String> = Vec::new();
|
let mut labels: Vec<String> = Vec::new();
|
||||||
let mut select_one = false;
|
|
||||||
match cur_mode {
|
match cur_mode {
|
||||||
|
Mode::Home => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Home");
|
||||||
|
}
|
||||||
Mode::InstallDrivers => {
|
Mode::InstallDrivers => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
title = String::from("Install Drivers");
|
title = String::from("Install Drivers");
|
||||||
select_one = true;
|
app.state
|
||||||
app.clone
|
|
||||||
.driver_list
|
.driver_list
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|driver| items.push(driver.to_string()));
|
.for_each(|driver| items.push(driver.to_string()));
|
||||||
}
|
}
|
||||||
Mode::SelectDisks => {
|
Mode::SelectDisks => {
|
||||||
|
select_type = SelectionType::Two;
|
||||||
title = String::from("Select Source and Destination Disks");
|
title = String::from("Select Source and Destination Disks");
|
||||||
labels.push(String::from("source"));
|
labels.push(String::from("source"));
|
||||||
labels.push(String::from("dest"));
|
labels.push(String::from("dest"));
|
||||||
let disk_list = app.clone.disk_list.lock().unwrap();
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
disk_list
|
disk_list
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|disk| items.push(disk.description.to_string()));
|
.for_each(|disk| items.push(disk.description.to_string()));
|
||||||
}
|
}
|
||||||
Mode::SelectTableType => {
|
Mode::SelectTableType => {
|
||||||
|
select_type = SelectionType::One;
|
||||||
title = String::from("Select Partition Table Type");
|
title = String::from("Select Partition Table Type");
|
||||||
select_one = true;
|
|
||||||
items.push(format!("{}", PartitionTableType::Guid));
|
items.push(format!("{}", PartitionTableType::Guid));
|
||||||
items.push(format!("{}", PartitionTableType::Legacy));
|
items.push(format!("{}", PartitionTableType::Legacy));
|
||||||
}
|
}
|
||||||
Mode::Confirm => {
|
Mode::Confirm => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
title = String::from("Confirm Selections");
|
title = String::from("Confirm Selections");
|
||||||
}
|
}
|
||||||
Mode::PreClone | Mode::Clone | Mode::PostClone | Mode::ScanDisks => {
|
Mode::ScanDisks | Mode::PreClone | Mode::Clone | Mode::PostClone => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
title = String::from("Processing");
|
title = String::from("Processing");
|
||||||
}
|
}
|
||||||
Mode::SelectParts => {
|
Mode::SelectParts => {
|
||||||
|
select_type = SelectionType::Two;
|
||||||
title = String::from("Select Boot and OS Partitions");
|
title = String::from("Select Boot and OS Partitions");
|
||||||
labels.push(String::from("boot"));
|
labels.push(String::from("boot"));
|
||||||
labels.push(String::from("os"));
|
labels.push(String::from("os"));
|
||||||
let disk_list = app.clone.disk_list.lock().unwrap();
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
if let Some(index) = app.clone.disk_index_dest {
|
if let Some(index) = app.state.disk_index_dest
|
||||||
if let Some(disk) = disk_list.get(index) {
|
&& let Some(disk) = disk_list.get(index)
|
||||||
disk.get_parts().iter().for_each(|part| {
|
{
|
||||||
items.push(part.to_string());
|
disk.get_parts().iter().for_each(|part| {
|
||||||
});
|
items.push(part.to_string());
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Mode::Done | Mode::Failed => title = String::from("Done"),
|
Mode::Done | Mode::Failed => {
|
||||||
|
select_type = SelectionType::Loop;
|
||||||
|
title = String::from("Done");
|
||||||
|
}
|
||||||
|
// Invalid states
|
||||||
|
Mode::BootDiags
|
||||||
|
| Mode::BootScan
|
||||||
|
| Mode::BootSetup
|
||||||
|
| Mode::DiagMenu
|
||||||
|
| Mode::InjectDrivers
|
||||||
|
| Mode::LogView
|
||||||
|
| Mode::PEMenu
|
||||||
|
| Mode::Process
|
||||||
|
| Mode::SetBootMode => panic!("This shouldn't happen?"),
|
||||||
};
|
};
|
||||||
(title, labels, items, select_one)
|
Action::UpdateLeft(title, labels, items, select_type)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_right_items(app: &App, cur_mode: Mode) -> (Vec<Vec<DVLine>>, usize, Vec<Vec<DVLine>>) {
|
fn build_right_items(app: &App, cur_mode: Mode) -> Action {
|
||||||
let mut items = Vec::new();
|
let mut items = Vec::new();
|
||||||
let mut labels: Vec<Vec<DVLine>> = Vec::new();
|
let mut labels: Vec<Vec<DVLine>> = Vec::new();
|
||||||
let mut start_index = 0;
|
let mut start_index = 0;
|
||||||
|
|
@ -651,7 +741,7 @@ fn build_right_items(app: &App, cur_mode: Mode) -> (Vec<Vec<DVLine>>, usize, Vec
|
||||||
],
|
],
|
||||||
line_colors: vec![Color::Cyan, Color::Red],
|
line_colors: vec![Color::Cyan, Color::Red],
|
||||||
};
|
};
|
||||||
if let Some(table_type) = &app.clone.table_type {
|
if let Some(table_type) = &app.state.table_type {
|
||||||
// Show table type
|
// Show table type
|
||||||
let type_str = match table_type {
|
let type_str = match table_type {
|
||||||
PartitionTableType::Guid => "GPT",
|
PartitionTableType::Guid => "GPT",
|
||||||
|
|
@ -667,33 +757,49 @@ fn build_right_items(app: &App, cur_mode: Mode) -> (Vec<Vec<DVLine>>, usize, Vec
|
||||||
} else {
|
} else {
|
||||||
labels.push(vec![dest_dv_line]);
|
labels.push(vec![dest_dv_line]);
|
||||||
}
|
}
|
||||||
let disk_list = app.clone.disk_list.lock().unwrap();
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
disk_list
|
disk_list
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|disk| items.push(get_disk_description_right(&disk)));
|
.for_each(|disk| items.push(get_disk_description_right(disk, &None)));
|
||||||
}
|
}
|
||||||
Mode::SelectParts => {
|
Mode::SelectParts => {
|
||||||
vec!["Boot", "OS"].iter().for_each(|s| {
|
for s in &["Boot", "OS"] {
|
||||||
labels.push(vec![DVLine {
|
labels.push(vec![DVLine {
|
||||||
line_parts: vec![String::from(*s)],
|
line_parts: vec![String::from(*s)],
|
||||||
line_colors: vec![Color::Cyan],
|
line_colors: vec![Color::Cyan],
|
||||||
}])
|
}]);
|
||||||
});
|
}
|
||||||
if let Some(index) = app.clone.disk_index_dest {
|
if let Some(index) = app.state.disk_index_dest {
|
||||||
start_index = 1;
|
start_index = 1;
|
||||||
let disk_list = app.clone.disk_list.lock().unwrap();
|
let disk_list = app.state.disk_list.lock().unwrap();
|
||||||
if let Some(disk) = disk_list.get(index) {
|
if let Some(disk) = disk_list.get(index) {
|
||||||
// Disk Details
|
// Disk Details
|
||||||
items.push(get_disk_description_right(&disk));
|
items.push(get_disk_description_right(disk, &None));
|
||||||
|
|
||||||
// Partition Details
|
// Partition Details
|
||||||
disk.parts
|
disk.parts
|
||||||
.iter()
|
.iter()
|
||||||
.for_each(|part| items.push(get_part_description(&part)));
|
.for_each(|part| items.push(get_part_description(part)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
(labels, start_index, items)
|
Action::UpdateRight(labels, start_index, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_system32_path(action_tx: &mpsc::UnboundedSender<Action>) -> String {
|
||||||
|
let mut system32_path = String::from(".");
|
||||||
|
if cfg!(windows) {
|
||||||
|
if let Ok(path) = env::var("SYSTEMROOT") {
|
||||||
|
system32_path = format!("{path}/System32");
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(String::from(
|
||||||
|
"ERROR\n\n\nFailed to find SYSTEMROOT",
|
||||||
|
)))
|
||||||
|
.expect("Failed to find SYSTEMROOT and then failed to send action");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
system32_path
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,44 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use color_eyre::Result;
|
use color_eyre::Result;
|
||||||
use core;
|
|
||||||
|
|
||||||
use crate::app::App;
|
use crate::app::App;
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
|
mod state;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
core::errors::init()?;
|
let mut msg = None;
|
||||||
core::logging::init()?;
|
if cfg!(windows) {
|
||||||
|
use check_elevation::is_elevated;
|
||||||
|
if !is_elevated().expect("Failed to get elevation status.") {
|
||||||
|
msg.replace("Administrator privedges required for Deja-Vu.");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if let Some(text) = msg {
|
||||||
|
println!("{text}");
|
||||||
|
} else {
|
||||||
|
core::errors::init()?;
|
||||||
|
core::logging::init()?;
|
||||||
|
|
||||||
let args = core::cli::Cli::parse();
|
let args = core::cli::Cli::parse();
|
||||||
let mut app = App::new(args.tick_rate, args.frame_rate)?;
|
let mut app = App::new(args.tick_rate, args.frame_rate)?;
|
||||||
app.run().await?;
|
app.run().await?;
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
47
deja_vu/src/state.rs
Normal file
47
deja_vu/src/state.rs
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use core::system::{
|
||||||
|
disk::{Disk, PartitionTableType},
|
||||||
|
drivers,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct State {
|
||||||
|
pub disk_index_dest: Option<usize>,
|
||||||
|
pub disk_index_source: Option<usize>,
|
||||||
|
pub disk_list: Arc<Mutex<Vec<Disk>>>,
|
||||||
|
pub driver_list: Vec<drivers::Driver>,
|
||||||
|
pub part_index_boot: Option<usize>,
|
||||||
|
pub part_index_os: Option<usize>,
|
||||||
|
pub driver: Option<drivers::Driver>,
|
||||||
|
pub table_type: Option<PartitionTableType>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl State {
|
||||||
|
pub fn new(disk_list: Arc<Mutex<Vec<Disk>>>) -> Self {
|
||||||
|
State {
|
||||||
|
disk_list,
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn scan_drivers(&mut self) {
|
||||||
|
self.driver_list = drivers::scan();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
name = 'Deja-Vu'
|
name = 'Deja-Vu'
|
||||||
command = 'X:\tools\deja-vu.exe'
|
command = 'X:\tools\deja-vu.exe'
|
||||||
|
description = "Windows clone assistant tool"
|
||||||
use_conemu = true
|
use_conemu = true
|
||||||
separator = false
|
separator = false
|
||||||
5
include/menu_entries/11_boot-diags.toml
Normal file
5
include/menu_entries/11_boot-diags.toml
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
name = 'Boot-Diagnostics'
|
||||||
|
command = 'X:\tools\boot-diags.exe'
|
||||||
|
description = "Boot issue assessment tool"
|
||||||
|
use_conemu = true
|
||||||
|
separator = false
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
name = ''
|
name = ''
|
||||||
command = ''
|
command = ''
|
||||||
|
description = ''
|
||||||
use_conemu = false
|
use_conemu = false
|
||||||
separator = true
|
separator = true
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
name = 'NTPWEdit'
|
name = 'NTPWEdit'
|
||||||
command = 'X:\Program Files\NTPWEdit\ntpwedit.exe'
|
command = 'X:\Program Files\NTPWEdit\ntpwedit.exe'
|
||||||
|
description = 'Mostly used to unlock the built-in admin account'
|
||||||
use_conemu = false
|
use_conemu = false
|
||||||
separator = false
|
separator = false
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
name = 'Some Clone Tool'
|
name = 'Some Clone Tool'
|
||||||
command = 'X:\Program Files\Some\Tool.exe'
|
command = 'X:\Program Files\Some\Tool.exe'
|
||||||
|
description = 'Run Some Clone tool'
|
||||||
use_conemu = false
|
use_conemu = false
|
||||||
separator = false
|
separator = false
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
name = 'Task Manager'
|
name = 'Task Manager'
|
||||||
command = 'X:\Windows\System32\taskmgr.exe'
|
command = 'X:\Windows\System32\taskmgr.exe'
|
||||||
|
description = 'Manage those tasks'
|
||||||
use_conemu = false
|
use_conemu = false
|
||||||
separator = false
|
separator = false
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
# This file is part of Deja-vu.
|
|
||||||
#
|
|
||||||
# Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
# under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
# See the GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
con_emu = 'X:\Program Files\ConEmu\ConEmu64.exe'
|
|
||||||
tools = []
|
|
||||||
BIN
logo.png
Normal file
BIN
logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 60 KiB |
BIN
logo.xcf
Normal file
BIN
logo.xcf
Normal file
Binary file not shown.
|
|
@ -1,29 +1,46 @@
|
||||||
# This file is part of Deja-vu.
|
# This file is part of Deja-Vu.
|
||||||
#
|
#
|
||||||
# Deja-vu is free software: you can redistribute it and/or modify it
|
# Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
# under the terms of the GNU General Public License as published by
|
# under the terms of the GNU General Public License as published by
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
# (at your option) any later version.
|
# (at your option) any later version.
|
||||||
#
|
#
|
||||||
# Deja-vu is distributed in the hope that it will be useful, but
|
# Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
# See the GNU General Public License for more details.
|
# See the GNU General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
# along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
[package]
|
[package]
|
||||||
name = "pe-menu"
|
name = "pe-menu"
|
||||||
authors = ["2Shirt <2xShirt@gmail.com>"]
|
authors = ["2Shirt <2xShirt@gmail.com>"]
|
||||||
edition = "2021"
|
edition = "2024"
|
||||||
license = "GPL"
|
license = "GPL"
|
||||||
version = "0.2.0"
|
version = "0.2.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
core = { path = "../core" }
|
||||||
|
clap = { version = "4.4.5", features = [
|
||||||
|
"derive",
|
||||||
|
"cargo",
|
||||||
|
"wrap_help",
|
||||||
|
"unicode",
|
||||||
|
"string",
|
||||||
|
"unstable-styles",
|
||||||
|
] }
|
||||||
|
color-eyre = "0.6.3"
|
||||||
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
crossterm = { version = "0.28.1", features = ["event-stream"] }
|
||||||
futures = "0.3.30"
|
futures = "0.3.30"
|
||||||
ratatui = "0.29.0"
|
ratatui = "0.29.0"
|
||||||
serde = { version = "1.0.217", features = ["derive"] }
|
serde = { version = "1.0.217", features = ["derive"] }
|
||||||
tokio = { version = "1.43.0", features = ["full"] }
|
tokio = { version = "1.43.0", features = ["full"] }
|
||||||
toml = "0.8.13"
|
toml = "0.8.13"
|
||||||
|
tracing = "0.1.41"
|
||||||
|
tracing-error = "0.2.0"
|
||||||
|
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde"] }
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
anyhow = "1.0.86"
|
||||||
|
vergen-gix = { version = "1.0.0", features = ["build", "cargo"] }
|
||||||
|
|
|
||||||
28
pe_menu/build.rs
Normal file
28
pe_menu/build.rs
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
// This file is part of Deja-Vu.
|
||||||
|
//
|
||||||
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
|
// under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
|
// See the GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
use anyhow::Result;
|
||||||
|
use vergen_gix::{BuildBuilder, CargoBuilder, Emitter, GixBuilder};
|
||||||
|
|
||||||
|
fn main() -> Result<()> {
|
||||||
|
let build = BuildBuilder::all_build()?;
|
||||||
|
let gix = GixBuilder::all_git()?;
|
||||||
|
let cargo = CargoBuilder::all_cargo()?;
|
||||||
|
Emitter::default()
|
||||||
|
.add_instructions(&build)?
|
||||||
|
.add_instructions(&gix)?
|
||||||
|
.add_instructions(&cargo)?
|
||||||
|
.emit()
|
||||||
|
}
|
||||||
|
|
@ -1,366 +1,485 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use ratatui::widgets::ListState;
|
use core::{
|
||||||
use serde::Deserialize;
|
action::Action,
|
||||||
|
components::{
|
||||||
|
Component,
|
||||||
|
footer::Footer,
|
||||||
|
left::{Left, SelectionType},
|
||||||
|
popup,
|
||||||
|
right::Right,
|
||||||
|
state::StatefulList,
|
||||||
|
title::Title,
|
||||||
|
},
|
||||||
|
config::Config,
|
||||||
|
line::DVLine,
|
||||||
|
state::Mode,
|
||||||
|
tasks::{TaskType, Tasks},
|
||||||
|
tui::{Event, Tui},
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
env, error, fs, io,
|
env, fs,
|
||||||
|
iter::zip,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{Command, Output},
|
sync::{Arc, Mutex},
|
||||||
thread::{self, JoinHandle},
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Application result type.
|
use color_eyre::Result;
|
||||||
#[allow(clippy::module_name_repetitions)]
|
use ratatui::{
|
||||||
pub type AppResult<T> = std::result::Result<T, Box<dyn error::Error>>;
|
crossterm::event::KeyEvent,
|
||||||
|
layout::{Constraint, Direction, Layout},
|
||||||
|
prelude::Rect,
|
||||||
|
style::Color,
|
||||||
|
};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use tokio::sync::mpsc;
|
||||||
|
use tracing::{debug, info};
|
||||||
|
|
||||||
/// Application exit reasons
|
#[derive(Clone, Debug, Default, Deserialize)]
|
||||||
#[derive(Debug, Default)]
|
|
||||||
pub enum QuitReason {
|
|
||||||
#[default]
|
|
||||||
Exit,
|
|
||||||
Poweroff,
|
|
||||||
Restart,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Config
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct Config {
|
|
||||||
con_emu: String,
|
|
||||||
tools: Vec<Tool>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Config {
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic for many reasons
|
|
||||||
#[must_use]
|
|
||||||
pub fn load() -> Option<Config> {
|
|
||||||
// Main config
|
|
||||||
let exe_path = env::current_exe().expect("Failed to find main executable");
|
|
||||||
let contents = fs::read_to_string(exe_path.with_file_name("pe-menu.toml"))
|
|
||||||
.expect("Failed to load config file");
|
|
||||||
let mut new_config: Config =
|
|
||||||
toml::from_str(&contents).expect("Failed to parse config file");
|
|
||||||
|
|
||||||
// Tools
|
|
||||||
let tool_config_path = exe_path.parent().unwrap().join("menu_entries");
|
|
||||||
let mut entries: Vec<PathBuf> = std::fs::read_dir(tool_config_path)
|
|
||||||
.expect("Failed to find any tool configs")
|
|
||||||
.map(|res| res.map(|e| e.path()))
|
|
||||||
.filter_map(Result::ok)
|
|
||||||
.collect();
|
|
||||||
entries.sort();
|
|
||||||
for entry in entries {
|
|
||||||
let contents = fs::read_to_string(&entry).expect("Failed to read tool config file");
|
|
||||||
let tool: Tool = toml::from_str(&contents).expect("Failed to parse tool config file");
|
|
||||||
new_config.tools.push(tool);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Done
|
|
||||||
Some(new_config)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `PopUp`
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct PopUp {
|
|
||||||
pub title: String,
|
|
||||||
pub body: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PopUp {
|
|
||||||
#[must_use]
|
|
||||||
pub fn new(title: &str, body: &str) -> PopUp {
|
|
||||||
PopUp {
|
|
||||||
title: String::from(title),
|
|
||||||
body: String::from(body),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `Tool`
|
|
||||||
#[derive(Debug, Deserialize)]
|
|
||||||
pub struct Tool {
|
pub struct Tool {
|
||||||
name: String,
|
name: String,
|
||||||
command: String,
|
command: String,
|
||||||
|
description: String,
|
||||||
args: Option<Vec<String>>,
|
args: Option<Vec<String>>,
|
||||||
use_conemu: bool,
|
use_conemu: bool,
|
||||||
separator: bool,
|
separator: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `MenuEntry`
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq)]
|
|
||||||
pub struct MenuEntry {
|
|
||||||
pub name: String,
|
|
||||||
pub command: String,
|
|
||||||
pub args: Vec<String>,
|
|
||||||
pub use_conemu: bool,
|
|
||||||
pub separator: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MenuEntry {
|
|
||||||
#[must_use]
|
|
||||||
pub fn new(
|
|
||||||
name: &str,
|
|
||||||
command: &str,
|
|
||||||
args: Option<Vec<String>>,
|
|
||||||
use_conemu: bool,
|
|
||||||
separator: bool,
|
|
||||||
) -> MenuEntry {
|
|
||||||
let mut my_args = Vec::new();
|
|
||||||
if let Some(a) = args {
|
|
||||||
my_args.clone_from(&a);
|
|
||||||
}
|
|
||||||
MenuEntry {
|
|
||||||
name: String::from(name),
|
|
||||||
command: String::from(command),
|
|
||||||
args: my_args,
|
|
||||||
use_conemu,
|
|
||||||
separator,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `StatefulList`
|
|
||||||
#[derive(Default, Debug, Clone, PartialEq)]
|
|
||||||
pub struct StatefulList<T> {
|
|
||||||
pub state: ListState,
|
|
||||||
pub items: Vec<T>,
|
|
||||||
pub last_selected: Option<usize>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T: Clone> StatefulList<T> {
|
|
||||||
#[must_use]
|
|
||||||
pub fn new() -> StatefulList<T> {
|
|
||||||
StatefulList {
|
|
||||||
state: ListState::default(),
|
|
||||||
items: Vec::new(),
|
|
||||||
last_selected: None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[must_use]
|
|
||||||
pub fn get_selected(&self) -> Option<&T> {
|
|
||||||
if let Some(i) = self.state.selected() {
|
|
||||||
self.items.get(i)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn pop_selected(&mut self) -> Option<T> {
|
|
||||||
if let Some(i) = self.state.selected() {
|
|
||||||
Some(self.items[i].clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn select_first_item(&mut self) {
|
|
||||||
if self.items.is_empty() {
|
|
||||||
self.state.select(None);
|
|
||||||
} else {
|
|
||||||
self.state.select(Some(0));
|
|
||||||
}
|
|
||||||
self.last_selected = None;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn set_items(&mut self, items: Vec<T>) {
|
|
||||||
// Clear list and rebuild with provided items
|
|
||||||
self.items.clear();
|
|
||||||
for item in items {
|
|
||||||
self.items.push(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset state and select first item (if available)
|
|
||||||
self.state = ListState::default();
|
|
||||||
self.select_first_item();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn next(&mut self) {
|
|
||||||
if self.items.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let i = match self.state.selected() {
|
|
||||||
Some(i) => {
|
|
||||||
if i >= self.items.len() - 1 {
|
|
||||||
0
|
|
||||||
} else {
|
|
||||||
i + 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => self.last_selected.unwrap_or(0),
|
|
||||||
};
|
|
||||||
self.state.select(Some(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn previous(&mut self) {
|
|
||||||
if self.items.is_empty() {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let i = match self.state.selected() {
|
|
||||||
Some(i) => {
|
|
||||||
if i == 0 {
|
|
||||||
self.items.len() - 1
|
|
||||||
} else {
|
|
||||||
i - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => self.last_selected.unwrap_or(0),
|
|
||||||
};
|
|
||||||
self.state.select(Some(i));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Application.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
pub config: Config,
|
// TUI
|
||||||
pub main_menu: StatefulList<MenuEntry>,
|
action_rx: mpsc::UnboundedReceiver<Action>,
|
||||||
pub popup: Option<PopUp>,
|
action_tx: mpsc::UnboundedSender<Action>,
|
||||||
pub quit_reason: QuitReason,
|
components: Vec<Box<dyn Component>>,
|
||||||
pub running: bool,
|
config: Config,
|
||||||
pub thread_pool: Vec<JoinHandle<Result<Output, io::Error>>>,
|
frame_rate: f64,
|
||||||
}
|
last_tick_key_events: Vec<KeyEvent>,
|
||||||
|
should_quit: bool,
|
||||||
impl Default for App {
|
should_suspend: bool,
|
||||||
fn default() -> Self {
|
tick_rate: f64,
|
||||||
let config = Config::load();
|
// App
|
||||||
Self {
|
list: StatefulList<Tool>,
|
||||||
config: config.unwrap(),
|
mode: Mode,
|
||||||
running: true,
|
tasks: Tasks,
|
||||||
quit_reason: QuitReason::Exit,
|
|
||||||
main_menu: StatefulList::new(),
|
|
||||||
popup: None,
|
|
||||||
thread_pool: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
/// Constructs a new instance of [`App`].
|
pub fn new(tick_rate: f64, frame_rate: f64) -> Result<Self> {
|
||||||
#[must_use]
|
let (action_tx, action_rx) = mpsc::unbounded_channel();
|
||||||
pub fn new() -> Self {
|
let disk_list_arc = Arc::new(Mutex::new(Vec::new()));
|
||||||
let mut app = Self::default();
|
let tasks = Tasks::new(action_tx.clone(), disk_list_arc.clone());
|
||||||
|
let mut list = StatefulList::default();
|
||||||
// Add MenuEntries
|
list.set_items(load_tools(action_tx.clone()));
|
||||||
for tool in &app.config.tools {
|
Ok(Self {
|
||||||
app.main_menu.items.push(MenuEntry::new(
|
// TUI
|
||||||
&tool.name,
|
action_rx,
|
||||||
&tool.command,
|
action_tx,
|
||||||
tool.args.clone(),
|
components: vec![
|
||||||
tool.use_conemu,
|
Box::new(Title::new("PE Menu")),
|
||||||
tool.separator,
|
Box::new(Left::new()),
|
||||||
));
|
Box::new(Right::new()),
|
||||||
}
|
Box::new(Footer::new()),
|
||||||
app.main_menu.select_first_item();
|
Box::new(popup::Popup::new()),
|
||||||
|
],
|
||||||
// Done
|
config: Config::new()?,
|
||||||
app
|
frame_rate,
|
||||||
|
last_tick_key_events: Vec::new(),
|
||||||
|
should_quit: false,
|
||||||
|
should_suspend: false,
|
||||||
|
tick_rate,
|
||||||
|
// App
|
||||||
|
list,
|
||||||
|
mode: Mode::default(),
|
||||||
|
tasks,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handles the tick event of the terminal.
|
pub async fn run(&mut self) -> Result<()> {
|
||||||
pub fn tick(&self) {}
|
let mut tui = Tui::new()?
|
||||||
|
// .mouse(true) // uncomment this line to enable mouse support
|
||||||
|
.tick_rate(self.tick_rate)
|
||||||
|
.frame_rate(self.frame_rate);
|
||||||
|
tui.enter()?;
|
||||||
|
|
||||||
/// Actually exit application
|
for component in &mut self.components {
|
||||||
///
|
component.register_action_handler(self.action_tx.clone())?;
|
||||||
/// # Errors
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic if wpeutil fails to reboot or shutdown
|
|
||||||
pub fn exit(&self) -> Result<(), &'static str> {
|
|
||||||
let mut argument: Option<String> = None;
|
|
||||||
match self.quit_reason {
|
|
||||||
QuitReason::Exit => {}
|
|
||||||
QuitReason::Poweroff => argument = Some(String::from("shutdown")),
|
|
||||||
QuitReason::Restart => argument = Some(String::from("reboot")),
|
|
||||||
}
|
}
|
||||||
if let Some(a) = argument {
|
for component in &mut self.components {
|
||||||
Command::new("wpeutil")
|
component.register_config_handler(self.config.clone())?;
|
||||||
.arg(a)
|
}
|
||||||
.output()
|
for component in &mut self.components {
|
||||||
.expect("Failed to run exit command");
|
component.init(tui.size()?)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let action_tx = self.action_tx.clone();
|
||||||
|
action_tx.send(Action::SetMode(Mode::PEMenu))?;
|
||||||
|
loop {
|
||||||
|
self.handle_events(&mut tui).await?;
|
||||||
|
self.handle_actions(&mut tui)?;
|
||||||
|
if self.should_suspend {
|
||||||
|
tui.suspend()?;
|
||||||
|
action_tx.send(Action::Resume)?;
|
||||||
|
action_tx.send(Action::ClearScreen)?;
|
||||||
|
// tui.mouse(true);
|
||||||
|
tui.enter()?;
|
||||||
|
} else if self.should_quit {
|
||||||
|
tui.stop()?;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tui.exit()?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn handle_events(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
let Some(event) = tui.next_event().await else {
|
||||||
|
return Ok(());
|
||||||
|
};
|
||||||
|
let action_tx = self.action_tx.clone();
|
||||||
|
match event {
|
||||||
|
Event::Quit => action_tx.send(Action::Quit)?,
|
||||||
|
Event::Tick => action_tx.send(Action::Tick)?,
|
||||||
|
Event::Render => action_tx.send(Action::Render)?,
|
||||||
|
Event::Resize(x, y) => action_tx.send(Action::Resize(x, y))?,
|
||||||
|
Event::Key(key) => self.handle_key_event(key)?,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
if let Some(action) = component.handle_events(Some(event.clone()))? {
|
||||||
|
action_tx.send(action)?;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set running to false to quit the application.
|
fn handle_key_event(&mut self, key: KeyEvent) -> Result<()> {
|
||||||
pub fn quit(&mut self, reason: QuitReason) {
|
let action_tx = self.action_tx.clone();
|
||||||
self.running = false;
|
let Some(keymap) = self.config.keybindings.get(&self.mode) else {
|
||||||
self.quit_reason = reason;
|
return Ok(());
|
||||||
}
|
|
||||||
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic if command fails to run
|
|
||||||
pub fn open_terminal(&mut self) {
|
|
||||||
Command::new("cmd.exe")
|
|
||||||
.arg("-new_console:n")
|
|
||||||
.output()
|
|
||||||
.expect("Failed to run command");
|
|
||||||
}
|
|
||||||
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic if menu entry isn't found
|
|
||||||
pub fn run_tool(&mut self) {
|
|
||||||
// Command
|
|
||||||
let tool: &MenuEntry;
|
|
||||||
if let Some(index) = self.main_menu.state.selected() {
|
|
||||||
tool = &self.main_menu.items[index];
|
|
||||||
} else {
|
|
||||||
self.popup = Some(PopUp::new(
|
|
||||||
"Failed to find menu entry",
|
|
||||||
"Check for an updated version of Deja-Vu",
|
|
||||||
));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
let command = if tool.use_conemu {
|
|
||||||
self.config.con_emu.clone()
|
|
||||||
} else {
|
|
||||||
tool.command.clone()
|
|
||||||
};
|
};
|
||||||
|
if let Some(action) = keymap.get(&vec![key]) {
|
||||||
// Separators
|
info!("Got action: {action:?}");
|
||||||
if tool.separator {
|
action_tx.send(action.clone())?;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Args
|
|
||||||
let mut args = tool.args.clone();
|
|
||||||
if tool.use_conemu {
|
|
||||||
args.insert(0, tool.command.clone());
|
|
||||||
args.push(String::from("-new_console:n"));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check path
|
|
||||||
let command_path = PathBuf::from(&command);
|
|
||||||
if let Ok(true) = command_path.try_exists() {
|
|
||||||
// File path exists
|
|
||||||
} else {
|
} else {
|
||||||
// File path doesn't exist or is a broken symlink/etc
|
// If the key was not handled as a single key action,
|
||||||
// The latter case would be Ok(false) rather than Err(_)
|
// then consider it for multi-key combinations.
|
||||||
self.popup = Some(PopUp::new("Tool Missing", &format!("Tool path: {command}")));
|
self.last_tick_key_events.push(key);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run
|
// Check for multi-key combinations
|
||||||
// TODO: This really needs refactored to use channels so we can properly check if the
|
if let Some(action) = keymap.get(&self.last_tick_key_events) {
|
||||||
// command fails.
|
info!("Got action: {action:?}");
|
||||||
let new_thread = thread::spawn(move || Command::new(command_path).args(args).output());
|
action_tx.send(action.clone())?;
|
||||||
self.thread_pool.push(new_thread);
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_actions(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
while let Ok(action) = self.action_rx.try_recv() {
|
||||||
|
if action != Action::Tick && action != Action::Render {
|
||||||
|
debug!("{action:?}");
|
||||||
|
}
|
||||||
|
match action {
|
||||||
|
Action::Tick => {
|
||||||
|
self.last_tick_key_events.drain(..);
|
||||||
|
// Check background task(s)
|
||||||
|
self.tasks.poll()?;
|
||||||
|
}
|
||||||
|
Action::Quit => self.should_quit = true,
|
||||||
|
Action::Suspend => self.should_suspend = true,
|
||||||
|
Action::Resume => self.should_suspend = false,
|
||||||
|
Action::ClearScreen => tui.terminal.clear()?,
|
||||||
|
Action::KeyUp => {
|
||||||
|
self.list.previous();
|
||||||
|
if let Some(tool) = self.list.get_selected()
|
||||||
|
&& tool.separator
|
||||||
|
{
|
||||||
|
// Skip over separator
|
||||||
|
self.list.previous();
|
||||||
|
if let Some(index) = self.list.selected() {
|
||||||
|
self.action_tx.send(Action::Highlight(index))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::KeyDown => {
|
||||||
|
self.list.next();
|
||||||
|
if let Some(tool) = self.list.get_selected()
|
||||||
|
&& tool.separator
|
||||||
|
{
|
||||||
|
// Skip over separator
|
||||||
|
self.list.next();
|
||||||
|
if let Some(index) = self.list.selected() {
|
||||||
|
self.action_tx.send(Action::Highlight(index))?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Error(ref msg) => {
|
||||||
|
self.action_tx
|
||||||
|
.send(Action::DisplayPopup(popup::Type::Error, msg.clone()))?;
|
||||||
|
self.action_tx.send(Action::SetMode(Mode::Failed))?;
|
||||||
|
}
|
||||||
|
Action::Process => {
|
||||||
|
// Run selected tool
|
||||||
|
if let Some(tool) = self.list.get_selected() {
|
||||||
|
info!("Run tool: {:?}", &tool);
|
||||||
|
self.tasks.add(build_tool_command(self, &tool));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Action::Resize(w, h) => self.handle_resize(tui, w, h)?,
|
||||||
|
Action::Render => self.render(tui)?,
|
||||||
|
Action::SetMode(mode) => {
|
||||||
|
self.mode = mode;
|
||||||
|
self.action_tx.send(Action::UpdateFooter(String::from(
|
||||||
|
"(Enter) to select / (t) for terminal / (p) to power off / (r) to restart",
|
||||||
|
)))?;
|
||||||
|
self.action_tx.send(build_left_items(self))?;
|
||||||
|
self.action_tx.send(build_right_items(self))?;
|
||||||
|
self.action_tx.send(Action::Select(None, None))?;
|
||||||
|
}
|
||||||
|
Action::OpenTerminal => {
|
||||||
|
self.tasks.add(TaskType::CommandNoWait(
|
||||||
|
PathBuf::from("cmd.exe"),
|
||||||
|
vec![String::from("-new_console:n")],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Action::Restart => {
|
||||||
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
|
popup::Type::Info,
|
||||||
|
String::from("Restarting..."),
|
||||||
|
))?;
|
||||||
|
self.tasks.add(TaskType::CommandWait(
|
||||||
|
PathBuf::from("X:/Windows/System32/sync64.exe"),
|
||||||
|
vec![String::from("-r")],
|
||||||
|
));
|
||||||
|
self.tasks.add(TaskType::CommandWait(
|
||||||
|
PathBuf::from("X:/Windows/System32/wpeutil.exe"),
|
||||||
|
vec![String::from("reboot")],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Action::Shutdown => {
|
||||||
|
// NOTE: Using 'Powering off' to match the key pressed
|
||||||
|
self.action_tx.send(Action::DisplayPopup(
|
||||||
|
popup::Type::Info,
|
||||||
|
String::from("Powering off..."),
|
||||||
|
))?;
|
||||||
|
self.tasks.add(TaskType::CommandWait(
|
||||||
|
PathBuf::from("X:/Windows/System32/sync64.exe"),
|
||||||
|
vec![String::from("-r")],
|
||||||
|
));
|
||||||
|
self.tasks.add(TaskType::CommandWait(
|
||||||
|
PathBuf::from("X:/Windows/System32/wpeutil.exe"),
|
||||||
|
vec![String::from("shutdown")],
|
||||||
|
));
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
for component in &mut self.components {
|
||||||
|
if let Some(action) = component.update(action.clone())? {
|
||||||
|
self.action_tx.send(action)?;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_resize(&mut self, tui: &mut Tui, w: u16, h: u16) -> Result<()> {
|
||||||
|
tui.resize(Rect::new(0, 0, w, h))?;
|
||||||
|
self.render(tui)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render(&mut self, tui: &mut Tui) -> Result<()> {
|
||||||
|
tui.draw(|frame| {
|
||||||
|
if let [header, _body, footer, left, right, popup] = get_chunks(frame.area())[..] {
|
||||||
|
let component_areas = vec![header, left, right, footer, popup];
|
||||||
|
for (component, area) in zip(self.components.iter_mut(), component_areas) {
|
||||||
|
if let Err(err) = component.draw(frame, area) {
|
||||||
|
let _ = self
|
||||||
|
.action_tx
|
||||||
|
.send(Action::Error(format!("Failed to draw: {err:?}")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
||||||
|
// Cut the given rectangle into three vertical pieces
|
||||||
|
let popup_layout = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
Constraint::Percentage(percent_y),
|
||||||
|
Constraint::Percentage((100 - percent_y) / 2),
|
||||||
|
])
|
||||||
|
.split(r);
|
||||||
|
|
||||||
|
// Then cut the middle vertical piece into three width-wise pieces
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
Constraint::Percentage(percent_x),
|
||||||
|
Constraint::Percentage((100 - percent_x) / 2),
|
||||||
|
])
|
||||||
|
.split(popup_layout[1])[1] // Return the middle chunk
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_chunks(r: Rect) -> Vec<Rect> {
|
||||||
|
let mut chunks: Vec<Rect> = Vec::with_capacity(6);
|
||||||
|
|
||||||
|
// Main sections
|
||||||
|
chunks.extend(
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(3),
|
||||||
|
Constraint::Min(1),
|
||||||
|
Constraint::Length(3),
|
||||||
|
])
|
||||||
|
.split(r)
|
||||||
|
.to_vec(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Left/Right
|
||||||
|
chunks.extend(
|
||||||
|
Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
|
.split(centered_rect(90, 90, chunks[1]))
|
||||||
|
.to_vec(),
|
||||||
|
);
|
||||||
|
|
||||||
|
// Popup
|
||||||
|
chunks.push(centered_rect(60, 25, r));
|
||||||
|
|
||||||
|
// Done
|
||||||
|
chunks
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn build_tool_command(app: &App, tool: &Tool) -> TaskType {
|
||||||
|
let cmd_path: PathBuf;
|
||||||
|
let mut cmd_args: Vec<String> = Vec::new();
|
||||||
|
let start_index: usize;
|
||||||
|
if tool.use_conemu {
|
||||||
|
cmd_path = app.config.conemu_path.clone();
|
||||||
|
cmd_args.push(String::from("-run"));
|
||||||
|
cmd_args.push(tool.command.clone());
|
||||||
|
cmd_args.push(String::from("-new_console:n"));
|
||||||
|
start_index = 1;
|
||||||
|
} else {
|
||||||
|
cmd_path = PathBuf::from(tool.command.clone());
|
||||||
|
start_index = 0;
|
||||||
|
}
|
||||||
|
if let Some(args) = &tool.args
|
||||||
|
&& args.len() > start_index
|
||||||
|
{
|
||||||
|
args[start_index..].iter().for_each(|a| {
|
||||||
|
cmd_args.push(a.clone());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
TaskType::CommandNoWait(cmd_path, cmd_args)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_left_items(app: &App) -> Action {
|
||||||
|
let title = String::from("Tools");
|
||||||
|
let labels = vec![String::new(), String::new()];
|
||||||
|
let items = app
|
||||||
|
.list
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.map(|tool| {
|
||||||
|
if tool.separator {
|
||||||
|
String::from("──────────────")
|
||||||
|
} else {
|
||||||
|
tool.name.clone()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
// ─
|
||||||
|
.collect();
|
||||||
|
Action::UpdateLeft(title, labels, items, SelectionType::Loop)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn build_right_items(app: &App) -> Action {
|
||||||
|
let labels: Vec<Vec<DVLine>> = Vec::new();
|
||||||
|
let items = app
|
||||||
|
.list
|
||||||
|
.items
|
||||||
|
.iter()
|
||||||
|
.map(|entry| {
|
||||||
|
vec![
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![entry.name.clone()],
|
||||||
|
line_colors: vec![Color::Cyan],
|
||||||
|
},
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![String::new()],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
},
|
||||||
|
DVLine {
|
||||||
|
line_parts: vec![entry.description.clone()],
|
||||||
|
line_colors: vec![Color::Reset],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let start_index = 0;
|
||||||
|
Action::UpdateRight(labels, start_index, items)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_tools(action_tx: mpsc::UnboundedSender<Action>) -> Vec<Tool> {
|
||||||
|
let mut entries: Vec<PathBuf>;
|
||||||
|
let mut tools: Vec<Tool> = Vec::new();
|
||||||
|
let exe_path = env::current_exe().expect("Failed to find main executable");
|
||||||
|
let tool_config_path = exe_path.parent().unwrap().join("menu_entries");
|
||||||
|
if let Ok(read_dir) = std::fs::read_dir(tool_config_path) {
|
||||||
|
entries = read_dir
|
||||||
|
.map(|res| res.map(|e| e.path()))
|
||||||
|
.filter_map(Result::ok)
|
||||||
|
.collect();
|
||||||
|
entries.sort();
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(String::from(
|
||||||
|
"Failed to find any tool configs",
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
entries = Vec::new();
|
||||||
|
}
|
||||||
|
entries.iter().for_each(|entry| {
|
||||||
|
if let Ok(toml_str) = fs::read_to_string(entry) {
|
||||||
|
if let Ok(tool) = toml::from_str(&toml_str) {
|
||||||
|
tools.push(tool);
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(format!(
|
||||||
|
"Failed to parse tool config file: {:?}",
|
||||||
|
&entry,
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
action_tx
|
||||||
|
.send(Action::Error(format!(
|
||||||
|
"Failed to read tool config file: {:?}",
|
||||||
|
&entry,
|
||||||
|
)))
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
tools
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,116 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
use std::time::Duration;
|
|
||||||
|
|
||||||
use crossterm::event::{Event as CrosstermEvent, KeyEvent, MouseEvent};
|
|
||||||
use futures::{FutureExt, StreamExt};
|
|
||||||
use tokio::sync::mpsc;
|
|
||||||
|
|
||||||
use crate::app::AppResult;
|
|
||||||
|
|
||||||
/// Terminal events.
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
|
||||||
pub enum Event {
|
|
||||||
/// Terminal tick.
|
|
||||||
Tick,
|
|
||||||
/// Key press.
|
|
||||||
Key(KeyEvent),
|
|
||||||
/// Mouse click/scroll.
|
|
||||||
Mouse(MouseEvent),
|
|
||||||
/// Terminal resize.
|
|
||||||
Resize(u16, u16),
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Terminal event handler.
|
|
||||||
#[allow(dead_code)]
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Handler {
|
|
||||||
/// Event sender channel.
|
|
||||||
sender: mpsc::UnboundedSender<Event>,
|
|
||||||
/// Event receiver channel.
|
|
||||||
receiver: mpsc::UnboundedReceiver<Event>,
|
|
||||||
/// Event handler thread.
|
|
||||||
handler: tokio::task::JoinHandle<()>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Handler {
|
|
||||||
/// Constructs a new instance of [`Handler`].
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic if `sender_clone ` doesn't unwrap
|
|
||||||
#[must_use]
|
|
||||||
pub fn new(tick_rate: u64) -> Self {
|
|
||||||
let tick_rate = Duration::from_millis(tick_rate);
|
|
||||||
let (sender, receiver) = mpsc::unbounded_channel();
|
|
||||||
let sender_clone = sender.clone();
|
|
||||||
let handler = tokio::spawn(async move {
|
|
||||||
let mut reader = crossterm::event::EventStream::new();
|
|
||||||
let mut tick = tokio::time::interval(tick_rate);
|
|
||||||
loop {
|
|
||||||
let tick_delay = tick.tick();
|
|
||||||
let crossterm_event = reader.next().fuse();
|
|
||||||
tokio::select! {
|
|
||||||
() = sender_clone.closed() => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
_ = tick_delay => {
|
|
||||||
sender_clone.send(Event::Tick).unwrap();
|
|
||||||
}
|
|
||||||
Some(Ok(evt)) = crossterm_event => {
|
|
||||||
match evt {
|
|
||||||
CrosstermEvent::Key(key) => {
|
|
||||||
if key.kind == crossterm::event::KeyEventKind::Press {
|
|
||||||
sender_clone.send(Event::Key(key)).unwrap();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
CrosstermEvent::Mouse(mouse) => {
|
|
||||||
sender_clone.send(Event::Mouse(mouse)).unwrap();
|
|
||||||
},
|
|
||||||
CrosstermEvent::Resize(x, y) => {
|
|
||||||
sender_clone.send(Event::Resize(x, y)).unwrap();
|
|
||||||
},
|
|
||||||
CrosstermEvent::FocusGained | CrosstermEvent::FocusLost | CrosstermEvent::Paste(_) => {},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
|
||||||
Self {
|
|
||||||
sender,
|
|
||||||
receiver,
|
|
||||||
handler,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Receive the next event from the handler thread.
|
|
||||||
///
|
|
||||||
/// This function will always block the current thread if
|
|
||||||
/// there is no data available and it's possible for more data to be sent.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Will return error if a event is not found
|
|
||||||
pub async fn next(&mut self) -> AppResult<Event> {
|
|
||||||
self.receiver
|
|
||||||
.recv()
|
|
||||||
.await
|
|
||||||
.ok_or(Box::new(std::io::Error::new(
|
|
||||||
std::io::ErrorKind::Other,
|
|
||||||
"This is an IO error",
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,61 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
use crate::app::{App, QuitReason};
|
|
||||||
use crossterm::event::{KeyCode, KeyEvent};
|
|
||||||
|
|
||||||
/// Handles the key events and updates the state of [`App`].
|
|
||||||
pub fn handle_key_events(key_event: KeyEvent, app: &mut App) {
|
|
||||||
match key_event.code {
|
|
||||||
KeyCode::F(5) => app.quit(QuitReason::Exit),
|
|
||||||
KeyCode::Char('p' | 'P') => app.quit(QuitReason::Poweroff),
|
|
||||||
KeyCode::Char('r' | 'R') => app.quit(QuitReason::Restart),
|
|
||||||
KeyCode::Char('t' | 'T') => app.open_terminal(),
|
|
||||||
KeyCode::Up => {
|
|
||||||
if app.popup.is_none() {
|
|
||||||
app.main_menu.previous();
|
|
||||||
if let Some(e) = app.main_menu.get_selected() {
|
|
||||||
if e.separator {
|
|
||||||
// Skip over separators
|
|
||||||
app.main_menu.previous();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Down => {
|
|
||||||
if app.popup.is_none() {
|
|
||||||
app.main_menu.next();
|
|
||||||
if let Some(e) = app.main_menu.get_selected() {
|
|
||||||
if e.separator {
|
|
||||||
// Skip over separators
|
|
||||||
app.main_menu.next();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Enter => {
|
|
||||||
if app.popup.is_some() {
|
|
||||||
// Clear popup and return to main menu
|
|
||||||
app.popup = None;
|
|
||||||
} else {
|
|
||||||
app.run_tool();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Esc | KeyCode::Char('q' | 'Q') => {
|
|
||||||
app.popup = None;
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
/// Application.
|
|
||||||
pub mod app;
|
|
||||||
|
|
||||||
/// Terminal events handler.
|
|
||||||
pub mod event;
|
|
||||||
|
|
||||||
/// Widget renderer.
|
|
||||||
pub mod ui;
|
|
||||||
|
|
||||||
/// Terminal user interface.
|
|
||||||
pub mod tui;
|
|
||||||
|
|
||||||
/// Event handler.
|
|
||||||
pub mod handler;
|
|
||||||
|
|
@ -1,52 +1,32 @@
|
||||||
// This file is part of Deja-vu.
|
// This file is part of Deja-Vu.
|
||||||
//
|
//
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
// Deja-Vu is free software: you can redistribute it and/or modify it
|
||||||
// under the terms of the GNU General Public License as published by
|
// under the terms of the GNU General Public License as published by
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
// (at your option) any later version.
|
// (at your option) any later version.
|
||||||
//
|
//
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
// Deja-Vu is distributed in the hope that it will be useful, but
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||||
// See the GNU General Public License for more details.
|
// See the GNU General Public License for more details.
|
||||||
//
|
//
|
||||||
// You should have received a copy of the GNU General Public License
|
// You should have received a copy of the GNU General Public License
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
// along with Deja-Vu. If not, see <https://www.gnu.org/licenses/>.
|
||||||
//
|
//
|
||||||
use pe_menu::app::{App, AppResult};
|
use clap::Parser;
|
||||||
use pe_menu::event::{Event, Handler};
|
use color_eyre::Result;
|
||||||
use pe_menu::handler::handle_key_events;
|
|
||||||
use pe_menu::tui::Tui;
|
use crate::app::App;
|
||||||
use ratatui::backend::CrosstermBackend;
|
|
||||||
use ratatui::Terminal;
|
mod app;
|
||||||
use std::io;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> AppResult<()> {
|
async fn main() -> Result<()> {
|
||||||
// Create an application.
|
core::errors::init()?;
|
||||||
let mut app = App::new();
|
core::logging::init()?;
|
||||||
|
|
||||||
// Initialize the terminal user interface.
|
let args = core::cli::Cli::parse();
|
||||||
let backend = CrosstermBackend::new(io::stderr());
|
let mut app = App::new(args.tick_rate, args.frame_rate)?;
|
||||||
let terminal = Terminal::new(backend)?;
|
app.run().await?;
|
||||||
let events = Handler::new(250);
|
|
||||||
let mut tui = Tui::new(terminal, events);
|
|
||||||
tui.init()?;
|
|
||||||
|
|
||||||
// Start the main loop.
|
|
||||||
while app.running {
|
|
||||||
// Render the user interface.
|
|
||||||
tui.draw(&mut app)?;
|
|
||||||
// Handle events.
|
|
||||||
match tui.events.next().await? {
|
|
||||||
Event::Tick => app.tick(),
|
|
||||||
Event::Key(key_event) => handle_key_events(key_event, &mut app),
|
|
||||||
Event::Mouse(_) | Event::Resize(_, _) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit the user interface.
|
|
||||||
tui.exit()?;
|
|
||||||
app.exit()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,111 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
use crate::app::{App, AppResult};
|
|
||||||
use crate::event::Handler;
|
|
||||||
use crate::ui;
|
|
||||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
|
||||||
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
|
|
||||||
use ratatui::backend::Backend;
|
|
||||||
use ratatui::Terminal;
|
|
||||||
use std::io;
|
|
||||||
use std::panic;
|
|
||||||
|
|
||||||
/// Representation of a terminal user interface.
|
|
||||||
///
|
|
||||||
/// It is responsible for setting up the terminal,
|
|
||||||
/// initializing the interface and handling the draw events.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Tui<B: Backend> {
|
|
||||||
/// Interface to the Terminal.
|
|
||||||
terminal: Terminal<B>,
|
|
||||||
/// Terminal event handler.
|
|
||||||
pub events: Handler,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<B: Backend> Tui<B> {
|
|
||||||
/// Constructs a new instance of [`Tui`].
|
|
||||||
pub fn new(terminal: Terminal<B>, events: Handler) -> Self {
|
|
||||||
Self { terminal, events }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Initializes the terminal interface.
|
|
||||||
///
|
|
||||||
/// It enables the raw mode and sets terminal properties.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Will return error if `enable_raw_mode` fails
|
|
||||||
///
|
|
||||||
/// # Panics
|
|
||||||
///
|
|
||||||
/// Will panic if `reset` fails
|
|
||||||
pub fn init(&mut self) -> AppResult<()> {
|
|
||||||
terminal::enable_raw_mode()?;
|
|
||||||
crossterm::execute!(io::stderr(), EnterAlternateScreen, EnableMouseCapture)?;
|
|
||||||
|
|
||||||
// Define a custom panic hook to reset the terminal properties.
|
|
||||||
// This way, you won't have your terminal messed up if an unexpected error happens.
|
|
||||||
let panic_hook = panic::take_hook();
|
|
||||||
panic::set_hook(Box::new(move |panic| {
|
|
||||||
Self::reset().expect("failed to reset the terminal");
|
|
||||||
panic_hook(panic);
|
|
||||||
}));
|
|
||||||
|
|
||||||
self.terminal.hide_cursor()?;
|
|
||||||
self.terminal.clear()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// [`Draw`] the terminal interface by [`rendering`] the widgets.
|
|
||||||
///
|
|
||||||
/// [`Draw`]: ratatui::Terminal::draw
|
|
||||||
/// [`rendering`]: crate::ui::render
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Will return error if `draw` fails
|
|
||||||
pub fn draw(&mut self, app: &mut App) -> AppResult<()> {
|
|
||||||
self.terminal.draw(|frame| ui::render(app, frame))?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Resets the terminal interface.
|
|
||||||
///
|
|
||||||
/// This function is also used for the panic hook to revert
|
|
||||||
/// the terminal properties if unexpected errors occur.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Will return error if `disable_raw_mode` fails
|
|
||||||
fn reset() -> AppResult<()> {
|
|
||||||
terminal::disable_raw_mode()?;
|
|
||||||
crossterm::execute!(io::stderr(), LeaveAlternateScreen, DisableMouseCapture)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Exits the terminal interface.
|
|
||||||
///
|
|
||||||
/// It disables the raw mode and reverts back the terminal properties.
|
|
||||||
///
|
|
||||||
/// # Errors
|
|
||||||
///
|
|
||||||
/// Will return error if either `reset` or `show_cursor` fails
|
|
||||||
pub fn exit(&mut self) -> AppResult<()> {
|
|
||||||
Self::reset()?;
|
|
||||||
self.terminal.show_cursor()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,127 +0,0 @@
|
||||||
// This file is part of Deja-vu.
|
|
||||||
//
|
|
||||||
// Deja-vu is free software: you can redistribute it and/or modify it
|
|
||||||
// under the terms of the GNU General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// Deja-vu is distributed in the hope that it will be useful, but
|
|
||||||
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
||||||
// See the GNU General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with Deja-vu. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
//
|
|
||||||
use crate::app::App;
|
|
||||||
use ratatui::{
|
|
||||||
layout::{Constraint, Direction, Layout, Rect},
|
|
||||||
style::{Color, Style, Stylize},
|
|
||||||
text::{Line, Span},
|
|
||||||
widgets::{Block, Borders, Clear, HighlightSpacing, List, ListItem, Padding, Paragraph, Wrap},
|
|
||||||
Frame,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Renders the user interface widgets.
|
|
||||||
pub fn render(app: &mut App, frame: &mut Frame) {
|
|
||||||
let chunks = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Length(3),
|
|
||||||
Constraint::Min(1),
|
|
||||||
Constraint::Length(3),
|
|
||||||
])
|
|
||||||
.split(frame.area());
|
|
||||||
|
|
||||||
// Title Block
|
|
||||||
let title_text = Span::styled("Deja-vu: PE Menu", Style::default().fg(Color::LightCyan));
|
|
||||||
let title = Paragraph::new(Line::from(title_text).centered())
|
|
||||||
.block(Block::default().borders(Borders::ALL));
|
|
||||||
frame.render_widget(title, chunks[0]);
|
|
||||||
|
|
||||||
// Main Block
|
|
||||||
let main_chunk = centered_rect(65, 90, chunks[1]);
|
|
||||||
render_main_pane(frame, app, main_chunk);
|
|
||||||
|
|
||||||
// Bottom Block
|
|
||||||
let footer_text = Span::styled(
|
|
||||||
"(Enter) to select / (p) to poweroff / (r) to restart / (t) for terminal",
|
|
||||||
Style::default().fg(Color::DarkGray),
|
|
||||||
);
|
|
||||||
let footer = Paragraph::new(Line::from(footer_text).centered())
|
|
||||||
.block(Block::default().borders(Borders::ALL));
|
|
||||||
frame.render_widget(footer, chunks[2]);
|
|
||||||
|
|
||||||
// Popup blocks
|
|
||||||
if app.popup.is_some() {
|
|
||||||
render_popup_pane(frame, app);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
|
|
||||||
// Cut the given rectangle into three vertical pieces
|
|
||||||
let popup_layout = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Percentage((100 - percent_y) / 2),
|
|
||||||
Constraint::Percentage(percent_y),
|
|
||||||
Constraint::Percentage((100 - percent_y) / 2),
|
|
||||||
])
|
|
||||||
.split(r);
|
|
||||||
|
|
||||||
// Then cut the middle vertical piece into three width-wise pieces
|
|
||||||
Layout::default()
|
|
||||||
.direction(Direction::Horizontal)
|
|
||||||
.constraints([
|
|
||||||
Constraint::Percentage((100 - percent_x) / 2),
|
|
||||||
Constraint::Percentage(percent_x),
|
|
||||||
Constraint::Percentage((100 - percent_x) / 2),
|
|
||||||
])
|
|
||||||
.split(popup_layout[1])[1] // Return the middle chunk
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_main_pane(frame: &mut Frame, app: &mut App, chunk: Rect) {
|
|
||||||
let mut list_items = Vec::<ListItem>::new();
|
|
||||||
for entry in &app.main_menu.items {
|
|
||||||
let text = if entry.separator {
|
|
||||||
if entry.name.is_empty() {
|
|
||||||
String::from("....................")
|
|
||||||
} else {
|
|
||||||
entry.name.clone()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
entry.name.clone()
|
|
||||||
};
|
|
||||||
list_items.push(ListItem::new(format!(" {text}\n\n\n")));
|
|
||||||
}
|
|
||||||
let list = List::new(list_items)
|
|
||||||
.block(
|
|
||||||
Block::default()
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.padding(Padding::new(20, 20, 5, 5)),
|
|
||||||
)
|
|
||||||
.highlight_spacing(HighlightSpacing::Always)
|
|
||||||
.highlight_style(Style::new().green().bold())
|
|
||||||
.highlight_symbol(" --> ")
|
|
||||||
.repeat_highlight_symbol(false);
|
|
||||||
frame.render_stateful_widget(list, chunk, &mut app.main_menu.state);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_popup_pane(frame: &mut Frame, app: &mut App) {
|
|
||||||
let popup_block = Block::default()
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.style(Style::default().red().bold());
|
|
||||||
if let Some(popup) = &app.popup {
|
|
||||||
let scan_paragraph = Paragraph::new(vec![
|
|
||||||
Line::from(Span::raw(&popup.title)),
|
|
||||||
Line::default(),
|
|
||||||
Line::from(Span::raw(&popup.body)),
|
|
||||||
])
|
|
||||||
.block(popup_block)
|
|
||||||
.centered()
|
|
||||||
.wrap(Wrap { trim: false });
|
|
||||||
let area = centered_rect(60, 25, frame.area());
|
|
||||||
frame.render_widget(Clear, area);
|
|
||||||
frame.render_widget(scan_paragraph, area);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue