omlish 0.0.0.dev96__py3-none-any.whl → 0.0.0.dev97__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
omlish/__about__.py CHANGED
@@ -1,5 +1,5 @@
1
- __version__ = '0.0.0.dev96'
2
- __revision__ = '0cb14143907ab35daec49550a83b060cb60de483'
1
+ __version__ = '0.0.0.dev97'
2
+ __revision__ = '4adbb70454ff03be6ca98d986f768deff72e6e7f'
3
3
 
4
4
 
5
5
  #
omlish/term.py CHANGED
@@ -1,10 +1,18 @@
1
1
  import enum
2
2
  import re
3
+ import sys
4
+ import time
3
5
  import typing as ta
4
6
 
5
7
  from . import lang
6
8
 
7
9
 
10
+ T = ta.TypeVar('T')
11
+
12
+
13
+ ##
14
+
15
+
8
16
  ESC = '\x1b'
9
17
  BEL = '\x07'
10
18
 
@@ -207,6 +215,136 @@ BG24_RGB = ControlSequence(
207
215
  lambda r, g, b: CSI + '48;2;' + str(r) + ';' + str(g) + ';' + str(b) + 'm',
208
216
  '24-Bit Background Color (RGB)')
209
217
 
218
+ ##
219
+
220
+
221
+ class ProgressBar:
222
+ """
223
+ TODO:
224
+ - ProgressBarRenderer
225
+ - right-justify
226
+ - ProgressBarGroup
227
+ - animate
228
+ """
229
+
230
+ def __init__(
231
+ self,
232
+ total: int | None = None,
233
+ *,
234
+ length: int = 40,
235
+ interval: float = .2,
236
+ start_time: float | None = None,
237
+ out: ta.TextIO | None = None,
238
+ ) -> None:
239
+ super().__init__()
240
+
241
+ self._total = total
242
+ self._length = length
243
+ self._interval = interval
244
+ if start_time is None:
245
+ start_time = time.time()
246
+ self._start_time = start_time
247
+ if out is None:
248
+ out = sys.stdout
249
+ self._out = out
250
+
251
+ self._i = 0
252
+ self._elapsed = 0.
253
+ self._last_print = 0.
254
+
255
+ def render(
256
+ self,
257
+ *,
258
+ complete: bool = False,
259
+ ) -> str:
260
+ iter_per_sec = self._i / self._elapsed if self._elapsed > 0 else 0
261
+
262
+ if self._total is not None:
263
+ remaining = (self._total - self._i) / iter_per_sec if iter_per_sec > 0 else 0
264
+ done = int(self._length * self._i / self._total)
265
+
266
+ bar = f'[{"█" * done}{"." * (self._length - done)}]'
267
+ info_parts = [
268
+ f'{self._i}/{self._total}',
269
+ f'{iter_per_sec:.2f} it/s',
270
+ f'{self._elapsed:.2f}s elapsed',
271
+ f'{remaining:.2f}s left',
272
+ ]
273
+
274
+ else:
275
+ bar = f'[{("█" if complete else "?") * self._length}]'
276
+ info_parts = [
277
+ f'{self._i}',
278
+ f'{iter_per_sec:.2f} it/s',
279
+ f'{self._elapsed:.2f}s elapsed',
280
+ ]
281
+
282
+ info = ' | '.join(info_parts)
283
+ return f'{bar} {info}'
284
+
285
+ def print(
286
+ self,
287
+ *,
288
+ now: float | None = None,
289
+ **kwargs: ta.Any,
290
+ ) -> None:
291
+ if now is None:
292
+ now = time.time()
293
+
294
+ line = self.render(**kwargs)
295
+ self._out.write(f'\033[2K\033[G{line}')
296
+ self._out.flush()
297
+
298
+ self._last_print = now
299
+
300
+ def update(
301
+ self,
302
+ n: int = 1,
303
+ *,
304
+ now: float | None = None,
305
+ silent: bool = False,
306
+ ) -> None:
307
+ if now is None:
308
+ now = time.time()
309
+
310
+ self._i += n
311
+ self._elapsed = now - self._start_time
312
+
313
+ if not silent:
314
+ if now - self._last_print >= self._interval:
315
+ self.print(now=now)
316
+
317
+
318
+ def progress_bar(
319
+ seq: ta.Iterable[T],
320
+ *,
321
+ no_tty_check: bool = False,
322
+ total: int | None = None,
323
+ **kwargs: ta.Any,
324
+ ) -> ta.Generator[T, None, None]:
325
+ if not no_tty_check and not sys.stdout.isatty():
326
+ yield from seq
327
+ return
328
+
329
+ if total is None:
330
+ if isinstance(seq, ta.Sized):
331
+ total = len(seq)
332
+
333
+ pb = ProgressBar(
334
+ total=total,
335
+ **kwargs,
336
+ )
337
+
338
+ for item in seq:
339
+ pb.update()
340
+ yield item
341
+
342
+ pb.print(complete=True)
343
+ sys.stdout.write('\n')
344
+
345
+
346
+ ##
347
+
210
348
 
