Skip to content

Commit

Permalink
Detect project from Slack as workspace name
Browse files Browse the repository at this point in the history
  • Loading branch information
alanhamlett committed Aug 17, 2024
1 parent fcafadd commit 02c8029
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions WakaTime/Watchers/MonitoredApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ enum MonitoredApp: String, CaseIterable {
func project(for element: AXUIElement) -> String? {
// TODO: detect repo from GitHub Desktop Client if possible
switch self {
case .slack:
return extractSuffix(element.rawTitle, separator: " - ", offset: 1)
case .zed:
return extractSuffix(element.rawTitle, separator: "")
default:
Expand Down Expand Up @@ -339,11 +341,20 @@ enum MonitoredApp: String, CaseIterable {
return nil
}

private func extractSuffix(_ str: String?, separator: String) -> String? {
private func extractSuffix(_ str: String?, separator: String, offset: Int = 0) -> String? {
guard let str = str else { return nil }

let parts = str.components(separatedBy: separator)
var parts = str.components(separatedBy: separator)
guard !parts.isEmpty else { return nil }
guard parts.count > 1 else { return nil }

var i = offset
while i > 0 {
guard parts.count > 1 else { return nil }

parts.removeLast()
i += 1
}
guard let item = parts.last else { return nil }

if item.trimmingCharacters(in: .whitespacesAndNewlines) != "" {
Expand Down

0 comments on commit 02c8029

Please sign in to comment.