Skip to main content

DiffViewerState

Struct DiffViewerState 

Source
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

Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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");
Source

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");
Source

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");
Source

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);
Source

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"));
Source

pub fn hunks(&self) -> &[DiffHunk]

Returns a reference to the diff hunks.

Source

pub fn hunk_count(&self) -> usize

Returns the number of hunks in the diff.

Source

pub fn current_hunk(&self) -> usize

Returns the current hunk index.

Source

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);
Source

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);
Source

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);
Source

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());
Source

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());
Source

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());
Source

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);
Source

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"));
Source

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"));
Source

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"));
Source

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"));
Source

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);
Source

pub fn scroll_offset(&self) -> usize

Returns the current scroll offset.

Source

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

Source§

fn clone(&self) -> DiffViewerState

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DiffViewerState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DiffViewerState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for DiffViewerState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for DiffViewerState

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for DiffViewerState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,