multiCMD 1.68__tar.gz → 1.70__tar.gz

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.
Files changed (43) hide show
  1. {multicmd-1.68 → multicmd-1.70}/PKG-INFO +33 -1
  2. {multicmd-1.68 → multicmd-1.70}/README.md +32 -0
  3. {multicmd-1.68 → multicmd-1.70}/multiCMD.egg-info/PKG-INFO +33 -1
  4. {multicmd-1.68 → multicmd-1.70}/multiCMD.py +111 -41
  5. {multicmd-1.68 → multicmd-1.70}/tests/test_interrupts.py +22 -4
  6. {multicmd-1.68 → multicmd-1.70}/tests/test_terminal_width.py +261 -8
  7. {multicmd-1.68 → multicmd-1.70}/LICENSE +0 -0
  8. {multicmd-1.68 → multicmd-1.70}/multiCMD.egg-info/SOURCES.txt +0 -0
  9. {multicmd-1.68 → multicmd-1.70}/multiCMD.egg-info/dependency_links.txt +0 -0
  10. {multicmd-1.68 → multicmd-1.70}/multiCMD.egg-info/entry_points.txt +0 -0
  11. {multicmd-1.68 → multicmd-1.70}/multiCMD.egg-info/top_level.txt +0 -0
  12. {multicmd-1.68 → multicmd-1.70}/scripts/multiCMD +0 -0
  13. {multicmd-1.68 → multicmd-1.70}/setup.cfg +0 -0
  14. {multicmd-1.68 → multicmd-1.70}/setup.py +0 -0
  15. {multicmd-1.68 → multicmd-1.70}/tests/test_api.py +0 -0
  16. {multicmd-1.68 → multicmd-1.70}/tests/test_async_executor.py +0 -0
  17. {multicmd-1.68 → multicmd-1.70}/tests/test_cli.py +0 -0
  18. {multicmd-1.68 → multicmd-1.70}/tests/test_cli_closed_stdout.py +0 -0
  19. {multicmd-1.68 → multicmd-1.70}/tests/test_cli_failed_stdout.py +0 -0
  20. {multicmd-1.68 → multicmd-1.70}/tests/test_cli_input.py +0 -0
  21. {multicmd-1.68 → multicmd-1.70}/tests/test_cli_summary.py +0 -0
  22. {multicmd-1.68 → multicmd-1.70}/tests/test_display.py +0 -0
  23. {multicmd-1.68 → multicmd-1.70}/tests/test_embed.py +0 -0
  24. {multicmd-1.68 → multicmd-1.70}/tests/test_env.py +0 -0
  25. {multicmd-1.68 → multicmd-1.70}/tests/test_expand_ranges.py +0 -0
  26. {multicmd-1.68 → multicmd-1.70}/tests/test_handover.py +0 -0
  27. {multicmd-1.68 → multicmd-1.70}/tests/test_helpers.py +0 -0
  28. {multicmd-1.68 → multicmd-1.70}/tests/test_packaging.py +0 -0
  29. {multicmd-1.68 → multicmd-1.70}/tests/test_parsing.py +0 -0
  30. {multicmd-1.68 → multicmd-1.70}/tests/test_pass_fds.py +0 -0
  31. {multicmd-1.68 → multicmd-1.70}/tests/test_ping.py +0 -0
  32. {multicmd-1.68 → multicmd-1.70}/tests/test_pool.py +0 -0
  33. {multicmd-1.68 → multicmd-1.70}/tests/test_process_groups.py +0 -0
  34. {multicmd-1.68 → multicmd-1.70}/tests/test_pty.py +0 -0
  35. {multicmd-1.68 → multicmd-1.70}/tests/test_reader.py +0 -0
  36. {multicmd-1.68 → multicmd-1.70}/tests/test_reader_quit.py +0 -0
  37. {multicmd-1.68 → multicmd-1.70}/tests/test_regressions.py +0 -0
  38. {multicmd-1.68 → multicmd-1.70}/tests/test_run_command.py +0 -0
  39. {multicmd-1.68 → multicmd-1.70}/tests/test_run_commands.py +0 -0
  40. {multicmd-1.68 → multicmd-1.70}/tests/test_sudo.py +0 -0
  41. {multicmd-1.68 → multicmd-1.70}/tests/test_supervision.py +0 -0
  42. {multicmd-1.68 → multicmd-1.70}/tests/test_task.py +0 -0
  43. {multicmd-1.68 → multicmd-1.70}/tests/test_windows.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.68
3
+ Version: 1.70
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -539,6 +539,8 @@ The module also bundles a few terminal/formatting helpers used internally and
539
539
  reusable on their own: `pretty_format_table`, `parseTable`, `print_progress_bar`,
540
540
  `format_bytes`, `get_terminal_size`, `input_with_timeout_and_countdown` (its prompt and countdown go to
541
541
  stderr), and `slugify`.
542
+ A progress bar counts its blocks as one column each, so a terminal set to draw East Asian ambiguous-width
543
+ characters wide wraps it ([docs/limitations.md](docs/limitations.md)).
542
544
 
543
545
  ## Platform support
544
546
 
@@ -558,6 +560,36 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
558
560
 
559
561
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
560
562
 
