feat(notifications): show how long a command ran when it finishes
The duration was measured to decide whether a command was long enough to warrant a notification, but it was never shown, so the notification could only say that the command had ended. Report it next to the exit status, formatted in milliseconds, seconds, minutes or hours depending on the length.
This commit is contained in:
2 files changed
+26
-1
No files matched your search
+14
-1
@@ -283,6 +283,19 @@ def decode_cmdline(x: str) -> str:
|
||||
return ''
|
||||
|
||||
|
||||
def format_cmd_duration(seconds: float) -> str:
|
||||
"""Human readable duration for the notification sent when a command ends."""
|
||||
if seconds < 1:
|
||||
return f'{seconds * 1000:.0f}ms'
|
||||
if seconds < 60:
|
||||
return f'{seconds:.1f}s'
|
||||
minutes, secs = divmod(int(seconds), 60)
|
||||
if minutes < 60:
|
||||
return f'{minutes}m {secs}s'
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
return f'{hours}h {minutes}m'
|
||||
|
||||
|
||||
class WindowDict(TypedDict):
|
||||
id: int
|
||||
is_focused: bool
|
||||
@@ -2074,7 +2087,7 @@ class Window:
|
||||
cmd = nm.create_notification_cmd()
|
||||
cmd.title = 'kitty'
|
||||
s = self.last_cmd_cmdline.replace('\\\n', ' ')
|
||||
cmd.body = f'Command {s} finished with status: {self.last_cmd_exit_status}.\nClick to focus.'
|
||||
cmd.body = f'Command {s} finished in {format_cmd_duration(last_cmd_output_duration)} with status: {self.last_cmd_exit_status}.\nClick to focus.'
|
||||
cmd.only_when = OnlyWhen(when)
|
||||
if not nm.is_notification_allowed(cmd, self.id):
|
||||
return
|
||||
|
||||
@@ -364,6 +364,18 @@ class TestNotifications(BaseTest):
|
||||
with tempfile.TemporaryDirectory() as tdir:
|
||||
do_test(self, tdir)
|
||||
|
||||
def test_command_finish_duration_formatting(self):
|
||||
from kitty.window import format_cmd_duration
|
||||
|
||||
self.ae(format_cmd_duration(0), '0ms')
|
||||
self.ae(format_cmd_duration(0.42), '420ms')
|
||||
self.ae(format_cmd_duration(1.0), '1.0s')
|
||||
self.ae(format_cmd_duration(59.94), '59.9s')
|
||||
self.ae(format_cmd_duration(60), '1m 0s')
|
||||
self.ae(format_cmd_duration(3599), '59m 59s')
|
||||
self.ae(format_cmd_duration(3600), '1h 0m')
|
||||
self.ae(format_cmd_duration(7325), '2h 2m')
|
||||
|
||||
def test_freedesktop_close_before_created(self):
|
||||
import kitty.fast_data_types as fdt
|
||||
|
||||
|
||||
Reference in new issue
Block a user