inquirer-textual 0.2.0__py3-none-any.whl → 0.4.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- inquirer_textual/InquirerApp.py +24 -22
- inquirer_textual/common/Answer.py +22 -0
- inquirer_textual/common/Choice.py +3 -0
- inquirer_textual/common/ChoiceLabel.py +14 -5
- inquirer_textual/common/InquirerResult.py +24 -0
- inquirer_textual/common/{PromptMessage.py → Prompt.py} +3 -3
- inquirer_textual/common/defaults.py +24 -0
- inquirer_textual/prompts.py +57 -45
- inquirer_textual/version.py +1 -1
- inquirer_textual/widgets/InquirerCheckbox.py +30 -22
- inquirer_textual/widgets/InquirerConfirm.py +31 -11
- inquirer_textual/widgets/InquirerEditor.py +72 -0
- inquirer_textual/widgets/InquirerMulti.py +16 -17
- inquirer_textual/widgets/InquirerNumber.py +20 -12
- inquirer_textual/widgets/InquirerPath.py +95 -0
- inquirer_textual/widgets/InquirerPattern.py +175 -0
- inquirer_textual/widgets/InquirerSecret.py +5 -6
- inquirer_textual/widgets/InquirerSelect.py +32 -28
- inquirer_textual/widgets/InquirerText.py +10 -7
- inquirer_textual/widgets/InquirerWidget.py +8 -2
- {inquirer_textual-0.2.0.dist-info → inquirer_textual-0.4.0.dist-info}/METADATA +42 -3
- inquirer_textual-0.4.0.dist-info/RECORD +30 -0
- inquirer_textual/common/Result.py +0 -15
- inquirer_textual-0.2.0.dist-info/RECORD +0 -25
- {inquirer_textual-0.2.0.dist-info → inquirer_textual-0.4.0.dist-info}/WHEEL +0 -0
- {inquirer_textual-0.2.0.dist-info → inquirer_textual-0.4.0.dist-info}/licenses/LICENSE +0 -0
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
from typing import Any
|
|
2
2
|
|
|
3
3
|
from textual.app import ComposeResult
|
|
4
|
-
from textual.widgets import ContentSwitcher
|
|
5
4
|
|
|
6
5
|
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
7
6
|
|
|
@@ -15,32 +14,32 @@ class InquirerMulti(InquirerWidget):
|
|
|
15
14
|
}
|
|
16
15
|
"""
|
|
17
16
|
|
|
18
|
-
def __init__(self, widgets:
|
|
17
|
+
def __init__(self, widgets: dict[str, InquirerWidget]) -> None:
|
|
19
18
|
"""
|
|
20
19
|
Args:
|
|
21
|
-
widgets (
|
|
20
|
+
widgets (dict[str, InquirerWidget]): A dictionary of InquirerWidget instances to present in sequence of definition.
|
|
22
21
|
"""
|
|
23
22
|
super().__init__()
|
|
24
23
|
self.widgets = widgets
|
|
25
24
|
self._current_widget_index = 0
|
|
26
|
-
self.
|
|
25
|
+
self._return_values_dict: dict[str, Any] = {}
|
|
27
26
|
|
|
28
|
-
def
|
|
29
|
-
self.
|
|
30
|
-
self.
|
|
31
|
-
|
|
32
|
-
def on_inquirer_widget_submit(self, message: InquirerWidget.Submit) -> None:
|
|
33
|
-
self._return_values.append(message.value)
|
|
27
|
+
async def on_inquirer_widget_submit(self, message: InquirerWidget.Submit) -> None:
|
|
28
|
+
current_item = list(self.widgets.items())[self._current_widget_index]
|
|
29
|
+
self._return_values_dict[current_item[0]] = message.value
|
|
30
|
+
await current_item[1].set_selected_value(message.value)
|
|
34
31
|
self._current_widget_index += 1
|
|
35
32
|
if self._current_widget_index < len(self.widgets):
|
|
36
33
|
message.stop()
|
|
37
|
-
self.query_one(
|
|
38
|
-
|
|
34
|
+
next_widget = self.query_one(f'#widget-{self._current_widget_index}')
|
|
35
|
+
next_widget.styles.display = 'block'
|
|
36
|
+
next_widget.focus()
|
|
39
37
|
else:
|
|
40
|
-
message.value = self.
|
|
38
|
+
message.value = self._return_values_dict
|
|
41
39
|
|
|
42
40
|
def compose(self) -> ComposeResult:
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
41
|
+
for idx, item in enumerate(self.widgets.items()):
|
|
42
|
+
item[1].id = f'widget-{idx}'
|
|
43
|
+
if idx > 0:
|
|
44
|
+
item[1].styles.display = 'none'
|
|
45
|
+
yield item[1]
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing_extensions import Self
|
|
4
|
-
|
|
5
3
|
from textual.app import ComposeResult
|
|
6
4
|
from textual.containers import HorizontalGroup
|
|
7
5
|
from textual.widgets import Input
|
|
6
|
+
from typing_extensions import Self, Literal
|
|
8
7
|
|
|
8
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
9
9
|
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
10
|
-
from inquirer_textual.common.PromptMessage import PromptMessage
|
|
11
10
|
|
|
12
11
|
|
|
13
12
|
class InquirerNumber(InquirerWidget):
|
|
@@ -19,24 +18,28 @@ class InquirerNumber(InquirerWidget):
|
|
|
19
18
|
}
|
|
20
19
|
#inquirer-number-input {
|
|
21
20
|
border: none;
|
|
22
|
-
|
|
23
|
-
color: $input-color;
|
|
21
|
+
color: $inquirer-textual-input-color;
|
|
24
22
|
padding: 0;
|
|
25
23
|
height: 1;
|
|
26
24
|
}
|
|
27
25
|
"""
|
|
28
26
|
|
|
29
|
-
def __init__(self, message: str
|
|
27
|
+
def __init__(self, message: str, name: str | None = None, input_type: Literal['integer', 'number'] = 'integer',
|
|
28
|
+
mandatory: bool = False):
|
|
30
29
|
"""
|
|
31
30
|
Args:
|
|
32
31
|
message (str): The prompt message to display.
|
|
32
|
+
name (str | None): The name of the input field.
|
|
33
|
+
input_type (Literal['integer', 'number']): The type of number input ('integer' or 'number').
|
|
34
|
+
mandatory (bool): Whether the input is mandatory.
|
|
33
35
|
"""
|
|
34
|
-
super().__init__()
|
|
36
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
35
37
|
self.message = message
|
|
38
|
+
self.input_type = input_type
|
|
36
39
|
self.input: Input | None = None
|
|
37
40
|
|
|
38
|
-
def on_input_submitted(self
|
|
39
|
-
self.
|
|
41
|
+
def on_input_submitted(self) -> None:
|
|
42
|
+
self.submit_current_value()
|
|
40
43
|
|
|
41
44
|
def focus(self, scroll_visible: bool = True) -> Self:
|
|
42
45
|
if self.input:
|
|
@@ -45,10 +48,15 @@ class InquirerNumber(InquirerWidget):
|
|
|
45
48
|
return super().focus(scroll_visible)
|
|
46
49
|
|
|
47
50
|
def current_value(self):
|
|
48
|
-
|
|
51
|
+
if self.input and self.input.value:
|
|
52
|
+
if self.input_type == 'integer':
|
|
53
|
+
return int(self.input.value)
|
|
54
|
+
elif self.input_type == 'number':
|
|
55
|
+
return float(self.input.value)
|
|
56
|
+
return None
|
|
49
57
|
|
|
50
58
|
def compose(self) -> ComposeResult:
|
|
51
59
|
with HorizontalGroup():
|
|
52
|
-
yield
|
|
53
|
-
self.input = Input(id="inquirer-number-input", type=
|
|
60
|
+
yield Prompt(self.message)
|
|
61
|
+
self.input = Input(id="inquirer-number-input", type=self.input_type)
|
|
54
62
|
yield self.input
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from enum import Enum
|
|
4
|
+
from pathlib import Path
|
|
5
|
+
|
|
6
|
+
from textual.app import ComposeResult
|
|
7
|
+
from textual.containers import HorizontalGroup
|
|
8
|
+
from textual.widgets import Input, Static
|
|
9
|
+
from typing_extensions import Self
|
|
10
|
+
|
|
11
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
12
|
+
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class PathType(Enum):
|
|
16
|
+
FILE = 'file'
|
|
17
|
+
DIRECTORY = 'directory'
|
|
18
|
+
ANY = 'any'
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class InquirerPath(InquirerWidget):
|
|
22
|
+
"""An input prompt that allows the user to enter a file path."""
|
|
23
|
+
|
|
24
|
+
DEFAULT_CSS = """
|
|
25
|
+
InquirerPath {
|
|
26
|
+
height: auto;
|
|
27
|
+
}
|
|
28
|
+
#inquirer-path-input {
|
|
29
|
+
border: none;
|
|
30
|
+
color: $inquirer-textual-input-color;
|
|
31
|
+
padding: 0;
|
|
32
|
+
height: 1;
|
|
33
|
+
}
|
|
34
|
+
#inquirer-path-error-message {
|
|
35
|
+
color: $error;
|
|
36
|
+
height: auto;
|
|
37
|
+
}
|
|
38
|
+
"""
|
|
39
|
+
|
|
40
|
+
def __init__(self, message: str, name: str | None = None, default: str = '', exists: bool = False,
|
|
41
|
+
path_type: PathType = PathType.ANY, mandatory: bool = False):
|
|
42
|
+
"""
|
|
43
|
+
Args:
|
|
44
|
+
message (str): The prompt message to display.
|
|
45
|
+
default (str): The default value if the user presses Enter without input.
|
|
46
|
+
exists (bool): If True, validate that the entered path exists.
|
|
47
|
+
"""
|
|
48
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
49
|
+
self.message = message
|
|
50
|
+
self.input: Input | None = None
|
|
51
|
+
self.default = default
|
|
52
|
+
self.exists = exists
|
|
53
|
+
self.path_type = path_type
|
|
54
|
+
|
|
55
|
+
def on_mount(self):
|
|
56
|
+
super().on_mount()
|
|
57
|
+
self.input.value = self.default
|
|
58
|
+
|
|
59
|
+
def on_input_submitted(self, submitted: Input.Submitted):
|
|
60
|
+
if self.exists:
|
|
61
|
+
if not Path(submitted.value).exists():
|
|
62
|
+
self._show_validation_error("The specified path does not exist.")
|
|
63
|
+
return
|
|
64
|
+
if self.path_type != PathType.ANY:
|
|
65
|
+
path = Path(submitted.value)
|
|
66
|
+
if self.path_type == PathType.FILE and not path.is_file():
|
|
67
|
+
self._show_validation_error("The specified path is not a file.")
|
|
68
|
+
return
|
|
69
|
+
if self.path_type == PathType.DIRECTORY and not path.is_dir():
|
|
70
|
+
self._show_validation_error("The specified path is not a directory.")
|
|
71
|
+
return
|
|
72
|
+
self._show_validation_error('')
|
|
73
|
+
if self.input:
|
|
74
|
+
self.input._cursor_visible = False
|
|
75
|
+
self.submit_current_value()
|
|
76
|
+
|
|
77
|
+
def _show_validation_error(self, message: str):
|
|
78
|
+
error_message = self.query_one('#inquirer-path-error-message', Static)
|
|
79
|
+
error_message.update(message)
|
|
80
|
+
|
|
81
|
+
def focus(self, scroll_visible: bool = True) -> Self:
|
|
82
|
+
if self.input:
|
|
83
|
+
return self.input.focus(scroll_visible)
|
|
84
|
+
else:
|
|
85
|
+
return super().focus(scroll_visible)
|
|
86
|
+
|
|
87
|
+
def current_value(self):
|
|
88
|
+
return self.input.value if self.input else None
|
|
89
|
+
|
|
90
|
+
def compose(self) -> ComposeResult:
|
|
91
|
+
with HorizontalGroup():
|
|
92
|
+
yield Prompt(self.message)
|
|
93
|
+
self.input = Input(id="inquirer-path-input")
|
|
94
|
+
yield self.input
|
|
95
|
+
yield Static("", id="inquirer-path-error-message")
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from textual import on, events
|
|
4
|
+
from textual.app import ComposeResult
|
|
5
|
+
from textual.containers import VerticalGroup, HorizontalGroup
|
|
6
|
+
from textual.css.query import NoMatches
|
|
7
|
+
from textual.reactive import reactive
|
|
8
|
+
from textual.widgets import ListView, ListItem, Input, Static
|
|
9
|
+
from typing_extensions import Self
|
|
10
|
+
|
|
11
|
+
from inquirer_textual.common.Answer import Answer
|
|
12
|
+
from inquirer_textual.common.Choice import Choice
|
|
13
|
+
from inquirer_textual.common.ChoiceLabel import ChoiceLabel
|
|
14
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
15
|
+
from inquirer_textual.common.defaults import POINTER_CHARACTER
|
|
16
|
+
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class InquirerPattern(InquirerWidget):
|
|
20
|
+
"""A select widget that allows a single selection from a list of choices with pattern filtering."""
|
|
21
|
+
|
|
22
|
+
DEFAULT_CSS = """
|
|
23
|
+
#inquirer-pattern-query-container {
|
|
24
|
+
width: auto;
|
|
25
|
+
}
|
|
26
|
+
#inquirer-pattern-query {
|
|
27
|
+
border: none;
|
|
28
|
+
color: $inquirer-textual-input-color;
|
|
29
|
+
padding: 0;
|
|
30
|
+
height: 1;
|
|
31
|
+
width: 20;
|
|
32
|
+
}
|
|
33
|
+
#inquirer-pattern-query-pointer {
|
|
34
|
+
width: auto;
|
|
35
|
+
color: $accent;
|
|
36
|
+
}
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
candidates: reactive[list[str | Choice]] = reactive([])
|
|
40
|
+
|
|
41
|
+
def __init__(self, message: str, choices: list[str | Choice], name: str | None = None,
|
|
42
|
+
default: str | Choice | None = None, mandatory: bool = True):
|
|
43
|
+
"""
|
|
44
|
+
Args:
|
|
45
|
+
message (str): The prompt message to display.
|
|
46
|
+
choices (list[str | Choice]): A list of choices to present to the user.
|
|
47
|
+
default (str | Choice | None): The default choice to pre-select.
|
|
48
|
+
mandatory (bool): Whether a response is mandatory.
|
|
49
|
+
"""
|
|
50
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
51
|
+
self.message = message
|
|
52
|
+
self.choices = choices
|
|
53
|
+
self.candidates = choices.copy()
|
|
54
|
+
self.list_view: ListView | None = None
|
|
55
|
+
self.selected_label: ChoiceLabel | None = None
|
|
56
|
+
self.selected_item: str | Choice | None = None
|
|
57
|
+
self.default = default
|
|
58
|
+
self.query: Input | None = None
|
|
59
|
+
self.selected_value: str | Choice | None = None
|
|
60
|
+
self.show_selected_value: bool = False
|
|
61
|
+
|
|
62
|
+
def on_mount(self):
|
|
63
|
+
super().on_mount()
|
|
64
|
+
if self.app.is_inline:
|
|
65
|
+
self.styles.height = min(10, len(self.choices) + 1)
|
|
66
|
+
else:
|
|
67
|
+
self.styles.height = '1fr'
|
|
68
|
+
|
|
69
|
+
def on_list_view_highlighted(self, event: ListView.Highlighted) -> None:
|
|
70
|
+
if self.selected_label:
|
|
71
|
+
self.selected_label.remove_pointer()
|
|
72
|
+
if event.item:
|
|
73
|
+
try:
|
|
74
|
+
label = event.item.query_one(ChoiceLabel)
|
|
75
|
+
label.add_pointer()
|
|
76
|
+
self.selected_label = label
|
|
77
|
+
self.selected_item = label.item
|
|
78
|
+
return
|
|
79
|
+
except NoMatches:
|
|
80
|
+
pass
|
|
81
|
+
self.selected_label = None
|
|
82
|
+
self.selected_item = None
|
|
83
|
+
|
|
84
|
+
def on_list_view_selected(self, _: ListView.Selected):
|
|
85
|
+
if isinstance(self.selected_item, Choice):
|
|
86
|
+
self.submit_current_value(self.selected_item.command)
|
|
87
|
+
else:
|
|
88
|
+
self.submit_current_value()
|
|
89
|
+
|
|
90
|
+
def focus(self, scroll_visible: bool = True) -> Self:
|
|
91
|
+
if self.query:
|
|
92
|
+
return self.query.focus(scroll_visible)
|
|
93
|
+
else:
|
|
94
|
+
return super().focus(scroll_visible)
|
|
95
|
+
|
|
96
|
+
def current_value(self):
|
|
97
|
+
return self.selected_item
|
|
98
|
+
|
|
99
|
+
def _collect_list_items(self):
|
|
100
|
+
items: list[ListItem] = []
|
|
101
|
+
for candidate in self.candidates:
|
|
102
|
+
list_item = ListItem(ChoiceLabel(candidate, self.query.value if self.query else None))
|
|
103
|
+
items.append(list_item)
|
|
104
|
+
return items
|
|
105
|
+
|
|
106
|
+
def _find_initial_index(self):
|
|
107
|
+
initial_index = 0
|
|
108
|
+
for idx, choice in enumerate(self.choices):
|
|
109
|
+
if self.default and choice == self.default:
|
|
110
|
+
initial_index = idx
|
|
111
|
+
return initial_index
|
|
112
|
+
|
|
113
|
+
@on(Input.Changed, '#inquirer-pattern-query')
|
|
114
|
+
async def handle_query_changed(self, event: Input.Changed):
|
|
115
|
+
query = event.value.lower()
|
|
116
|
+
if query == '':
|
|
117
|
+
self.candidates = self.choices.copy()
|
|
118
|
+
else:
|
|
119
|
+
filtered = []
|
|
120
|
+
for choice in self.choices:
|
|
121
|
+
name = choice.name if isinstance(choice, Choice) else choice
|
|
122
|
+
if query in name.lower():
|
|
123
|
+
filtered.append(choice)
|
|
124
|
+
self.candidates = filtered
|
|
125
|
+
assert isinstance(self.list_view, ListView)
|
|
126
|
+
await self.list_view.clear()
|
|
127
|
+
list_items = self._collect_list_items()
|
|
128
|
+
await self.list_view.extend(list_items)
|
|
129
|
+
if list_items:
|
|
130
|
+
self.list_view.index = 0
|
|
131
|
+
|
|
132
|
+
def watch_candidates(self, candidates: list[str | Choice]) -> None:
|
|
133
|
+
count_suffix = f'[{len(candidates)}/{len(self.choices)}]'
|
|
134
|
+
try:
|
|
135
|
+
count_widget = self.query_one('#inquirer-pattern-query-count-suffix', Static)
|
|
136
|
+
count_widget.update(count_suffix)
|
|
137
|
+
except NoMatches:
|
|
138
|
+
pass
|
|
139
|
+
|
|
140
|
+
def on_key(self, event: events.Key):
|
|
141
|
+
assert isinstance(self.list_view, ListView)
|
|
142
|
+
if event.key == 'down':
|
|
143
|
+
event.stop()
|
|
144
|
+
self.list_view.action_cursor_down()
|
|
145
|
+
elif event.key == 'up':
|
|
146
|
+
event.stop()
|
|
147
|
+
self.list_view.action_cursor_up()
|
|
148
|
+
elif event.key == 'enter':
|
|
149
|
+
event.stop()
|
|
150
|
+
self.list_view.action_select_cursor()
|
|
151
|
+
|
|
152
|
+
async def set_selected_value(self, value: str | Choice) -> None:
|
|
153
|
+
self.selected_value = value
|
|
154
|
+
self.styles.height = 1
|
|
155
|
+
self.show_selected_value = True
|
|
156
|
+
await self.recompose()
|
|
157
|
+
|
|
158
|
+
def compose(self) -> ComposeResult:
|
|
159
|
+
if self.show_selected_value:
|
|
160
|
+
with HorizontalGroup():
|
|
161
|
+
yield Prompt(self.message)
|
|
162
|
+
yield Answer(str(self.selected_value))
|
|
163
|
+
else:
|
|
164
|
+
with VerticalGroup():
|
|
165
|
+
self.list_view = ListView(*self._collect_list_items(), id='inquirer-pattern-list-view',
|
|
166
|
+
initial_index=self._find_initial_index())
|
|
167
|
+
with HorizontalGroup():
|
|
168
|
+
yield Prompt(self.message)
|
|
169
|
+
yield Static(f'[{len(self.candidates)}/{len(self.choices)}]',
|
|
170
|
+
id='inquirer-pattern-query-count-suffix')
|
|
171
|
+
with HorizontalGroup(id='inquirer-pattern-query-container'):
|
|
172
|
+
yield Static(f'{POINTER_CHARACTER} ', id='inquirer-pattern-query-pointer')
|
|
173
|
+
self.query = Input(id="inquirer-pattern-query")
|
|
174
|
+
yield self.query
|
|
175
|
+
yield self.list_view
|
|
@@ -5,7 +5,7 @@ from textual.containers import HorizontalGroup
|
|
|
5
5
|
from textual.widgets import Input
|
|
6
6
|
from typing_extensions import Self
|
|
7
7
|
|
|
8
|
-
from inquirer_textual.common.
|
|
8
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
9
9
|
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
10
10
|
|
|
11
11
|
|
|
@@ -18,19 +18,18 @@ class InquirerSecret(InquirerWidget):
|
|
|
18
18
|
}
|
|
19
19
|
#inquirer-secret-input {
|
|
20
20
|
border: none;
|
|
21
|
-
|
|
22
|
-
color: $input-color;
|
|
21
|
+
color: $inquirer-textual-input-color;
|
|
23
22
|
padding: 0;
|
|
24
23
|
height: 1;
|
|
25
24
|
}
|
|
26
25
|
"""
|
|
27
26
|
|
|
28
|
-
def __init__(self, message: str):
|
|
27
|
+
def __init__(self, message: str, name: str | None = None, mandatory: bool = False):
|
|
29
28
|
"""
|
|
30
29
|
Args:
|
|
31
30
|
message (str): The prompt message to display.
|
|
32
31
|
"""
|
|
33
|
-
super().__init__()
|
|
32
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
34
33
|
self.message = message
|
|
35
34
|
self.input: Input | None = None
|
|
36
35
|
|
|
@@ -48,7 +47,7 @@ class InquirerSecret(InquirerWidget):
|
|
|
48
47
|
|
|
49
48
|
def compose(self) -> ComposeResult:
|
|
50
49
|
with HorizontalGroup():
|
|
51
|
-
yield
|
|
50
|
+
yield Prompt(self.message)
|
|
52
51
|
self.input = Input(id="inquirer-secret-input")
|
|
53
52
|
self.input.password = True
|
|
54
53
|
yield self.input
|
|
@@ -1,31 +1,22 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
3
|
from textual.app import ComposeResult
|
|
4
|
-
from textual.containers import VerticalGroup
|
|
4
|
+
from textual.containers import VerticalGroup, HorizontalGroup
|
|
5
5
|
from textual.widgets import ListView, ListItem
|
|
6
6
|
from typing_extensions import Self
|
|
7
7
|
|
|
8
|
+
from inquirer_textual.common.Answer import Answer
|
|
8
9
|
from inquirer_textual.common.Choice import Choice
|
|
9
10
|
from inquirer_textual.common.ChoiceLabel import ChoiceLabel
|
|
10
|
-
from inquirer_textual.common.
|
|
11
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
11
12
|
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
12
13
|
|
|
13
14
|
|
|
14
15
|
class InquirerSelect(InquirerWidget):
|
|
15
16
|
"""A select widget that allows a single selection from a list of choices."""
|
|
16
17
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
background: transparent;
|
|
20
|
-
}
|
|
21
|
-
#inquirer-select-list-view ListItem.-highlight {
|
|
22
|
-
color: $select-list-item-highlight-foreground;
|
|
23
|
-
background: transparent;
|
|
24
|
-
}
|
|
25
|
-
"""
|
|
26
|
-
|
|
27
|
-
def __init__(self, message: str, choices: list[str | Choice], default: str | Choice | None = None,
|
|
28
|
-
mandatory: bool = True):
|
|
18
|
+
def __init__(self, message: str, choices: list[str | Choice], name: str | None = None,
|
|
19
|
+
default: str | Choice | None = None, mandatory: bool = True):
|
|
29
20
|
"""
|
|
30
21
|
Args:
|
|
31
22
|
message (str): The prompt message to display.
|
|
@@ -33,13 +24,15 @@ class InquirerSelect(InquirerWidget):
|
|
|
33
24
|
default (str | Choice | None): The default choice to pre-select.
|
|
34
25
|
mandatory (bool): Whether a response is mandatory.
|
|
35
26
|
"""
|
|
36
|
-
super().__init__(mandatory)
|
|
27
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
37
28
|
self.message = message
|
|
38
29
|
self.choices = choices
|
|
39
30
|
self.list_view: ListView | None = None
|
|
40
31
|
self.selected_label: ChoiceLabel | None = None
|
|
41
32
|
self.selected_item: str | Choice | None = None
|
|
42
33
|
self.default = default
|
|
34
|
+
self.selected_value: str | Choice | None = None
|
|
35
|
+
self.show_selected_value: bool = False
|
|
43
36
|
|
|
44
37
|
def on_mount(self):
|
|
45
38
|
super().on_mount()
|
|
@@ -58,9 +51,9 @@ class InquirerSelect(InquirerWidget):
|
|
|
58
51
|
|
|
59
52
|
def on_list_view_selected(self, _: ListView.Selected):
|
|
60
53
|
if isinstance(self.selected_item, Choice):
|
|
61
|
-
self.
|
|
54
|
+
self.submit_current_value(self.selected_item.command)
|
|
62
55
|
else:
|
|
63
|
-
self.
|
|
56
|
+
self.submit_current_value()
|
|
64
57
|
|
|
65
58
|
def focus(self, scroll_visible: bool = True) -> Self:
|
|
66
59
|
if self.list_view:
|
|
@@ -71,15 +64,26 @@ class InquirerSelect(InquirerWidget):
|
|
|
71
64
|
def current_value(self):
|
|
72
65
|
return self.selected_item
|
|
73
66
|
|
|
67
|
+
async def set_selected_value(self, value: str | Choice) -> None:
|
|
68
|
+
self.selected_value = value
|
|
69
|
+
self.styles.height = 1
|
|
70
|
+
self.show_selected_value = True
|
|
71
|
+
await self.recompose()
|
|
72
|
+
|
|
74
73
|
def compose(self) -> ComposeResult:
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
74
|
+
if self.show_selected_value:
|
|
75
|
+
with HorizontalGroup():
|
|
76
|
+
yield Prompt(self.message)
|
|
77
|
+
yield Answer(str(self.selected_value))
|
|
78
|
+
else:
|
|
79
|
+
with VerticalGroup():
|
|
80
|
+
initial_index = 0
|
|
81
|
+
items: list[ListItem] = []
|
|
82
|
+
for idx, choice in enumerate(self.choices):
|
|
83
|
+
list_item = ListItem(ChoiceLabel(choice))
|
|
84
|
+
items.append(list_item)
|
|
85
|
+
if self.default and choice == self.default:
|
|
86
|
+
initial_index = idx
|
|
87
|
+
self.list_view = ListView(*items, id='inquirer-select-list-view', initial_index=initial_index)
|
|
88
|
+
yield Prompt(self.message)
|
|
89
|
+
yield self.list_view
|
|
@@ -8,7 +8,7 @@ from textual.validation import Validator
|
|
|
8
8
|
from textual.widgets import Input
|
|
9
9
|
from typing_extensions import Self
|
|
10
10
|
|
|
11
|
-
from inquirer_textual.common.
|
|
11
|
+
from inquirer_textual.common.Prompt import Prompt
|
|
12
12
|
from inquirer_textual.widgets.InquirerWidget import InquirerWidget
|
|
13
13
|
|
|
14
14
|
|
|
@@ -21,14 +21,15 @@ class InquirerText(InquirerWidget):
|
|
|
21
21
|
}
|
|
22
22
|
#inquirer-text-input {
|
|
23
23
|
border: none;
|
|
24
|
-
|
|
25
|
-
color: $input-color;
|
|
24
|
+
color: $inquirer-textual-input-color;
|
|
26
25
|
padding: 0;
|
|
27
26
|
height: 1;
|
|
28
27
|
}
|
|
29
28
|
"""
|
|
30
29
|
|
|
31
|
-
def __init__(self, message: str,
|
|
30
|
+
def __init__(self, message: str, name: str | None = None, default: str = '',
|
|
31
|
+
validators: Validator | Iterable[Validator] | None = None,
|
|
32
|
+
mandatory: bool = False):
|
|
32
33
|
"""
|
|
33
34
|
Args:
|
|
34
35
|
message (str): The prompt message to display.
|
|
@@ -36,7 +37,7 @@ class InquirerText(InquirerWidget):
|
|
|
36
37
|
validators (Validator | Iterable[Validator] | None): A validator or list of validators to validate the
|
|
37
38
|
input.
|
|
38
39
|
"""
|
|
39
|
-
super().__init__()
|
|
40
|
+
super().__init__(name=name, mandatory=mandatory)
|
|
40
41
|
self.message = message
|
|
41
42
|
self.input: Input | None = None
|
|
42
43
|
self.default = default
|
|
@@ -48,7 +49,9 @@ class InquirerText(InquirerWidget):
|
|
|
48
49
|
|
|
49
50
|
def on_input_submitted(self, submitted: Input.Submitted):
|
|
50
51
|
if self.validators is None or submitted.validation_result.is_valid:
|
|
51
|
-
self.
|
|
52
|
+
if self.input:
|
|
53
|
+
self.input._cursor_visible = False
|
|
54
|
+
self.submit_current_value()
|
|
52
55
|
|
|
53
56
|
def focus(self, scroll_visible: bool = True) -> Self:
|
|
54
57
|
if self.input:
|
|
@@ -61,6 +64,6 @@ class InquirerText(InquirerWidget):
|
|
|
61
64
|
|
|
62
65
|
def compose(self) -> ComposeResult:
|
|
63
66
|
with HorizontalGroup():
|
|
64
|
-
yield
|
|
67
|
+
yield Prompt(self.message)
|
|
65
68
|
self.input = Input(id="inquirer-text-input", validators=self.validators)
|
|
66
69
|
yield self.input
|
|
@@ -13,8 +13,8 @@ class InquirerWidget(Widget):
|
|
|
13
13
|
self.value = value
|
|
14
14
|
self.command = command
|
|
15
15
|
|
|
16
|
-
def __init__(self, mandatory: bool =
|
|
17
|
-
super().__init__()
|
|
16
|
+
def __init__(self, name: str | None = None, mandatory: bool = False):
|
|
17
|
+
super().__init__(name=name)
|
|
18
18
|
self.mandatory = mandatory
|
|
19
19
|
|
|
20
20
|
def on_mount(self):
|
|
@@ -26,3 +26,9 @@ class InquirerWidget(Widget):
|
|
|
26
26
|
|
|
27
27
|
def current_value(self):
|
|
28
28
|
raise NotImplementedError("Subclasses must implement current_value method")
|
|
29
|
+
|
|
30
|
+
async def set_selected_value(self, value: Any) -> None:
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
def submit_current_value(self, command: str | None = "select"):
|
|
34
|
+
self.post_message(InquirerWidget.Submit(self.current_value(), command))
|