211
349
  def main() -> None:
212
350
  import sys
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: omlish
3
- Version: 0.0.0.dev96
3
+ Version: 0.0.0.dev97
4
4
  Summary: omlish
5
5
  Author: wrmsr
6
6
  License: BSD-3-Clause
@@ -1,5 +1,5 @@
1
1
  omlish/.manifests.json,sha256=hTFp9tvE72BxKloIq1s1SS0LRQlIsvMtO69Sbc47rKg,1704
2
- omlish/__about__.py,sha256=xYYDai8LigmF6omaHMQLajgl5laeQZWUoKdsMxJXys4,3345
2
+ omlish/__about__.py,sha256=eIwAtZZmvmulh4BBWUEeTYaDxG6LjN2S_3Q71pIDNfI,3345
3
3
  omlish/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  omlish/argparse.py,sha256=Dc73G8lyoQBLvXhMYUbzQUh4SJu_OTvKUXjSUxq_ang,7499
5
5
  omlish/c3.py,sha256=4vogWgwPb8TbNS2KkZxpoWbwjj7MuHG2lQG-hdtkvjI,8062
@@ -18,7 +18,7 @@ omlish/multiprocessing.py,sha256=QZT4C7I-uThCAjaEY3xgUYb-5GagUlnE4etN01LDyU4,518
18
18
  omlish/os.py,sha256=5nJ-a9JKSMoaZVZ1eOa5BAbfL7o7CF7ue_PyJwufnwY,1460
19
19
  omlish/runmodule.py,sha256=PWvuAaJ9wQQn6bx9ftEL3_d04DyotNn8dR_twm2pgw0,700
20
20
  omlish/sync.py,sha256=AqwIfIuCMVHLwlJUa7dmaSjfA4sM5AYPCD5-nsz3XVQ,1516
21
- omlish/term.py,sha256=NEmxqAhicyInGtmFamZAizI2xdu819MzFYPEp0Fx97M,6111
21
+ omlish/term.py,sha256=pgzEZ8FigEvaaRK38N4HhPSI-M9GOYIXy3XLu_CSROc,9197
22
22
  omlish/antlr/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
23
23
  omlish/antlr/_runtime/BufferedTokenStream.py,sha256=1Rnhm62MZCWSuQeRs7lRUbdtdyo7Gyg8r4gAETjv-cE,10793
24
24
  omlish/antlr/_runtime/CommonTokenFactory.py,sha256=QrSSTH0gYhOpPeOHqrs6-2g1PGcgYvjhR6J6pynKLOc,2147
@@ -457,9 +457,9 @@ omlish/text/delimit.py,sha256=ubPXcXQmtbOVrUsNh5gH1mDq5H-n1y2R4cPL5_DQf68,4928
457
457
  omlish/text/glyphsplit.py,sha256=Ug-dPRO7x-OrNNr8g1y6DotSZ2KH0S-VcOmUobwa4B0,3296
458
458
  omlish/text/indent.py,sha256=6Jj6TFY9unaPa4xPzrnZemJ-fHsV53IamP93XGjSUHs,1274
459
459
  omlish/text/parts.py,sha256=7vPF1aTZdvLVYJ4EwBZVzRSy8XB3YqPd7JwEnNGGAOo,6495
460
- omlish-0.0.0.dev96.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
461
- omlish-0.0.0.dev96.dist-info/METADATA,sha256=iXdvwNxhVdlcnIH8lTiXDRPrhbVOy6uHDmpchaAJPsE,3987
462
- omlish-0.0.0.dev96.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
463
- omlish-0.0.0.dev96.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
464
- omlish-0.0.0.dev96.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
465
- omlish-0.0.0.dev96.dist-info/RECORD,,
460
+ omlish-0.0.0.dev97.dist-info/LICENSE,sha256=B_hVtavaA8zCYDW99DYdcpDLKz1n3BBRjZrcbv8uG8c,1451
461
+ omlish-0.0.0.dev97.dist-info/METADATA,sha256=wmMErtqPFPYd69wceqTe57xiockAvRmzLhdroKiURz4,3987
462
+ omlish-0.0.0.dev97.dist-info/WHEEL,sha256=P9jw-gEje8ByB7_hXoICnHtVCrEwMQh-630tKvQWehc,91
463
+ omlish-0.0.0.dev97.dist-info/entry_points.txt,sha256=Lt84WvRZJskWCAS7xnQGZIeVWksprtUHj0llrvVmod8,35
464
+ omlish-0.0.0.dev97.dist-info/top_level.txt,sha256=pePsKdLu7DvtUiecdYXJ78iO80uDNmBlqe-8hOzOmfs,7
465
+ omlish-0.0.0.dev97.dist-info/RECORD,,