File: number.md

package info (click to toggle)
python-inquirerpy 0.3.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,088 kB
  • sloc: python: 9,463; makefile: 15
file content (266 lines) | stat: -rw-r--r-- 5,269 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# number

A prompt for entering numbers. All non number input will be disabled for this prompt.

## Example

![demo](https://assets.kazhala.me/InquirerPy/number.gif)

<details>
  <summary>Classic Syntax (PyInquirer)</summary>

```{eval-rst}
.. literalinclude :: ../../../examples/classic/number.py
   :language: python
```

</details>

<details open>
  <summary>Alternate Syntax</summary>

```{eval-rst}
.. literalinclude :: ../../../examples/alternate/number.py
   :language: python
```

</details>

## Keybindings

```{seealso}
{ref}`pages/kb:Keybindings`
```

```{include} ../kb.md
:start-after: <!-- start kb -->
:end-before: <!-- end kb -->
```

The following dictionary contains the additional keybindings created by this prompt.

```
{
  "down": [
    {"key": "down"},  # decrement the number
    {"key": "c-n"},
  ],
  "up": [
    {"key": "up"},  # increment the number
    {"key": "c-p"},
  ],
  "left": [
    {"key": "left"},  # move cursor to the left
    {"key": "c-b"},
  ],
  "right": [
    {"key": "right"},   # move cursor to the right
    {"key": "c-f"},
  ],
  "focus": [
    {"key": "c-i"},   # focus the alternate input buffer when float_allowed=True
    {"key": "s-tab"},
  ],
  "negative_toggle": [{"key": "-"}], # toggle result negativity
  "dot": [{"key": "."}],  # focus the integral buffer when float_allowed=True to enter decimal points
}
```

When `vi_mode` is True, the direction navigation key will be changed.

```{tip}
Additionally, the input buffer can also enter normal mode by pressing `esc` when `vi_mode` is True.
```

```
{
  "down": [
    {"key": "down"},
    {"key": "j"},
  ],
  "up": [
    {"key": "up"},
    {"key": "k"},
  ],
  "left": [
    {"key": "left"},
    {"key": "h"},
  ],
  "right": [
    {"key": "right"},
    {"key": "l"},
  ],
}
```

## Default Value

The default value of the input buffer is set to `0` to help differentiate with {ref}`InputPrompt <pages/prompts/input:text>`. You could disable
this value and have an empty input buffer by setting the parameter `default=None`.

<details>
  <summary>Classic Syntax (PyInquirer)</summary>

```{code-block} python
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "default": None,
  }
]

result = prompt(questions)
```

</details>

<details open>
  <summary>Alternate Syntax</summary>

```{code-block} python
from InquirerPy import inquirer

result = inquirer.number(message="Number:", default=None).execute()
```

</details>

## Max and Min

You can set the maximum allowed value as well as the minimum allowed value for the prompt via `max_allowed` and `min_allowed`.

```{hint}
When the input value goes above/below the max/min value, the input value will automatically reset to the
configured max/min value.
```

<details>
  <summary>Classic Syntax (PyInquirer)</summary>

```{code-block} python
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "max_allowed": 10,
    "min_allowed": -100
  }
]

result = prompt(questions)
```

</details>

<details open>
  <summary>Alternate Syntax</summary>

```{code-block} python
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", max_allowed=10, min_allowed=-100,
).execute()
```

</details>

## Decimal Input

```{tip}
Once you enable decimal input, the prompt will have a second input buffer. You can keep navigating `left`/`right`
to enter the other input buffer or you can use the `tab`/`shit-tab` to focus the other buffer.
```

You can enable decimal input by setting the argument `float_allowed` to True.

<details>
  <summary>Classic Syntax (PyInquirer)</summary>

```{code-block} python
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "float_allowed": True,
  }
]

result = prompt(questions)
```

</details>

<details open>
  <summary>Alternate Syntax</summary>

```{code-block} python
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", float_allowed=True,
).execute()
```

</details>

## Replace Mode

By default, all input buffer has the exact same behavior as terminal input behavior. There is an optional replace mode
which you could enable for a better experience when working with decimal points input. You can enable it via
parameter `replace_mode=True`.

```{warning}
Replace mode introduce some slight inconsistency with the terminal input behavior that we are used to.
```

<details>
  <summary>Classic Syntax (PyInquirer)</summary>

```{code-block} python
from InquirerPy import prompt

questions = [
  {
    "type": "number",
    "message": "Number:",
    "replace_mode": True,
  }
]

result = prompt(questions)
```

</details>

<details open>
  <summary>Alternate Syntax</summary>

```{code-block} python
from InquirerPy import inquirer

result = inquirer.number(
  message="Number:", replace_mode=True,
).execute()
```

</details>

The following gif demonstrate the different behavior when we are trying to input number "123.102". The first prompt is `replace_mode=False`
and the second prompt is `replace_mode=True`.

![demo](https://assets.kazhala.me/InquirerPy/number-replace.gif)

## Reference

```{eval-rst}
.. autoclass:: InquirerPy.prompts.number.NumberPrompt
    :noindex:
```