Validate file arguments: show non-images as errors in sidebar
Direct file args are now checked with is_image_file(); valid images are displayed as before, while non-image files appear in a red "Not images" section at the bottom of the sidebar. Directory args continue to be scanned recursively (or shallowly with --shallow). No-arg behavior is unchanged. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
1 parent
5ca5978e85
commit
6234f5ed52
4 files changed
+50
-5
No files matched your search
+10
-2
@@ -2,13 +2,14 @@ use gpui::*;
|
||||
use std::path::PathBuf;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use crate::image::{get_image_dimensions, scan_directory_for_images};
|
||||
use crate::image::{get_image_dimensions, is_image_file, scan_directory_for_images};
|
||||
use crate::rename::rename_file;
|
||||
use crate::text_input::TextInput;
|
||||
|
||||
pub struct ImageViewer {
|
||||
pub current_directory: Option<PathBuf>,
|
||||
pub image_files: Vec<PathBuf>,
|
||||
pub non_image_files: Vec<PathBuf>,
|
||||
pub current_index: usize,
|
||||
pub current_image_path: Option<PathBuf>,
|
||||
pub current_image_dimensions: Option<(u32, u32)>,
|
||||
@@ -31,6 +32,7 @@ impl ImageViewer {
|
||||
let mut app = Self {
|
||||
current_directory: None,
|
||||
image_files: Vec::new(),
|
||||
non_image_files: Vec::new(),
|
||||
current_index: 0,
|
||||
current_image_path: None,
|
||||
current_image_dimensions: None,
|
||||
@@ -48,13 +50,19 @@ impl ImageViewer {
|
||||
} else {
|
||||
// Collect explicit files and scan any directories
|
||||
let mut files: Vec<PathBuf> = Vec::new();
|
||||
let mut non_image_files: Vec<PathBuf> = Vec::new();
|
||||
for path in &paths {
|
||||
if path.is_file() {
|
||||
files.push(path.clone());
|
||||
if is_image_file(path) {
|
||||
files.push(path.clone());
|
||||
} else {
|
||||
non_image_files.push(path.clone());
|
||||
}
|
||||
} else if path.is_dir() {
|
||||
files.extend(scan_directory_for_images(path, recursive));
|
||||
}
|
||||
}
|
||||
app.non_image_files = non_image_files;
|
||||
files.sort();
|
||||
files.dedup();
|
||||
|
||||
|
||||
@@ -31,6 +31,10 @@ pub fn button_danger() -> Rgba {
|
||||
rgb(0x991b1b)
|
||||
}
|
||||
|
||||
pub fn text_error() -> Rgba {
|
||||
rgb(0xf87171)
|
||||
}
|
||||
|
||||
pub fn button_disabled() -> Rgba {
|
||||
rgb(0x6b7280)
|
||||
}
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ pub fn get_image_dimensions(path: &Path) -> Option<(u32, u32)> {
|
||||
/// Checks if a file is an image by reading its magic number/header
|
||||
/// Only accepts formats that the `image` crate can load (default-formats feature).
|
||||
/// Supported formats: avif, bmp, dds, exr, ff, gif, hdr, ico, jpeg, png, pnm, qoi, tga, tiff, webp
|
||||
fn is_image_file(path: &Path) -> bool {
|
||||
pub fn is_image_file(path: &Path) -> bool {
|
||||
// Skip if not a file
|
||||
if !path.is_file() {
|
||||
return false;
|
||||
|
||||
@@ -77,7 +77,24 @@ fn render_sidebar(app: &mut ImageViewer, cx: &mut Context<ImageViewer>) -> impl
|
||||
}
|
||||
}
|
||||
|
||||
div()
|
||||
let mut error_items: Vec<AnyElement> = Vec::new();
|
||||
for path in &app.non_image_files {
|
||||
let filename = path
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().to_string())
|
||||
.unwrap_or_else(|| path.to_string_lossy().to_string());
|
||||
error_items.push(
|
||||
div()
|
||||
.w_full()
|
||||
.px_3()
|
||||
.py_2()
|
||||
.text_color(colors::text_error())
|
||||
.child(format!("{}: not an image", filename))
|
||||
.into_any_element(),
|
||||
);
|
||||
}
|
||||
|
||||
let mut sidebar = div()
|
||||
.w(px(300.0))
|
||||
.h_full()
|
||||
.bg(colors::background_medium())
|
||||
@@ -102,7 +119,23 @@ fn render_sidebar(app: &mut ImageViewer, cx: &mut Context<ImageViewer>) -> impl
|
||||
.min_h_0()
|
||||
.overflow_y_scroll()
|
||||
.child(div().flex().flex_col().children(items)),
|
||||
)
|
||||
);
|
||||
|
||||
if !error_items.is_empty() {
|
||||
sidebar = sidebar
|
||||
.child(
|
||||
div()
|
||||
.px_3()
|
||||
.py_2()
|
||||
.border_t_1()
|
||||
.border_color(colors::background_dark())
|
||||
.text_color(colors::text_error())
|
||||
.child(format!("Not images ({})", error_items.len())),
|
||||
)
|
||||
.child(div().flex().flex_col().children(error_items));
|
||||
}
|
||||
|
||||
sidebar
|
||||
}
|
||||
|
||||
pub fn view(
|
||||
|
||||
Reference in new issue
Block a user