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

Add support for buffers other than a String in TextEdit #399

Merged
merged 11 commits into from
May 20, 2021
Merged

Add support for buffers other than a String in TextEdit #399

merged 11 commits into from
May 20, 2021

Conversation

Zenithsiz
Copy link
Contributor

Adds support for custom buffers to be used with the TextEdit widget.

I haven't made an issue because this was relatively simple to implement and it's something I need for one of my projects, so I'd likely re-use it even if you'd prefer to not make this change.

The motivation behind this for me is to use an AsciiString from the ascii crate as the underlying buffer, and also limit it's size to a certain number of max characters.
In order to achieve this the TextBuffer trait hands responsibility of any actual insertions / removals to the type itself, with a default impl for String.
Due to the default type of String I believe this doesn't break any existing code and a default of String is likely the best option either way.
This also modifies most operations to be in-place instead of creating a new string and assigning it to it. This should (?) also increase performance slightly, but I doubt it's too noticeable.

Copy link
Owner

@emilk emilk left a comment

Choose a reason for hiding this comment

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

Looks good!

@@ -478,18 +523,24 @@ impl<'t> TextEdit<'t> {
let did_mutate_text = match event {
Event::Copy => {
if cursorp.is_empty() {
copy_if_not_password(ui, text.clone());
copy_if_not_password(ui, text.as_ref().to_owned());
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
copy_if_not_password(ui, text.as_ref().to_owned());
copy_if_not_password(ui, text.to_string());

is a bit shorter (and iirc it should work since text implements Display)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wouldn't calling to_owned on a str bypass the std::fmt formatting, though?
I don't know how it works in general, but there might be a specialization for String and str so it might not matter.

@Zenithsiz
Copy link
Contributor Author

The branch conflict seems to be because of the changelog, I added the "Added" heading, should I rebase on master?

@emilk
Copy link
Owner

emilk commented May 18, 2021

yeah, rebase on or merge in master

Zenithsiz and others added 10 commits May 18, 2021 19:04
This allows the user to implement text inserting depedent on their type instead of using a `String` and converting back to `S`, which may be a lossless convertion.
…ed into the buffer.

This allows implementations to "saturate" the buffer, only allowing for a limited length of characters to be inserted.
Removed `From<String>` bound for `TextBuffer`.
Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
@Zenithsiz
Copy link
Contributor Author

Should be rebased now? I had to do a git push --force, but not sure if that was what I was supposed to do after rebasing.

@emilk emilk merged commit 57981d4 into emilk:master May 20, 2021
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.

2 participants