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 is my 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 text and the cursor_position = 5 Then the cursor is at my te|xt.

is_cursor_at_begin_of_line() bool[source]#

Check if the cursor is at the beginning of the line.

is_cursor_at_end_of_line() bool[source]#

Check if the cursor is at the end of the line.

enter_text(text: str)[source]#

Enter text into the line editor.

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_home()[source]#

Move cursor to the beginning of the line.

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.

press_end()[source]#

Move cursor to the end of the line.

clear_line()[source]#

Delete all user inputs and move cursor to the beginning of the line.

clear_backward()[source]#

Delete all user inputs before the cursor and move cursor to the beginning of the line.

clear_forward()[source]#

Delete all user inputs after the cursor and the cursor stays.

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.

move_word_backward()[source]#

Move cursor to the beginning of previous word.

delete_word_backward()[source]#

Delete the previous word.

move_word_forward()[source]#

Move cursor to the beginning of next word.

delete_word_forward()[source]#

Delete the next word.

property line: str#

Return the displayed line.

Example: ali|ce -> line = alice

property value: str#

The value of the user input, it is the text before the cursor.

Example: ali|ce -> value = ali