I want my read-only TextEditor to have a limit on the number of lines. When new lines are added to the bottom (I’m using it for logging), I need to see if I’m over the limit, and delete lines from the top if necessary.
I thought I’d already given you the answer!
the simplest way is to use a StringArray for your logging (each entry is a ‘line’), prune early entries as necessary, and dump it to the TextEditor after an async notification.
I’ve never had any issues doing it like that, it’s certainly the easiest option but then whenever I’ve used such a component it’s always been for debugging purposes anyway.
I guess you could use the caret operations to select text at the start and replace with String::empty. That’d require that you store how long the removed strings are though, so you know much to chop off.
If you are really concerned with efficiency though, you’d be using a log viewing component that actually thought in lines rather than a giant string. Perhaps the CodeEditorComponent would be worth experimenting with? From the looks of it, it’d need you to store the log in a CodeDocument. It doesn’t look like CodeEditorComponent currently has a read-only mode yet though - if not, I’m sure it’d be a sensible request.
String text = editor->getText();
for (int i = 0; i < nbLinesToTrim; i++) text = text.fromFirstOccurrenceOf("\n", false, false);
editor->setText(text, whatever_here);
Sorry to exhume that old thread, but is anybody has already implemented something efficient for that, since those antediluvian ages? It seems a rather usual feature, and i’m surprised to found nothing already done for that nowadays.
That just delete half of the content every nth lines logged.
It seems a good tradeoff concerning efficiency.
I tried other approaches without (better) success.
Lots of usefull functions are kept private in TextEditor.
Use moveCaretDown from top to the nth lines seems (at first glance) too heavy.
Or even a simplest if preserving lines is not required.