You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I searched for ideas about how to do this and, failing to find anything useful, finally came up with this working example:
from prompt_toolkit import Application
from prompt_toolkit.widgets import TextArea
from prompt_toolkit.layout import Layout
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.lexers import Lexer
from prompt_toolkit.document import Document
from prompt_toolkit.styles import Style
import shutil
width = shutil.get_terminal_size()[0] - 1
kb = KeyBindings()
def is_current_row(line_number: int):
cursor_row = input_area.document.cursor_position_row
return int(line_number) == int(cursor_row)
# Custom lexer to highlight the current cursor row
class HighlightCurrentLineLexer(Lexer):
def __init__(self, buffer):
self.buffer = buffer
def lex_document(self, document: Document):
lines = document.lines
def get_line_tokens(line_number: int):
line = lines[line_number]
if is_current_row(line_number):
# Apply style to the whole line with a background for the current line
return [('class:highlighted-line', f"{line:<{width}}")]
else:
# Apply no special style for other lines
return [('class:default', line)]
return get_line_tokens
# Create a TextArea with an empty buffer first
input_area = TextArea(text='', multiline=True)
# Set the custom lexer with input_area's buffer
input_area.lexer = HighlightCurrentLineLexer(input_area.buffer)
# Initialize the content for input_area
input_area.text = '\n'.join([f"line {i}" for i in range(10)])
# Style definitions
style = Style.from_dict({
'highlighted-line': 'bg:#ffffcc', # Yellow background for the current line
'default': '' # No special styling for other lines
})
# Keybinding to refresh/redraw the screen when the cursor moves
@kb.add('c-q')
def exit_app(event):
"""Exit the application."""
app.exit()
# Create the layout and application
layout = Layout(input_area)
# Create the Application with the key bindings and the style
app = Application(layout=layout, key_bindings=kb, full_screen=True, style=style)
# Run the application
app.run()
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I searched for ideas about how to do this and, failing to find anything useful, finally came up with this working example:
Beta Was this translation helpful? Give feedback.
All reactions