14个稳定版本 (3个主要版本)
新版本 3.1.4 | 2024年8月19日 |
---|---|
3.1.3 | 2024年7月27日 |
3.1.2 | 2023年1月30日 |
3.1.0 | 2022年3月6日 |
0.1.0 | 2021年10月21日 |
#286 in 文本处理
每月292次下载
用于 2 crate
69KB
379 行
termdiff
在终端中对字符串进行diff以便展示给用户。
用法
use termdiff::{SignsTheme, DrawDiff};
let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let theme = SignsTheme::default();
let actual = format!("{}", DrawDiff::new(old, new, &theme));
assert_eq!(
actual,
"--- remove | insert +++
-The quick brown fox and
-jumps over the sleepy dog
+The quick red fox and
+jumps over the lazy dog
"
);
或者您也可以使用此接口
use termdiff::{ArrowsTheme, diff};
let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let theme = ArrowsTheme::default();
let mut buffer: Vec<u8> = Vec::new();
diff(&mut buffer, old, new, &theme).unwrap();
let actual: String = String::from_utf8(buffer).expect("Not valid UTF-8");
assert_eq!(
actual,
"< left / > right
<The quick brown fox and
<jumps over the sleepy dog
>The quick red fox and
>jumps over the lazy dog
"
);
更多信息请参阅 Docs.rs
主题
我们有一些内置的主题
箭头
符号
自定义
use termdiff::DrawDiff;
use termdiff::Theme;
use crossterm::style::Stylize;
use std::borrow::Cow;
#[derive(Debug)]
struct MyTheme {}
impl Theme for MyTheme {
fn highlight_insert<'this>(&self, input: &'this str) -> Cow<'this, str> {
input.into()
}
fn highlight_delete<'this>(&self, input: &'this str) -> Cow<'this, str> {
input.into()
}
fn equal_content<'this>(&self, input: &'this str) -> Cow<'this, str> {
input.into()
}
fn delete_content<'this>(&self, input: &'this str) -> Cow<'this, str> {
input.into()
}
fn equal_prefix<'this>(&self) -> Cow<'this, str> {
"=".into()
}
fn delete_prefix<'this>(&self) -> Cow<'this, str> {
"!".into()
}
fn insert_line<'this>(&self, input: &'this str) -> Cow<'this, str> {
input.into()
}
fn insert_prefix<'this>(&self) -> Cow<'this, str> {
"|".into()
}
fn line_end<'this>(&self) -> Cow<'this, str> {
"\n".into()
}
fn header<'this>(&self) -> Cow<'this, str> {
format!("{}\n", "Header").into()
}
}
let my_theme = MyTheme {};
let old = "The quick brown fox and\njumps over the sleepy dog";
let new = "The quick red fox and\njumps over the lazy dog";
let actual = format!("{}", DrawDiff::new(old, new, &my_theme));
assert_eq!(
actual,
"Header
!The quick brown fox and
!jumps over the sleepy dog
|The quick red fox and
|jumps over the lazy dog
"
);
依赖
~2–11MB
~131K SLoC