clean up inspector switching

This commit is contained in:
alice pellerin
2026-04-10 20:38:34 -05:00
parent 75158ee771
commit a091b504fb
2 changed files with 34 additions and 16 deletions
+20 -7
View File
@@ -1,7 +1,7 @@
use std::{cmp::min, collections::hash_set::Entry, convert::identity, fs::File, io::Write, iter, mem::{replace, swap}}; use std::{cmp::min, collections::hash_set::Entry, convert::identity, fs::File, io::Write, iter, mem::{replace, swap}};
use ratatui::{style::{Color, Stylize}, text::Span}; use ratatui::{style::{Color, Stylize}, text::Span};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{BYTES_OF_PADDING, BYTES_PER_LINE, LINES_OF_PADDING, app::WindowSize, buffer::{Buffer, Mode, PartialAction, Popup}, cursor::Cursor, edit_action::EditAction}; use crate::{BYTES_OF_PADDING, BYTES_PER_LINE, LINES_OF_PADDING, app::WindowSize, buffer::{Buffer, InspectionStatus, Mode, PartialAction, Popup}, cursor::Cursor, edit_action::EditAction};
#[derive(Clone, Copy, Serialize, Deserialize)] #[derive(Clone, Copy, Serialize, Deserialize)]
#[derive(Debug)] #[derive(Debug)]
@@ -23,6 +23,13 @@ impl Action {
Cursor(cursor_action) => cursor_action.clears_popups(), Cursor(cursor_action) => cursor_action.clears_popups(),
} }
} }
pub const fn is_inspection(self) -> bool {
use Action::*;
use BufferAction::*;
matches!(self, Buffer(InspectSelection | InspectSelectionColor))
}
} }
impl From<Action> for &str { impl From<Action> for &str {
@@ -1179,9 +1186,12 @@ impl Buffer {
#[allow(clippy::too_many_lines)] #[allow(clippy::too_many_lines)]
fn inspect_selection(&mut self) { fn inspect_selection(&mut self) {
if self.inspecting_selection { return; } if self.inspection_status == Some(InspectionStatus::Normal) {
self.inspection_status = None;
return;
}
self.inspecting_selection = true; self.inspection_status = Some(InspectionStatus::Normal);
self.popups.extend( self.popups.extend(
iter::once(&self.primary_cursor) iter::once(&self.primary_cursor)
@@ -1200,14 +1210,17 @@ impl Buffer {
); );
if self.popups.is_empty() { if self.popups.is_empty() {
self.inspecting_selection = false; self.inspection_status = None;
} }
} }
fn inspect_selection_color(&mut self) { fn inspect_selection_color(&mut self) {
if self.inspecting_selection { return; } if self.inspection_status == Some(InspectionStatus::ColorsOnly) {
self.inspection_status = None;
return;
}
self.inspecting_selection = true; self.inspection_status = Some(InspectionStatus::ColorsOnly);
self.popups.extend( self.popups.extend(
iter::once(&self.primary_cursor) iter::once(&self.primary_cursor)
@@ -1226,7 +1239,7 @@ impl Buffer {
); );
if self.popups.is_empty() { if self.popups.is_empty() {
self.inspecting_selection = false; self.inspection_status = None;
} }
} }
} }
+14 -9
View File
@@ -26,7 +26,7 @@ pub struct Buffer {
pub alert_message: Span<'static>, pub alert_message: Span<'static>,
pub popups: Vec<Popup>, pub popups: Vec<Popup>,
pub inspecting_selection: bool, pub inspection_status: Option<InspectionStatus>,
pub edit_history: Vec<EditAction>, pub edit_history: Vec<EditAction>,
// the index *after* the latest edit action // the index *after* the latest edit action
@@ -59,6 +59,11 @@ pub struct Popup {
lines: Vec<Span<'static>> lines: Vec<Span<'static>>
} }
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum InspectionStatus {
Normal, ColorsOnly
}
impl Mode { impl Mode {
pub const fn label(self) -> &'static str { pub const fn label(self) -> &'static str {
match self { match self {
@@ -190,7 +195,7 @@ impl Buffer {
alert_message: "".into(), alert_message: "".into(),
popups: Vec::new(), popups: Vec::new(),
inspecting_selection: false, inspection_status: None,
edit_history: Vec::new(), edit_history: Vec::new(),
time_traveling: None, time_traveling: None,
@@ -273,6 +278,8 @@ impl Buffer {
config: &Config, config: &Config,
window_size: WindowSize window_size: WindowSize
) -> Option<AppAction> { ) -> Option<AppAction> {
use Action::*;
let mut result = None; let mut result = None;
let should_reset_partial = self.partial_action.is_some(); let should_reset_partial = self.partial_action.is_some();
@@ -285,12 +292,10 @@ impl Buffer {
self.popups.clear(); self.popups.clear();
} }
let was_inspecting_selection = self.inspecting_selection;
match action { match action {
Action::App(app_action) => result = Some(*app_action), App(app_action) => result = Some(*app_action),
Action::Buffer(buffer_action) => self.execute(*buffer_action, window_size), Buffer(buffer_action) => self.execute(*buffer_action, window_size),
Action::Cursor(cursor_action) => { Cursor(cursor_action) => {
let max_contents_index = self.max_contents_index(); let max_contents_index = self.max_contents_index();
self.primary_cursor.execute(*cursor_action, max_contents_index); self.primary_cursor.execute(*cursor_action, max_contents_index);
@@ -305,8 +310,8 @@ impl Buffer {
}, },
} }
if was_inspecting_selection { if !action.is_inspection() {
self.inspecting_selection = false; self.inspection_status = None;
} }
} }