pub struct DiffLine {
pub kind: DiffLineKind,
pub content: String,
pub old_line_num: Option<usize>,
pub new_line_num: Option<usize>,
}Expand description
Represents a single line in a diff output.
A DiffLine contains the content of the line, its kind (context, added, removed,
or hunk header), and the line numbers from both the old and new file versions.
§Fields
kind- The type of diff line (context, added, removed, or hunk header)content- The text content of the lineold_line_num- Line number in the old version (None for added lines)new_line_num- Line number in the new version (None for removed lines)
Fields§
§kind: DiffLineKindThe type of this diff line.
content: StringThe text content of the line.
old_line_num: Option<usize>Line number in the old/original file.
None for added lines since they don’t exist in the old version.
new_line_num: Option<usize>Line number in the new/modified file.
None for removed lines since they don’t exist in the new version.
Implementations§
Source§impl DiffLine
impl DiffLine
Sourcepub fn added(content: impl Into<String>, new_line_num: usize) -> Self
pub fn added(content: impl Into<String>, new_line_num: usize) -> Self
Creates an added diff line.
Added lines exist only in the new version of the file and are typically displayed with a green background and a ‘+’ prefix.
§Arguments
content- The text content of the linenew_line_num- Line number in the new version
§Returns
A new DiffLine with DiffLineKind::Added
§Example
use ratatui_toolkit::code_diff::{DiffLine, DiffLineKind};
let line = DiffLine::added("new line", 10);
assert!(matches!(line.kind, DiffLineKind::Added));
assert!(line.old_line_num.is_none());Source§impl DiffLine
impl DiffLine
Sourcepub fn context(
content: impl Into<String>,
old_line_num: usize,
new_line_num: usize,
) -> Self
pub fn context( content: impl Into<String>, old_line_num: usize, new_line_num: usize, ) -> Self
Creates a context (unchanged) diff line.
Context lines appear in both the old and new versions of the file and are shown without highlighting to provide surrounding context.
§Arguments
content- The text content of the lineold_line_num- Line number in the old versionnew_line_num- Line number in the new version
§Returns
A new DiffLine with DiffLineKind::Context
§Example
use ratatui_toolkit::code_diff::{DiffLine, DiffLineKind};
let line = DiffLine::context("unchanged line", 5, 7);
assert!(matches!(line.kind, DiffLineKind::Context));Source§impl DiffLine
impl DiffLine
Sourcepub fn hunk_header(content: impl Into<String>) -> Self
pub fn hunk_header(content: impl Into<String>) -> Self
Creates a hunk header diff line.
Hunk headers mark the start of a diff section and contain line number
information (e.g., @@ -1,4 +1,5 @@ function_name).
§Arguments
content- The full hunk header text
§Returns
A new DiffLine with DiffLineKind::HunkHeader
§Example
use ratatui_toolkit::code_diff::{DiffLine, DiffLineKind};
let line = DiffLine::hunk_header("@@ -1,4 +1,5 @@ fn main()");
assert!(matches!(line.kind, DiffLineKind::HunkHeader));Source§impl DiffLine
impl DiffLine
Sourcepub fn new(
kind: DiffLineKind,
content: impl Into<String>,
old_line_num: Option<usize>,
new_line_num: Option<usize>,
) -> Self
pub fn new( kind: DiffLineKind, content: impl Into<String>, old_line_num: Option<usize>, new_line_num: Option<usize>, ) -> Self
Creates a new diff line with all fields specified.
§Arguments
kind- The type of diff linecontent- The text content of the lineold_line_num- Line number in the old version (None for added lines)new_line_num- Line number in the new version (None for removed lines)
§Returns
A new DiffLine instance
§Example
use ratatui_toolkit::code_diff::{DiffLine, DiffLineKind};
let line = DiffLine::new(
DiffLineKind::Context,
"unchanged line",
Some(10),
Some(12),
);Source§impl DiffLine
impl DiffLine
Sourcepub fn removed(content: impl Into<String>, old_line_num: usize) -> Self
pub fn removed(content: impl Into<String>, old_line_num: usize) -> Self
Creates a removed diff line.
Removed lines exist only in the old version of the file and are typically displayed with a red background and a ‘-’ prefix.
§Arguments
content- The text content of the lineold_line_num- Line number in the old version
§Returns
A new DiffLine with DiffLineKind::Removed
§Example
use ratatui_toolkit::code_diff::{DiffLine, DiffLineKind};
let line = DiffLine::removed("deleted line", 5);
assert!(matches!(line.kind, DiffLineKind::Removed));
assert!(line.new_line_num.is_none());Source§impl DiffLine
impl DiffLine
Sourcepub fn prefix(&self) -> char
pub fn prefix(&self) -> char
Returns the prefix character for this line kind.
- Context lines: ’ ’ (space)
- Added lines: ‘+’
- Removed lines: ‘-’
- Hunk headers: ‘@’
§Returns
A single character prefix for display
§Example
use ratatui_toolkit::code_diff::DiffLine;
let added = DiffLine::added("new line", 5);
assert_eq!(added.prefix(), '+');
let removed = DiffLine::removed("old line", 5);
assert_eq!(removed.prefix(), '-');Trait Implementations§
impl Eq for DiffLine
impl StructuralPartialEq for DiffLine
Auto Trait Implementations§
impl Freeze for DiffLine
impl RefUnwindSafe for DiffLine
impl Send for DiffLine
impl Sync for DiffLine
impl Unpin for DiffLine
impl UnwindSafe for DiffLine
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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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