Skip to content

Commit

Permalink
ExtendTitleToSubtitle
Browse files Browse the repository at this point in the history
  • Loading branch information
drgrib committed Jul 20, 2023
1 parent cbcb1a4 commit a0b837c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions alfred.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package alfred
import (
"encoding/json"
. "fmt"
"strings"
)

// Indent specifies the indent string used for the JSON output for String() and Run(). If set to "", no indentation will be used.
Expand Down Expand Up @@ -65,6 +66,23 @@ func Add(item Item) {
Items = append(Items, item)
}

// ExtendTitleToSubtitle extends words of item.Title that cause its string length to exceed maxTitle to item.Subtitle.
func ExtendTitleToSubtitle(item Item, maxTitle int) Item {
if len(item.Title) > maxTitle {
words := strings.Split(item.Title, " ")
titleLen := len(words[0])
titleWords := []string{words[0]}
i := 1
for ; titleLen+1+len(words[i]) < maxTitle; i++ {
titleWords = append(titleWords, words[i])
titleLen += 1 + len(words[i])
}
item.Title = strings.Join(titleWords, " ")
item.Subtitle = strings.Join(words[i:], " ")
}
return item
}

type output struct {
Rerun float64 `json:"rerun,omitempty"`
Variables map[string]string `json:"variables,omitempty"`
Expand Down

0 comments on commit a0b837c

Please sign in to comment.