Package: textual-textarea
Textual TextareaNote: This isNOTthe official TextArea widget!With v0.38.0, Textual added a built-in TextArea widget. You probably want to use
that widget instead of this one. This project predated the official widget; versions < v0.8.0
had a completely separate implmentation.Since v0.8.0, this project uses the built-in TextArea widget, but adds the features outlined below.Installationpip install textual-textareaFeaturesFull-featured text editor experience with VS-Code-like bindings, in your Textual App:Syntax highlighting and support for Pygments themes.Move cursor and scroll with mouse or keys (includingctrl+arrow,PgUp/Dn,ctrl+Home/End).Open (ctrl+o) and save (ctrl+s) files.Cut (ctrl+x), copy (ctrl+c), paste (ctrl+u/v), optionally using the system clipboard.Comment selections withctrl+/.Indent and dedent (optionally for a multiline selection) to tab stops withTabandshift+Tab.Automatic completions of quotes and brackets.Select text by double-, triple-, or quadruple-clicking.Quit withctrl+q.UsageInitializing the WidgetThe TextArea is a Textual Widget. You can add it to a Textual
app usingcomposeormount:fromtextual_textareaimportTextEditorfromtextual.appimportApp,ComposeResultclassTextApp(App,inherit_bindings=False):defcompose(self)->ComposeResult:yieldTextEditor(text="hi",language="python",theme="nord-darker",id="ta")defon_mount(self)->None:editor=self.query_one("#id",expect_type=TextEditor)editor.focus()app=TextApp()app.run()In addition to the standard Widget arguments, TextArea accepts three additional, optional arguments when initializing the widget:language (str): Must beNoneor the short name of aPygments lexer, e.g.,python,sql,as3. Defaults toNone.theme (str): Must be name of aPygments style, e.g.,bw,github-dark,solarized-light. Defaults tomonokai.use_system_clipboard (bool): Set toFalseto make the TextArea's copy and paste operations ignore the system clipboard. Defaults toTrue. Some Linux users may need to apt-installxcliporxselto enable the system clipboard features.The TextArea supports many actions and key bindings.For proper binding ofctrl+cto the COPY action,
you must initialize your App withinherit_bindings=False(as shown above), so thatctrl+cdoes not quit the app. The TextArea implementsctrl+qas quit; you way wish to mimic that in your app so that other in-focus widgets use the same behavior.Interacting with the WidgetGetting and Setting TextThe TextArea exposes atextproperty that contains the full text contained in the widget. You can retrieve or set the text by interacting with this property:editor=self.query_one(TextEditor)old_text=editor.texteditor.text="New Text!\n\nMany Lines!"Similarly, the TextEditor exposes aselected_textproperty (read-only):editor=self.query_one(TextEditor)selection=editor.selected_textInserting TextYou can insert text at the current selection:editor=self.query_one(TextEditor)editor.text="01234"editor.selection=Selection((0,2),(0,2))editor.insert_text_at_selection("\nabc\n")asserteditor.text=="01\nabc\n234"asserteditor.selection==Selection((2,0),(2,0))Getting and Setting The Cursor PositionThe TextEditor exposes aselectionproperty that returns a textual.widgets.text_area.Selection:editor=self.query_one(TextEditor)old_selection=editor.selectioneditor.selection=Selection((999,0),(999,0))# the cursor will move as close to line 999, pos 0 as possiblecursor_line_number=editor.selection.end[0]cursor_x_position=editor.selection.end[1]Getting and Setting The LanguageSyntax highlighting and comment insertion depends on the configured language for the TextEditor.The TextArea exposes alanguageproperty that returnsNoneor a string that is equal to the short name of an installed tree-sitter language:editor=self.query_one(TextEditor)old_language=editor.languageeditor.language="python"Getting Theme ColorsIf you would like the rest of your app to match the colors from the TextArea's theme, they are exposed via thetheme_colorsproperty.editor=self.query_one(TextEditor)color=editor.theme_colors.contrast_text_colorbgcolor=editor.theme_colors.bgcolorhighlight=editor.theme_colors.selection_bgcolorAdding Bindings and other BehaviorYou can subclass TextEditor to add your own behavior. This snippet adds an action that posts a Submitted message containing the text of the TextEditor when the user pressesctrl+j:fromtextual.messageimportMessagefromtextual_textareaimportTextEditorclassCodeEditor(TextEditor):BINDINGS=[("ctrl+j","submit","Run Query"),]classSubmitted(Message,bubble=True):def__init__(self,text:str)->None:super().__init__()self.text=textasyncdefaction_submit(self)->None:self.post_message(self.Submitted(self.text))