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 CodePart objects.

compress()

Looks for sequence of parts in a row with the same token type and merges them.

spawn()

Returns a new CodeLine with the same settings as this one but an empty parts list.

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)

LexerSpec wraps a Pygments Lexer and contains data that changes the behaviour of the display of the code.

Parameters:
  • description – description text about what this LexerSpec is for

  • lexer_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 LexerSpec object 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 LexerSpec objects, a LexerSpec itself, 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 Code object containing CodeLine objects. You configure a Parser with a LexerSpec which you can get through the LexerSpec.get_spec() method.

Parameters:

lexer_spec – a LexerSpec object

parse(content, code_obj)

Parses the given content using the class’s associated lexer.

Parameters:
  • content – String to be parsed

  • code_obj – A Code object to add the resulting CodeLine objects into

class purdy.parser.PartsList(iterable=None)

Subclass of list that tracks the length of the text fields within the CodePart objects 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 Section objects like Code and StringSection. Constructor optionally takes a section to include. Manages display attributes that get used across multiple sections like line numbering.

spawn(sections=None)

Creates a new Document instance copying settings from this existing one.

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:
  • documentDocument that will be rendered

  • future_length – When calculating width for line numbers, add this value to the max length. Useful when the RenderState needs to be created before content gets added to the Document.

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.

next_line_number_part()

Gets the next line number as a CodePart 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 TriangleScrollRender when it is out of focus

class purdy.tui.widgets.CodeWidget(border='', title=None)

Textual widget to display a single CodeBox object.

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.

Example:

```python def compose(self) -> ComposeResult:

yield Header() yield Label(“Press the button below:”) yield Button() yield Footer()

```

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 CodeBox objects

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.

Example:

```python def compose(self) -> ComposeResult:

yield Header() yield Label(“Press the button below:”) yield Button() yield Footer()

```

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