563
+ ## Upgrading to 1.70 from 1.69
564
+
565
+ - Nothing needs doing. `mcmd` is unchanged.
566
+ - Changed, library: beside a `print_progress_bar()` bar of 10 cells, the prefix gives way from its end down to its first `|`
567
+ followed by a blank before the percent goes, and what followed that `|` stays gone once the percent has gone (1.69
568
+ dropped the percent first). At 132 columns hpcp's copy bar shows `16 Scheduled` and its percent where 1.69 showed
569
+ `16 Scheduled 4.0 F/Job` and no percent (with typical strings). A prefix with no such `|` before its end gives way in
570
+ 1.69's order, with the room of the widest percent (the next point).
571
+ - Changed, library: what a bar keeps of its prefix and suffix, and whether it has a percent, hold as its percent grows, up
572
+ to 999.9% (a wider one decides for itself; the bar's own length follows each frame's percent). The layout is decided with
573
+ the widest percent (`|100.0% `, 8 cells; the frame's own is 6 at 0.1% to 9.9% and 7 to 99.9%), so the frame left on the
574
+ screen at the total keeps what the frames before it showed: 1.69 showed hpcp's copy bar's percent at 99.9% and none at
575
+ the total at 135 columns. The percent starts at 110 columns (the rate-limited line: 118).
576
+
577
+ ## Upgrading to 1.69 from 1.68
578
+
579
+ - Nothing needs doing. `mcmd` is unchanged.
580
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
581
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
582
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start (a first field
583
+ wider than the room goes before it), a whole field at a time, so a number keeps its unit and its label
584
+ (1.68 shrank the bar to two cells, then dropped the percent and the prefix whole). At 80 columns hpcp's copy bar shows its
585
+ count and its time left (1.68: no count).
586
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
587
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
588
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
589
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
590
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
591
+ suffix coloured with escape codes shows them as text (`?[32m`).
592
+
561
593
  ## Upgrading to 1.68 from 1.67
562
594
 
563
595
  - Nothing needs doing.
@@ -514,6 +514,8 @@ The module also bundles a few terminal/formatting helpers used internally and
514
514
  reusable on their own: `pretty_format_table`, `parseTable`, `print_progress_bar`,
515
515
  `format_bytes`, `get_terminal_size`, `input_with_timeout_and_countdown` (its prompt and countdown go to
516
516
  stderr), and `slugify`.
517
+ A progress bar counts its blocks as one column each, so a terminal set to draw East Asian ambiguous-width
518
+ characters wide wraps it ([docs/limitations.md](docs/limitations.md)).
517
519
 
518
520
  ## Platform support
519
521
 
@@ -533,6 +535,36 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
533
535
 
534
536
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
535
537
 
538
+ ## Upgrading to 1.70 from 1.69
539
+
540
+ - Nothing needs doing. `mcmd` is unchanged.
541
+ - Changed, library: beside a `print_progress_bar()` bar of 10 cells, the prefix gives way from its end down to its first `|`
542
+ followed by a blank before the percent goes, and what followed that `|` stays gone once the percent has gone (1.69
543
+ dropped the percent first). At 132 columns hpcp's copy bar shows `16 Scheduled` and its percent where 1.69 showed
544
+ `16 Scheduled 4.0 F/Job` and no percent (with typical strings). A prefix with no such `|` before its end gives way in
545
+ 1.69's order, with the room of the widest percent (the next point).
546
+ - Changed, library: what a bar keeps of its prefix and suffix, and whether it has a percent, hold as its percent grows, up
547
+ to 999.9% (a wider one decides for itself; the bar's own length follows each frame's percent). The layout is decided with
548
+ the widest percent (`|100.0% `, 8 cells; the frame's own is 6 at 0.1% to 9.9% and 7 to 99.9%), so the frame left on the
549
+ screen at the total keeps what the frames before it showed: 1.69 showed hpcp's copy bar's percent at 99.9% and none at
550
+ the total at 135 columns. The percent starts at 110 columns (the rate-limited line: 118).
551
+
552
+ ## Upgrading to 1.69 from 1.68
553
+
554
+ - Nothing needs doing. `mcmd` is unchanged.
555
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
556
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
557
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start (a first field
558
+ wider than the room goes before it), a whole field at a time, so a number keeps its unit and its label
559
+ (1.68 shrank the bar to two cells, then dropped the percent and the prefix whole). At 80 columns hpcp's copy bar shows its
560
+ count and its time left (1.68: no count).
561
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
562
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
563
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
564
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
565
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
566
+ suffix coloured with escape codes shows them as text (`?[32m`).
567
+
536
568
  ## Upgrading to 1.68 from 1.67
537
569
 
538
570
  - Nothing needs doing.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: multiCMD
3
- Version: 1.68
3
+ Version: 1.70
4
4
  Summary: Run commands simultaneously
5
5
  Home-page: https://github.com/yufei-pan/multiCMD
6
6
  Author: Yufei Pan
@@ -539,6 +539,8 @@ The module also bundles a few terminal/formatting helpers used internally and
539
539
  reusable on their own: `pretty_format_table`, `parseTable`, `print_progress_bar`,
540
540
  `format_bytes`, `get_terminal_size`, `input_with_timeout_and_countdown` (its prompt and countdown go to
541
541
  stderr), and `slugify`.
542
+ A progress bar counts its blocks as one column each, so a terminal set to draw East Asian ambiguous-width
543
+ characters wide wraps it ([docs/limitations.md](docs/limitations.md)).
542
544
 
543
545
  ## Platform support
544
546
 
@@ -558,6 +560,36 @@ pytest -m "not slow" # skip intentional sleep-heavy cases
558
560
 
559
561
  `test_expand_ranges_perf.py` is a manual performance microbenchmark and is not collected by pytest.
560
562
 
563
+ ## Upgrading to 1.70 from 1.69
564
+
565
+ - Nothing needs doing. `mcmd` is unchanged.
566
+ - Changed, library: beside a `print_progress_bar()` bar of 10 cells, the prefix gives way from its end down to its first `|`
567
+ followed by a blank before the percent goes, and what followed that `|` stays gone once the percent has gone (1.69
568
+ dropped the percent first). At 132 columns hpcp's copy bar shows `16 Scheduled` and its percent where 1.69 showed
569
+ `16 Scheduled 4.0 F/Job` and no percent (with typical strings). A prefix with no such `|` before its end gives way in
570
+ 1.69's order, with the room of the widest percent (the next point).
571
+ - Changed, library: what a bar keeps of its prefix and suffix, and whether it has a percent, hold as its percent grows, up
572
+ to 999.9% (a wider one decides for itself; the bar's own length follows each frame's percent). The layout is decided with
573
+ the widest percent (`|100.0% `, 8 cells; the frame's own is 6 at 0.1% to 9.9% and 7 to 99.9%), so the frame left on the
574
+ screen at the total keeps what the frames before it showed: 1.69 showed hpcp's copy bar's percent at 99.9% and none at
575
+ the total at 135 columns. The percent starts at 110 columns (the rate-limited line: 118).
576
+
577
+ ## Upgrading to 1.69 from 1.68
578
+
579
+ - Nothing needs doing. `mcmd` is unchanged.
580
+ - Changed, library: a `print_progress_bar()` frame with a total keeps a bar of at least 10 cells (from 12 columns up; the first
581
+ frame holds ` Calculating... ` or blanks in its place). Beside it the percent
582
+ gives way first, then the prefix from its end down to its first field, then the suffix from its start (a first field
583
+ wider than the room goes before it), a whole field at a time, so a number keeps its unit and its label
584
+ (1.68 shrank the bar to two cells, then dropped the percent and the prefix whole). At 80 columns hpcp's copy bar shows its
585
+ count and its time left (1.68: no count).
586
+ - Fix, library: the first frame at or past the total ends its line, so the program's next output does not join a count
587
+ that stepped over its total (1.68 ended the line only at a frame equal to the total). A count that goes on past its
588
+ total draws its later frames on the next line, and the last stays open: the next output joins it.
589
+ - Fix, library: a control character in a frame's prefix or suffix (a tab, a newline, an escape: a file name can hold one)
590
+ is drawn as `?` (1.68: a newline started a row, so each frame scrolled, and a tab could wrap the frame). A prefix or
591
+ suffix coloured with escape codes shows them as text (`?[32m`).
592
+
561
593
  ## Upgrading to 1.68 from 1.67
562
594
 
563
595
  - Nothing needs doing.
@@ -29,9 +29,9 @@ import traceback
29
29
  from pprint import pformat
30
30
 
31
31
  #%% global vars
32
- version = '1.68'
32
+ version = '1.70'
33
33
  __version__ = version
34
- COMMIT_DATE = '2026-11-16'
34
+ COMMIT_DATE = '2026-11-23'
35
35
  # fire-and-forget threads (run_commands with wait_for_return=False) that join_threads() waits on. multiCMD takes no
36
36
  # lock for it: each update is one set operation and each snapshot one set.copy(), as for _live_procs below
37
37
  __running_threads = set()
@@ -3589,11 +3589,17 @@ def _cells(text):
3589
3589
  return len(text)
3590
3590
  return sum(map(_char_width, text))
3591
3591
 
3592
- def _fit(text, width, from_start=False):
3592
+ def _cuts_at(text, i):
3593
3593
  '''
3594
- text cut to width terminal columns ( internal ): its end, back to the last blank that fits and follows neither a digit
3595
- nor a colon, so a number keeps its label and its unit ( format_bytes writes '1.23 K' ); from_start, its start, at the
3596
- column
3594
+ Whether text can be cut at its index i ( internal ): at a blank that follows neither a digit nor a colon. A blank after a digit
3595
+ may sit between a number and its unit ( format_bytes writes '1.23 K' ), one after a colon between a label and its number
3596
+ '''
3597
+ return text[i] == ' ' and not (i and (text[i - 1].isdigit() or text[i - 1] == ':'))
3598
+
3599
+ def _fit(text, width, from_start=False, whole=False):
3600
+ '''
3601
+ text cut to width terminal columns ( internal ): its end, back to the last blank it can be cut at ( _cuts_at ), so a number
3602
+ keeps its label and its unit, else at the column ( with whole, not at all: '' ); from_start, its start, at the column
3597
3603
  '''
3598
3604
  used = 0
3599
3605
  for i in (range(len(text) - 1, -1, -1) if from_start else range(len(text))):
@@ -3601,14 +3607,70 @@ def _fit(text, width, from_start=False):
3601
3607
  if used > width:
3602
3608
  if from_start:
3603
3609
  return text[i + 1:]
3604
- # a blank after a digit may sit between a number and its unit ( '1.23 K' ), one after a colon between a label and
3605
- # its number: neither is a place to cut
3606
3610
  blank = i
3607
- while blank > 0 and (text[blank] != ' ' or text[blank - 1].isdigit() or text[blank - 1] == ':'):
3611
+ while blank > 0 and not _cuts_at(text, blank):
3608
3612
  blank -= 1
3609
- return text[:blank] if blank > 0 else text[:i]
3613
+ return text[:blank] if blank > 0 else '' if whole else text[:i]
3610
3614
  return text
3611
3615
 
3616
+ # The least bar of a frame with a total, in cells, whatever the total, so it reads at a glance. Its text gives way before the bar
3617
+ # shrinks below it ( 1.68 shrank the bar first, down to two cells, then dropped the percent and the prefix whole: hpcp's copy
3618
+ # bar at 80 columns showed no count )
3619
+ _LEAST_BAR = 10
3620
+
3621
+ # the control characters ( C0, DEL, C1 ), each a '?' in a frame's text: a newline starts a row, a tab jumps to a tab stop, an
3622
+ # escape starts a sequence the terminal acts on, so the frame cannot be measured ( a file name may hold any of them )
3623
+ _CONTROLS = dict.fromkeys(list(range(0x20)) + list(range(0x7f, 0xa0)), '?')
3624
+
3625
+ def _cut_prefix(prefix, width):
3626
+ '''
3627
+ A frame's prefix in width terminal columns ( internal ): whole, or its start, cut after a whole field ( _fit ) and joined
3628
+ to the bar by ' |' again; '' where not even its first field fits
3629
+ '''
3630
+ if _cells(prefix) <= width:
3631
+ return prefix
3632
+ kept = _fit(prefix, width - 2, whole=True).rstrip(' |')
3633
+ return kept + ' |' if kept else ''
3634
+
3635
+ def _first_field(prefix):
3636
+ '''A frame's prefix cut after its first field ( internal; see _cut_prefix ), or '' if it holds only the joint to the bar'''
3637
+ for i in range(1, len(prefix) + 1):
3638
+ if i == len(prefix) or _cuts_at(prefix, i):
3639
+ kept = prefix[:i].rstrip(' |')
3640
+ if kept:
3641
+ return kept + ' |'
3642
+ return ''
3643
+
3644
+ def _first_part(prefix):
3645
+ '''
3646
+ A frame's prefix cut after its first '|' that ends a field ( internal; see _cut_prefix ), else whole: what follows it gives
3647
+ way before the percent ( hpcp's jobs, after its count, size and word )
3648
+ '''
3649
+ for i in range(1, len(prefix)):
3650
+ if prefix[i] == ' ' and prefix[i - 1] == '|':
3651
+ kept = prefix[:i].rstrip(' |')
3652
+ if kept:
3653
+ return kept + ' |'
3654
+ return prefix
3655
+
3656
+ def _cut_suffix(suffix, width):
3657
+ '''
3658
+ A frame's suffix in width terminal columns ( internal ): whole, or its end, joined to the bar by '| ' again, from a blank it
3659
+ can be cut at ( _cuts_at ) before a number, a label ( a word that ends in a colon ) or a '|': a word after a number's unit
3660
+ ( '1.23 M Files', '5.00 KF/s Limit' ) never starts it without its number. '' where not even its last such part fits
3661
+ '''
3662
+ if _cells(suffix) <= width:
3663
+ return suffix
3664
+ # the first character of the end that fits beside the '| '
3665
+ start = len(suffix) - len(_fit(suffix, width - 2, from_start=True))
3666
+ for i in range(max(start - 1, 0), len(suffix)):
3667
+ if _cuts_at(suffix, i):
3668
+ word = suffix[i + 1:].lstrip(' ').split(' ', 1)[0]
3669
+ if word[:1].isdigit() or word[:1] == '|' or word.endswith(':'):
3670
+ kept = suffix[i + 1:].lstrip('| ')
3671
+ return '| ' + kept if kept else ''
3672
+ return ''
3673
+
3612
3674
  def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3613
3675
  '''
3614
3676
  Generate a progress bar string: one line, within one column less than columns ( a line that reaches the last column
@@ -3624,10 +3686,8 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3624
3686
  @returns:
3625
3687
  str: the progress bar string
3626
3688
  '''
3627
- noPrefix = False
3628
- noSuffix = False
3629
- noPercent = False
3630
- noBar = False
3689
+ # a control character would break the frame over rows or columns it cannot count: it is drawn as a '?'
3690
+ prefix, suffix = prefix.translate(_CONTROLS), suffix.translate(_CONTROLS)
3631
3691
  width = columns - 1
3632
3692
  # if total is 0, we don't want to divide by 0: the iteration, padded to clear the last frame. What does not fit gives way
3633
3693
  # from the prefix's start ( hpcp's is the end of a path ), then from the end
@@ -3636,23 +3696,33 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3636
3696
  line = _fit(_fit(prefix, width - _cells(rest), from_start=True) + rest, width)
3637
3697
  return line + ' ' * (width - _cells(line))
3638
3698
  percent = f'|{("{0:.1f}").format(100 * (iteration / float(total)))}% '
3699
+ # a bar is length + 1 wide, full or not ( see below ), so a frame is at most columns - 2 wide. Beside a bar of _LEAST_BAR
3700
+ # cells, what does not fit gives way from the bar outward, a whole field at a time: the prefix from its end down to its first
3701
+ # '|' that ends a field ( hpcp's jobs ), the percent, the prefix on down to its first field, the suffix from its start, then
3702
+ # that field ( a first field wider than the room goes before the suffix is cut ). The line keeps what it starts and ends with
3703
+ # longest: hpcp's count, and its time left ( 1.69 dropped the percent first: hpcp's copy bar at 127 to 134 columns showed its
3704
+ # jobs and no percent, where 1.68 showed both ). The choice is made with the widest percent, '|100.0% ' ( 6 cells at 0.1%, 7 at
3705
+ # 10.0%, 8 at 100.0% to 999.9% ), so the prefix and the suffix a frame keeps, and whether it has a percent, do not change as
3706
+ # the percent grows: the frame left on the screen, at the total, keeps what the frames before it showed. A percent wider than
3707
+ # that ( a total that was off tenfold ) decides for itself
3708
+ room = columns - 2 - _LEAST_BAR
3639
3709
  prefix_cells, suffix_cells = _cells(prefix), _cells(suffix)
3640
- # a bar is length + 1 wide, full or not ( see below ), so a frame is at most columns - 2 wide
3641
- length = columns - prefix_cells - suffix_cells - len(percent) - 3
3642
- if length <= 0:
3643
- length = columns - prefix_cells - suffix_cells - 3
3644
- noPercent = True
3645
- if length <= 0:
3646
- length = columns - suffix_cells - 3
3647
- noPrefix = True
3648
- if length <= 0:
3649
- length = columns - 3
3650
- noSuffix = True
3710
+ reserve = max(len(percent), len('|100.0% '))
3711
+ if prefix_cells + suffix_cells + reserve > room:
3712
+ # the prefix down to its first part, kept whole until the percent has gone; what followed it does not come back then
3713
+ cut, first = _cut_prefix(prefix, room - suffix_cells - reserve), _first_part(prefix)
3714
+ prefix = cut if _cells(cut) > _cells(first) else first
3715
+ if _cells(prefix) + suffix_cells + reserve > room:
3716
+ percent = ''
3717
+ if _cells(prefix) + suffix_cells > room:
3718
+ prefix = _cut_prefix(prefix, room - suffix_cells) or _first_field(prefix)
3719
+ if _cells(prefix) > room:
3720
+ prefix = ''
3721
+ suffix = _cut_suffix(suffix, room - _cells(prefix))
3722
+ length = columns - _cells(prefix) - _cells(suffix) - len(percent) - 3
3651
3723
  if length <= 0:
3652
3724
  # 3 columns or fewer: no room for a bar
3653
3725
  return ''
3654
- if iteration == 0:
3655
- noBar = True
3656
3726
  filled_length = int(length * iteration // total)
3657
3727
  progress_chars = _BAR_BLOCKS
3658
3728
  fractional_progress = (length * iteration / total) - filled_length
@@ -3664,19 +3734,11 @@ def _genrate_progress_bar(iteration, total, prefix='', suffix='',columns=240):
3664
3734
  bar = progress_chars[-1] * (length + 1)
3665
3735
  else:
3666
3736
  bar = progress_chars[-1] * filled_length + bar_char + '_' * (length - filled_length)
3667
- lineOut = ''
3668
- if not noPrefix:
3669
- lineOut += prefix
3670
- if not noBar:
3671
- lineOut += f'{bar}'
3672
- if not noPercent:
3673
- lineOut += percent
3674
- else:
3675
- if length >= 16:
3676
- lineOut += ' Calculating... '
3677
- if not noSuffix:
3678
- lineOut += suffix
3679
- return lineOut
3737
+ if iteration == 0:
3738
+ # the place of the bar ( length + 1 cells ): ' Calculating... ' when it fits, else blanks, so the first frame is cut as a frame
3739
+ # under 10% is ( a percent as wide as its own ), and the prefix's joint and the suffix's never stand side by side ( '||' )
3740
+ return prefix + (' Calculating... ' if length + 1 >= 16 else ' ' * (length + 1)) + suffix
3741
+ return prefix + bar + percent + suffix
3680
3742
 
3681
3743
  def _writable(text, stream):
3682
3744
  '''
@@ -3700,6 +3762,9 @@ def _writable(text, stream):
3700
3762
  text = text.translate(_ASCII_BAR)
3701
3763
  return text.encode(encoding, 'replace').decode(encoding)
3702
3764
 
3765
+ # the total of the bar whose last frame was at or past it, else None ( see print_progress_bar )
3766
+ _bar_done = [None]
3767
+
3703
3768
  def print_progress_bar(iteration, total, prefix='', suffix=''):
3704
3769
  '''
3705
3770
  Call in a loop to create terminal progress bar
@@ -3724,8 +3789,13 @@ def print_progress_bar(iteration, total, prefix='', suffix=''):
3724
3789
  # that wraps or leaves the one before it showing
3725
3790
  prefix, suffix = _writable(prefix, sys.stdout), _writable(suffix, sys.stdout)
3726
3791
  _display._write(_writable(f'\r{_genrate_progress_bar(iteration, total, prefix, suffix, columns)}', sys.stdout))
3727
- if iteration == total and total > 0:
3792
+ # the frame at the total ends its line, each time, and so does the first one past it ( a total that was an estimate, a
3793
+ # count that steps over it ), so the program's next output starts a line of its own. A later frame past the total is
3794
+ # drawn on the line after it, and left open: ending each would scroll a line per frame
3795
+ done = total > 0 and iteration >= total
3796
+ if done and (iteration == total or _bar_done[0] != total):
3728
3797
  _display._write('\n')
3798
+ _bar_done[0] = total if done else None
3729
3799
  except Exception:
3730
3800
  if iteration % 5 == 0:
3731
3801
  _display._write(_writable(_genrate_progress_bar(iteration, total, prefix, suffix), sys.stdout) + '\n')
@@ -603,13 +603,28 @@ def handler(signum, frame):
603
603
  armed[0] = False
604
604
  raise KeyboardInterrupt
605
605
  signal.signal(signal.SIGALRM, handler)
606
+ def reap_strays():
607
+ # a stop that lands as Popen's fork returns leaves a child that no Popen knows ( docs/limitations.md, "A Ctrl-C inside
608
+ # Popen during a serial start ..." ), a zombie until the probe exits: in 5,000 stops one run reaped 770 ( 3.6 ), 782 ( 3.9 ),
609
+ # 1,821 ( 3.11 ), 1,822 ( 3.12 ) and 2,032 ( 3.14 ), so on 3.11 and later a process limit of 1,024 ( Docker's default ) runs
610
+ # out, and the next start fails with EAGAIN. The test asserts that some were reaped: a probe that reaps none passes wherever
611
+ # the process limit is above its strays
612
+ count = 0
613
+ while True:
614
+ try:
615
+ pid, _ = os.waitpid(-1, os.WNOHANG)
616
+ except ChildProcessError:
617
+ return count
618
+ if not pid:
619
+ return count
620
+ count += 1
606
621
  times = []
607
622
  for _ in range(50):
608
623
  start = time.perf_counter()
609
624
  multiCMD.run_command(['true'], quiet=True)
610
625
  times.append(time.perf_counter() - start)
611
626
  window = sorted(times)[len(times) // 2] * 1.5
612
- stops = others = 0
627
+ stops = others = strays = 0
613
628
  for i in range(n):
614
629
  try:
615
630
  try:
@@ -630,12 +645,13 @@ for i in range(n):
630
645
  print('LEAK: threading._active_limbo_lock held after %d calls' % (i + 1), flush=True)
631
646
  os._exit(2)
632
647
  threading._active_limbo_lock.release()
648
+ strays += reap_strays()
633
649
  done[0] = i + 1
634
650
  assert multiCMD.run_command(['true'], quiet=True, return_code_only=True) == 0
635
651
  # ( Python 3.6 - 3.8 block-buffer a redirected stderr, where the stops that landed in a finalizer are told: a result line
636
652
  # printed while a flush at 8 KB had left half a line there would land on the end of it, so the line is told after a flush )
637
653
  sys.stderr.flush()
638
- print('%d calls, %d stopped, %d other errors' % (n, stops, others), flush=True)
654
+ print('%d calls, %d stopped, %d other errors, %d strays reaped' % (n, stops, others, strays), flush=True)
639
655
  '''
640
656
 
641
657
 
@@ -646,13 +662,15 @@ def test_stops_in_serial_commands_never_jam_thread_starts(tmp_path):
646
662
  # left threading's _active_limbo_lock held, and every later thread start hung: up to 1.59 on 3.14 this probe hung in 6 of
647
663
  # 6 runs of 20,000 calls, after 30 to 9,000 calls ( three runs by the plan, three by the build ). 3.6 never hung so in
648
664
  # 60,000 calls. 5,000 calls take about 75 s on 3.6 ( a call and the sleep after it ): 600 s leaves eight times that
649
- # under -n auto
665
+ # under -n auto. The probe reaps the zombies such stops leave ( 1.69: 1.68's probe failed under a process limit of 1,024 )
650
666
  probe = tmp_path / 'probe.py'
651
667
  probe.write_text(STOP_PROBE)
652
668
  run = subprocess.run([sys.executable, '-I', str(probe), str(PACKAGE_ROOT), '5000'], stdout=subprocess.PIPE,
653
669
  stderr=subprocess.STDOUT, universal_newlines=True, timeout=600)
654
670
  # ( the probe flushes stderr before its result line: see there )
655
- assert run.returncode == 0 and re.search(r'^5000 calls, \d+ stopped, 0 other errors$', run.stdout, re.M), run.stdout[-4000:]
671
+ result = re.search(r'^5000 calls, \d+ stopped, 0 other errors, (\d+) strays reaped$', run.stdout, re.M)
672
+ # ( 770 to 2,032 were reaped in 5,000 stops on the five Pythons: a probe that reaps none has lost its reaper )
673
+ assert run.returncode == 0 and result and int(result.group(1)) > 0, run.stdout[-4000:]
656
674
 
657
675
 
658
676
  def _dummy_threads():
@@ -1,5 +1,7 @@
1
1
  '''A progress line fits its terminal (1.68): a terminal of no size counts as unknown, and a frame is cut to one column less
2
- than the width, in terminal columns.'''
2
+ than the width, in terminal columns. A bar with a total keeps 10 cells, and its text gives way a field at a time; a bar past
3
+ its total ends its line; a control character in a frame's text is a '?' (1.69). What follows a frame's prefix's first '|'
4
+ gives way before the percent, and the prefix and suffix a bar keeps hold as its percent grows (1.70).'''
3
5
  import os
4
6
  import re
5
7
  import select
@@ -29,6 +31,13 @@ def fb(count):
29
31
  REMOVAL = f'Removed: {fb(1230)} Folders: {fb(45)} | Overall: {fb(1230000)} Files'
30
32
  LISTING = f'Files: {fb(1230)} Links: {fb(12)} Folders: {fb(45)} Size: {multiCMD.format_bytes(1.15 * 2 ** 30)}B | Overall: {fb(1230000)} Files'
31
33
  WIDE_PATH = '/数据/项目/二〇二六/运行记录/输出结果/检查点/第一千二百步/模型权重文件.safetensors'
34
+ # hpcp 9.96's copy bar ( Adaptive_Progress_Bar.print_progress, read, not imported ), with this module's format_bytes: its count,
35
+ # size and jobs, then its rates, the last jobs' rates and the time left; with a rate limit set, the limits in the jobs' place
36
+ COPY_PREFIX = f'{fb(123456)}F ({multiCMD.format_bytes(45.6 * 2 ** 30)}B) Copied | 16 Scheduled {4.0:0>3.1f} F/Job '
37
+ COPY = (f'{multiCMD.format_bytes(1.23 * 2 ** 30)}B/s {fb(2345.6)}F/s | {2.5:.1f}s: {multiCMD.format_bytes(1.1 * 2 ** 30)}B/s '
38
+ f'{fb(2100)}F/s | 1h2m3s')
39
+ COPY_LIMITED = (f'{multiCMD.format_bytes(1.23 * 2 ** 30)}B/s {fb(2345.6)}F/s | {multiCMD.format_bytes(2 ** 30)}B/s Limit | '
40
+ f'{fb(5000)}F/s Limit | 1h2m3s')
32
41
 
33
42
 
34
43
  def cells(text):
@@ -89,6 +98,10 @@ def test_the_test_strings_are_what_format_bytes_writes():
89
98
  # '1.23K' and cut 'Size: 123.45 GiB' after its number )
90
99
  assert REMOVAL == 'Removed: 1.23 K Folders: 45.00 | Overall: 1.23 M Files'
91
100
  assert LISTING == 'Files: 1.23 K Links: 12.00 Folders: 45.00 Size: 1.15 GiB | Overall: 1.23 M Files'
101
+ # and hpcp 9.96's copy bar ( the 1.69 tests )
102
+ assert COPY_PREFIX == '123.46 KF (45.60 GiB) Copied | 16 Scheduled 4.0 F/Job '
103
+ assert COPY == '1.23 GiB/s 2.35 KF/s | 2.5s: 1.10 GiB/s 2.10 KF/s | 1h2m3s'
104
+ assert COPY_LIMITED == '1.23 GiB/s 2.35 KF/s | 1.00 GiB/s Limit | 5.00 KF/s Limit | 1h2m3s'
92
105
 
93
106
 
94
107
  @pytest.mark.parametrize('reported, environ, expected', [
@@ -314,9 +327,9 @@ def test_a_frame_never_ends_inside_a_field_at_any_width(prefix, suffix):
314
327
  ('x' * 300 + '\u4e2d', 302),
315
328
  ])
316
329
  def test_cells_counts_a_character_by_its_unicode_width(text, expected):
317
- # the module's own count, by Unicode's width of each character: no terminal measures it ( a tab or a newline counts one:
318
- # docs/limitations.md, deferred to 1.69 ). ASCII and Latin-1, the usual text, is counted by length: a fast path that must give
319
- # what the count by character gives
330
+ # the module's own count, by Unicode's width of each character: no terminal measures it ( a tab or a newline counts one, and a
331
+ # frame draws each as a '?' before it counts: 1.69 ). ASCII and Latin-1, the usual text, is counted by length: a fast path that
332
+ # must give what the count by character gives
320
333
  assert multiCMD._cells(text) == expected
321
334
 
322
335
 
@@ -343,9 +356,13 @@ def test_a_bar_of_a_known_total_at_a_terminal_too_narrow_for_it():
343
356
  assert _genrate_progress_bar(5, 10, 'p |', '| s', 5) == '\N{FULL BLOCK}\N{LOWER ONE EIGHTH BLOCK}_'
344
357
 
345
358
 
346
- def test_a_bar_of_a_known_total_drops_its_percent_before_it_shrinks():
347
- # 15 columns: no room for the percent, so the bar takes its place and the prefix and suffix stay
348
- assert _genrate_progress_bar(5, 10, 'p |', '| s', 15) == 'p |' + '\N{FULL BLOCK}' * 3 + '\N{LOWER ONE EIGHTH BLOCK}' + '_' * 3 + '| s'
359
+ def test_a_bar_of_a_known_total_keeps_10_cells_before_its_text():
360
+ # 15 columns: the bar keeps its 10 cells, the percent goes, then the suffix, and the prefix's first field stays ( 1.68: a bar
361
+ # of 7 cells and both ). 12 columns: the bar alone, of 10; fewer: what there is
362
+ half = '\N{FULL BLOCK}' * 4 + '\N{LOWER HALF BLOCK}' + '_' * 5
363
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 15) == 'p |' + half
364
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 12) == half
365
+ assert _genrate_progress_bar(5, 10, 'p |', '| s', 8) == '\N{FULL BLOCK}' * 2 + '\N{LOWER HALF BLOCK}' + '_' * 3
349
366
 
350
367
 
351
368
  def test_a_bar_of_a_known_total_drops_a_long_prefix_and_its_percent():
@@ -368,10 +385,246 @@ def test_the_last_frame_of_a_bar_covers_the_one_before_it(columns):
368
385
  line = []
369
386
  for frame in drawn:
370
387
  line[:len(frame)] = list(frame)
371
- assert ''.join(line) == drawn[-1] and drawn[-1].endswith('| 3.2 F/s | 00:00:00')
388
+ # ( at 40 columns '3.2 F/s' gives way, to keep a bar of 10 cells or more, and the bar takes the room: 1.68 drew '3.2 F/s' beside
389
+ # a bar of 8 cells there )
390
+ tail = '|100.0% | 3.2 F/s | 00:00:00' if columns > 40 else '| 00:00:00'
391
+ assert ''.join(line) == drawn[-1] == '10 files |' + '\N{FULL BLOCK}' * (columns - 2 - 10 - len(tail)) + tail
372
392
 
373
393
 
374
394
  def test_print_progress_bar_draws_at_one_column_less_than_the_terminal(monkeypatch, capsys):
375
395
  monkeypatch.setattr(multiCMD, 'get_terminal_size', lambda: os.terminal_size((80, 24)))
376
396
  print_progress_bar(7, 0, PATH[-50:], REMOVAL)
377
397
  assert capsys.readouterr().out == '\r' + ('tensors | iteration:7 | ' + REMOVAL).ljust(79)
398
+
399
+
400
+ # ---- 1.69: a bar with a total keeps 10 cells; a bar past its total ends its line; a control character is a '?'
401
+
402
+ BAR = re.compile('[' + multiCMD._BAR_BLOCKS + '_]+')
403
+
404
+
405
+ def parts(frame):
406
+ '''A frame of a known total as ( what is before its bar, the bar's cells, what is after it ).'''
407
+ found = BAR.search(frame)
408
+ return frame[:found.start()], found.end() - found.start(), frame[found.end():]
409
+
410
+
411
+ def a_broken_start(kept, whole):
412
+ '''Why a frame's text that keeps the end of whole, from kept on, starts badly, else None: inside a word, on a unit without
413
+ its number, on a number without its label, or on a word whose number went ( '| Files' of '1.23 M Files' ): it starts with
414
+ a number, a label or a '|' ( the mirror of a_broken_end ).'''
415
+ if not whole.endswith(kept):
416
+ return 'is not the end of the line'
417
+ before = whole[:len(whole) - len(kept)]
418
+ if before and before[-1] != ' ':
419
+ return 'starts inside a word'
420
+ before = before.split()
421
+ if before and re.fullmatch(r'\d+(\.\d+)?', before[-1]):
422
+ return 'starts on a unit without its number'
423
+ if before and before[-1].endswith(':'):
424
+ return 'starts on a number without its label'
425
+ first = kept.split()[0]
426
+ if before and not (first[0].isdigit() or first[0] == '|' or first.endswith(':')):
427
+ return 'starts on a word whose number went'
428
+ return None
429
+
430
+
431
+ @pytest.mark.parametrize('columns, suffix, shown', [
432
+ # 1.68 dropped the percent and the whole prefix: ( '', 18, '| ' + COPY ), with no count, size or percent
433
+ (80, COPY, ('123.46 KF |', 18, '| 2.35 KF/s | 2.5s: 1.10 GiB/s 2.10 KF/s | 1h2m3s')),
434
+ (80, COPY_LIMITED, ('123.46 KF |', 10, '| 2.35 KF/s | 1.00 GiB/s Limit | 5.00 KF/s Limit | 1h2m3s')),
435
+ (100, COPY, ('123.46 KF (45.60 GiB) |', 15, '| ' + COPY)),
436
+ (100, COPY_LIMITED, ('123.46 KF |', 19, '| ' + COPY_LIMITED)),
437
+ # 1.68: the whole prefix and a bar of 2 cells; 1.69: its jobs' count ( '16 Scheduled' ) and no percent
438
+ (120, COPY, ('123.46 KF (45.60 GiB) Copied |', 21, '|50.0% | ' + COPY)),
439
+ # 1.68: all of it beside a bar of 2 to 9 cells; 1.69: all of its jobs and no percent. 1.70 decides with the percent at the
440
+ # total ( '|100.0% ', one cell wider than '|50.0% ' ), so a bar's prefix and suffix hold from its first frame to its
441
+ # last: all of its jobs and the percent from 135 columns ( 1.68: from 127, beside a bar of 2 to 9 cells; 1.69: the
442
+ # percent from 135 )
443
+ (127, COPY, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled |', 13, '|50.0% | ' + COPY)),
444
+ (134, COPY, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled |', 20, '|50.0% | ' + COPY)),
445
+ (135, COPY, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled 4.0 F/Job |', 11, '|50.0% | ' + COPY)),
446
+ (135, COPY_LIMITED, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled |', 13, '|50.0% | ' + COPY_LIMITED)),
447
+ (143, COPY_LIMITED, ('123.46 KF (45.60 GiB) Copied | 16 Scheduled 4.0 F/Job |', 11, '|50.0% | ' + COPY_LIMITED)),
448
+ (160, COPY, (COPY_PREFIX + ' |', 35, '|50.0% | ' + COPY)),
449
+ ], ids=['80', '80-limited', '100', '100-limited', '120', '127', '134', '135', '135-limited', '143-limited', '160'])
450
+ def test_hpcp_copy_bar_keeps_its_count_and_its_time_left(columns, suffix, shown):
451
+ # the bar keeps 10 cells; beside it the prefix's jobs give way ( what follows its first '|' ), then the percent, then the
452
+ # prefix on down to its count, then the suffix from its start: the line keeps what it starts and ends with ( the count, the
453
+ # time left )
454
+ frame = _genrate_progress_bar(5, 10, COPY_PREFIX + ' |', '| ' + suffix, columns)
455
+ assert parts(frame) == shown and cells(frame) == columns - 2, frame
456
+
457
+
458
+ # a stage of three after the count: a part after the first '|' that is narrower than the percent
459
+ STAGE_PREFIX = f'{fb(1234)}F Copied | 2/3'
460
+
461
+
462
+ @pytest.mark.parametrize('suffix', [COPY, COPY_LIMITED], ids=['copy', 'copy-limited'])
463
+ @pytest.mark.parametrize('prefix', [COPY_PREFIX, COPY_PREFIX.replace(' Copied ', ' Deleted '), STAGE_PREFIX],
464
+ ids=['copy', 'deleted', 'stage'])
465
+ def test_the_percent_outlasts_what_follows_the_prefixs_first_bar(prefix, suffix):
466
+ # 1.69 dropped the percent first: hpcp's copy bar at 127 to 134 columns showed its jobs and no percent ( 1.68 showed both,
467
+ # beside a bar of 2 to 9 cells ). What follows the prefix's first '|' gives way first, from its end, then the percent, and
468
+ # it does not come back once the percent has gone ( a part narrower than the percent would fit in its place )
469
+ first = prefix[:prefix.index(' | ')] + ' |'
470
+ prefix, suffix = prefix + ' |', '| ' + suffix
471
+ for columns in range(12, 201):
472
+ frame = _genrate_progress_bar(5, 10, prefix, suffix, columns)
473
+ head, bar, tail = parts(frame)
474
+ percent = tail.startswith('|50.0% ')
475
+ assert bar >= 10 and cells(frame) == columns - 2, (columns, frame)
476
+ if cells(head) > cells(first):
477
+ assert percent, (columns, frame)
478
+ # the percent that must fit is the widest, the one at the total ( see the test below )
479
+ if cells(first) + cells('|100.0% ') + cells(suffix) + 10 <= columns - 2:
480
+ assert percent and head.startswith(first), (columns, frame)
481
+
482
+
483
+ def test_a_prefix_with_no_bar_inside_gives_way_after_the_percent():
484
+ # as in 1.69: hpcp's symbolic link bar is one part, so at 55 columns its percent goes and its prefix stays whole
485
+ frame = _genrate_progress_bar(5, 10, '1234 Symbolic Links Created |', '| 5678.12 F/s', 55)
486
+ assert parts(frame) == ('1234 Symbolic Links Created |', 11, '| 5678.12 F/s'), frame
487
+
488
+
489
+ def test_a_bar_inside_a_word_is_not_the_prefixs_first_bar():
490
+ # a '|' with no blank after it ( a name's 'a|b' ) ends no part: the prefix gives way as one part, after the percent
491
+ frame = _genrate_progress_bar(5, 10, 'a|b 1234 Links Created |', '| 5678.12 F/s', 50)
492
+ assert parts(frame) == ('a|b 1234 Links Created |', 11, '| 5678.12 F/s'), frame
493
+
494
+
495
+ @pytest.mark.parametrize('prefix, suffix', [
496
+ (COPY_PREFIX, COPY), (COPY_PREFIX, COPY_LIMITED),
497
+ (COPY_PREFIX.replace(' Copied ', ' Deleted '), COPY), (COPY_PREFIX.replace(' Copied ', ' Deleted '), COPY_LIMITED),
498
+ (STAGE_PREFIX, COPY), ('1234 Symbolic Links Created', '5678.12 F/s'), (PATH[-50:], REMOVAL),
499
+ ], ids=['copy', 'copy-limited', 'deleted', 'deleted-limited', 'stage', 'symlinks', 'path'])
500
+ def test_a_bars_parts_do_not_change_as_its_percent_grows(prefix, suffix):
501
+ # the percent is 6 cells at 0.1% to 9.9%, 7 at 10.0% to 99.9% and 8 at 100.0% to 999.9% ( '|100.0% ' ). A layout decided with
502
+ # the frame's own percent drew hpcp's copy bar with its percent at 99.9% and none at the total, the frame left on the screen
503
+ # ( with this file's strings: 1.69 at 135 columns; 1.70's first build at 109, and its jobs at 124 and 134 ). The prefix, the
504
+ # suffix and whether the percent shows hold from the first frame ( which has no percent ) to the last; the bar's length
505
+ # follows each frame's percent
506
+ prefix, suffix = prefix + ' |', '| ' + suffix
507
+ for columns in range(12, 201):
508
+ seen = set()
509
+ for iteration in (1, 50, 99, 100, 500, 999, 1000, 1100, 9999):
510
+ head, bar, tail = parts(_genrate_progress_bar(iteration, 1000, prefix, suffix, columns))
511
+ percent = re.match(r'\|\d+\.\d% ', tail)
512
+ seen.add((head, bool(percent), tail[percent.end():] if percent else tail))
513
+ assert bar >= 10, (columns, iteration, bar)
514
+ assert len(seen) == 1, (columns, seen)
515
+ (head, _, rest), = seen
516
+ first = _genrate_progress_bar(0, 1000, prefix, suffix, columns)
517
+ assert first.startswith(head) and first.endswith(rest), (columns, first)
518
+
519
+
520
+ def test_a_percent_wider_than_the_one_at_the_total_keeps_a_bar_of_10_cells():
521
+ # a count tenfold over its total ( '|1000.0% ', 9 cells ) decides with its own percent: the bar keeps its 10 cells
522
+ for columns in range(12, 201):
523
+ frame = _genrate_progress_bar(10000, 1000, COPY_PREFIX + ' |', '| ' + COPY, columns)
524
+ assert parts(frame)[1] >= 10 and cells(frame) == columns - 2, (columns, frame)
525
+
526
+
527
+ @pytest.mark.parametrize('suffix', [COPY, COPY_LIMITED, REMOVAL], ids=['copy', 'copy-limited', 'removal'])
528
+ @pytest.mark.parametrize('prefix', [COPY_PREFIX, PATH[-50:], WIDE_PATH[-50:]], ids=['copy', 'path', 'wide-path'])
529
+ def test_a_frame_of_a_known_total_keeps_whole_fields_beside_10_cells_at_any_width(prefix, suffix):
530
+ # at 12 to 200 columns: a bar of 10 cells or more, the start of the prefix and the end of the suffix, each cut only between
531
+ # fields ( a number keeps its unit, a label its number ), and the prefix's first field and the suffix's last as long as
532
+ # there is room for them
533
+ # ( a path has no blank: it is its one field )
534
+ first = '123.46 KF |' if prefix == COPY_PREFIX else prefix + ' |'
535
+ last = '| Overall: 1.23 M Files' if suffix == REMOVAL else '| 1h2m3s'
536
+ prefix, suffix = prefix + ' |', '| ' + suffix
537
+ for columns in range(12, 201):
538
+ frame = _genrate_progress_bar(5, 10, prefix, suffix, columns)
539
+ head, bar, tail = parts(frame)
540
+ if tail.startswith('|50.0% '):
541
+ tail = tail[len('|50.0% '):]
542
+ assert cells(frame) == columns - 2 and bar >= 10, (columns, frame)
543
+ assert head == '' or head == prefix or head.endswith(' |') and a_broken_end(head[:-2], prefix) is None, (columns, frame)
544
+ assert tail == '' or tail == suffix or tail.startswith('| ') and a_broken_start(tail[2:], suffix) is None, (columns, frame)
545
+ if cells(first) + 10 <= columns - 2:
546
+ assert head.startswith(first[:-2]), (columns, frame)
547
+ if cells(head) + cells(last) + 10 <= columns - 2:
548
+ assert tail.endswith(last), (columns, frame)
549
+
550
+
551
+ @pytest.mark.parametrize('suffix', [COPY, COPY_LIMITED, REMOVAL], ids=['copy', 'copy-limited', 'removal'])
552
+ @pytest.mark.parametrize('prefix', [COPY_PREFIX, PATH[-50:], WIDE_PATH[-50:]], ids=['copy', 'path', 'wide-path'])
553
+ def test_the_first_frame_of_a_known_total_has_the_place_of_its_bar_and_no_double_joint(prefix, suffix):
554
+ # iteration 0 ( hpcp's first copy bar frame ) is cut as the frames after it, and its text stands where theirs does: the bar's
555
+ # place holds ' Calculating... ' when it fits, else blanks ( 1.69 first cut it for a bar it never drew and joined prefix and
556
+ # suffix with nothing between: '0.00 F || 0.00 F/s | ∞' at 40 columns ). 1 of 1000 has a percent as long as 0's
557
+ prefix, suffix = prefix + ' |', '| ' + suffix
558
+ for columns in range(12, 201):
559
+ first = _genrate_progress_bar(0, 1000, prefix, suffix, columns)
560
+ head, bar, tail = parts(_genrate_progress_bar(1, 1000, prefix, suffix, columns))
561
+ if tail.startswith('|0.1% '):
562
+ tail = tail[len('|0.1% '):]
563
+ place = ' Calculating... ' if bar >= 16 else ' ' * bar
564
+ assert first == head + place + tail, (columns, first)
565
+ assert '||' not in first and cells(first) <= columns - 2, (columns, first)
566
+
567
+
568
+ def test_a_bar_past_its_total_ends_its_line(monkeypatch, capsys):
569
+ # 1.68 ended the line only at a frame equal to the total: a count that stepped over it left the line open, and the program's
570
+ # next output joined the bar ( '...|110.0% | snext' ). The first frame at or past the total ends it now; the frame at the
571
+ # total still ends it each time; a later frame past it is drawn on the next line and left open, so it does not scroll
572
+ monkeypatch.setattr(multiCMD, 'get_terminal_size', lambda: os.terminal_size((40, 24)))
573
+ monkeypatch.setattr(multiCMD, '_bar_done', [None])
574
+ print_progress_bar(9, 10, 'p', 's')
575
+ print_progress_bar(11, 10, 'p', 's')
576
+ print('next')
577
+ shown = capsys.readouterr().out.split('\n')
578
+ assert len(shown) == 3 and shown[0].endswith('|110.0% | s') and shown[1:] == ['next', ''], shown
579
+ for frames, lines in [
580
+ # a count that steps over the total, then goes on: one line end, after the first frame past it
581
+ ([9, 12, 13, 15], 1),
582
+ # a frame at the total, each time ( as 1.68 )
583
+ ([5, 10, 10], 2),
584
+ # at the total, then past it: as 1.68
585
+ ([9, 10, 11], 1),
586
+ # a new bar of the same total: a frame below it starts it again
587
+ ([12, 3, 12], 2),
588
+ ]:
589
+ for iteration in frames:
590
+ print_progress_bar(iteration, 10, 'p', 's')
591
+ assert capsys.readouterr().out.count('\n') == lines, frames
592
+ print_progress_bar(0, 10, 'p', 's')
593
+ capsys.readouterr()
594
+
595
+
596
+ @posix_only
597
+ def test_the_output_after_a_bar_past_its_total_starts_a_line_of_its_own():
598
+ # at a terminal of no size ( 80x24 ): the bar's line ends with the first frame past the total, and 'next' starts the line after it
599
+ code = ('import multiCMD\n'
600
+ 'for i in (5, 11): multiCMD.print_progress_bar(i, 10, "p", "s")\n'
601
+ 'print("next")')
602
+ shown = at_a_pty(code, 0, 0)
603
+ assert shown.endswith('|110.0% | s\nnext\n') and shown.count('\n') == 2, shown
604
+
605
+
606
+ CONTROL = re.compile('[\x00-\x1f\x7f-\x9f]')
607
+
608
+
609
+ @pytest.mark.parametrize('name', ['run\tlog', 'run\nlog', 'run\rlog', 'run\x1b[2Jlog', 'run\x7flog', 'run\x9b2Jlog', 'run\x00log'],
610
+ ids=['tab', 'newline', 'carriage-return', 'escape', 'delete', 'c1-csi', 'nul'])
611
+ def test_a_control_character_in_a_frame_is_a_question_mark(name):
612
+ # a file name may hold any of them ( hpcp's removal and listing lines show the end of a path ): 1.68 wrote a newline as a
613
+ # new row, so each redraw scrolled, and a tab to the next tab stop, so the frame could reach the last column and wrap
614
+ shown = CONTROL.sub('?', name)
615
+ assert shown.startswith('run?') and shown.endswith('log')
616
+ frame = _genrate_progress_bar(7, 0, '/data/' + name + ' |', '| Removed: 1.23 K', 80)
617
+ assert frame == ('/data/' + shown + ' | iteration:7 | Removed: 1.23 K').ljust(79), frame
618
+ frame = _genrate_progress_bar(5, 10, name + ' |', '| ' + name, 80)
619
+ assert not CONTROL.search(frame) and cells(frame) == 78 and frame.startswith(shown + ' |') and frame.endswith('| ' + shown)
620
+
621
+
622
+ @posix_only
623
+ def test_a_name_with_a_tab_and_a_newline_stays_on_one_row():
624
+ # at an 80-column terminal: three frames, no row of their own and no tab ( 1.68: a row more per frame, each scrolling )
625
+ code = ('import multiCMD\n'
626
+ 'for i in range(3): multiCMD.print_progress_bar(i, 0, "/data/run\\tlog\\nold", "Removed: 1.23 K")\n')
627
+ shown = at_a_pty(code, 24, 80)
628
+ assert '\n' not in shown and '\t' not in shown and len(frames(shown)) == 3, shown
629
+ assert [frame.rstrip() for frame in frames(shown)] == ['/data/run?log?old | iteration:%d | Removed: 1.23 K' % i for i in range(3)]
630
+ assert all(cells(frame) == 79 for frame in frames(shown)), shown
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes