From 4c3e91930fec63406672f0bf25f89f8161527074 Mon Sep 17 00:00:00 2001 From: alice pellerin Date: Sun, 22 Mar 2026 19:56:30 -0500 Subject: [PATCH] add inspector just for colors --- src/action.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/buffer/mod.rs | 2 ++ src/config.rs | 2 ++ 3 files changed, 48 insertions(+) diff --git a/src/action.rs b/src/action.rs index d2a164e..4bcefbf 100644 --- a/src/action.rs +++ b/src/action.rs @@ -90,6 +90,7 @@ pub enum BufferAction { ExtendToFF, InspectSelection, + InspectSelectionColor, } impl From for Action { @@ -197,6 +198,7 @@ impl Buffer { BufferAction::ExtendToFF => self.extend_to_FF(window_size), BufferAction::InspectSelection => self.inspect_selection(), + BufferAction::InspectSelectionColor => self.inspect_selection_color(), } } @@ -886,6 +888,48 @@ impl Buffer { }) ); } + + fn inspect_selection_color(&mut self) { + if self.inspecting_selection { return; } + + self.inspecting_selection = true; + + self.popups.extend( + iter::once(&self.primary_cursor) + .chain(&self.cursors) + .map(|cursor| { + let selection = &self.contents[cursor.range()]; + + let nat = bytes_to_nat(selection); + + let color888 = (selection.len() == 3) + .then(|| [selection[0], selection[1], selection[2]]) + .map(|[red, green, blue]| { + Span::from(format!("#{red:02X}{green:02X}{blue:02X}")) + .fg(Color::Rgb(red, green, blue)) + + }); + + let color555 = nat + .filter(|_| selection.len() == 2) + .filter(|&nat| nat >> 15 == 0) + .map(|nat| color555_to_color888(nat as u16)) + .map(|[red, green, blue]| { + Span::from(format!("#{red:02X}{green:02X}{blue:02X}")) + .fg(Color::Rgb(red, green, blue)) + + }); + + Popup::new( + cursor.lower_bound(), + color888 + .into_iter() + .chain(color555) + .collect() + ) + }) + ); + } } // helpers diff --git a/src/buffer/mod.rs b/src/buffer/mod.rs index d364cda..46d2fa2 100644 --- a/src/buffer/mod.rs +++ b/src/buffer/mod.rs @@ -170,6 +170,8 @@ impl Buffer { self.alert_message = "".into(); self.popups.clear(); + // self.logs.push(format!("{event:?}")); + let was_inspecting_selection = self.inspecting_selection; let app_action = match self.partial_action { diff --git a/src/config.rs b/src/config.rs index fc5c274..a68cc3d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -169,6 +169,7 @@ impl Default for Config { ("y".try_into().unwrap(), AppAction::Yank.into()), ("C- ".try_into().unwrap(), BufferAction::InspectSelection.into()), + ("A- ".try_into().unwrap(), BufferAction::InspectSelectionColor.into()), ].into()), (Some(PartialAction::Goto), [ ("j".try_into().unwrap(), CursorAction::GotoLineStart.into()), @@ -285,6 +286,7 @@ impl Default for Config { ("y".try_into().unwrap(), AppAction::Yank.into()), ("C- ".try_into().unwrap(), BufferAction::InspectSelection.into()), + ("A- ".try_into().unwrap(), BufferAction::InspectSelectionColor.into()), ].into()), (Some(PartialAction::View), [ ("z".try_into().unwrap(), BufferAction::AlignViewCenter.into()),