deja-vu/core/src/line.rs
2025-11-01 21:08:42 -07:00

118 lines
3.6 KiB
Rust

// 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 ratatui::{
style::{Color, Style},
text::{Line, Span},
};
use serde::{Deserialize, Serialize};
use std::iter::zip;
use crate::system::disk::{Disk, Partition};
#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DVLine {
pub line_parts: Vec<String>,
pub line_colors: Vec<Color>,
}
impl DVLine {
/// Convert to Line with colored span(s)
#[must_use]
pub fn as_line(&self) -> Line<'_> {
let mut spans = Vec::new();
zip(self.line_parts.clone(), self.line_colors.clone())
.for_each(|(part, color)| spans.push(Span::styled(part, Style::default().fg(color))));
Line::from(spans)
}
#[must_use]
pub fn blank() -> Self {
Self {
line_parts: vec![String::new()],
line_colors: vec![Color::Reset],
}
}
}
#[must_use]
pub fn get_disk_description_right(
disk: &Disk,
boot_os_indicies: &Option<Vec<usize>>,
) -> Vec<DVLine> {
let mut description: Vec<DVLine> = vec![
DVLine {
line_parts: vec![format!(
"{:<8} {:>11} {:<4} {:<4} {}",
"Disk ID", "Size", "Conn", "Type", "Model (Serial)"
)],
line_colors: vec![Color::Green],
},
DVLine {
line_parts: vec![disk.description.clone()],
line_colors: vec![Color::Reset],
},
DVLine::blank(),
DVLine {
line_parts: vec![format!(
"{:<8} {:>11} {:<7} {}",
"Part ID", "Size", "(FS)", "\"Label\""
)],
line_colors: vec![Color::Blue],
},
];
disk.parts_description
.iter()
.enumerate()
.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
}
#[must_use]
pub fn get_part_description(part: &Partition) -> Vec<DVLine> {
let description: Vec<DVLine> = vec![
DVLine {
line_parts: vec![format!(
"{:<8} {:>11} {:<7} {}",
"Part ID", "Size", "(FS)", "\"Label\""
)],
line_colors: vec![Color::Blue],
},
DVLine {
line_parts: vec![format!("{part}")],
line_colors: vec![Color::Reset],
},
];
description
}