Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from textwrap to bwrap #1765

Closed
wants to merge 7 commits into from
Closed

Switch from textwrap to bwrap #1765

wants to merge 7 commits into from

Conversation

TheBlackSheep3
Copy link
Contributor

This Pull Request fixes/closes #1762.

It changes the following:

  • removed dependency textwrap
  • added dependency bwrap
  • changed commit detail wrapping to work with bwrap api

I followed the checklist:

  • I added unittests The functionality was verfied by existing unittests
  • I ran make check without errors
  • I tested the overall application
  • I added an appropriate item to the changelog

Copy link
Contributor

@AmmarAbouZor AmmarAbouZor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the struct bwrap::Wrapper will use manual memory management and ask to provide the bytes-buffers (which can go wrong easily), but they have the other struct bwrap::EasyWrapper which uses the standard library for memory management.

With the standard library feature you can actually use the macro bwrap::wrap! to wrap the text easily without having to change the method signatures since it has similar signature to textwrap::wrap methods and will save you all the headache from dealing with converting between utf8 and byte-slices because the current implementation will fail with none-ASCII characters.

Comment on lines +147 to +153
let title_len = message.subject.len();
let mut title_buffer =
vec![0; title_len + title_len / width + 1];
let message_len =
message.body.as_ref().map_or(0usize, String::len);
let mut message_buffer =
vec![0; message_len + message_len / width + 1];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Buffers length assumes that all the characters are ASCII so each char will occupy one byte only. But that's not the case and this will fail with non-ASCII chars since chars in UTF8 could occupy ab to 4 bytes. For such cases you could use as_bytes() method to get the actual number of bytes.

Comment on lines +355 to +374
let (title_len, message_len) =
self.data.as_ref().map_or((0usize, 0usize), |data| {
data.message.as_ref().map_or(
(0usize, 0usize),
|message| {
let title_len = message.subject.len();
let message_len = message
.body
.as_ref()
.map_or(0usize, String::len);
(title_len, message_len)
},
)
});
let mut title_buffer =
vec![0u8; title_len + title_len / usize::from(width) + 1];
let mut message_buffer =
vec![
0u8;
message_len + message_len / usize::from(width) + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as the above and it should fail with none-ASCII chars

@TheBlackSheep3
Copy link
Contributor Author

Closed in favor of #1792

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

switch to bwrap
2 participants