Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect project from Slack as workspace name #290

Merged
merged 1 commit into from
Aug 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading