Skip to content

Commit 9e74767

Browse files
committed
Renamed functions based on code review comments. Fixed Python warnings.
1 parent 88e308b commit 9e74767

File tree

4 files changed

+60
-58
lines changed

4 files changed

+60
-58
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
## 0.9.22 (December 10, 2019)
1+
## 0.9.22 (December 9, 2019)
22
* Bug Fixes
33
* Fixed bug where a redefined `ansi.style_error` was not being used in all `cmd2` files
44
* Enhancements
55
* Enabled line buffering when redirecting output to a file
6-
* Added `ljustify_text()`, `center_text()`, and `rjustify_text()` to utils.py. All 3 of these functions support
6+
* Added `align_left()`, `align_center()`, and `align_right()` to utils.py. All 3 of these functions support
77
ANSI escape sequences and characters with display widths greater than 1.
8-
8+
99
## 0.9.21 (November 26, 2019)
1010
* Bug Fixes
1111
* Fixed bug where pipe processes were not being stopped by Ctrl-C

cmd2/cmd2.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2718,6 +2718,7 @@ def _print_topics(self, header: str, cmds: List[str], verbose: bool) -> None:
27182718
self.stdout.write("\n")
27192719

27202720
shortcuts_parser = DEFAULT_ARGUMENT_PARSER(description="List available shortcuts")
2721+
27212722
@with_argparser(shortcuts_parser)
27222723
def do_shortcuts(self, _: argparse.Namespace) -> None:
27232724
"""List available shortcuts"""
@@ -2727,13 +2728,15 @@ def do_shortcuts(self, _: argparse.Namespace) -> None:
27272728
self.poutput("Shortcuts for other commands:\n{}".format(result))
27282729

27292730
eof_parser = DEFAULT_ARGUMENT_PARSER(description="Called when <Ctrl>-D is pressed", epilog=INTERNAL_COMMAND_EPILOG)
2731+
27302732
@with_argparser(eof_parser)
27312733
def do_eof(self, _: argparse.Namespace) -> bool:
27322734
"""Called when <Ctrl>-D is pressed"""
27332735
# Return True to stop the command loop
27342736
return True
27352737

27362738
quit_parser = DEFAULT_ARGUMENT_PARSER(description="Exit this application")
2739+
27372740
@with_argparser(quit_parser)
27382741
def do_quit(self, _: argparse.Namespace) -> bool:
27392742
"""Exit this application"""
@@ -3215,6 +3218,7 @@ def do_run_pyscript(self, args: argparse.Namespace) -> Optional[bool]:
32153218
# Only include the do_ipy() method if IPython is available on the system
32163219
if ipython_available: # pragma: no cover
32173220
ipython_parser = DEFAULT_ARGUMENT_PARSER(description="Enter an interactive IPython shell")
3221+
32183222
@with_argparser(ipython_parser)
32193223
def do_ipy(self, _: argparse.Namespace) -> None:
32203224
"""Enter an interactive IPython shell"""
@@ -3223,6 +3227,7 @@ def do_ipy(self, _: argparse.Namespace) -> None:
32233227
'Run Python code from external files with: run filename.py\n')
32243228
exit_msg = 'Leaving IPython, back to {}'.format(sys.argv[0])
32253229

