migrate some more to unicode-truncate crate

This commit is contained in:
Stephan Dilly 2021-03-25 09:43:30 +01:00
parent 36b8ca65d4
commit dcc01fb80e
4 changed files with 9 additions and 28 deletions

4
Cargo.lock generated
View file

@ -54,6 +54,7 @@ dependencies = [
"serial_test",
"tempfile",
"thiserror",
"unicode-truncate",
"url",
]
@ -1177,7 +1178,8 @@ checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
[[package]]
name = "unicode-truncate"
version = "0.2.0"
source = "git+https://github.com/Aetf/unicode-truncate.git#b1821b0af6801b81e1f1f900526748754f8cd44f"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a04be5ca5f7a4a7270ffea82bc41c59b87c611ed04f20e77c338e8d3c2348e42"
dependencies = [
"unicode-width",
]

View file

@ -40,7 +40,7 @@ serde = "1.0"
anyhow = "1.0"
unicode-width = "0.1"
textwrap = "0.13"
unicode-truncate = {version="0.2", git="https://github.com/Aetf/unicode-truncate.git"}
unicode-truncate = "0.2.0"
[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
which = "4.0"

View file

@ -19,6 +19,7 @@ crossbeam-channel = "0.5"
log = "0.4"
thiserror = "1.0"
url = "2.2"
unicode-truncate = "0.2.0"
[dev-dependencies]
tempfile = "3.2"

View file

@ -2,6 +2,7 @@ use super::utils::repo;
use crate::error::Result;
use git2::{Commit, Error, Oid};
use scopetime::scope_time;
use unicode_truncate::UnicodeTruncateStr;
/// identifies a single commit
#[derive(
@ -99,32 +100,18 @@ pub fn get_message(
message_length_limit: Option<usize>,
) -> String {
let msg = String::from_utf8_lossy(c.message_bytes());
let msg = msg.trim_start();
let msg = msg.trim();
if let Some(limit) = message_length_limit {
limit_str(msg, limit).to_string()
msg.unicode_truncate(limit).0.to_string()
} else {
msg.to_string()
}
}
#[inline]
fn limit_str(s: &str, limit: usize) -> &str {
if let Some(first) = s.lines().next() {
let mut limit = limit.min(first.len());
while !first.is_char_boundary(limit) {
limit += 1
}
&first[0..limit]
} else {
""
}
}
#[cfg(test)]
mod tests {
use super::{get_commits_info, limit_str};
use super::get_commits_info;
use crate::error::Result;
use crate::sync::{
commit, stage_add_file, tests::repo_init_empty,
@ -183,13 +170,4 @@ mod tests {
Ok(())
}
#[test]
fn test_limit_string_utf8() {
assert_eq!(limit_str("里里", 1), "");
let test_src = "导入按钮由选文件改为选目录因为整个过程中要用到多个mdb文件这些文件是在程序里写死的暂且这么来做有时间了后 再做调整";
let test_dst = "导入按钮由选文";
assert_eq!(limit_str(test_src, 20), test_dst);
}
}