Skip to content

Commit f4ddddc

Browse files
JosephMartmitchellh
authored andcommitted
macos: refactor command finish notification duration handling
1 parent 3b5a7b7 commit f4ddddc

File tree

2 files changed

+27
-34
lines changed

2 files changed

+27
-34
lines changed

macos/Sources/Ghostty/Ghostty.App.swift

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,6 @@ extension Ghostty {
638638
case GHOSTTY_ACTION_PRESENT_TERMINAL:
639639
return presentTerminal(app, target: target)
640640

641-
642641
case GHOSTTY_ACTION_TOGGLE_TAB_OVERVIEW:
643642
fallthrough
644643
case GHOSTTY_ACTION_TOGGLE_WINDOW_DECORATIONS:
@@ -1403,15 +1402,22 @@ extension Ghostty {
14031402
guard let surface = target.target.surface else { return }
14041403
guard let surfaceView = self.surfaceView(from: surface) else { return }
14051404

1406-
guard let appState = (NSApplication.shared.delegate as? AppDelegate)?.ghostty else { return }
1407-
let config = appState.config
1405+
// Determine if we even care about command finish notifications
1406+
guard let config = (NSApplication.shared.delegate as? AppDelegate)?.ghostty.config else { return }
1407+
switch config.notifyOnCommandFinish {
1408+
case .never:
1409+
return
1410+
1411+
case .unfocused:
1412+
if surfaceView.focused { return }
14081413

1409-
let mode = config.notifyOnCommandFinish
1410-
if mode == .never { return }
1411-
if mode == .unfocused && surfaceView.focused { return }
1414+
case .always:
1415+
break
1416+
}
14121417

1413-
let durationMs = v.duration / 1_000_000
1414-
if durationMs < config.notifyOnCommandFinishAfter { return }
1418+
// Determine if the command was slow enough
1419+
let duration = Duration.nanoseconds(v.duration)
1420+
guard Duration.nanoseconds(v.duration) >= config.notifyOnCommandFinishAfter else { return }
14151421

14161422
let actions = config.notifyOnCommandFinishAction
14171423

@@ -1433,7 +1439,13 @@ extension Ghostty {
14331439
}
14341440

14351441
let body: String
1436-
let formattedDuration = Self.formatDuration(ns: v.duration)
1442+
let formattedDuration = duration.formatted(
1443+
.units(
1444+
allowed: [.hours, .minutes, .seconds, .milliseconds],
1445+
width: .abbreviated,
1446+
fractionalPart: .hide
1447+
)
1448+
)
14371449
if v.exit_code < 0 {
14381450
body = "Command took \(formattedDuration)."
14391451
} else {
@@ -1448,26 +1460,6 @@ extension Ghostty {
14481460
}
14491461
}
14501462

1451-
private static func formatDuration(ns: UInt64) -> String {
1452-
let totalSeconds = ns / 1_000_000_000
1453-
let ms = (ns / 1_000_000) % 1000
1454-
1455-
if totalSeconds == 0 {
1456-
return "\(ms)ms"
1457-
}
1458-
1459-
let seconds = totalSeconds % 60
1460-
let minutes = (totalSeconds / 60) % 60
1461-
let hours = totalSeconds / 3600
1462-
1463-
var parts: [String] = []
1464-
if hours > 0 { parts.append("\(hours)h") }
1465-
if minutes > 0 { parts.append("\(minutes)m") }
1466-
if seconds > 0 || (hours == 0 && minutes == 0) { parts.append("\(seconds)s") }
1467-
1468-
return parts.joined(separator: " ")
1469-
}
1470-
14711463
private static func toggleFloatWindow(
14721464
_ app: ghostty_app_t,
14731465
target: ghostty_target_s,

macos/Sources/Ghostty/Ghostty.Config.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,19 +144,20 @@ extension Ghostty {
144144
}
145145

146146
var notifyOnCommandFinishAction: NotifyOnCommandFinishAction {
147-
guard let config = self.config else { return .bell }
147+
let defaultValue = NotifyOnCommandFinishAction.bell
148+
guard let config = self.config else { return defaultValue }
148149
var v: CUnsignedInt = 0
149150
let key = "notify-on-command-finish-action"
150-
guard ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) else { return .bell }
151+
guard ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8))) else { return defaultValue }
151152
return .init(rawValue: v)
152153
}
153154

154-
var notifyOnCommandFinishAfter: UInt {
155-
guard let config = self.config else { return 5000 }
155+
var notifyOnCommandFinishAfter: Duration {
156+
guard let config = self.config else { return .seconds(5) }
156157
var v: UInt = 0
157158
let key = "notify-on-command-finish-after"
158159
_ = ghostty_config_get(config, &v, key, UInt(key.lengthOfBytes(using: .utf8)))
159-
return v
160+
return .milliseconds(v)
160161
}
161162

162163
var splitPreserveZoom: SplitPreserveZoom {

0 commit comments

Comments
 (0)