Implementation
This is the documentation for select implementing classes. For the most part you don’t need to understand it if you’re just calling into the purdy library.
Parser
Parser
Contains methods and classes to manage parsing of code.
- class purdy.parser.CodeLine(lexer_spec: ~purdy.parser.LexerSpec, parts: ~purdy.parser.PartsList = <factory>, has_newline: bool = False)
Represents a line of code, made up of
CodePartobjects.- compress()
Looks for sequence of parts in a row with the same token type and merges them.
- class purdy.parser.CodePart(token: Token, text: str)
Represents a token and its content in a line of code
- class purdy.parser.LexerSpec(description: str, lexer_cls: object, console: bool, category: str)
LexerSpecwraps a Pygments Lexer and contains data that changes the behaviour of the display of the code.- Parameters:
description – description text about what this
LexerSpecis forlexer_cls – The Pygments Lexer class used for parsing
console – True if the content is a REPL or console, False otherwise. This setting effects how animations and certain kinds of output are displayed
category – Classification to use for the colourization and rendering map
- classmethod get_spec(lexer, hint='')
Return a
LexerSpecobject based on the given identifier.- Parameters:
lexer – An indicator as to what underlying lexer to use. It can be the string value “detect” to attempt to auto-detect the appropriate lexer, a string corresponding to one of the built-in
LexerSpecobjects, aLexerSpecitself, or a Pygments Lexer class. When a Pygments Lexer is provided it is assumed to be for code and not in console mode.hint – when using lexer=”detect”, this provides information for doing the detection, like the filename
- class purdy.parser.Parser(lexer_spec)
Parser is responsible for parsing code and returning a
Codeobject containingCodeLineobjects. You configure a Parser with aLexerSpecwhich you can get through theLexerSpec.get_spec()method.- Parameters:
lexer_spec – a
LexerSpecobject
- class purdy.parser.PartsList(iterable=None)
Subclass of list that tracks the length of the text fields within the
CodePartobjects inside of it.- append(value)
Append object to the end of the list.
- extend(other)
Extend list by appending elements from the iterable.
- insert(index, value)
Insert object before index.
Content Management
- class purdy.content.Document(sections=None)
Container for renderable
Sectionobjects likeCodeandStringSection. Constructor optionally takes a section to include. Manages display attributes that get used across multiple sections like line numbering.
- class purdy.content.RenderState(document, future_length=0)
Encapsulates the data needed for rendering and will contain the rendered output of of a
Document.- Parameters:
document – Document that will be rendered
future_length – When calculating width for line numbers, add this value to the max length. Useful when the
RenderStateneeds to be created before content gets added to theDocument.
- line_number_gap()
Returns a whitespace string the width of a line number, useful for when dealing with wrapping
- next_line_number()
Gets the next line number and increments the count.
- class purdy.content.Section
Abstract base class for classes that contain a list of lines which can be collected inside of a class:Document
- class purdy.content.StringSection(content=None)
TUI
- class purdy.tui.tui_content.EscapeText
Class to distinguish plain text from Textual markup to present escaped text.
- class purdy.tui.tui_content.TextSection(content=None, theme=None)
Abstracts one or more lines of Textual markup strings
- class purdy.tui.widgets.AlwaysVerticalScroll(*children: Widget, name: str | None = None, id: str | None = None, classes: str | None = None, disabled: bool = False, can_focus: bool | None = None, can_focus_children: bool | None = None, can_maximize: bool | None = None)
A container with vertical layout and an always-on scrollbar on the Y axis.
- DEFAULT_CSS: ClassVar[str] = '\n AlwaysVerticalScroll {\n layout: vertical;\n overflow-x: hidden;\n overflow-y: scroll;\n }\n '
Default TCSS.
- can_focus: bool = True
Widget may receive focus.
- can_focus_children: bool = True
Widget’s children may receive focus.
- class purdy.tui.widgets.BlurredTriangleScrollRender(virtual_size: int = 100, window_size: int = 0, position: float = 0, thickness: int = 1, vertical: bool = True, style: str | Style = 'bright_magenta on #555555')
Used with
TriangleScrollRenderwhen it is out of focus
- class purdy.tui.widgets.CodeWidget(border='', title=None)
Textual widget to display a single
CodeBoxobject.- can_focus: bool = False
Widget may receive focus.
- can_focus_children: bool = True
Widget’s children may receive focus.
- compose() Iterable[Widget]
Called by Textual to create child widgets.
This method is called when a widget is mounted or by setting recompose=True when calling [refresh()][textual.widget.Widget.refresh].
Note that you don’t typically need to explicitly call this method.
- class purdy.tui.widgets.DarkThinBarRender(virtual_size: int = 100, window_size: int = 0, position: float = 0, thickness: int = 1, vertical: bool = True, style: str | Style = 'bright_magenta on #555555')
Scroll bar renderer that is dark and thin
- class purdy.tui.widgets.LightThinBarRender(virtual_size: int = 100, window_size: int = 0, position: float = 0, thickness: int = 1, vertical: bool = True, style: str | Style = 'bright_magenta on #555555')
Scroll bar renderer that is light and thin
- class purdy.tui.widgets.PurdyContainer(owner, row_specs, max_height=None)
Textual Container for the widgets that display purdy
CodeBoxobjects- can_focus: bool = False
Widget may receive focus.
- can_focus_children: bool = True
Widget’s children may receive focus.
- compose() Iterable[Widget]
Called by Textual to create child widgets.
This method is called when a widget is mounted or by setting recompose=True when calling [refresh()][textual.widget.Widget.refresh].
Note that you don’t typically need to explicitly call this method.
- class purdy.tui.widgets.TriangleScrollRender(virtual_size: int = 100, window_size: int = 0, position: float = 0, thickness: int = 1, vertical: bool = True, style: str | Style = 'bright_magenta on #555555')
Scroll bar renderer that uses arrow icons at top and bottom, and a thin line to indicate position