flexablelib-components 3.3.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.
@@ -0,0 +1,234 @@
1
+ """
2
+ Dialogs and overlays: a modal with buttons, confirm and prompt dialogs, a
3
+ loading overlay, a popover, and the help line that shows the focused
4
+ component's hint (the terminal's tooltip).
5
+ """
6
+
7
+ from flexablelib import _frames as _fx
8
+
9
+ from .base import Button, _Themed, frame
10
+ from .display import Spinner
11
+ from .inputs import TextInput
12
+ from .navigation import render_into
13
+
14
+ __all__ = ['Modal', 'ConfirmDialog', 'PromptDialog', 'LoadingOverlay',
15
+ 'Popover', 'HelpLine']
16
+
17
+
18
+ class Modal(_Themed):
19
+ """
20
+ A framed dialog over the screen: ``content`` and a row of ``buttons``
21
+ (``(label, action)`` pairs or :class:`Button` s; an action returning
22
+ anything but False closes the dialog). ``open(app)`` shows it; Escape
23
+ closes it when ``closable``. ``width`` caps its width.
24
+ """
25
+
26
+ def __init__(self, content, title=None, buttons=(), width=60,
27
+ closable=True, on_close=None, **kwargs):
28
+ super(Modal, self).__init__(**kwargs)
29
+ self.content = content
30
+ self.title = title
31
+ self.width = width
32
+ self.closable = bool(closable)
33
+ self.on_close = on_close
34
+ self.buttons = []
35
+ colors = kwargs.get('use_colors')
36
+ for b in buttons:
37
+ if not isinstance(b, Button):
38
+ label, action = b[0], b[1] if len(b) > 1 else None
39
+ kind = b[2] if len(b) > 2 else 'default'
40
+ b = Button(label, None, kind=kind, theme=self.theme,
41
+ use_colors=colors)
42
+ b.on_press = self._wrap(action)
43
+ self.buttons.append(b)
44
+ self.is_open = False
45
+
46
+ def _wrap(self, action):
47
+ def run():
48
+ result = action() if action is not None else None
49
+ if result is not False:
50
+ self.close()
51
+ return run
52
+
53
+ def children(self):
54
+ kids = [] if isinstance(self.content, str) else [self.content]
55
+ return kids + self.buttons
56
+
57
+ def natural_width(self):
58
+ return self.width
59
+
60
+ def open(self, app):
61
+ app.push(self)
62
+ self.is_open = True
63
+ return self
64
+
65
+ def close(self):
66
+ if self.app is not None and self.is_open:
67
+ self.app.pop(self)
68
+ self.is_open = False
69
+ if self.on_close is not None:
70
+ self.on_close()
71
+
72
+ def handle_key(self, key):
73
+ k = str(key)
74
+ if k == 'escape' and self.closable:
75
+ self.close()
76
+ return True
77
+ if k in ('left', 'right') and self.app is not None and \
78
+ self.app.focused in self.buttons:
79
+ i = self.buttons.index(self.app.focused)
80
+ step = 1 if k == 'right' else -1
81
+ self.app.focus(self.buttons[(i + step) % len(self.buttons)])
82
+ return True
83
+ return super(Modal, self).handle_key(key)
84
+
85
+ def draw(self, width, height):
86
+ inner = max(1, width - 4)
87
+ body = render_into(self.content, inner) if not \
88
+ isinstance(self.content, str) else []
89
+ if isinstance(self.content, str):
90
+ from flexablelib.text_width import wrap_to_width
91
+ for paragraph in self.content.split('\n'):
92
+ body.extend(wrap_to_width(paragraph, inner) or [''])
93
+ lines = [''] + body + ['']
94
+ if self.buttons:
95
+ row = ' '.join(b.render(width=b.natural_width())
96
+ for b in self.buttons)
97
+ lines.append(_fx.fit_line(row, inner, 'r'))
98
+ footer = 'esc' if self.closable else None
99
+ return frame([' ' + l for l in lines], width, self.title,
100
+ self.theme.accent, self.paint,
101
+ title_color='bold ' + self.theme.accent, footer=footer)
102
+
103
+
104
+ class ConfirmDialog(Modal):
105
+ """Yes / no: ``on_confirm()`` or ``on_cancel()``; ``danger=True``
106
+ colours the confirm button as destructive."""
107
+
108
+ def __init__(self, message, on_confirm=None, on_cancel=None,
109
+ title='Confirmar', confirm_label='Aceptar',
110
+ cancel_label='Cancelar', danger=False, **kwargs):
111
+ self.result = None
112
+
113
+ def yes():
114
+ self.result = True
115
+ if on_confirm is not None:
116
+ on_confirm()
117
+
118
+ def no():
119
+ self.result = False
120
+ if on_cancel is not None:
121
+ on_cancel()
122
+ super(ConfirmDialog, self).__init__(
123
+ message, title=title, width=kwargs.pop('width', 50),
124
+ buttons=[(cancel_label, no),
125
+ (confirm_label, yes, 'danger' if danger else 'primary')],
126
+ **kwargs)
127
+
128
+
129
+ class PromptDialog(Modal):
130
+ """Asks for one value: ``on_submit(value)`` on Enter or Aceptar."""
131
+
132
+ def __init__(self, label, on_submit=None, value='', title=None,
133
+ validators=None, **kwargs):
134
+ colors = kwargs.get('use_colors')
135
+ self.input = TextInput(label, value=value, validators=validators,
136
+ theme=kwargs.get('theme'), use_colors=colors)
137
+ self.value = None
138
+ self._callback = on_submit
139
+
140
+ def accept():
141
+ if self.input.validate():
142
+ return False
143
+ self.value = self.input.value
144
+ if self._callback is not None:
145
+ self._callback(self.value)
146
+ self.input.on_submit = lambda v: self._accept_and_close(accept)
147
+ super(PromptDialog, self).__init__(
148
+ self.input, title=title, width=kwargs.pop('width', 50),
149
+ buttons=[('Cancelar', None), ('Aceptar', accept, 'primary')],
150
+ **kwargs)
151
+
152
+ def _accept_and_close(self, accept):
153
+ if accept() is not False:
154
+ self.close()
155
+
156
+
157
+ class LoadingOverlay(Modal):
158
+ """
159
+ A spinner and message over the screen that holds the focus while work
160
+ runs: ``show(app)`` / ``hide()``; ``progress`` (0-1) adds a bar.
161
+ """
162
+
163
+ def __init__(self, message='Cargando…', progress=None, **kwargs):
164
+ self.spinner = Spinner(message, theme=kwargs.get('theme'),
165
+ use_colors=kwargs.get('use_colors'))
166
+ self.progress = progress
167
+ super(LoadingOverlay, self).__init__(self.spinner, closable=False,
168
+ width=kwargs.pop('width', 40),
169
+ **kwargs)
170
+
171
+ def children(self):
172
+ return []
173
+
174
+ def show(self, app):
175
+ return self.open(app)
176
+
177
+ def hide(self):
178
+ self.close()
179
+
180
+ def handle_key(self, key):
181
+ return True
182
+
183
+ def draw(self, width, height):
184
+ lines = self.spinner.render(width=max(1, width - 4)).split('\n')
185
+ if self.progress is not None:
186
+ inner = max(1, width - 10)
187
+ fraction = max(0.0, min(1.0, self.progress))
188
+ done = int(round(inner * fraction))
189
+ lines.append(self.paint('█' * done, self.theme.accent) +
190
+ self.paint('░' * (inner - done), self.theme.muted) +
191
+ ' {0:>3.0f}%'.format(fraction * 100))
192
+ return frame([' ' + l for l in [''] + lines + ['']], width, None,
193
+ self.theme.accent, self.paint)
194
+
195
+
196
+ class Popover(Modal):
197
+ """Floating content at a position (``'top-right'``, ``(x, y)``...);
198
+ ``modal=False`` leaves the focus where it was."""
199
+
200
+ def __init__(self, content, title=None, width=40, **kwargs):
201
+ super(Popover, self).__init__(content, title=title, width=width,
202
+ **kwargs)
203
+
204
+ def open(self, app, position='center', modal=True):
205
+ app.push(self, position=position, modal=modal)
206
+ self.is_open = True
207
+ return self
208
+
209
+
210
+ class HelpLine(_Themed):
211
+ """
212
+ One line with the hint of whatever has the focus: its ``help``,
213
+ ``tooltip`` or ``hint`` attribute. Put it above the footer; it is the
214
+ terminal version of a tooltip.
215
+ """
216
+
217
+ def __init__(self, default='', **kwargs):
218
+ super(HelpLine, self).__init__(**kwargs)
219
+ self.default = default
220
+
221
+ def natural_width(self):
222
+ return 40
223
+
224
+ def draw(self, width, height):
225
+ node = self.app.focused if self.app is not None else None
226
+ text = ''
227
+ while node is not None and not text:
228
+ text = getattr(node, 'tooltip', None) or \
229
+ getattr(node, 'hint', None) or getattr(node, 'help', None) \
230
+ or ''
231
+ node = getattr(node, 'parent', None)
232
+ text = text or self.default
233
+ return [self.paint(_fx.truncate('ⓘ ' + text if text else '', width,
234
+ '…'), self.theme.muted)]
File without changes
@@ -0,0 +1,44 @@
1
+ Metadata-Version: 2.4
2
+ Name: flexablelib-components
3
+ Version: 3.3.0
4
+ Summary: Interactive terminal components for flexablelib: inputs, dates, tables, forms, menus, tabs, dialogs and more.
5
+ Home-page: https://github.com/Opus-Perpetuus/flexablelib
6
+ Author: Benjamin Ramirez
7
+ Author-email: chilerito12@gmail.com
8
+ License: MIT
9
+ Classifier: Development Status :: 5 - Production/Stable
10
+ Classifier: Environment :: Console
11
+ Classifier: Environment :: Console :: Curses
12
+ Classifier: License :: OSI Approved :: MIT License
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Programming Language :: Python :: 3
15
+ Classifier: Topic :: Terminals
16
+ Classifier: Topic :: Software Development :: User Interfaces
17
+ Requires-Python: >=3.8
18
+ Description-Content-Type: text/markdown
19
+ License-File: LICENSE
20
+ Requires-Dist: flexablelib>=3.3.0
21
+ Requires-Dist: flexablelib-tui>=3.3.0
22
+ Dynamic: author
23
+ Dynamic: author-email
24
+ Dynamic: classifier
25
+ Dynamic: description
26
+ Dynamic: description-content-type
27
+ Dynamic: home-page
28
+ Dynamic: license
29
+ Dynamic: license-file
30
+ Dynamic: requires-dist
31
+ Dynamic: requires-python
32
+ Dynamic: summary
33
+
34
+ # flexablelib-components
35
+
36
+ Interactive terminal components for [flexablelib](https://github.com/Opus-Perpetuus/flexablelib): form inputs, dates and times, interactive and editable tables, filters, forms, menus, tabs, wizard, tree, command palette, dialogs, charts and more, all keyboard driven.
37
+
38
+ Install it with `pip install flexablelib[components]`.
39
+
40
+ Documentation: [COMPONENTS.md](https://github.com/Opus-Perpetuus/flexablelib/blob/main/docs/COMPONENTS.md)
41
+
42
+ ![Components](https://raw.githubusercontent.com/Opus-Perpetuus/flexablelib/main/docs/img/v33/08_entradas.png)
43
+
44
+ ![An application built with the components](https://raw.githubusercontent.com/Opus-Perpetuus/flexablelib/main/docs/img/v33/01_demo_140.png)
@@ -0,0 +1,14 @@
1
+ flexablelib_components/__init__.py,sha256=veePgfw_yKCjkgCBNamqkeAtA6gAkTWuHbMtagX3NHU,3645
2
+ flexablelib_components/base.py,sha256=hA3qOWmYqbwPukOOOjAO3NxleUwxIrlRA4S3Xq8QWzc,10976
3
+ flexablelib_components/data.py,sha256=xaduhE3XQ-abgFSZTnfZiOBGcDcrqULydDbpCGR8YxY,35671
4
+ flexablelib_components/dates.py,sha256=8QsZsz-5f1Blq3wyfBh3Kv6CjqR3Jdc9ST8gqG1zyOM,29222
5
+ flexablelib_components/display.py,sha256=V10fE4ENwkB37elNrx7i0k_Hjy2SMyppiiqF7WLLtQY,41564
6
+ flexablelib_components/inputs.py,sha256=8MOwfjI1eAPZTqn4U1o0_BsKs1oS5TIrvM6WSYwU6LU,56286
7
+ flexablelib_components/navigation.py,sha256=RpugOoJP76H_wnysHVoLstagtD6VeleTYImQMT2UIT8,46281
8
+ flexablelib_components/overlays.py,sha256=046xWcC0zlusFKO3KKqxWkkGZ1az0PVJYZ8KdcQ_lOs,8502
9
+ flexablelib_components/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ flexablelib_components-3.3.0.dist-info/licenses/LICENSE,sha256=TsktDCE3E8N8WasPUHkR5Azor67EyerUJdk54L4wavM,1077
11
+ flexablelib_components-3.3.0.dist-info/METADATA,sha256=4ED3XUp0awovCasA4kdPQciQl_T2k1lZSIyxztwWCNQ,1794
12
+ flexablelib_components-3.3.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
13
+ flexablelib_components-3.3.0.dist-info/top_level.txt,sha256=f_rlaJQr0X1xNczBPYbFkM-lagHyMSTl-e9CFTm1c7I,23
14
+ flexablelib_components-3.3.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021-2022 Benjamin Ramirez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1 @@
1
+ flexablelib_components