diff --git a/Cargo.lock b/Cargo.lock index 407c84f..7d9a277 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -85,9 +85,9 @@ dependencies = [ [[package]] name = "android-build" -version = "0.1.3" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cac4c64175d504608cf239756339c07f6384a476f97f20a7043f92920b0b8fd" +checksum = "f9fc9904ad2ad097c3c1cfe2eacaaf0fc24710936fa9ed941cb310b7c6ed2ab7" dependencies = [ "windows-sys 0.52.0", ] @@ -397,9 +397,9 @@ dependencies = [ [[package]] name = "avif-serialize" -version = "0.8.8" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "375082f007bd67184fb9c0374614b29f9aaa604ec301635f72338bb65386a53d" +checksum = "e7178fe5f7d460b13895ebb9dcb28a3a6216d2df2574a0806cb51b555d297f38" dependencies = [ "arrayvec", ] @@ -602,9 +602,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.2.61" +version = "1.2.62" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d16d90359e986641506914ba71350897565610e87ce0ad9e6f28569db3dd5c6d" +checksum = "a1dce859f0832a7d088c4f1119888ab94ef4b5d6795d1ce05afb7fe159d79f98" dependencies = [ "find-msvc-tools", "jobserver", @@ -935,9 +935,9 @@ checksum = "edf234dd1594d6dd434a8fb8cada51ddbbc593e40e4a01556a0b31c62da2775b" [[package]] name = "either" -version = "1.15.0" +version = "1.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +checksum = "91622ff5e7162018101f2fea40d6ebf4a78bbe5a49736a2020649edf9693679e" [[package]] name = "endi" @@ -1541,9 +1541,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.17.0" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f467dd6dccf739c208452f8014c75c18bb8301b050ad1cfb27153803edb0f51" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" [[package]] name = "heck" @@ -1858,7 +1858,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", - "hashbrown 0.17.0", + "hashbrown 0.17.1", "serde", "serde_core", ] @@ -1986,9 +1986,9 @@ dependencies = [ [[package]] name = "js-sys" -version = "0.3.97" +version = "0.3.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1840c94c045fbcf8ba2812c95db44499f7c64910a912551aaaa541decebcacf" +checksum = "67df7112613f8bfd9150013a0314e196f4800d3201ae742489d999db2f979f08" dependencies = [ "cfg-if", "futures-util", @@ -2103,7 +2103,7 @@ dependencies = [ "bitflags 2.11.1", "libc", "plain", - "redox_syscall 0.7.4", + "redox_syscall 0.7.5", ] [[package]] @@ -2342,9 +2342,9 @@ checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" [[package]] name = "no_std_io2" -version = "0.9.3" +version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b51ed7824b6e07d354605f4abb3d9d300350701299da96642ee084f5ce631550" +checksum = "418abd1b6d34fbf6cae440dc874771b0525a604428704c76e48b29a5e67b8003" dependencies = [ "memchr", ] @@ -2366,9 +2366,9 @@ checksum = "0676bb32a98c1a483ce53e500a81ad9c3d5b3f7c920c28c24e9cb0980d0b5bc8" [[package]] name = "normpath" -version = "1.5.0" +version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf23ab2b905654b4cb177e30b629937b3868311d4e1cba859f899c041046e69b" +checksum = "b9985ef7269fa99f3b12437bb698381da2428743ab90f20393f399fa14cab21a" dependencies = [ "windows-sys 0.61.2", ] @@ -2898,18 +2898,18 @@ checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" [[package]] name = "pin-project" -version = "1.1.11" +version = "1.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f1749c7ed4bcaf4c3d0a3efc28538844fb29bcdd7d2b67b2be7e20ba861ff517" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "1.1.11" +version = "1.1.13" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b20ed30f105399776b9c883e68e536ef602a16ae6f596d2c473591d6ad64c6" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" dependencies = [ "proc-macro2", "quote", @@ -3051,18 +3051,18 @@ dependencies = [ [[package]] name = "profiling" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3eb8486b569e12e2c32ad3e204dbaba5e4b5b216e9367044f25f1dba42341773" +checksum = "3d595e54a326bc53c1c197b32d295e14b169e3cfeaa8dc82b529f947fba6bcf5" dependencies = [ "profiling-procmacros", ] [[package]] name = "profiling-procmacros" -version = "1.0.17" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "52717f9a02b6965224f95ca2a81e2e0c5c43baacd28ca057577988930b6c3d5b" +checksum = "4488a4a36b9a4ba6b9334a32a39971f77c1436ec82c38707bce707699cc3bbcb" dependencies = [ "quote", "syn", @@ -3091,9 +3091,9 @@ checksum = "a993555f31e5a609f617c12db6250dedcac1b0a85076912c436e6fc9b2c8e6a3" [[package]] name = "quick-xml" -version = "0.39.2" +version = "0.39.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "958f21e8e7ceb5a1aa7fa87fab28e7c75976e0bfe7e23ff069e0a260f894067d" +checksum = "cdcc8dd4e2f670d309a5f0e83fe36dfdc05af317008fea29144da1a2ac858e5e" dependencies = [ "memchr", ] @@ -3294,9 +3294,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f450ad9c3b1da563fb6948a8e0fb0fb9269711c9c73d9ea1de5058c79c8d643a" +checksum = "4666a1a60d8412eab19d94f6d13dcc9cea0a5ef4fdf6a5db306537413c661b1b" dependencies = [ "bitflags 2.11.1", ] @@ -3641,9 +3641,9 @@ checksum = "e3a9fe34e3e7a50316060351f37187a3f546bce95496156754b601a5fa71b76e" [[package]] name = "siphasher" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b2aa850e253778c88a04c3d7323b043aeda9d3e30d5971937c1855769763678e" +checksum = "8ee5873ec9cce0195efcb7a4e9507a04cd49aec9c83d0389df45b1ef7ba2e649" [[package]] name = "skrifa" @@ -3877,10 +3877,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" dependencies = [ "fastrand 2.4.1", - "getrandom 0.4.2", + "getrandom 0.3.4", "once_cell", "rustix 1.1.4", - "windows-sys 0.61.2", + "windows-sys 0.52.0", ] [[package]] @@ -4034,7 +4034,7 @@ dependencies = [ "toml_datetime 1.1.1+spec-1.1.0", "toml_parser", "toml_writer", - "winnow 1.0.2", + "winnow 1.0.3", ] [[package]] @@ -4078,7 +4078,7 @@ dependencies = [ "indexmap", "toml_datetime 1.1.1+spec-1.1.0", "toml_parser", - "winnow 1.0.2", + "winnow 1.0.3", ] [[package]] @@ -4087,7 +4087,7 @@ version = "1.1.2+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2abe9b86193656635d2411dc43050282ca48aa31c2451210f4202550afb7526" dependencies = [ - "winnow 1.0.2", + "winnow 1.0.3", ] [[package]] @@ -4277,9 +4277,9 @@ dependencies = [ [[package]] name = "wasm-bindgen" -version = "0.2.120" +version = "0.2.121" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "df52b6d9b87e0c74c9edfa1eb2d9bf85e5d63515474513aa50fa181b3c4f5db1" +checksum = "49ace1d07c165b0864824eee619580c4689389afa9dc9ed3a4c75040d82e6790" dependencies = [ "cfg-if", "once_cell", @@ -4290,9 +4290,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.70" +version = "0.4.71" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af934872acec734c2d80e6617bbb5ff4f12b052dd8e6332b0817bce889516084" +checksum = "96492d0d3ffba25305a7dc88720d250b1401d7edca02cc3bcd50633b424673b8" dependencies = [ "js-sys", "wasm-bindgen", @@ -4300,9 +4300,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.120" +version = "0.2.121" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78b1041f495fb322e64aca85f5756b2172e35cd459376e67f2a6c9dffcedb103" +checksum = "8e68e6f4afd367a562002c05637acb8578ff2dea1943df76afb9e83d177c8578" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -4310,9 +4310,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.120" +version = "0.2.121" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dcd0ff20416988a18ac686d4d4d0f6aae9ebf08a389ff5d29012b05af2a1b41" +checksum = "d95a9ec35c64b2a7cb35d3fead40c4238d0940c86d107136999567a4703259f2" dependencies = [ "bumpalo", "proc-macro2", @@ -4323,9 +4323,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.120" +version = "0.2.121" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "49757b3c82ebf16c57d69365a142940b384176c24df52a087fb748e2085359ea" +checksum = "c4e0100b01e9f0d03189a92b96772a1fb998639d981193d7dbab487302513441" dependencies = [ "unicode-ident", ] @@ -4515,9 +4515,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.97" +version = "0.3.98" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2eadbac71025cd7b0834f20d1fe8472e8495821b4e9801eb0a60bd1f19827602" +checksum = "4b572dff8bcf38bad0fa19729c89bb5748b2b9b1d8be70cf90df697e3a8f32aa" dependencies = [ "js-sys", "wasm-bindgen", @@ -5041,9 +5041,9 @@ dependencies = [ [[package]] name = "winnow" -version = "1.0.2" +version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2ee1708bef14716a11bae175f579062d4554d95be2c6829f518df847b7b3fdd0" +checksum = "0592e1c9d151f854e6fd382574c3a0855250e1d9b2f99d9281c6e6391af352f1" dependencies = [ "memchr", ] @@ -5246,7 +5246,7 @@ dependencies = [ "uds_windows", "uuid", "windows-sys 0.61.2", - "winnow 1.0.2", + "winnow 1.0.3", "zbus_macros", "zbus_names", "zvariant", @@ -5274,7 +5274,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7074f3e50b894eac91750142016d30d0a89be8e67dbfd9704fb875825760e52d" dependencies = [ "serde", - "winnow 1.0.2", + "winnow 1.0.3", "zvariant", ] @@ -5336,23 +5336,23 @@ dependencies = [ [[package]] name = "zvariant" -version = "5.10.1" +version = "5.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4db0ecb8987cf5e92653c57c098f7f0e39a03112edb796f4fe089fb7eaa14ff" +checksum = "1c1567a6ec68df868cbbfde844cfc6d81649fe5109a62b116b19fabd53e618ee" dependencies = [ "endi", "enumflags2", "serde", - "winnow 1.0.2", + "winnow 1.0.3", "zvariant_derive", "zvariant_utils", ] [[package]] name = "zvariant_derive" -version = "5.10.1" +version = "5.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b949b639ab1b4bed763aa7481ba0e368af68d8b55532f8ed4bec86a59f2ca98" +checksum = "c7d5b780599bbde114e39d9a0799577fad1ced5105d38515745f7b3099d8ceda" dependencies = [ "proc-macro-crate", "proc-macro2", @@ -5371,5 +5371,5 @@ dependencies = [ "quote", "serde", "syn", - "winnow 1.0.2", + "winnow 1.0.3", ] diff --git a/Cargo.toml b/Cargo.toml index d6aa9fd..fcee38c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ edition = "2024" [dependencies] blocking = "1.6" -iced = { version = "0.14", features = ["smol", "image"] } +iced = { version = "0.14", default-features = false, features = ["crisp", "linux-theme-detection", "web-colors", "wgpu", "x11", "wayland", "smol", "image"] } iced_moving_picture = "0" rust-i18n = "3" toml = "1" diff --git a/src/kira_color_bar.rs b/src/kira_color_bar.rs index 0f8c399..066ff34 100644 --- a/src/kira_color_bar.rs +++ b/src/kira_color_bar.rs @@ -9,6 +9,7 @@ pub fn color_bar( let mut c_b = Row::new().height(bar_height); if let Some(items) = maybe_items { + for (bar_text, bar_fill, bar_color) in items { c_b = c_b.push( container(text(bar_text).color(Color::BLACK).size(iced::Pixels(14.0))) @@ -16,9 +17,13 @@ pub fn color_bar( .width(Length::FillPortion(bar_fill)) .style(move |_| container::background(bar_color)) .align_x(Alignment::Center) - .align_y(Alignment::Center), + .align_y(Alignment::Center) + .padding(2), ); } } - c_b.into() + container(c_b.spacing(2)) + .style(container::rounded_box) + .padding(3) + .into() } diff --git a/src/kira_disk_layout.rs b/src/kira_disk_layout.rs index d6012e8..b410901 100644 --- a/src/kira_disk_layout.rs +++ b/src/kira_disk_layout.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::ops::{Add, Sub}; pub fn format_bytes_size(size: u64) -> String { let suffixs: [&str; 5] = ["KB", "MB", "GB", "TB", "PB"]; @@ -19,35 +20,124 @@ pub fn format_bytes_size(size: u64) -> String { } } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct BlkSize { + size_bytes: u64, +} + +impl std::fmt::Display for BlkSize { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if self.size_bytes < 1000 { + write!(f, "{}B", self.size_bytes) + } else { + let mut s = self.size_bytes as f64; + let mut idx = 0; + for i in 0..5 { + s = s / 1024.0; + idx = i; + if s < 1000.0 { + break; + } + } + write!(f, "{:.2}{}", s, Self::SUFFIXS[idx]) + } + } +} + +impl BlkSize { + pub const SUFFIXS: [&str; 5] = ["KB", "MB", "GB", "TB", "PB"]; + pub fn new(size:u64) -> Self { + Self { size_bytes: size } + } + pub fn new_kb(size:u64) -> Self { + Self { size_bytes: size * 1024 } + } + pub fn new_mb(size:u64) -> Self { + Self { size_bytes: size * 1024u64.pow(2) } + } + pub fn new_gb(size:u64) -> Self { + Self { size_bytes: size * 1024u64.pow(3) } + } + pub fn new_tb(size:u64) -> Self { + Self { size_bytes: size * 1024u64.pow(4) } + } + pub fn new_pb(size:u64) -> Self { + Self { size_bytes: size * 1024u64.pow(5) } + } + + pub fn b(&self) -> u64 { + self.size_bytes + } + pub fn kb(&self) -> u64 { + self.size_bytes / 1024u64.pow(2) + } + pub fn mb(&self) -> u64 { + self.size_bytes / 1024u64.pow(3) + } + pub fn gb(&self) -> u64 { + self.size_bytes / 1024u64.pow(4) + } + pub fn tb(&self) -> u64 { + self.size_bytes / 1024u64.pow(5) + } + pub fn pb(&self) -> u64 { + self.size_bytes / 1024u64.pow(6) + } + + pub fn from_str_bytes(s: &str) -> Option { + Some(Self::new(u64::from_str_radix(s, 10).ok()?)) + } +} + +impl Add for BlkSize { + type Output = BlkSize; + + fn add(self, other: BlkSize) -> BlkSize { + BlkSize { + size_bytes: self.size_bytes + other.size_bytes, + } + } +} + +impl Sub for BlkSize { + type Output = BlkSize; + + fn sub(self, other: BlkSize) -> BlkSize { + BlkSize { + size_bytes: self.size_bytes.strict_sub(other.size_bytes), + } + } +} + /// /// size - device size in bytes /// sector size - default 4096 #[derive(Debug, Clone, PartialEq, Eq)] pub struct BlkDev { pub name: String, - pub size: u64, - pub sector_size: u32, + pub size: BlkSize, + pub sector_size: BlkSize, } impl std::fmt::Display for BlkDev { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{} ({})", &self.name, format_bytes_size(self.size)) + write!(f, "{} ({})", &self.name, self.size) } } impl BlkDev { - pub fn new(name: String, size: u64) -> Self { + pub fn new(name: String, size: BlkSize) -> Self { Self { name: name, size: size, - sector_size: 4096, + sector_size: BlkSize::new(4096), } } pub fn from_hash_map(data: &HashMap) -> Option { Some(Self { name: data.get("NAME")?.clone(), - size: u64::from_str_radix(data.get("SIZE")?, 10).ok()?, - sector_size: 4096, + size: BlkSize::from_str_bytes(data.get("SIZE")?)?, + sector_size: BlkSize::new(4096), }) } /// this is blocking function @@ -151,15 +241,26 @@ impl FSType { // } // } +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum PartRole { + EFI, + ROOT, + HOME, + SWAP, + LuksRoot, + LuksHome, +} + #[derive(Debug, Clone, PartialEq, Eq)] pub struct PartInfo { pub name: String, - pub size: u64, - pub start: u64, + pub size: BlkSize, + pub start: BlkSize, pub fs_type: FSType, pub gpt_label: Option, pub fs_label: Option, pub mount_point: Option, + pub role: Option, } impl std::fmt::Display for PartInfo { @@ -169,7 +270,7 @@ impl std::fmt::Display for PartInfo { "{} {} {}", &self.name, &self.fs_type, - format_bytes_size(self.size) + self.size ) } } @@ -178,8 +279,8 @@ impl PartInfo { pub fn from_hash_map(data: &HashMap) -> Option { Some(Self { name: data.get("NAME")?.clone(), - size: u64::from_str_radix(data.get("SIZE")?, 10).ok()?, - start: 0, + size: BlkSize::from_str_bytes(data.get("SIZE")?)?, + start: BlkSize::new(0), fs_type: FSType::from_str(data.get("FSTYPE")?), gpt_label: data .get("PARTLABEL") @@ -190,6 +291,7 @@ impl PartInfo { mount_point: data .get("MOUNTPOINT") .and_then(|v| if v.is_empty() { None } else { Some(v.clone()) }), + role: None, }) } } @@ -287,7 +389,7 @@ pub fn align_part(size: u64, start: u64, align: u64) -> Option<(u64, u64, u64)> pub struct PartLayout { pub dev: BlkDev, - pub empty_space: u64, + pub empty_space: BlkSize, pub part_list: Vec, } @@ -302,7 +404,7 @@ impl PartLayout { pub fn read_from_disk(dev: &BlkDev) -> Result { let part_list = get_dev_part_blocking(&dev.name)?; - let parts_size = part_list.iter().fold(0, |acc, part| acc + part.size); + let parts_size = part_list.iter().fold(BlkSize::new(0), |acc, part| acc + part.size); Ok(Self { dev: dev.clone(), empty_space: dev.size - parts_size, @@ -318,45 +420,86 @@ impl PartLayout { pub fn add_part( mut self, - part_size: u64, + part_size: BlkSize, fs_type: FSType, gpt_label: Option, fs_label: Option, mount_point: Option, + role: Option, ) -> Self { let part_start = if self.part_list.is_empty() { - 2u64 * 1024u64 * 1024u64 + BlkSize::new_mb(2) } else { - self.dev.size.checked_sub(self.empty_space).unwrap() + self.dev.size - self.empty_space }; let (size, start, _) = - align_part(part_size, part_start, self.dev.sector_size as u64).unwrap(); + align_part(part_size.b(), part_start.b(), self.dev.sector_size.b()).unwrap(); + let part_name = format!("{}p{}", self.dev.name, self.part_list.len() + 1); self.part_list.push(PartInfo { - name: "_".into(), - size: size, - start: start, + name: part_name, + size: BlkSize::new(size), + start: BlkSize::new(start), fs_type: fs_type, gpt_label: gpt_label, fs_label: fs_label, mount_point: mount_point, + role: role, }); - self.empty_space = self.empty_space.checked_sub(size).unwrap(); + self.empty_space = self.empty_space - BlkSize::new(size); self } pub fn add_part_reserve( self, - reserve_space: u64, + reserve_space: BlkSize, fs_type: FSType, gpt_label: Option, fs_label: Option, mount_point: Option, + role: Option, ) -> Self { - let p_size = self.empty_space.checked_sub(reserve_space).unwrap(); - self.add_part(p_size, fs_type, gpt_label, fs_label, mount_point) + let p_size = self.empty_space - reserve_space; + self.add_part(p_size, fs_type, gpt_label, fs_label, mount_point, role) + } + + pub fn gen_classic_layout(dev: BlkDev, efi_size: BlkSize, swap_size: BlkSize) -> Self { + let mut res: PartLayout = PartLayout::new(dev); + + // UEFI partition + res = res + .add_part( + efi_size, + FSType::VFAT, + Some("efi".into()), + Some("EFI".into()), + Some("/efi".into()), + Some(PartRole::EFI), + ) + .add_part_reserve( + swap_size, + FSType::XFS, + Some("root".into()), + None, + Some("/".into()), + Some(PartRole::ROOT), + ); + + // swap + if swap_size.b() > 0 { + res = res.add_part( + swap_size, + FSType::SWAP, + Some("swap".into()), + None, + Some("SWAP".into()), + Some(PartRole::SWAP), + ); + } + + res } } diff --git a/src/stages/partition/mod.rs b/src/stages/partition/mod.rs index 7363b35..9d78a16 100644 --- a/src/stages/partition/mod.rs +++ b/src/stages/partition/mod.rs @@ -21,10 +21,10 @@ use crate::stage::{StageAction, StageResult}; use crate::kira_color_bar; -use crate::kira_disk_layout::{BlkDev,PartInfo, PartLayout, FSType}; -use toml::Table; -use iced::{Alignment, Color, Length, widget}; +use crate::kira_disk_layout::{BlkDev, BlkSize, FSType, PartInfo, PartLayout}; +use iced::{Alignment, Color, widget}; use rust_i18n::t; +use toml::Table; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SwapMode { @@ -61,13 +61,12 @@ fn part_type_to_color(t: &FSType) -> Color { } } -fn part_to_ct_params(part: &PartInfo, dev_size: u64) -> (String, u16, Color) { - let fill_ratio: u16 = ((part.size as f32 / dev_size as f32) * 12.0) as u16; - let fill_ratio = fill_ratio.max(1); - let part_color = part_type_to_color(&part.fs_type); - (part.to_string(), fill_ratio, part_color) - } - +fn part_to_ct_params(part: &PartInfo, dev_size: BlkSize) -> (String, u16, Color) { + let fill_ratio: u16 = ((part.size.b() as f32 / dev_size.b() as f32) * 11.0) as u16; + let fill_ratio = fill_ratio.max(1); + let part_color = part_type_to_color(&part.fs_type); + (part.to_string(), fill_ratio, part_color) +} #[derive(Debug, Clone)] pub struct PartitionStage { @@ -77,6 +76,7 @@ pub struct PartitionStage { use_zram: bool, secure_boot: bool, selected_disk_parts: Option>, + generated_disk_parts: Option>, } #[derive(Debug, Clone)] @@ -89,32 +89,55 @@ pub enum Message { Back, } - - impl PartitionStage { - pub fn gen_disk_layout_estimate(&self) -> PartLayout { - let dev = self.device.clone().unwrap(); + // pub fn gen_disk_layout_estimate(&self) -> PartLayout { + // let dev = self.device.clone().unwrap(); - let swap_size: u64 = match self.swap_mode { - Some(SwapMode::NoSwap) => 0, - Some(SwapMode::SwapHibernate) => 666*1024*1024, - Some(SwapMode::SwapNoHibernate) => 1024*1024*1024, - _ => 1024*1024*1024, - }; - let mut res: PartLayout = PartLayout::new(dev); + // let swap_size: u64 = match self.swap_mode { + // Some(SwapMode::NoSwap) => 0, + // Some(SwapMode::SwapHibernate) => 666 * 1024 * 1024, + // Some(SwapMode::SwapNoHibernate) => 1024 * 1024 * 1024, + // _ => 1024 * 1024 * 1024, + // }; + // let mut res: PartLayout = PartLayout::new(dev); - // UEFI partition - res = res.add_part(256*1024*1024, FSType::VFAT, Some("kira-efi".into()), Some("EFI".into()), Some("/efi".into())) - .add_part(512*1024*1024, FSType::XFS, Some("kira-boot-a".into()), None, Some("/".into())) - .add_part_reserve(swap_size, FSType::XFS, Some("kira-home".into()), None, Some("/home".into())); - - // swap - if swap_size > 0 { - res = res.add_part(swap_size, FSType::SWAP, Some("kira-swap".into()), None, Some("SWAP".into())); - } + // // UEFI partition + // res = res + // .add_part( + // 256 * 1024 * 1024, + // FSType::VFAT, + // Some("kira-efi".into()), + // Some("EFI".into()), + // Some("/efi".into()), + // ) + // .add_part( + // 512 * 1024 * 1024, + // FSType::XFS, + // Some("kira-boot-a".into()), + // None, + // Some("/".into()), + // ) + // .add_part_reserve( + // swap_size, + // FSType::XFS, + // Some("kira-home".into()), + // None, + // Some("/home".into()), + // ); - res - } + // // swap + // if swap_size > 0 { + // res = res.add_part( + // swap_size, + // FSType::SWAP, + // Some("kira-swap".into()), + // None, + // Some("SWAP".into()), + // ); + // } + + // res + // } pub fn new(toml_config: &Table) -> Result { let maybe_dev_list = BlkDev::list_sys_blk_dev(); @@ -126,13 +149,13 @@ impl PartitionStage { .and_then(|v| v.as_integer()) .unwrap_or(8192); - let min_disk_size: u64 = min_disk_size_mb.try_into().unwrap_or(8192) * 1024 * 1024; + let min_disk_size: BlkSize = BlkSize::new_mb(min_disk_size_mb.try_into().unwrap_or(8192)); match maybe_dev_list { Ok(dev_list) => { let acepted_dev: Vec = dev_list .iter() - .filter(|v| v.size >= min_disk_size) + .filter(|v| v.size.b() >= min_disk_size.b()) .cloned() .collect(); @@ -143,6 +166,7 @@ impl PartitionStage { use_zram: true, secure_boot: false, selected_disk_parts: None, + generated_disk_parts: None, }) } Err(ex) => Err(ex), @@ -174,7 +198,8 @@ impl PartitionStage { Message::SelectDevice(dev) => { self.selected_disk_parts = match PartLayout::read_from_disk(&dev) { Ok(dev_parts) => Some( - dev_parts.part_list + dev_parts + .part_list .iter() .map(|part_inf| part_to_ct_params(&part_inf, dev.size)) .collect(), @@ -185,10 +210,44 @@ impl PartitionStage { } }; + let swap_size: BlkSize = match self.swap_mode { + Some(SwapMode::NoSwap) => BlkSize::new(0), + Some(SwapMode::SwapHibernate) => BlkSize::new_gb(4), + Some(SwapMode::SwapNoHibernate) => BlkSize::new_gb(2), + _ => BlkSize::new_gb(2), + }; + self.generated_disk_parts = Some( + PartLayout::gen_classic_layout(dev.clone(), BlkSize::new_mb(512), swap_size) + .part_list + .iter() + .map(|part_info| part_to_ct_params(part_info, dev.size)) + .collect(), + ); + self.device = Some(dev); StageAction::None } Message::SelectSwapMode(sm) => { + + if let Some(dev) = self.device.clone() { + let swap_size: BlkSize = match sm { + SwapMode::NoSwap => BlkSize::new(0), + SwapMode::SwapHibernate => BlkSize::new_gb(4), + SwapMode::SwapNoHibernate => BlkSize::new_gb(2), + }; + self.generated_disk_parts = Some( + PartLayout::gen_classic_layout( + dev.clone(), + BlkSize::new_mb(512), + swap_size, + ) + .part_list + .iter() + .map(|part_info| part_to_ct_params(part_info, dev.size)) + .collect(), + ); + } + self.swap_mode = Some(sm); StageAction::None } @@ -206,23 +265,11 @@ impl PartitionStage { } pub fn view(&self) -> iced::Element<'_, Message> { - let dev_parts_es: Option> = if self.selected_disk_parts.is_some() - { - let p_l = self.gen_disk_layout_estimate(); - Some( - p_l.part_list.iter() - .map(|part_info| part_to_ct_params(part_info, p_l.dev.size)) - .collect() - ) - } else { - None - }; - - let dev_parts_text = match dev_parts_es { + let dev_parts_text = match self.generated_disk_parts { Some(_) => widget::text(t!("partition.current_after_install")), None => widget::text(""), }; - let dev_parts_content = kira_color_bar::color_bar(dev_parts_es, 48); + let dev_parts_content = kira_color_bar::color_bar(self.generated_disk_parts.clone(), 56); let dev_parts_content = widget::column![dev_parts_text, dev_parts_content]; let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back); @@ -232,7 +279,7 @@ impl PartitionStage { Some(_) => widget::text(t!("partition.current_dev_layout")), None => widget::text(""), }; - let selected_dev_content = kira_color_bar::color_bar(self.selected_disk_parts.clone(), 48); + let selected_dev_content = kira_color_bar::color_bar(self.selected_disk_parts.clone(), 56); let selected_dev_content = widget::column![selected_dev_text, selected_dev_content]; widget::column![