6 files changed, 161 insertions(+), 158 deletions(-)
@@ -779,6 +779,7 @@
"crossterm",
"ratatui",
"serde_json",
"sysinfo",
]
[[package]]
@@ -898,6 +899,25 @@
]
[[package]]
name = "objc2-core-foundation"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2a180dd8642fa45cdb7dd721cd4c11b1cadd4929ce112ebd8b9f5803cc79d536"
dependencies = [
"bitflags 2.11.0",
]
[[package]]
name = "objc2-io-kit"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33fafba39597d6dc1fb709123dfa8289d39406734be322956a69f0931c73bb15"
dependencies = [
"libc",
"objc2-core-foundation",
]
[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1458,6 +1478,18 @@
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "sysinfo"
version = "0.39.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2071df9448915b71c4fe6d25deaf1c22f12bd234f01540b77312bb8e41361e6"
dependencies = [
"libc",
"objc2-core-foundation",
"objc2-io-kit",
"windows",
]
[[package]]
@@ -1916,18 +1948,122 @@
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580"
dependencies = [
"windows-collections",
"windows-core",
"windows-future",
"windows-numerics",
]
[[package]]
name = "windows-collections"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610"
dependencies = [
"windows-core",
]
[[package]]
name = "windows-core"
version = "0.62.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
dependencies = [
"windows-implement",
"windows-interface",
"windows-link",
"windows-result",
"windows-strings",
]
[[package]]
name = "windows-future"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb"
dependencies = [
"windows-core",
"windows-link",
"windows-threading",
]
[[package]]
name = "windows-implement"
version = "0.60.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "windows-interface"
version = "0.59.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-numerics"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26"
dependencies = [
"windows-core",
"windows-link",
]
[[package]]
name = "windows-result"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-strings"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
[[package]]
name = "windows-threading"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37"
dependencies = [
"windows-link",
]
@@ -10,3 +10,4 @@
crossterm = "0.29.0"
ratatui = "0.30.0"
serde_json = "1.0.149"
sysinfo = { version = "0.39.6", default-features = false, features = ["disk"] }
@@ -1,5 +1,5 @@
use crate::disks::BlockDevice;
use clap::{ArgAction, Parser};
use sysinfo::Disks;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@@ -15,12 +15,12 @@
#[derive(Debug)]
pub struct App {
pub disks: Vec<BlockDevice>,
pub disks: Disks,
pub args: Args
}
impl App {
pub fn new(disks: Vec<BlockDevice>) -> Self {
pub fn new(disks: Disks) -> Self {
let args = Args::parse();
Self { disks, args }
@@ -1,135 +1,0 @@
use std::{path::PathBuf, process::Command};
use bytesize::ByteSize;
use color_eyre::{Result, eyre::Context};
#[derive(Debug, Default, PartialEq, Clone)]
pub struct BlockDevice {
// like "nvme0n1"
pub name: Option<String>,
// like "/dev/nvme0n1"
pub path: Option<PathBuf>,
// like "/home"
pub mountpoint: Option<PathBuf>,
pub mountpoints: Option<Vec<PathBuf>>,
// like "aaa123-bbb456-ccc789"
pub uuid: Option<String>,
pub vendor: Option<String>,
pub label: Option<String>,
pub children: Option<Vec<BlockDevice>>,
pub partlabel: Option<String>,
pub partflags: Option<String>,
pub partuuid: Option<String>,
pub size: Option<ByteSize>,
pub fssize: Option<ByteSize>,
pub fsused: Option<ByteSize>,
pub fsavailable: Option<ByteSize>,
}
pub struct Disks();
// TODO: find library or using code not command.
impl Disks {
pub fn list() -> Result<Vec<BlockDevice>> {
let json_disks: serde_json::Value = self::Disks::run_lsblk()?;
let mut disks: Vec<BlockDevice> = Vec::new();
if let Some(device_list) = json_disks["blockdevices"].as_array() {
for disk in device_list {
let mut mountpoints: Vec<PathBuf> = Vec::new();
if let Some(mountpoints_list) = disk["mountpoints"].as_array() {
for mountpoint in mountpoints_list {
mountpoints.push(PathBuf::from(mountpoint.as_str().unwrap_or_default()));
}
}
let mut children: Vec<BlockDevice> = Vec::new();
if let Some(child_list) = disk["children"].as_array() {
for child in child_list {
let mut mountpoints: Vec<PathBuf> = Vec::new();
if let Some(mountpoints_list) = child["mountpoints"].as_array() {
for mountpoint in mountpoints_list {
mountpoints
.push(PathBuf::from(mountpoint.as_str().unwrap_or_default()));
}
}
children.push(BlockDevice {
name: Some(child["name"].as_str().unwrap_or_default().to_string()),
path: Some(PathBuf::from(child["path"].as_str().unwrap_or_default())),
mountpoint: Some(PathBuf::from(
child["mountpoint"].as_str().unwrap_or_default(),
)),
mountpoints: Some(mountpoints),
uuid: Some(child["uuid"].as_str().unwrap_or_default().to_string()),
vendor: Some(child["vendor"].as_str().unwrap_or_default().to_string()),
label: Some(child["label"].as_str().unwrap_or_default().to_string()),
partlabel: Some(child["partlabel"].as_str().unwrap_or_default().to_string()),
partflags: Some(child["partflags"].as_str().unwrap_or_default().to_string()),
partuuid: Some(child["partuuid"].as_str().unwrap_or_default().to_string()),
size: Some(ByteSize::b(child["size"].as_u64().unwrap_or(0))),
fssize: Some(ByteSize::b(child["fssize"].as_u64().unwrap_or(0))),
fsavailable: Some(ByteSize::b(
child["fssize"].as_u64().unwrap_or(0)
- child["fsused"].as_u64().unwrap_or(0),
)),
fsused: Some(ByteSize::b(child["fsused"].as_u64().unwrap_or(0))),
..Default::default()
});
}
}
disks.push(BlockDevice {
name: Some(disk["name"].as_str().unwrap_or_default().to_string()),
path: Some(PathBuf::from(disk["path"].as_str().unwrap_or_default())),
mountpoint: Some(PathBuf::from(
disk["mountpoint"].as_str().unwrap_or_default(),
)),
mountpoints: Some(mountpoints),
uuid: Some(disk["uuid"].as_str().unwrap_or_default().to_string()),
vendor: Some(disk["vendor"].as_str().unwrap_or_default().to_string()),
label: Some(disk["label"].as_str().unwrap_or_default().to_string()),
size: Some(ByteSize::b(disk["size"].as_u64().unwrap_or(0))),
fssize: Some(ByteSize::b(disk["fssize"].as_u64().unwrap_or(0))),
fsavailable: Some(ByteSize::b(
disk["fssize"].as_u64().unwrap_or(0) - disk["fsused"].as_u64().unwrap_or(0),
)),
fsused: Some(ByteSize::b(disk["fsused"].as_u64().unwrap_or(0))),
children: Some(children),
..Default::default()
});
}
}
Ok(disks)
}
pub fn run_lsblk() -> Result<serde_json::Value> {
let output = Command::new("lsblk")
.args([
"-o",
"NAME,MOUNTPOINT,PATH,SIZE,UUID,LABEL,PARTLABEL,PARTFLAGS,PARTUUID,FSSIZE,FSUSED",
"-Jb",
])
.output().context("failed to run lsblk")?;
let stdout_str = String::from_utf8(output.stdout).context("failed to extract string from cmd output")?;
let json_output: serde_json::Value =
serde_json::from_str(&stdout_str).context("failed to parse cmd output")?;
Ok(json_output)
}
}
@@ -1,21 +1,21 @@
use crate::{app::App, disks::Disks};
use color_eyre::{Result, eyre::Context};
use color_eyre::Result;
use sysinfo::{DiskRefreshKind, Disks};
mod disks;
mod tui;
mod app;
// use crate::app::App;
// mod tui;
// mod app;
fn main() -> Result<()> {
color_eyre::install()?;
let disks = Disks::list().context("failed to run disks")?;
let mut app = App::new(disks);
let mut terminal = tui::setup_terminal(&mut app)?;
tui::run(&mut terminal, &mut app).context("failed to run tui")?;
// TODO: add mountpoints, vendor, uuid && use ratatui and crossterm instead od raw output
let disks = Disks::new_with_refreshed_list_specifics(DiskRefreshKind::everything());
// let mut app = App::new(disks);
// let mut terminal = tui::setup_terminal(&mut app)?;
// tui::run(&mut terminal, &mut app).context("failed to run tui")?;
// for disk in disks {
for disk in disks.list() {
println!("{:#?}", disk)
// println!("name: {}", disk.name.as_deref().unwrap_or_default());
// println!("path: {}", disk.path.unwrap().as_os_str().to_str().unwrap_or_default());
// println!("label: {}", disk.label.as_deref().unwrap_or_default());
@@ -39,6 +39,6 @@
// }
// }
// println!("---");
// }
}
Ok(())
}
@@ -9,23 +9,24 @@
style::{Color, Style, Stylize},
widgets::{Block, BorderType, Paragraph, Row, Table},
};
use sysinfo::{Disk, Disks};
use crate::{app::App, disks::BlockDevice};
use crate::{app::App};
pub fn run(terminal: &mut DefaultTerminal, app: &mut App) -> Result<()> {
terminal.draw(|frame| render(frame, &app.disks))?;
terminal.draw(|frame| render(frame, app))?;
teardown_terminal(terminal)?;
Ok(())
}
fn render(frame: &mut Frame, disks: &Vec<BlockDevice>) {
fn render(frame: &mut Frame, app: &mut App) {
let header = Row::new(["Name", "Path", "Label"])
.style(Style::new().bold())
.bottom_margin(1);
let mut rows: Vec<Row> = Vec::new();
for disk in disks {
for disk in app.disks.list() {
rows.push(make_row(disk, ""));
if let Some(children) = &disk.children {
let mut iter = children.iter().peekable();
@@ -62,14 +63,14 @@
frame.render_widget(table, frame.area());
}
fn make_row(disk: &BlockDevice, prefix: &str) -> Row<'static> {
let name = format!("{}{}", prefix, disk.name.as_deref().unwrap_or_default());
fn make_row(disk: &Disk, prefix: &str) -> Row<'static> {
let name = format!("{}{:?}", prefix, disk.name());
let path = disk
.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or_default();
let label = disk.label.as_deref().unwrap_or_default();
let label = disk.as_deref().unwrap_or_default();
Row::new([name.to_string(), path.to_string(), label.to_string()])
}