1
|
|
|
"""Caret module.""" |
2
|
|
|
from .context import Context |
3
|
|
|
|
4
|
|
|
|
5
|
|
|
class Caret: |
6
|
|
|
"""Caret (cursor) class which indicate the cursor locus in a prompt. |
7
|
|
|
|
8
|
|
|
Note: |
9
|
|
|
This class defines ``__slots__`` attribute so sub-class must override |
10
|
|
|
the attribute to extend available attributes. |
11
|
|
|
|
12
|
|
|
Attributes: |
13
|
|
|
context (Context): The ``prompt.context.Context`` instance. |
14
|
|
|
""" |
15
|
|
|
|
16
|
|
|
__slots__ = ('context',) |
17
|
|
|
|
18
|
|
|
def __init__(self, context: Context) -> None: |
19
|
|
|
"""Constructor. |
20
|
|
|
|
21
|
|
|
Args: |
22
|
|
|
context (Context): The ``prompt.context.Context`` instance. |
23
|
|
|
""" |
24
|
|
|
self.context = context |
25
|
|
|
|
26
|
|
|
@property |
27
|
|
|
def locus(self) -> int: |
28
|
|
|
"""int: Read and write current locus index of the caret in the prompt. |
29
|
|
|
|
30
|
|
|
When a value is smaller than the ``head`` attribute or larger than the |
31
|
|
|
``tail`` attribute, the value is regualted to ``head`` or ``tail``. |
32
|
|
|
|
33
|
|
|
Example: |
34
|
|
|
>>> from neovim_prompt.context import Context |
35
|
|
|
>>> context = Context() |
36
|
|
|
>>> context.text = "Hello" |
37
|
|
|
>>> caret = Caret(context) |
38
|
|
|
>>> caret.locus |
39
|
|
|
0 |
40
|
|
|
>>> caret.locus = 3 |
41
|
|
|
>>> caret.locus |
42
|
|
|
3 |
43
|
|
|
>>> caret.locus = -1 |
44
|
|
|
>>> caret.locus |
45
|
|
|
0 |
46
|
|
|
>>> caret.locus = 100 # beyond text length |
47
|
|
|
>>> caret.locus |
48
|
|
|
5 |
49
|
|
|
|
50
|
|
|
""" |
51
|
|
|
return self.context.caret_locus |
52
|
|
|
|
53
|
|
|
@locus.setter |
54
|
|
|
def locus(self, value: int) -> None: |
55
|
|
|
if value < self.head: |
56
|
|
|
self.context.caret_locus = self.head |
57
|
|
|
elif value > self.tail: |
58
|
|
|
self.context.caret_locus = self.tail |
59
|
|
|
else: |
60
|
|
|
self.context.caret_locus = value |
61
|
|
|
|
62
|
|
|
@property |
63
|
|
|
def head(self) -> int: |
64
|
|
|
"""int: Readonly head locus index of the caret in the prompt. |
65
|
|
|
|
66
|
|
|
Example: |
67
|
|
|
>>> from neovim_prompt.context import Context |
68
|
|
|
>>> context = Context() |
69
|
|
|
>>> context.text = "Hello" |
70
|
|
|
>>> caret = Caret(context) |
71
|
|
|
>>> caret.head |
72
|
|
|
0 |
73
|
|
|
|
74
|
|
|
""" |
75
|
|
|
return 0 |
76
|
|
|
|
77
|
|
|
@property |
78
|
|
|
def lead(self) -> int: |
79
|
|
|
"""int: Readonly lead locus index of the caret in the prompt. |
80
|
|
|
|
81
|
|
|
The lead indicate a minimum index for a first printable character. |
82
|
|
|
|
83
|
|
|
Examples: |
84
|
|
|
For example, the ``lead`` become ``4`` in the following case. |
85
|
|
|
|
86
|
|
|
>>> from .context import Context |
87
|
|
|
>>> context = Context() |
88
|
|
|
>>> context.text = ' Hello world!' |
89
|
|
|
>>> caret = Caret(context) |
90
|
|
|
>>> caret.lead |
91
|
|
|
4 |
92
|
|
|
""" |
93
|
|
|
return len(self.context.text) - len(self.context.text.lstrip()) |
94
|
|
|
|
95
|
|
|
@property |
96
|
|
|
def tail(self) -> int: |
97
|
|
|
"""int: Readonly tail locus index of the caret in the prompt. |
98
|
|
|
|
99
|
|
|
Example: |
100
|
|
|
>>> from neovim_prompt.context import Context |
101
|
|
|
>>> context = Context() |
102
|
|
|
>>> context.text = "Hello" |
103
|
|
|
>>> caret = Caret(context) |
104
|
|
|
>>> caret.tail |
105
|
|
|
5 |
106
|
|
|
""" |
107
|
|
|
return len(self.context.text) |
108
|
|
|
|
109
|
|
|
def get_backward_text(self) -> str: |
110
|
|
|
"""A backward text from the caret. |
111
|
|
|
|
112
|
|
|
Returns: |
113
|
|
|
str: A backward part of the text from the caret |
114
|
|
|
|
115
|
|
|
Examples: |
116
|
|
|
>>> from .context import Context |
117
|
|
|
>>> context = Context() |
118
|
|
|
>>> # Caret: | |
119
|
|
|
>>> context.text = ' Hello world!' |
120
|
|
|
>>> context.caret_locus = 8 |
121
|
|
|
>>> caret = Caret(context) |
122
|
|
|
>>> caret.get_backward_text() |
123
|
|
|
' Hell' |
124
|
|
|
""" |
125
|
|
|
if self.locus == self.head: |
126
|
|
|
return '' |
127
|
|
|
return self.context.text[:self.locus] |
128
|
|
|
|
129
|
|
|
def get_selected_text(self) -> str: |
130
|
|
|
"""A selected text under the caret. |
131
|
|
|
|
132
|
|
|
Returns: |
133
|
|
|
str: A part of the text under the caret |
134
|
|
|
|
135
|
|
|
Examples: |
136
|
|
|
>>> from .context import Context |
137
|
|
|
>>> context = Context() |
138
|
|
|
>>> # Caret: | |
139
|
|
|
>>> context.text = ' Hello world!' |
140
|
|
|
>>> context.caret_locus = 8 |
141
|
|
|
>>> caret = Caret(context) |
142
|
|
|
>>> caret.get_selected_text() |
143
|
|
|
'o' |
144
|
|
|
""" |
145
|
|
|
if self.locus == self.tail: |
146
|
|
|
return '' |
147
|
|
|
return self.context.text[self.locus] |
148
|
|
|
|
149
|
|
|
def get_forward_text(self) -> str: |
150
|
|
|
"""A forward text from the caret. |
151
|
|
|
|
152
|
|
|
Returns: |
153
|
|
|
str: A forward part of the text from the caret |
154
|
|
|
|
155
|
|
|
Examples: |
156
|
|
|
>>> from .context import Context |
157
|
|
|
>>> context = Context() |
158
|
|
|
>>> # Caret: | |
159
|
|
|
>>> context.text = ' Hello world!' |
160
|
|
|
>>> context.caret_locus = 8 |
161
|
|
|
>>> caret = Caret(context) |
162
|
|
|
>>> caret.get_forward_text() |
163
|
|
|
' world!' |
164
|
|
|
""" |
165
|
|
|
if self.locus >= self.tail - 1: |
166
|
|
|
return '' |
167
|
|
|
return self.context.text[self.locus + 1:] |
168
|
|
|
|