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

Ability to replace text while keeping TextArea history #4257

Closed
BongoKnight opened this issue Mar 5, 2024 · 7 comments
Closed

Ability to replace text while keeping TextArea history #4257

BongoKnight opened this issue Mar 5, 2024 · 7 comments

Comments

@BongoKnight
Copy link

When you replace text in a TextArea with TextArea.text setter or with load_text, it could be great to not clean the history of the TextArea widget.

from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, TextArea


class TextAreaApp(App):

    def compose(self) -> ComposeResult:
        """Create child widgets for the app."""
        yield Header()
        yield TextArea("Initial text", classes="maintext")
        yield Footer()
        
    
    def on_mount(self):
        textArea = self.query_one(TextArea)
        textArea.insert("Updated text with insert 1")
        textArea.text = "Updated text with load_text"
        textArea.clear()
        textArea.insert("Updated text with insert 2")


if __name__ == "__main__":
    app = TextAreaApp()
    app.run()

In this MRE I would like to be able to use Ctrl-Z in the TextArea and come back to the Updated text with insert 1 text.

Copy link

github-actions bot commented Mar 5, 2024

We found the following entries in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@TomJGooding
Copy link
Contributor

TomJGooding commented Mar 5, 2024

It makes sense to me that load_text will "clear the edit history" per the docs as you're loading a new document?

What problem would this solve rather than using the existing replace method?

@BongoKnight
Copy link
Author

Thanks for pointing out this function that I missed.
However I found it quite confusing to use, to replace the whole document the syntax is quite complex (maybe once again I missed some property to access rapidly to the position of the end of the document...):

from textual.app import App, ComposeResult
from textual.widgets import Header, Footer, TextArea



class TextAreaApp(App):

    def compose(self) -> ComposeResult:
        """Create child widgets for the app."""
        yield Header()
        yield TextArea("Initial text", classes="maintext")
        yield Footer()
        
    
    def on_mount(self):
        textArea = self.query_one(TextArea)
        textArea.insert("Updated text with insert 1")
        textArea.text = "Updated text with load_text"
        textArea.replace("Updated with replace", (0,0), (textArea.document.line_count, len(textArea.document.get_line(textArea.document.line_count-1))))
        textArea.insert("Updated text with insert 2")


if __name__ == "__main__":
    app = TextAreaApp()
    app.run()

@TomJGooding
Copy link
Contributor

This is slightly shorter:

            end=(text_area.document.line_count, len(text_area.document[-1])),

But perhaps a convenience "document end" property would be useful.

@TomJGooding
Copy link
Contributor

A Document.end property was added in #4267 so will be in the next release. Hopefully this closes this issue?

@BongoKnight
Copy link
Author

Yes it's perfect! Thanks a lot! 😊

Copy link

github-actions bot commented Apr 1, 2024

Don't forget to star the repository!

Follow @textualizeio for Textual updates.

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

No branches or pull requests

2 participants