3230+
# noinspection PyUnusedLocal
32263231
def load_ipy(cmd2_app: Cmd, py_bridge: PyBridge):
32273232
"""
32283233
Embed an IPython shell in an environment that is restricted to only the variables in this function
@@ -3715,7 +3720,7 @@ class TestMyAppCase(Cmd2TestCase):
37153720
verinfo = ".".join(map(str, sys.version_info[:3]))
37163721
num_transcripts = len(transcripts_expanded)
37173722
plural = '' if len(transcripts_expanded) == 1 else 's'
3718-
self.poutput(ansi.style(utils.center_text(' cmd2 transcript test ', fill_char='='), bold=True))
3723+
self.poutput(ansi.style(utils.align_center(' cmd2 transcript test ', fill_char='='), bold=True))
37193724
self.poutput('platform {} -- Python {}, cmd2-{}, readline-{}'.format(sys.platform, verinfo, cmd2.__version__,
37203725
rl_type))
37213726
self.poutput('cwd: {}'.format(os.getcwd()))
@@ -3734,7 +3739,7 @@ class TestMyAppCase(Cmd2TestCase):
37343739
if test_results.wasSuccessful():
37353740
ansi.ansi_aware_write(sys.stderr, stream.read())
37363741
finish_msg = ' {0} transcript{1} passed in {2:.3f} seconds '.format(num_transcripts, plural, execution_time)
3737-
finish_msg = ansi.style_success(utils.center_text(finish_msg, fill_char='='))
3742+
finish_msg = ansi.style_success(utils.align_center(finish_msg, fill_char='='))
37383743
self.poutput(finish_msg)
37393744
else:
37403745
# Strip off the initial traceback which isn't particularly useful for end users

cmd2/utils.py

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -637,21 +637,21 @@ class TextAlignment(Enum):
637637
RIGHT = 3
638638

639639

640-
def align_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4,
641-
alignment: TextAlignment) -> str:
640+
def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
641+
width: Optional[int] = None, tab_width: int = 4) -> str:
642642
"""
643643
Align text for display within a given width. Supports characters with display widths greater than 1.
644644
ANSI escape sequences are safely ignored and do not count toward the display width. This means colored text is
645645
supported. If text has line breaks, then each line is aligned independently.
646646
647-
There are convenience wrappers around this function: ljustify_text(), center_text(), and rjustify_text()
647+
There are convenience wrappers around this function: align_left(), align_center(), and align_right()
648648
649-
:param text: text to align (Can contain multiple lines)
649+
:param text: text to align (can contain multiple lines)
650+
:param alignment: how to align the text
650651
:param fill_char: character that fills the alignment gap. Defaults to space. (Cannot be a line breaking character)
651652
:param width: display width of the aligned text. Defaults to width of the terminal.
652653
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
653654
be converted to a space.
654-
:param alignment: how to align the text
655655
:return: aligned text
656656
:raises: TypeError if fill_char is more than one character
657657
ValueError if text or fill_char contains an unprintable character
@@ -725,32 +725,31 @@ def align_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None,
725725
return text_buf.getvalue()
726726

727727

