Struct similar::udiff::UnifiedDiff
source · pub struct UnifiedDiff<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> { /* private fields */ }
Expand description
Unified diff formatter.
use similar::TextDiff;
let text_diff = TextDiff::from_lines(old_text, new_text);
print!("{}", text_diff
.unified_diff()
.context_radius(10)
.header("old_file", "new_file"));
§Unicode vs Bytes
The UnifiedDiff
type supports both unicode and byte diffs for all
types compatible with DiffableStr
. You can pick between the two
versions by using [UnifiedDiff.to_string
] or [UnifiedDiff.to_writer
].
The former uses DiffableStr::to_string_lossy
, the latter uses
DiffableStr::as_bytes
for each line.
Implementations§
source§impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
impl<'diff, 'old, 'new, 'bufs, T: DiffableStr + ?Sized> UnifiedDiff<'diff, 'old, 'new, 'bufs, T>
sourcepub fn from_text_diff(diff: &'diff TextDiff<'old, 'new, 'bufs, T>) -> Self
pub fn from_text_diff(diff: &'diff TextDiff<'old, 'new, 'bufs, T>) -> Self
Creates a formatter from a text diff object.
sourcepub fn context_radius(&mut self, n: usize) -> &mut Self
pub fn context_radius(&mut self, n: usize) -> &mut Self
Changes the context radius.
The context radius is the number of lines between changes that should
be emitted. This defaults to 3
.
sourcepub fn header(&mut self, a: &str, b: &str) -> &mut Self
pub fn header(&mut self, a: &str, b: &str) -> &mut Self
Sets a header to the diff.
a
and b
are the file names that are added to the top of the unified
file format. The names are accepted verbatim which lets you encode
a timestamp into it when separated by a tab (\t
). For more information,
see the unified diff format specification.
sourcepub fn missing_newline_hint(&mut self, yes: bool) -> &mut Self
pub fn missing_newline_hint(&mut self, yes: bool) -> &mut Self
Controls the missing newline hint.
By default a special \ No newline at end of file
marker is added to
the output when a file is not terminated with a final newline. This can
be disabled with this flag.
sourcepub fn iter_hunks(
&self,
) -> impl Iterator<Item = UnifiedDiffHunk<'diff, 'old, 'new, 'bufs, T>>
pub fn iter_hunks( &self, ) -> impl Iterator<Item = UnifiedDiffHunk<'diff, 'old, 'new, 'bufs, T>>
Iterates over all hunks as configured.