Skip to content

Commit 69c1ba8

Browse files
committed
Add size field, improve info, fix eject
1 parent 7746a76 commit 69c1ba8

5 files changed

Lines changed: 68 additions & 18 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mmtui"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
authors = ["Lutsai Aleksandr <s.lyra@ya.ru>"]
66
description = "Terminal User Interface disk mount manager for TUI file managers"

src/drives.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ use std::collections::HashMap;
44
#[derive(Debug, Clone)]
55
pub struct Block {
66
pub object_path: String,
7+
pub drive_path: String,
78
pub dev: String,
89
pub label: String,
910
pub mount: Option<String>,
1011
pub fstype: String,
1112
pub mounted: bool,
13+
pub size: String,
1214
}
1315

1416
#[derive(Debug, Clone)]
@@ -56,8 +58,14 @@ pub async fn collect_drives_from_udisk() -> udisks2::Result<Vec<Drive>> {
5658
}
5759
} else if let Ok(blk) = i.block().await {
5860
let drv_path = blk.drive().await?.to_string();
61+
let size = if let Ok(size) = blk.size().await {
62+
client.size_for_display(size, true, false)
63+
} else {
64+
String::new()
65+
};
5966
let block = Block {
6067
object_path: path,
68+
drive_path: drv_path.clone(),
6169
dev: String::from_utf8_lossy(&blk.device().await?)
6270
.chars()
6371
.filter(|c| c != &'\0')
@@ -66,6 +74,7 @@ pub async fn collect_drives_from_udisk() -> udisks2::Result<Vec<Drive>> {
6674
mount: None,
6775
fstype: blk.id_type().await?,
6876
mounted: false,
77+
size,
6978
};
7079

7180
if let Some(d) = drives.iter_mut().find(|i| i.object_path == drv_path) {
@@ -108,11 +117,13 @@ pub async fn collect_all() -> udisks2::Result<Vec<Drive>> {
108117
} else {
109118
fstab.blocks.push(Block {
110119
object_path: String::new(),
120+
drive_path: fstab.object_path.clone(),
111121
dev: i.dev,
112122
label: String::new(),
113123
mount: i.path,
114124
fstype: i.fs,
115125
mounted: i.mounted,
126+
size: String::new(),
116127
});
117128
}
118129
}
@@ -151,3 +162,16 @@ pub async fn unmount(block: &Block) -> udisks2::Result<()> {
151162

152163
Ok(())
153164
}
165+
166+
pub async fn eject(block: &Block) -> udisks2::Result<()> {
167+
let client = udisks2::Client::new().await?;
168+
169+
client
170+
.object(block.drive_path.clone())?
171+
.drive()
172+
.await?
173+
.eject(HashMap::new())
174+
.await?;
175+
176+
Ok(())
177+
}

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ async fn main() -> udisks2::Result<()> {
2727
let s = state.clone();
2828
tokio::spawn(async move {
2929
loop {
30-
let drv = drives::collect_all().await.unwrap();
31-
s.lock().await.clone_from(&drv);
30+
if let Ok(drv) = drives::collect_all().await {
31+
s.lock().await.clone_from(&drv);
32+
};
3233
tokio::time::sleep(Duration::from_millis(500)).await;
3334
}
3435
});

src/tui.rs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@ pub struct Tui {
2424
pub last_status: String,
2525
}
2626

27+
const HELP: &str = "j/▲⋮k/▼⋮l/o/▶ - CD⋮m - Mount⋮u - Unmount⋮e - Eject⋮q - Quit";
28+
2729
impl Tui {
30+
fn set_status(&mut self, res: udisks2::Result<()>) {
31+
self.last_status = match res {
32+
Ok(()) => String::from("Ok"),
33+
Err(e) => format!("Error: {e:?}"),
34+
}
35+
}
36+
2837
pub async fn input(&mut self, key: KeyEvent) -> InputResult {
2938
match key.code {
3039
KeyCode::Up | KeyCode::Char('k') => {
@@ -37,21 +46,31 @@ impl Tui {
3746
}
3847
KeyCode::Char('m') => {
3948
if let Some(b) = &self.selected {
40-
self.last_status = format!("{:?}", drives::mount(b).await);
49+
self.set_status(drives::mount(b).await);
4150
}
4251
InputResult::None
4352
}
4453
KeyCode::Char('u') => {
4554
if let Some(b) = &self.selected {
46-
self.last_status = format!("{:?}", drives::unmount(b).await);
55+
self.set_status(drives::unmount(b).await);
4756
}
4857
InputResult::None
4958
}
50-
KeyCode::Esc | KeyCode::Char('q') => InputResult::Quit,
51-
KeyCode::Enter => {
52-
let output = self.selected.clone().unwrap().mount.unwrap();
53-
InputResult::QuitChangeDirectory(output)
59+
KeyCode::Char('e') => {
60+
if let Some(b) = &self.selected {
61+
self.set_status(drives::eject(b).await);
62+
}
63+
InputResult::None
5464
}
65+
KeyCode::Enter | KeyCode::Char('l' | 'o') => {
66+
if let Some(s) = &self.selected {
67+
let output = s.clone().mount.unwrap_or_default();
68+
InputResult::QuitChangeDirectory(output)
69+
} else {
70+
InputResult::Quit
71+
}
72+
}
73+
KeyCode::Esc | KeyCode::Char('q') => InputResult::Quit,
5574
_ => InputResult::None,
5675
}
5776
}
@@ -81,11 +100,20 @@ impl Tui {
81100
.and_then(|n| rows.get(n).cloned())
82101
.clone_into(&mut self.selected);
83102

103+
let max_size_field_length: u16 = rows
104+
.iter()
105+
.map(|r| r.size.len())
106+
.max()
107+
.unwrap_or(0)
108+
.try_into()
109+
.unwrap_or(0);
110+
84111
let rows = rows.iter().map(|i| {
85112
Row::new(vec![
86113
i.dev.clone(),
87114
i.label.clone(),
88115
i.mount.clone().unwrap_or_default(),
116+
i.size.clone(),
89117
if i.mounted {
90118
"M".to_owned()
91119
} else {
@@ -97,7 +125,8 @@ impl Tui {
97125
Constraint::Ratio(1, 3),
98126
Constraint::Ratio(1, 3),
99127
Constraint::Ratio(1, 3),
100-
Constraint::Length(3),
128+
Constraint::Length(max_size_field_length),
129+
Constraint::Length(1),
101130
];
102131
let table = Table::new(rows, widths)
103132
.row_highlight_style(Color::Green)
@@ -123,18 +152,14 @@ impl Tui {
123152
};
124153

125154
format!(
126-
"dev: {:?} label: {:?} type: {:?} {mounted} ",
127-
s.dev, s.label, s.fstype
155+
"dev: {:?} label: {:?} type: {:?} size {} {mounted}",
156+
s.dev, s.label, s.fstype, s.size
128157
)
129158
}
130159
None => String::new(),
131160
};
132161

133-
let info = format!(
134-
"j - UP, k - DOWN, l - Goto mountpoint, m - Mount, u - Unmount, e - Eject\n{descr} {:?}",
135-
self.last_status
136-
);
137-
162+
let info = format!("{} | {HELP}\n{descr}", self.last_status);
138163
let info = Paragraph::new(info).wrap(Wrap { trim: true });
139164
frame.render_widget(info, layout[1]);
140165
}

0 commit comments

Comments
 (0)