earwax.editor module

Provides the Editor class.

class earwax.editor.Editor(game: Game, dismissible: bool = True, text: str = '', cursor_position: Optional[int] = None, vertical_position: Optional[int] = None, validator: Optional[earwax.editor.TextValidator] = None)

Bases: earwax.level.Level, earwax.mixins.DismissibleMixin

A basic text editor.

By default, the enter key dispatches the on_submit event, with the contents of earwax.Editor.text.

Below is an example of how to use this class:

e: Editor = Editor(game)

@e.event
def on_submit(text: str) -> None:
    # Do something with text...

game.push_level(e)
Variables:
  • text – The text which can be edited by this object.
  • cursor_position – The position of the cursor.
  • vertical_position – The position in the alphabet of the hat.
  • validator

    Used to validate the text.

    The text will be validated before the on_submit() event is dispatched.

beginning_of_line() → None

Move to the start of the current line.

By default, this method is called when the home key is pressed.

clear() → None

Clear this editor.

By default, this method is called when control + u is pressed.

copy() → None

Copy the contents of this editor to the clipboard.

cut() → None

Cut the contents of this editor to the clipboard.

do_delete() → None

Perform a forward delete.

Used by motion_delete(), as well as the vertical hat movement methods.

echo(text: str) → None

Speak the provided text.

Parameters:text – The text to speak, using tts.speak.
echo_current_character() → None

Echo the current character.

Used when moving through the text.

end_of_line() → None

Move to the end of the line.

By default, this method is called when the end key is pressed.

hat_down() → None

Move down through the list of letters.

hat_up() → None

Change the current letter to the previous one in the configured alphabet.

If the cursor is at the end of the line, moving up will select a “save” button.

If the cursor is not at the end of the line, moving up will select a “delete” button.

insert_text(text: str) → None

Insert text at the current cursor position.

motion_backspace() → None

Delete the previous character.

This will do nothing if the cursor is at the beginning of the line, or there is no text to delete.

motion_delete() → None

Delete the character under the cursor.

Nothing will happen if we are at the end of the line (or there is no text, which will amount to the same thing).

motion_down() → None

Arrow down.

Since we’re not bothering with multiline text fields at this stage, just move the cursor to the end of the line, and read the whole thing.

By default, this method is called when the down arrow key is pressed.

motion_left() → None

Move left in the editor.

By default, this method is called when the left arrow key is pressed.

motion_right() → None

Move right in the editor.

By default, this method is called when the right arrow key is pressed.

motion_up() → None

Arrow up.

Since we’re not bothering with multiline text fields at this stage, just move the cursor to the start of the line, and read the whole thing.

By default, this method is called when the up arrow key is pressed.

on_submit(text: str) → None

Code to be run when this editor is submitted.

The event which is dispatched if the enter key is pressed.

Parameters:text – The contents of self.text.
on_text(text: str) → None

Text has been entered.

If the cursor is at the end of the line, append the text. Otherwise, insert it.

Parameters:text – The text that has been entered.
paste() → None

Paste the contents of the clipboard into this editor.

set_cursor_position(pos: Optional[int]) → None

Set the cursor position within text.

If pos is None, then the cursor will be at the end of the line. Otherwise, pos should be an integer between 0 and len(self.text) - 1.

Parameters:pos – The new cursor position.
submit() → None

Submit self.text.

Dispatch the on_submit event with the contents of self.text after checking the validator is happy.

By default, this method is called when the enter key is pressed.

class earwax.editor.TextValidator(func: Callable[[str], Optional[str]])

Bases: object

A class to validate the text entered into editors.

This class takes a function which must either return None to indicate success, or a message which will be output to the player.

Parameters:func – The function to validate the text with.
classmethod float(message: str = 'Invalid decimal: {}.') → T

Return a validator which ensures text can be cast to a float.

Parameters:message – The message which will be shown if an invalid float is given.
classmethod int(message: str = 'Invalid number: {}.', base: int = 10) → T

Return a validator which ensures text can be cast to an integer.

Parameters:
  • message – The message which will be returned if the cast fails.
  • base – The base for to use when casting the text.
classmethod not_empty(message: str = 'You must supply a value.') → T

Make a validator that does not except an empty string.

Parameters:message – The message which will be shown if an empty string is provided.
classmethod regexp(pattern: re.Pattern, message: str = 'Invalid value: {}.') → T

Make a regexp validator.

Parameters:
  • pattern – The regular expression which the text in the editor must match.
  • message – The message which will be returned if no match is found.