Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
Start supporting more specific window title detection (#75)
Browse files Browse the repository at this point in the history
* Add default set of assumptions about window titles

This will leave the special cases for Google Chrome and Slack, but start getting granularity for free on windows that follow the general convention of ` - ` to separate out the application name at the end of the window title.

* Clean up name identification to make it similar for both directions

This will make it very easy to continue adding special cases for other applications

* Add support for Microsoft Edge
  • Loading branch information
Olreich authored and beyang committed Sep 12, 2016
1 parent 2f65f8e commit b19b30d
Showing 1 changed file with 44 additions and 13 deletions.
57 changes: 44 additions & 13 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,32 +155,63 @@ func (w *Window) IsOnDesktop(desktop int64) bool {
return w.IsSticky() || w.Desktop == desktop
}

const (
defaultWindowTitleSeparator = " - "
microsoftEdgeWindowTitleSeparator = "\u200e- "
)

// Info returns more structured metadata about a window. The metadata
// is extracted using heuristics.
//
// Assumptions:
// 1) Most windows use " - " to separate their window names from their content
// 2) Most windows use the " - " with the application name at the end.
// 3) The few programs that reverse this convention only reverse it.
func (w *Window) Info() *Winfo {
fields := strings.Split(w.Name, " - ")
first := strings.TrimSpace(fields[0])
last := strings.TrimSpace(fields[len(fields)-1])
if last == "Google Chrome" {
if len(fields) > 1 {
// Special Cases
fields := strings.Split(w.Name, defaultWindowTitleSeparator)
if len(fields) > 1 {
last := strings.TrimSpace(fields[len(fields)-1])
if last == "Google Chrome" {
return &Winfo{
App: "Google Chrome",
SubApp: strings.TrimSpace(fields[len(fields)-2]),
Title: strings.Join(fields[0:len(fields)-2], " - "),
Title: strings.Join(fields[0:len(fields)-2], defaultWindowTitleSeparator),
}
} else {
}
}

if strings.Contains(w.Name, microsoftEdgeWindowTitleSeparator) {
// App Name Last
beforeSep := strings.LastIndex(w.Name, microsoftEdgeWindowTitleSeparator)
afterSep := beforeSep + len(microsoftEdgeWindowTitleSeparator)
return &Winfo{
App: strings.TrimSpace(w.Name[afterSep:]),
Title: strings.TrimSpace(w.Name[:beforeSep]),
}
}

// Normal Cases
if beforeSep := strings.Index(w.Name, defaultWindowTitleSeparator); beforeSep > -1 {
// App Name First
if w.Name[:beforeSep] == "Slack" {
afterSep := beforeSep + len(defaultWindowTitleSeparator)
return &Winfo{
App: "Google Chrome",
SubApp: "",
Title: "",
App: strings.TrimSpace(w.Name[:beforeSep]),
Title: strings.TrimSpace(w.Name[afterSep:]),
}
}
} else if first == "Slack" {

// App Name Last
beforeSep := strings.LastIndex(w.Name, defaultWindowTitleSeparator)
afterSep := beforeSep + len(defaultWindowTitleSeparator)
return &Winfo{
App: "Slack",
SubApp: strings.TrimSpace(strings.Join(fields[1:], " - ")),
App: strings.TrimSpace(w.Name[afterSep:]),
Title: strings.TrimSpace(w.Name[:beforeSep]),
}
}

// No Application name separator
return &Winfo{
Title: w.Name,
}
Expand Down

0 comments on commit b19b30d

Please sign in to comment.