Skip to content

Commit 04de43d

Browse files
committed
feat(cmd2): improve bottom toolbar with colors and timestamp
Updated Cmd._bottom_toolbar to display the application name in green on the left and the current ISO timestamp in blue on the right when include_bottom_toolbar is enabled. Used padding to achieve right-alignment of the timestamp. Updated associated tests to match the new return format.
1 parent 2c4da8c commit 04de43d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

cmd2/cmd2.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,26 @@ def _reset_completion_defaults(self) -> None:
16301630
def _bottom_toolbar(self) -> Any:
16311631
"""Get the bottom toolbar content."""
16321632
if self.include_bottom_toolbar:
1633-
return sys.argv[0]
1633+
import datetime
1634+
import shutil
1635+
1636+
# Get the current time in ISO format
1637+
now = datetime.datetime.now(datetime.timezone.utc).astimezone().isoformat()
1638+
left_text = sys.argv[0]
1639+
1640+
# Get terminal width to calculate padding for right-alignment
1641+
cols, _ = shutil.get_terminal_size()
1642+
padding_size = cols - len(left_text) - len(now) - 1
1643+
if padding_size < 1:
1644+
padding_size = 1
1645+
padding = ' ' * padding_size
1646+
1647+
# Return formatted text for prompt-toolkit
1648+
return [
1649+
('ansigreen', left_text),
1650+
('', padding),
1651+
('ansiblue', now),
1652+
]
16341653
return None
16351654

16361655
def tokens_for_completion(self, line: str, begidx: int, endidx: int) -> tuple[list[str], list[str]]:

tests/test_cmd2.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,10 @@ def test_bottom_toolbar(base_app, monkeypatch):
33723372
# Test enabled
33733373
base_app.include_bottom_toolbar = True
33743374
monkeypatch.setattr(sys, 'argv', ['myapp.py'])
3375-
assert base_app._bottom_toolbar() == 'myapp.py'
3375+
toolbar = base_app._bottom_toolbar()
3376+
assert isinstance(toolbar, list)
3377+
assert toolbar[0] == ('ansigreen', 'myapp.py')
3378+
assert toolbar[2][0] == 'ansiblue'
33763379

33773380

33783381
def test_multiline_complete_statement_keyboard_interrupt(multiline_app, monkeypatch):

0 commit comments

Comments
 (0)