6 files changed, 469 insertions(+), 45 deletions(-)
@@ -3,6 +3,21 @@
version = 4
[[package]]
name = "addr2line"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b5d307320b3181d6d7954e663bd7c774a838b8220fe0593c86d9fb09f498b4b"
dependencies = [
"gimli",
]
[[package]]
name = "adler2"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"
[[package]]
name = "aho-corasick"
version = "1.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -18,6 +33,56 @@
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "anstream"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000"
[[package]]
name = "anstyle-parse"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc"
dependencies = [
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d"
dependencies = [
"anstyle",
"once_cell_polyfill",
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.102"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -37,6 +102,21 @@
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "backtrace"
version = "0.3.76"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb531853791a215d7c62a30daf0dde835f381ab5de4589cfe7c649d2cbe92bd6"
dependencies = [
"addr2line",
"cfg-if",
"libc",
"miniz_oxide",
"object",
"rustc-demangle",
"windows-link",
]
[[package]]
name = "base64"
@@ -118,8 +198,81 @@
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724"
[[package]]
name = "clap"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ddb117e43bbf7dacf0a4190fef4d345b9bad68dfc649cb349e7d17d28428e51"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ce8604710f6733aa641a2b3731eaa1e8b3d9973d5e3565da11800813f997a9"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.117",
]
[[package]]
name = "clap_lex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9"
[[package]]
name = "color-eyre"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5920befb47832a6d61ee3a3a846565cfa39b331331e68a3b1d1116630f2f26d"
dependencies = [
"backtrace",
"color-spantrace",
"eyre",
"indenter",
"once_cell",
"owo-colors",
"tracing-error",
]
[[package]]
name = "color-spantrace"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8b88ea9df13354b55bc7234ebcce36e6ef896aca2e42a15de9e10edce01b427"
dependencies = [
"once_cell",
"owo-colors",
"tracing-core",
"tracing-error",
]
[[package]]
name = "colorchoice"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570"
[[package]]
name = "compact_str"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -320,6 +473,16 @@
]
[[package]]
name = "eyre"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec"
dependencies = [
"indenter",
"once_cell",
]
[[package]]
name = "fancy-regex"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -406,6 +569,12 @@
]
[[package]]
name = "gimli"
version = "0.32.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e629b9b98ef3dd8afe6ca2bd0f89306cec16d43d907889945bc5d6687f2f13c7"
[[package]]
name = "hashbrown"
version = "0.15.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -448,6 +617,12 @@
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "indenter"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "964de6e86d545b246d84badc0fef527924ace5134f30641c203ef52ba83f58d5"
[[package]]
name = "indexmap"
@@ -484,6 +659,12 @@
]
[[package]]
name = "is_terminal_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695"
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -593,6 +774,8 @@
version = "0.1.0"
dependencies = [
"bytesize",
"clap",
"color-eyre",
"crossterm",
"ratatui",
"serde_json",
@@ -636,6 +819,15 @@
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
[[package]]
name = "miniz_oxide"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316"
dependencies = [
"adler2",
]
[[package]]
name = "mio"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -703,6 +895,15 @@
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "object"
version = "0.37.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff76201f031d8863c38aa7f905eca4f53abbfa15f609db4277d44cd8938f33fe"
dependencies = [
"memchr",
]
[[package]]
@@ -710,6 +911,12 @@
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "once_cell_polyfill"
version = "1.70.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe"
[[package]]
name = "ordered-float"
@@ -719,6 +926,12 @@
dependencies = [
"num-traits",
]
[[package]]
name = "owo-colors"
version = "4.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d211803b9b6b570f68772237e415a029d5a50c65d382910b879fb19d3271f94d"
[[package]]
name = "parking_lot"
@@ -839,6 +1052,12 @@
]
[[package]]
name = "pin-project-lite"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd"
[[package]]
name = "portable-atomic"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1027,6 +1246,12 @@
version = "0.8.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc897dd8d9e8bd1ed8cdad82b5966c3e0ecae09fb1907d58efaa013543185d0a"
[[package]]
name = "rustc-demangle"
version = "0.1.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b50b8869d9fc858ce7266cce0194bd74df58b9d0e3f6df3a9fc8eb470d95c09d"
[[package]]
name = "rustc_version"
@@ -1129,6 +1354,15 @@
]
[[package]]
name = "sharded-slab"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6"
dependencies = [
"lazy_static",
]
[[package]]
name = "signal-hook"
version = "0.3.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1330,6 +1564,15 @@
]
[[package]]
name = "thread_local"
version = "1.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185"
dependencies = [
"cfg-if",
]
[[package]]
name = "time"
version = "0.3.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1349,8 +1592,49 @@
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7694e1cfe791f8d31026952abf09c69ca6f6fa4e1a1229e18988f06a04a12dca"
[[package]]
name = "tracing"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100"
dependencies = [
"pin-project-lite",
"tracing-core",
]
[[package]]
name = "tracing-core"
version = "0.1.36"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a"
dependencies = [
"once_cell",
"valuable",
]
[[package]]
name = "tracing-error"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b1581020d7a273442f5b45074a6a57d5757ad0a47dac0e9f0bd57b81936f3db"
dependencies = [
"tracing",
"tracing-subscriber",
]
[[package]]
name = "tracing-subscriber"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319"
dependencies = [
"sharded-slab",
"thread_local",
"tracing-core",
]
[[package]]
name = "typenum"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -1414,6 +1698,12 @@
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "valuable"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65"
[[package]]
name = "version_check"
@@ -5,6 +5,8 @@
[dependencies]
bytesize = "2.3.1"
clap = { version = "4.6.1", features = ["derive"] }
color-eyre = "0.6.5"
crossterm = "0.29.0"
ratatui = "0.30.0"
serde_json = "1.0.149"
@@ -1,0 +1,28 @@
use crate::disks::BlockDevice;
use clap::{ArgAction, Parser};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
pub struct Args {
#[arg(short = 'O', long, action = ArgAction::SetTrue)]
/// output all columns
pub output_all: bool,
#[arg(short, long)]
/// output columns see (--list-columns)
pub output: Option<String>
}
#[derive(Debug)]
pub struct App {
pub disks: Vec<BlockDevice>,
pub args: Args
}
impl App {
pub fn new(disks: Vec<BlockDevice>) -> Self {
let args = Args::parse();
Self { disks, args }
}
}
@@ -1,10 +1,9 @@
use std::{path::PathBuf, process::Command};
use bytesize::ByteSize;
use color_eyre::{Result, eyre::Context};
use crate::LsblyError;
#[derive(Debug, Default, PartialEq)]
#[derive(Debug, Default, PartialEq, Clone)]
pub struct BlockDevice {
// like "nvme0n1"
pub name: Option<String>,
@@ -45,7 +44,7 @@
// TODO: find library or using code not command.
impl Disks {
pub fn list() -> Result<Vec<BlockDevice>, LsblyError> {
pub fn list() -> Result<Vec<BlockDevice>> {
let json_disks: serde_json::Value = self::Disks::run_lsblk()?;
let mut disks: Vec<BlockDevice> = Vec::new();
@@ -117,20 +116,19 @@
Ok(disks)
}
pub fn run_lsblk() -> Result<serde_json::Value, LsblyError> {
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()
.map_err(|_| LsblyError::CantRunLsblk)?;
.output().context("failed to run lsblk")?;
let stdout_str = String::from_utf8(output.stdout).map_err(|_| LsblyError::InvalidUtf8)?;
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).map_err(|_| LsblyError::JsonParseError)?;
serde_json::from_str(&stdout_str).context("failed to parse cmd output")?;
Ok(json_output)
}
@@ -1,44 +1,44 @@
use crate::disks::{Disks};
use crate::{app::App, disks::Disks};
use color_eyre::{Result, eyre::Context};
mod disks;
mod tui;
mod app;
#[derive(Debug)]
pub enum LsblyError {
CantRunLsblk,
InvalidUtf8,
JsonParseError,
}
fn main() -> Result<()> {
color_eyre::install()?;
fn main() -> Result<(), LsblyError> {
println!("Hello, world!");
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")?;
let disks = Disks::list()?;
// TODO: add mountpoints, vendor, uuid && use ratatui and crossterm instead od raw output
for disk in disks {
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());
println!("size: {}", disk.size.unwrap().display());
println!("fssize: {}", disk.fssize.unwrap().display());
println!("fsused: {}", disk.fsused.unwrap().display());
println!("fsavailable: {}", disk.fsavailable.unwrap().display());
if let Some(child_list) = disk.children {
for partition in child_list {
println!(" name: {}", partition.name.as_deref().unwrap_or_default());
println!(" path: {}", partition.path.unwrap().as_os_str().to_str().unwrap_or_default());
println!(" label: {}", partition.label.as_deref().unwrap_or_default());
println!(" partlabel: {}", partition.partlabel.as_deref().unwrap_or_default());
println!(" partflags: {}", partition.partflags.as_deref().unwrap_or_default());
println!(" partuuid: {}", partition.partuuid.as_deref().unwrap_or_default());
println!(" size: {}", partition.size.unwrap().display());
println!(" fssize: {}", partition.fssize.unwrap().display());
println!(" fsused: {}", partition.fsused.unwrap().display());
println!(" fsavailable: {}", partition.fsavailable.unwrap().display());
println!(" ---");
}
}
println!("---");
}
// for disk in disks {
// 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());
// println!("size: {}", disk.size.unwrap().display());
// println!("fssize: {}", disk.fssize.unwrap().display());
// println!("fsused: {}", disk.fsused.unwrap().display());
// println!("fsavailable: {}", disk.fsavailable.unwrap().display());
// if let Some(child_list) = disk.children {
// for partition in child_list {
// println!(" name: {}", partition.name.as_deref().unwrap_or_default());
// println!(" path: {}", partition.path.unwrap().as_os_str().to_str().unwrap_or_default());
// println!(" label: {}", partition.label.as_deref().unwrap_or_default());
// println!(" partlabel: {}", partition.partlabel.as_deref().unwrap_or_default());
// println!(" partflags: {}", partition.partflags.as_deref().unwrap_or_default());
// println!(" partuuid: {}", partition.partuuid.as_deref().unwrap_or_default());
// println!(" size: {}", partition.size.unwrap().display());
// println!(" fssize: {}", partition.fssize.unwrap().display());
// println!(" fsused: {}", partition.fsused.unwrap().display());
// println!(" fsavailable: {}", partition.fsavailable.unwrap().display());
// println!(" ---");
// }
// }
// println!("---");
// }
Ok(())
}
@@ -1,0 +1,106 @@
use std::io::{Stdout, Write};
use color_eyre::{Result, eyre::Context};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use ratatui::{
DefaultTerminal, Frame, Terminal, TerminalOptions, Viewport,
layout::Constraint,
prelude::CrosstermBackend,
style::{Color, Style, Stylize},
widgets::{Block, BorderType, Paragraph, Row, Table},
};
use crate::{app::App, disks::BlockDevice};
pub fn run(terminal: &mut DefaultTerminal, app: &mut App) -> Result<()> {
terminal.draw(|frame| render(frame, &app.disks))?;
teardown_terminal(terminal)?;
Ok(())
}
fn render(frame: &mut Frame, disks: &Vec<BlockDevice>) {
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 {
rows.push(make_row(disk, ""));
if let Some(children) = &disk.children {
let mut iter = children.iter().peekable();
while let Some(child) = iter.next() {
let is_last_child = iter.peek().is_none();
let prefix = if is_last_child {
" └─ "
} else {
" ├─ "
};
rows.push(make_row(child, prefix));
}
}
}
let widths = [
Constraint::Length(20),
Constraint::Length(25),
Constraint::Length(10),
];
let table = Table::new(rows, widths)
.header(header)
.column_spacing(1)
.block(Block::bordered().border_type(BorderType::Rounded))
.style(Color::White)
.row_highlight_style(Style::new().on_black().bold())
.column_highlight_style(Color::Gray)
.cell_highlight_style(Style::new().reversed().yellow())
.highlight_symbol("🍴 ");
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());
let path = disk
.path
.as_ref()
.and_then(|p| p.to_str())
.unwrap_or_default();
let label = disk.label.as_deref().unwrap_or_default();
Row::new([name.to_string(), path.to_string(), label.to_string()])
}
pub fn setup_terminal(app: &mut App) -> Result<Terminal<CrosstermBackend<Stdout>>> {
let stdout = std::io::stdout();
enable_raw_mode()?;
let total_devices: u16 = app
.disks
.iter()
.map(|disk| 1 + disk.children.as_ref().map_or(0, |child| child.len()))
.sum::<usize>() as u16;
let needed_lines: u16 = total_devices + 4; // 4 = borders(2) + header(2)
let backend = CrosstermBackend::new(stdout);
Terminal::with_options(
backend,
TerminalOptions {
viewport: Viewport::Inline(needed_lines),
},
)
.context("Failed to setup terminal")
}
fn teardown_terminal(_terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
let mut stdout = std::io::stdout();
disable_raw_mode()?;
// Clean line so percentage sign dont show.
println!();
stdout.flush()?;
Ok(())
}