From daa8c084859e7ed7e39e4bda6e382d503f63cf54 Mon Sep 17 00:00:00 2001 From: TheBlackSheep3 <40034835+TheBlackSheep3@users.noreply.github.com> Date: Fri, 4 Aug 2023 11:40:01 +0200 Subject: [PATCH 1/5] switch from textwrap to bwrap --- Cargo.lock | 54 +++++------------------- Cargo.toml | 2 +- src/components/commit_details/details.rs | 12 +++++- 3 files changed, 22 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c4c0ed65e..7844a66a58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,17 +17,6 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" -[[package]] -name = "ahash" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fcb51a0695d8f838b1ee009b3fbf66bda078cd64590202a864a8f3e8c4315c47" -dependencies = [ - "getrandom", - "once_cell", - "version_check", -] - [[package]] name = "ahash" version = "0.8.3" @@ -173,6 +162,15 @@ version = "3.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535" +[[package]] +name = "bwrap" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d13da3319ae5c5771712fba7b79b028793149bc462a08990bc8fd7c7554dbb95" +dependencies = [ + "unicode-width", +] + [[package]] name = "bytemuck" version = "1.13.0" @@ -753,6 +751,7 @@ dependencies = [ "backtrace", "bitflags", "bugreport", + "bwrap", "bytesize", "chrono", "clap", @@ -780,7 +779,6 @@ dependencies = [ "struct-patch", "syntect", "tempfile", - "textwrap", "unicode-segmentation", "unicode-truncate", "unicode-width", @@ -792,9 +790,6 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" -dependencies = [ - "ahash 0.7.6", -] [[package]] name = "hermit-abi" @@ -858,7 +853,7 @@ version = "0.11.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fb7c1b80a1dfa604bb4a649a5c5aeef3d913f7c520cb42b40e534e8a61bcdfc" dependencies = [ - "ahash 0.8.3", + "ahash", "indexmap", "is-terminal", "itoa", @@ -1662,12 +1657,6 @@ version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "62bb4feee49fdd9f707ef802e22365a35de4b7b299de4763d44bfea899442ff9" -[[package]] -name = "smawk" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f67ad224767faa3c7d8b6d91985b78e70a1324408abcb1cfcc2be4c06bc06043" - [[package]] name = "stable_deref_trait" version = "1.2.0" @@ -1801,17 +1790,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "textwrap" -version = "0.16.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" -dependencies = [ - "smawk", - "unicode-linebreak", - "unicode-width", -] - [[package]] name = "thiserror" version = "1.0.39" @@ -1896,16 +1874,6 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84a22b9f218b40614adcb3f4ff08b703773ad44fa9423e4e0d346d5db86e4ebc" -[[package]] -name = "unicode-linebreak" -version = "0.1.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c5faade31a542b8b35855fff6e8def199853b2da8da256da52f52f1316ee3137" -dependencies = [ - "hashbrown", - "regex", -] - [[package]] name = "unicode-normalization" version = "0.1.22" diff --git a/Cargo.toml b/Cargo.toml index 79468a6675..760c4bb41a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ asyncgit = { path = "./asyncgit", version = "0.23", default-features = false } backtrace = "0.3" bitflags = "1.3" bugreport = "0.5" +bwrap = { version = "1.3.0", features = ["use_std"] } bytesize = { version = "1.2", default-features = false } chrono = { version = "0.4", default-features = false, features = [ "clock" ] } clap = { version = "4.1", features = [ "env", "cargo" ] } @@ -49,7 +50,6 @@ serde = "1.0" simplelog = { version = "0.12", default-features = false } struct-patch = "0.2" syntect = { version = "5.0", default-features = false, features = ["parsing", "default-syntaxes", "default-themes", "html"] } -textwrap = "0.16" unicode-segmentation = "1.10" unicode-truncate = "0.2" unicode-width = "0.1" diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 8ff2775e81..827a4f61fb 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -86,11 +86,19 @@ impl DetailsComponent { message: &CommitMessage, width: usize, ) -> WrappedCommitMessage<'_> { - let wrapped_title = textwrap::wrap(&message.subject, width); + let wrapped_title = bwrap::wrap!(&message.subject, width) + .split('\n') + .map(String::from) + .map(Cow::from) + .collect(); if let Some(ref body) = message.body { let wrapped_message: Vec> = - textwrap::wrap(body, width).into_iter().collect(); + bwrap::wrap!(body, width) + .split('\n') + .map(String::from) + .map(Cow::from) + .collect(); (wrapped_title, wrapped_message) } else { From 073d1bdd3d0ac043c77457133fca646fc7faddeb Mon Sep 17 00:00:00 2001 From: TheBlackSheep3 <40034835+TheBlackSheep3@users.noreply.github.com> Date: Sat, 5 Aug 2023 18:54:46 +0200 Subject: [PATCH 2/5] handle edge case width 0 prevent panic by overwriting with width 1 and added test case --- src/components/commit_details/details.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index 827a4f61fb..aff2e701e3 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -86,6 +86,7 @@ impl DetailsComponent { message: &CommitMessage, width: usize, ) -> WrappedCommitMessage<'_> { + let width = if width < 1 { 1 } else { width }; let wrapped_title = bwrap::wrap!(&message.subject, width) .split('\n') .map(String::from) @@ -437,6 +438,10 @@ mod tests { get_wrapped_lines(&message, 14), vec!["Commit message"] ); + assert_eq!( + get_wrapped_lines(&message, 0), + vec!["Commit", "message"] + ); let message_with_newline = CommitMessage::from("Commit message\n"); @@ -449,6 +454,10 @@ mod tests { get_wrapped_lines(&message_with_newline, 14), vec!["Commit message"] ); + assert_eq!( + get_wrapped_lines(&message, 0), + vec!["Commit", "message"] + ); let message_with_body = CommitMessage::from( "Commit message\nFirst line\nSecond line", @@ -465,6 +474,13 @@ mod tests { get_wrapped_lines(&message_with_body, 14), vec!["Commit message", "First line", "Second line"] ); + assert_eq!( + get_wrapped_lines(&message_with_body, 7), + vec![ + "Commit", "message", "First", "line", "Second", + "line" + ] + ); } } From 94f09cacd5e837f600fbec642e68d55a99f0a089 Mon Sep 17 00:00:00 2001 From: TheBlackSheep3 <40034835+TheBlackSheep3@users.noreply.github.com> Date: Sun, 6 Aug 2023 08:21:38 +0200 Subject: [PATCH 3/5] added changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f94682f6fb..baf743c5d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * support 'n'/'p' key to move to the next/prev hunk in diff component [[@hamflx](https://github.com/hamflx)] ([#1523](https://github.com/extrawurst/gitui/issues/1523)) * simplify theme overrides [[@cruessler](https://github.com/cruessler)] ([#1367](https://github.com/extrawurst/gitui/issues/1367)) * support for sign-off of commits [[@domtac](https://github.com/domtac)]([#1757](https://github.com/extrawurst/gitui/issues/1757)) +* switched from textwrap to bwrap for text wrapping [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1762](https://github.com/extrawurst/gitui/issues/1762)) ### Fixes * fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726)) From 4ec0547dd7de882f5fa6256c8d93ea8d81759753 Mon Sep 17 00:00:00 2001 From: TheBlackSheep3 <40034835+TheBlackSheep3@users.noreply.github.com> Date: Mon, 7 Aug 2023 22:36:47 +0200 Subject: [PATCH 4/5] simplify maximum --- src/components/commit_details/details.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index aff2e701e3..be28f1b20d 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -86,7 +86,7 @@ impl DetailsComponent { message: &CommitMessage, width: usize, ) -> WrappedCommitMessage<'_> { - let width = if width < 1 { 1 } else { width }; + let width = width.max(1); let wrapped_title = bwrap::wrap!(&message.subject, width) .split('\n') .map(String::from) From d03d1d186303e1f9f6f784b8db8b5818a0408ddb Mon Sep 17 00:00:00 2001 From: TheBlackSheep3 <40034835+TheBlackSheep3@users.noreply.github.com> Date: Mon, 7 Aug 2023 22:37:11 +0200 Subject: [PATCH 5/5] os independent line splitting --- src/components/commit_details/details.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/commit_details/details.rs b/src/components/commit_details/details.rs index be28f1b20d..6c1a86c7a5 100644 --- a/src/components/commit_details/details.rs +++ b/src/components/commit_details/details.rs @@ -88,7 +88,7 @@ impl DetailsComponent { ) -> WrappedCommitMessage<'_> { let width = width.max(1); let wrapped_title = bwrap::wrap!(&message.subject, width) - .split('\n') + .lines() .map(String::from) .map(Cow::from) .collect(); @@ -96,7 +96,7 @@ impl DetailsComponent { if let Some(ref body) = message.body { let wrapped_message: Vec> = bwrap::wrap!(body, width) - .split('\n') + .lines() .map(String::from) .map(Cow::from) .collect();