diff --git a/Cargo.lock b/Cargo.lock index 69527442..0902c9f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", ] diff --git a/Cargo.toml b/Cargo.toml index 25ddc6d3..7cc5ad9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/asyncgit/Cargo.toml b/asyncgit/Cargo.toml index 75e91f43..34b70d79 100644 --- a/asyncgit/Cargo.toml +++ b/asyncgit/Cargo.toml @@ -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" diff --git a/asyncgit/src/sync/commits_info.rs b/asyncgit/src/sync/commits_info.rs index 65740cc1..293c3d87 100644 --- a/asyncgit/src/sync/commits_info.rs +++ b/asyncgit/src/sync/commits_info.rs @@ -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, ) -> 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); - } }