Skip to content

Commit

Permalink
Add custom right panel to file dialog to show more info about files
Browse files Browse the repository at this point in the history
  • Loading branch information
crumblingstatue committed Oct 25, 2024
1 parent d66f3bc commit 4a59e37
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ parking_lot = "0.12.1"
egui_code_editor = "0.2.10"
# luajit breaks with panic=abort, because it relies on unwinding for exception handling
mlua = { version = "0.9.7", features = ["lua54", "vendored"] }
egui-file-dialog = { git = "https://github.com/fluxxcode/egui-file-dialog.git" }
egui-file-dialog = { git = "https://github.com/crumblingstatue/egui-file-dialog.git", branch = "active-entry" }
human_bytes = "0.4.3"
shlex = "1.3.0"
smart-default = "0.7.1"
Expand Down
84 changes: 82 additions & 2 deletions src/gui/file_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@ use {
meta::{region::Region, ViewKey},
shell::{msg_fail, msg_if_fail},
source::Source,
util::human_size_u64,
value_color::{self, ColorMethod},
},
anyhow::Context as _,
egui_file_dialog::FileDialog,
std::{fs::OpenOptions, io::Write as _, path::Path},
std::{
fs::OpenOptions,
io::Write as _,
path::{Path, PathBuf},
},
};

struct EntInfo {
meta: std::io::Result<std::fs::Metadata>,
mime: Option<&'static str>,
}

pub struct FileOps {
pub dialog: FileDialog,
pub op: Option<FileOp>,
preview_cache: PathCache<EntInfo>,
}

impl Default for FileOps {
Expand All @@ -28,6 +39,35 @@ impl Default for FileOps {
.anchor(egui::Align2::CENTER_CENTER, egui::vec2(0., 0.))
.allow_path_edit_to_save_file_without_extension(true),
op: Default::default(),
preview_cache: PathCache::default(),
}
}
}

pub struct PathCache<V> {
key: PathBuf,
value: Option<V>,
}

impl<V> Default for PathCache<V> {
fn default() -> Self {
Self {
key: PathBuf::default(),
value: None,
}
}
}

impl<V> PathCache<V> {
fn get_or_compute<F: FnOnce(&Path) -> V>(&mut self, k: &Path, f: F) -> &V {
if self.key != k {
self.key = k.to_path_buf();
self.value.insert(f(k))
} else {
self.value.get_or_insert_with(|| {
self.key = k.to_path_buf();
f(k)
})
}
}
}
Expand Down Expand Up @@ -60,7 +100,47 @@ impl FileOps {
font_size: u16,
line_spacing: u16,
) {
self.dialog.update(ctx);
self.dialog.update_with_custom_right_panel(ctx, &mut |ui, dia| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Truncate);
if let Some(highlight) = dia.active_entry() {
if let Some(parent) = highlight.as_path().parent() {
ui.label(egui::RichText::new(parent.display().to_string()).small());
}
if let Some(filename) = highlight.as_path().file_name() {
ui.label(filename.to_string_lossy());
}
ui.separator();
let ent_info =
self.preview_cache.get_or_compute(highlight.as_path(), |path| EntInfo {
meta: std::fs::metadata(path),
mime: tree_magic_mini::from_filepath(path),
});
if let Some(mime) = ent_info.mime {
ui.label(mime);
}
match &ent_info.meta {
Ok(meta) => {
let ft = meta.file_type();
if ft.is_file() {
ui.label(format!("Size: {}", human_size_u64(meta.len())));
}
if ft.is_symlink() {
ui.label("Symbolic link");
}
}
Err(e) => {
ui.label(e.to_string());
}
}
if ui.button("📋 Copy path to clipboard").clicked() {
ui.output_mut(|out| {
out.copied_text = highlight.as_path().display().to_string();
});
}
} else {
ui.heading("Hexerator");
}
});
if let Some(path) = self.dialog.take_selected()
&& let Some(op) = self.op.take()
{
Expand Down
8 changes: 8 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@
pub fn human_size(size: usize) -> String {
human_bytes::human_bytes(size as f64)
}

#[expect(
clippy::cast_precision_loss,
reason = "This is just an approximation of data size"
)]
pub fn human_size_u64(size: u64) -> String {
human_bytes::human_bytes(size as f64)
}

0 comments on commit 4a59e37

Please sign in to comment.