add inspector just for colors

This commit is contained in:
alice pellerin
2026-03-22 19:56:30 -05:00
parent fcf6ab5aff
commit 4c3e91930f
3 changed files with 48 additions and 0 deletions
+44
View File
@@ -90,6 +90,7 @@ pub enum BufferAction {
ExtendToFF,
InspectSelection,
InspectSelectionColor,
}
impl From<BufferAction> 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
+2
View File
@@ -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 {
+2
View File
@@ -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()),