Howto highlight cursor row #1917
Replies: 2 comments
|
You can use processors for that |
|
A Processor is the better abstraction for this than a Lexer. The current cursor row is display/UI state, not lexical information derived from the document text. A Lexer can make your example work because it is called while rendering lines, but it couples syntax tokenization to cursor position and makes composition with a real syntax lexer harder. A Processor can style the fragments produced for display while leaving the underlying document and lexer unchanged. prompt_toolkit already uses processors for UI-state overlays such as selection/search highlighting. For the full-row background, don't pad text to shutil.get_terminal_size(). That bypasses prompt_toolkit's layout width calculation and becomes wrong when the TextArea is inside a split/frame or the terminal resizes. Let the Window own its width. The processor should change styles for the fragments on the cursor's logical row; if you need the background to fill unused cells to the right, use Window/style behavior for the line rather than manufacturing spaces based on the physical terminal width. Cursor movement through normal key bindings already invalidates/redraws the application. If you change the highlighted-row state from outside the normal input path, call app.invalidate(). So your prototype demonstrates the desired effect, but I would move the cursor-dependent styling out of HighlightCurrentLineLexer and into an input Processor, and remove the fixed terminal-width padding. |
Uh oh!
There was an error while loading. Please reload this page.
I searched for ideas about how to do this and, failing to find anything useful, finally came up with this working example:
All reactions