line_editor#
Line editor implementation.
- class zelfred.line_editor.LineEditor[source]#
Simulate a user input line editor. User can type characters, move cursor, backspace, delete, clear line, etc …
For example, the
(Query): beautiful|(|is the cursor) in the following UI is the line editor.(Query): beautiful| [x] Beautiful is better than ugly. subtitle 01 [ ] Explicit is better than implicit. subtitle 02 [ ] Simple is better than complex. subtitle 03
Empty line editor:
|User entered some text, and the cursor is at the end:
my text|
User entered some text, and the cursor is in the middle:
my |text
- Parameters:
chars – a list of characters, representing the current line. For example, if
chars = ["m", "y", " ", "t", "e", "x", "t"], then the current line ismy text.cursor_position – the current cursor position. 0 means the cursor is at the beginning of the line. 1 means it is after the first character. when cursor_position == len(chars), it means the cursor is at the end. For example, if the text is
my textand thecursor_position = 5Then the cursor is atmy te|xt.
- press_key(key: str, n: int = 1)[source]#
Enter a key into the line editor. Also move cursor to the right.
- Parameters:
key – the entered character of the key.
n – number of times to enter the key.
- press_backspace(n: int = 1)[source]#
Delete character backwards in the line editor. Also move cursor to the left.
- Parameters:
n – number of characters to delete.
- press_left(n: int = 1)[source]#
Move cursor to the left.
- Parameters:
n – number of times to move cursor to the left.
- press_delete(n: int = 1)[source]#
Delete character forwards in the line editor. Also, the cursor stays.
- Parameters:
n – number of characters to delete.
- press_right(n: int = 1)[source]#
Move cursor to the right.
- Parameters:
n – number of times to move cursor to the right.
- clear_backward()[source]#
Delete all user inputs before the cursor and move cursor to the beginning of the line.
- replace_text(text: str)[source]#
Replace all user inputs with the given text.
- Parameters:
text – the text to replace with.
- move_to_start()#
Move cursor to the beginning of the line.
- move_to_end()#
Move cursor to the end of the line.