728-
def ljustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
728+
def align_left(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
729729
"""
730-
Left justify text for display within a given width. Supports characters with display widths greater than 1.
730+
Left align text for display within a given width. Supports characters with display widths greater than 1.
731731
ANSI escape sequences are safely ignored and do not count toward the display width. This means colored text is
732732
supported. If text has line breaks, then each line is aligned independently.
733733
734-
:param text: text to left justify (Can contain multiple lines)
734+
:param text: text to left align (can contain multiple lines)
735735
:param fill_char: character that fills the alignment gap. Defaults to space. (Cannot be a line breaking character)
736736
:param width: display width of the aligned text. Defaults to width of the terminal.
737737
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
738738
be converted to a space.
739-
:return: left-justified text
739+
:return: left-aligned text
740740
:raises: TypeError if fill_char is more than one character
741741
ValueError if text or fill_char contains an unprintable character
742742
"""
743-
return align_text(text, fill_char=fill_char, width=width,
744-
tab_width=tab_width, alignment=TextAlignment.LEFT)
743+
return align_text(text, TextAlignment.LEFT, fill_char=fill_char, width=width, tab_width=tab_width)
745744

746745

747-
def center_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
746+
def align_center(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
748747
"""
749748
Center text for display within a given width. Supports characters with display widths greater than 1.
750749
ANSI escape sequences are safely ignored and do not count toward the display width. This means colored text is
751750
supported. If text has line breaks, then each line is aligned independently.
752751
753-
:param text: text to center (Can contain multiple lines)
752+
:param text: text to center (can contain multiple lines)
754753
:param fill_char: character that fills the alignment gap. Defaults to space. (Cannot be a line breaking character)
755754
:param width: display width of the aligned text. Defaults to width of the terminal.
756755
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
@@ -759,24 +758,22 @@ def center_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None,
759758
:raises: TypeError if fill_char is more than one character
760759
ValueError if text or fill_char contains an unprintable character
761760
"""
762-
return align_text(text, fill_char=fill_char, width=width,
763-
tab_width=tab_width, alignment=TextAlignment.CENTER)
761+
return align_text(text, TextAlignment.CENTER, fill_char=fill_char, width=width, tab_width=tab_width)
764762

765763

766-
def rjustify_text(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
764+
def align_right(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width: int = 4) -> str:
767765
"""
768-
Right justify text for display within a given width. Supports characters with display widths greater than 1.
766+
Right align text for display within a given width. Supports characters with display widths greater than 1.
769767
ANSI escape sequences are safely ignored and do not count toward the display width. This means colored text is
770768
supported. If text has line breaks, then each line is aligned independently.
771769
772-
:param text: text to right justify (Can contain multiple lines)
770+
:param text: text to right align (can contain multiple lines)
773771
:param fill_char: character that fills the alignment gap. Defaults to space. (Cannot be a line breaking character)
774772
:param width: display width of the aligned text. Defaults to width of the terminal.
775773
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
776774
be converted to a space.
777-
:return: right-justified text
775+
:return: right-aligned text
778776
:raises: TypeError if fill_char is more than one character
779777
ValueError if text or fill_char contains an unprintable character
780778
"""
781-
return align_text(text, fill_char=fill_char, width=width,
782-
tab_width=tab_width, alignment=TextAlignment.RIGHT)
779+
return align_text(text, TextAlignment.RIGHT, fill_char=fill_char, width=width, tab_width=tab_width)

tests/test_utils.py

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -354,121 +354,121 @@ def test_align_text_term_width():
354354
aligned = cu.align_text(text, fill_char=fill_char, alignment=cu.TextAlignment.LEFT)
355355
assert aligned == text + expected_fill
356356

357-
def test_left_text():
357+
def test_align_left():
358358
text = 'foo'
359359
fill_char = '-'
360360
width = 5
361-
aligned = cu.ljustify_text(text, fill_char=fill_char, width=width)
361+
aligned = cu.align_left(text, fill_char=fill_char, width=width)
362362
assert aligned == text + fill_char + fill_char
363363

364-
def test_left_text_multiline():
364+
def test_align_left_multiline():
365365
text = "foo\nshoes"
366366
fill_char = '-'
367367
width = 7
368-
aligned = cu.ljustify_text(text, fill_char=fill_char, width=width)
368+
aligned = cu.align_left(text, fill_char=fill_char, width=width)
369369
assert aligned == ('foo----\n'
370370
'shoes--')
371371

372-
def test_left_wide_display_text():
372+
def test_align_left_wide_text():
373373
text = '苹'
374374
fill_char = '-'
375375
width = 4
376-
aligned = cu.ljustify_text(text, fill_char=fill_char, width=width)
376+
aligned = cu.align_left(text, fill_char=fill_char, width=width)
377377
assert aligned == text + fill_char + fill_char
378378

379-
def test_left_text_wide_display_fill():
379+
def test_align_left_wide_fill():
380380
text = 'foo'
381381
fill_char = '苹'
382382
width = 5
383-
aligned = cu.ljustify_text(text, fill_char=fill_char, width=width)
383+
aligned = cu.align_left(text, fill_char=fill_char, width=width)
384384
assert aligned == text + fill_char
385385

386-
def test_left_text_wide_display_fill_needs_padding():
386+
def test_align_left_wide_fill_needs_padding():
387387
"""Test when fill_char's display width does not divide evenly into gap"""
388388
text = 'foo'
389389
fill_char = '苹'
390390
width = 6
391-
aligned = cu.ljustify_text(text, fill_char=fill_char, width=width)
391+
aligned = cu.align_left(text, fill_char=fill_char, width=width)
392392
assert aligned == text + fill_char + ' '
393393

394-
def test_center_text():
394+
def test_align_center():
395395
text = 'foo'
396396
fill_char = '-'
397397
width = 5
398-
aligned = cu.center_text(text,fill_char=fill_char, width=width)
398+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
399399
assert aligned == fill_char + text + fill_char
400400

401-
def test_center_text_multiline():
401+
def test_align_center_multiline():
402402
text = "foo\nshoes"
403403
fill_char = '-'
404404
width = 7
405-
aligned = cu.center_text(text, fill_char=fill_char, width=width)
405+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
406406
assert aligned == ('--foo--\n'
407407
'-shoes-')
408408

409-
def test_center_wide_display_text():
409+
def test_align_center_wide_text():
410410
text = '苹'
411411
fill_char = '-'
412412
width = 4
413-
aligned = cu.center_text(text, fill_char=fill_char, width=width)
413+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
414414
assert aligned == fill_char + text + fill_char
415415

416-
def test_center_text_wide_display_fill():
416+
def test_align_center_wide_fill():
417417
text = 'foo'
418418
fill_char = '苹'
419419
width = 7
420-
aligned = cu.center_text(text, fill_char=fill_char, width=width)
420+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
421421
assert aligned == fill_char + text + fill_char
422422

423-
def test_center_text_wide_display_fill_needs_right_padding():
423+
def test_align_center_wide_fill_needs_right_padding():
424424
"""Test when fill_char's display width does not divide evenly into right gap"""
425425
text = 'foo'
426426
fill_char = '苹'
427427
width = 8
428-
aligned = cu.center_text(text, fill_char=fill_char, width=width)
428+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
429429
assert aligned == fill_char + text + fill_char + ' '
430430

431-
def test_center_text_wide_display_fill_needs_left_and_right_padding():
431+
def test_align_center_wide_fill_needs_left_and_right_padding():
432432
"""Test when fill_char's display width does not divide evenly into either gap"""
433433
text = 'foo'
434434
fill_char = '苹'
435435
width = 9
436-
aligned = cu.center_text(text, fill_char=fill_char, width=width)
436+
aligned = cu.align_center(text, fill_char=fill_char, width=width)
437437
assert aligned == fill_char + ' ' + text + fill_char + ' '
438438

439-
def test_right_text():
439+
def test_align_right():
440440
text = 'foo'
441441
fill_char = '-'
442442
width = 5
443-
aligned = cu.rjustify_text(text, fill_char=fill_char, width=width)
443+
aligned = cu.align_right(text, fill_char=fill_char, width=width)
444444
assert aligned == fill_char + fill_char + text
445445

446-
def test_right_text_multiline():
446+
def test_align_right_multiline():
447447
text = "foo\nshoes"
448448
fill_char = '-'
449449
width = 7
450-
aligned = cu.rjustify_text(text, fill_char=fill_char, width=width)
450+
aligned = cu.align_right(text, fill_char=fill_char, width=width)
451451
assert aligned == ('----foo\n'
452452
'--shoes')
453453

454-
def test_right_wide_display_text():
454+
def test_align_right_wide_text():
455455
text = '苹'
456456
fill_char = '-'
457457
width = 4
458-
aligned = cu.rjustify_text(text, fill_char=fill_char, width=width)
458+
aligned = cu.align_right(text, fill_char=fill_char, width=width)
459459
assert aligned == fill_char + fill_char + text
460460

461-
def test_right_text_wide_display_fill():
461+
def test_align_right_wide_fill():
462462
text = 'foo'
463463
fill_char = '苹'
464464
width = 5
465-
aligned = cu.rjustify_text(text, fill_char=fill_char, width=width)
465+
aligned = cu.align_right(text, fill_char=fill_char, width=width)
466466
assert aligned == fill_char + text
467467

468-
def test_right_text_wide_display_fill_needs_padding():
468+
def test_align_right_wide_fill_needs_padding():
469469
"""Test when fill_char's display width does not divide evenly into gap"""
470470
text = 'foo'
471471
fill_char = '苹'
472472
width = 6
473-
aligned = cu.rjustify_text(text, fill_char=fill_char, width=width)
473+
aligned = cu.align_right(text, fill_char=fill_char, width=width)
474474
assert aligned == fill_char + ' ' + text

0 commit comments

Comments
 (0)