pub struct DiffViewerState { /* private fields */ }Expand description
State for a DiffViewer component.
Contains the diff hunks, display mode, scroll position, and UI options.
Implementations§
Source§impl DiffViewerState
impl DiffViewerState
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty diff viewer state.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new();
assert!(state.hunks().is_empty());
assert_eq!(state.current_hunk(), 0);Sourcepub fn from_diff(diff_text: &str) -> Self
pub fn from_diff(diff_text: &str) -> Self
Creates a diff viewer from a unified diff string.
§Example
use envision::component::DiffViewerState;
let diff = "\
--- a/file.rs
+++ b/file.rs
@@ -1,3 +1,3 @@
fn main() {
- old();
+ new();
}
";
let state = DiffViewerState::from_diff(diff);
assert_eq!(state.hunk_count(), 1);Sourcepub fn from_texts(old: &str, new: &str) -> Self
pub fn from_texts(old: &str, new: &str) -> Self
Creates a diff viewer by computing a diff from two text strings.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::from_texts("old line", "new line");
assert_eq!(state.hunk_count(), 1);Sourcepub fn with_mode(self, mode: DiffMode) -> Self
pub fn with_mode(self, mode: DiffMode) -> Self
Sets the display mode (builder pattern).
§Example
use envision::component::{DiffViewerState, DiffMode};
let state = DiffViewerState::new().with_mode(DiffMode::SideBySide);Sourcepub fn with_context_lines(self, lines: usize) -> Self
pub fn with_context_lines(self, lines: usize) -> Self
Sets the number of context lines for compute_diff (builder pattern).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_context_lines(5);Sourcepub fn with_show_line_numbers(self, show: bool) -> Self
pub fn with_show_line_numbers(self, show: bool) -> Self
Sets whether to show line numbers (builder pattern).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_show_line_numbers(false);Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Sets the title (builder pattern).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_title("My Diff");Sourcepub fn with_old_label(self, label: impl Into<String>) -> Self
pub fn with_old_label(self, label: impl Into<String>) -> Self
Sets the label for the old file (builder pattern).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_old_label("original.rs");Sourcepub fn with_new_label(self, label: impl Into<String>) -> Self
pub fn with_new_label(self, label: impl Into<String>) -> Self
Sets the label for the new file (builder pattern).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_new_label("modified.rs");Sourcepub fn title(&self) -> Option<&str>
pub fn title(&self) -> Option<&str>
Returns the title, if set.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_title("My Diff");
assert_eq!(state.title(), Some("My Diff"));
let state2 = DiffViewerState::new();
assert_eq!(state2.title(), None);Sourcepub fn set_title(&mut self, title: impl Into<String>)
pub fn set_title(&mut self, title: impl Into<String>)
Sets the title.
§Example
use envision::component::DiffViewerState;
let mut state = DiffViewerState::new();
state.set_title("Code Review");
assert_eq!(state.title(), Some("Code Review"));Sourcepub fn hunk_count(&self) -> usize
pub fn hunk_count(&self) -> usize
Returns the number of hunks in the diff.
Sourcepub fn current_hunk(&self) -> usize
pub fn current_hunk(&self) -> usize
Returns the current hunk index.
Sourcepub fn total_lines(&self) -> usize
pub fn total_lines(&self) -> usize
Returns the total number of displayable lines across all hunks.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::from_texts("old line", "new line");
assert!(state.total_lines() > 0);Sourcepub fn added_count(&self) -> usize
pub fn added_count(&self) -> usize
Returns the count of added lines across all hunks.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::from_texts("old", "new\nextra");
assert!(state.added_count() >= 1);Sourcepub fn removed_count(&self) -> usize
pub fn removed_count(&self) -> usize
Returns the count of removed lines across all hunks.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::from_texts("old\nextra", "new");
assert!(state.removed_count() >= 1);Sourcepub fn changed_count(&self) -> usize
pub fn changed_count(&self) -> usize
Returns the count of changed lines (added + removed).
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::from_texts("old line", "new line");
assert_eq!(state.changed_count(), state.added_count() + state.removed_count());Sourcepub fn show_line_numbers(&self) -> bool
pub fn show_line_numbers(&self) -> bool
Returns whether line numbers are shown.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new();
assert!(state.show_line_numbers());Sourcepub fn set_show_line_numbers(&mut self, show: bool)
pub fn set_show_line_numbers(&mut self, show: bool)
Sets whether line numbers are shown.
§Example
use envision::component::DiffViewerState;
let mut state = DiffViewerState::new();
state.set_show_line_numbers(false);
assert!(!state.show_line_numbers());Sourcepub fn context_lines(&self) -> usize
pub fn context_lines(&self) -> usize
Returns the number of context lines.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new();
assert_eq!(state.context_lines(), 3);Sourcepub fn old_label(&self) -> Option<&str>
pub fn old_label(&self) -> Option<&str>
Returns the old label, if set.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_old_label("original.rs");
assert_eq!(state.old_label(), Some("original.rs"));Sourcepub fn set_old_label(&mut self, label: impl Into<String>)
pub fn set_old_label(&mut self, label: impl Into<String>)
Sets the old label.
§Example
use envision::component::DiffViewerState;
let mut state = DiffViewerState::new();
state.set_old_label("original.rs");
assert_eq!(state.old_label(), Some("original.rs"));Sourcepub fn new_label(&self) -> Option<&str>
pub fn new_label(&self) -> Option<&str>
Returns the new label, if set.
§Example
use envision::component::DiffViewerState;
let state = DiffViewerState::new().with_new_label("modified.rs");
assert_eq!(state.new_label(), Some("modified.rs"));Sourcepub fn set_new_label(&mut self, label: impl Into<String>)
pub fn set_new_label(&mut self, label: impl Into<String>)
Sets the new label.
§Example
use envision::component::DiffViewerState;
let mut state = DiffViewerState::new();
state.set_new_label("modified.rs");
assert_eq!(state.new_label(), Some("modified.rs"));Sourcepub fn mode(&self) -> &DiffMode
pub fn mode(&self) -> &DiffMode
Returns the display mode.
§Example
use envision::component::{DiffViewerState, DiffMode};
let state = DiffViewerState::new();
assert_eq!(state.mode(), &DiffMode::Unified);
let state = DiffViewerState::new().with_mode(DiffMode::SideBySide);
assert_eq!(state.mode(), &DiffMode::SideBySide);Sourcepub fn scroll_offset(&self) -> usize
pub fn scroll_offset(&self) -> usize
Returns the current scroll offset.
Sourcepub fn update(&mut self, msg: DiffViewerMessage) -> Option<DiffViewerOutput>
pub fn update(&mut self, msg: DiffViewerMessage) -> Option<DiffViewerOutput>
Updates the state with a message, returning any output.
§Example
use envision::component::{DiffViewerState, DiffViewerMessage};
let old = "line1\nline2\nline3";
let new = "line1\nchanged\nline3";
let mut state = DiffViewerState::from_texts(old, new);
let _output = state.update(DiffViewerMessage::ScrollDown);Trait Implementations§
Source§impl Clone for DiffViewerState
impl Clone for DiffViewerState
Source§fn clone(&self) -> DiffViewerState
fn clone(&self) -> DiffViewerState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DiffViewerState
impl Debug for DiffViewerState
Source§impl Default for DiffViewerState
impl Default for DiffViewerState
Source§impl<'de> Deserialize<'de> for DiffViewerState
impl<'de> Deserialize<'de> for DiffViewerState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for DiffViewerState
impl PartialEq for DiffViewerState
Auto Trait Implementations§
impl Freeze for DiffViewerState
impl RefUnwindSafe for DiffViewerState
impl Send for DiffViewerState
impl Sync for DiffViewerState
impl Unpin for DiffViewerState
impl UnsafeUnpin for DiffViewerState
impl UnwindSafe for DiffViewerState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more