gui-cs / Terminal.Gui

Cross Platform Terminal UI toolkit for .NET
MIT License
9.7k stars 690 forks source link

TextView: mapping between text position to line and column? #3265

Open lzanoni-stm opened 8 months ago

lzanoni-stm commented 8 months ago

I've a TextView and I'd like to color(hightlight) some word knowing the start position from the beginning of the text, and the length. I overrode protected override void SetNormalColor(List line, int idx) but of course, I receive it for each line, and thus, it's coloring a rectangle spanning all the lines.

Any idea how to know on to convert from absolute text position to line and column? I also don't see how I will know on which line I'm, from inside the override.

Or maybe I should not use TextView in this case ?

BDisp commented 1 month ago

See the "Syntax Highlighting" scenario in the UICatalog project and you will have all answers for these questions. You can use the CurrentColumn and the CurrentRow to get it's positions.

tznind commented 1 month ago

https://github.com/gui-cs/Terminal.Gui/blob/v2_develop/UICatalog%2FScenarios%2FSyntaxHighlighting.cs#L300

Here is the method.

See we call get line and then assign a color scheme.

We use regex to do multi line 'are we in comment block' etc