Skip to content

Commit

Permalink
update desktop notiications with icon and optional title
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Aug 6, 2023
1 parent ff852fc commit 0e616bb
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
22 changes: 22 additions & 0 deletions lib/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"os/exec"
"os/signal"
"path"
"sync"
"syscall"

Expand Down Expand Up @@ -94,6 +95,7 @@ func (a *Application) init() {
a.Workflow = a.loadWorkflowFile(a.ConfigFilename)
a.JsEngine = CreateNewJavascriptEngine()
a.cronEngine = cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DiscardLogger)))
a.DownloadApplicationIcon()
}

func (a *Application) hookSignals() {
Expand Down Expand Up @@ -352,6 +354,26 @@ func (a *Application) handleFlagOptions() {
}
}

func (a *Application) GetConfigurationPath() string {
pathname, _ := utils.EnsureConfigDirExists("stackup")

return pathname
}

func (a *Application) DownloadApplicationIcon() {
filename := a.GetApplicationIconPath()

if utils.FileExists(filename) && utils.IsFile(filename) {
return
}

utils.SaveUrlToFile("https://raw.githubusercontent.com/permafrost-dev/stackup/main/assets/stackup-app-512px.png", filename)
}

func (a *Application) GetApplicationIconPath() string {
return path.Join(a.GetConfigurationPath(), "/stackup-icon.png")
}

func (a *Application) Run() {
godotenv.Load()
a.init()
Expand Down
8 changes: 6 additions & 2 deletions lib/app/scriptNotifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func (dn *DesktopNotification) create() *DesktopNotification {
}

func (dn *DesktopNotification) Send() bool {
result := notifications.NewDesktopNotification().
result := notifications.NewDesktopNotification(App.GetApplicationIconPath()).
Send(dn.state.title, dn.state.message)

dn.resetState()
Expand All @@ -72,10 +72,14 @@ func (dn *DesktopNotification) resetState() {
dn.state.message = ""
}

func (dn *DesktopNotification) Message(message string) *DesktopNotification {
func (dn *DesktopNotification) Message(message string, title ...string) *DesktopNotification {
dn.state.message = message
dn.state.title = "notification"

if len(title) > 0 {
dn.state.title = title[0]
}

return dn
}

Expand Down
15 changes: 9 additions & 6 deletions lib/notifications/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
)

type DesktopNotification struct {
IconPath string
}

// NewDesktopNotification creates a new instance of the DesktopNotification
// struct.
func NewDesktopNotification() *DesktopNotification {
return &DesktopNotification{}
func NewDesktopNotification(iconPath string) *DesktopNotification {
return &DesktopNotification{
IconPath: iconPath,
}
}

func (tn *DesktopNotification) Send(title, message string) error {
return beeep.Notify(title, message, "")
func (dn *DesktopNotification) Send(title, message string) error {
beeep.DefaultDuration = 8

return beeep.Notify(title, message, dn.IconPath)
}
25 changes: 25 additions & 0 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ func IsDir(filename string) bool {
return info.IsDir()
}

func FileExists(filename string) bool {
if _, err := os.Stat(filename); err == nil {
return true
}
return false
}

func MatchPattern(s string, pattern string) []string {
regex := regexp.MustCompile(pattern)
if !regex.MatchString(s) {
Expand Down Expand Up @@ -356,3 +363,21 @@ func GetUniqueStrings(items []string) []string {

return uniqueItems
}

func EnsureConfigDirExists(appName string) (string, error) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", err
}

// Append the directory name to the home directory
configDir := filepath.Join(homeDir, "."+appName)

// Ensure the directory exists
err = os.MkdirAll(configDir, 0744)
if err != nil {
return "", err
}

return configDir, nil
}

0 comments on commit 0e616bb

Please sign in to comment.