From 8288a0a287d89c6f17a9046be54cc0ca0fe3d1da Mon Sep 17 00:00:00 2001 From: electria Date: Tue, 3 Nov 2026 13:04:16 -0800 Subject: [PATCH] feat: "x" to copy image_path --- src/main.rs | 43 +++++++++++++++++++++++++++++-------------- src/usage.txt | 2 ++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index 271fcb5..b6447ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,4 @@ -use std::{ - borrow::Cow, - env, - ffi::OsStr, - os::unix::fs::MetadataExt, - path::{Path, PathBuf}, -}; +use std::{borrow::Cow, env, ffi::OsStr, os::unix::fs::MetadataExt, path::PathBuf}; use iced::{ Alignment, Color, Length, Renderer, Subscription, Task, Theme, Widget, clipboard, event, @@ -31,6 +25,9 @@ enum Message { ImageYanked(Result<(), clipboard::Error>), ImagePut(Result), + YankPath, + PathYanked(Result<(), clipboard::Error>), + FocusNext, FocusPrevious, @@ -46,6 +43,7 @@ struct State { image: Option, image_display: Option, image_filter: widget::image::FilterMethod, + image_path: Option, error: Option>, info: Option, @@ -176,9 +174,6 @@ impl State { } Message::Put => return clipboard::read_image().map(Message::ImagePut), - Message::ImageYanked(result) => { - self.error = result.err().map(|e| format!("{e:?}").into()); - } Message::ImagePut(result) => { self.error = result.as_ref().err().map(|e| format!("{e:?}").into()); @@ -192,6 +187,21 @@ impl State { } } + Message::YankPath => { + if let Some(path) = self.image_path.as_ref() { + return clipboard::write(clipboard::Content::Text( + path.to_string_lossy().to_string(), + )) + .map(Message::PathYanked); + } else { + self.error = Some("no path to yank".into()); + } + } + + Message::ImageYanked(result) | Message::PathYanked(result) => { + self.error = result.err().map(|e| format!("{e:?}").into()); + } + Message::FileDropped(path) => match self.load_image(path) { Ok(task) => { self.error = None; @@ -204,14 +214,14 @@ impl State { Task::none() } - fn load_image(&mut self, path: impl AsRef) -> Result, String> { + fn load_image(&mut self, path: impl Into) -> Result, String> { + let path = path.into(); utils::load_image(&path).map(|image| { self.info = Some(format!( "{} {} {}x{}", - path.as_ref() - .file_name() + path.file_name() .map_or(Cow::Borrowed("[no file]"), OsStr::to_string_lossy), - path.as_ref().metadata().map_or_else( + path.metadata().map_or_else( |e| { eprintln!("failed to read metadata: {e}"); Cow::Borrowed("[no size]") @@ -221,9 +231,13 @@ impl State { image.width(), image.height(), )); + + self.image_path = Some(path); + if image.dimensions() < (192, 192) { self.image_filter = widget::image::FilterMethod::Nearest; } + self.image = Some(image); self.allocate_image() }) @@ -275,6 +289,7 @@ impl State { Key::Character("y" | "c") => Some(Message::Yank), Key::Character("p" | "v") => Some(Message::Put), + Key::Character("x") => Some(Message::YankPath), _ => None, } diff --git a/src/usage.txt b/src/usage.txt index 9df1bc2..b9a227f 100644 --- a/src/usage.txt +++ b/src/usage.txt @@ -8,3 +8,5 @@ 'y' or 'c' to yank 'p' or 'v' to put + +'x' to yank the path