Skip to content

Commit

Permalink
Do not create the icon file at each notification
Browse files Browse the repository at this point in the history
  • Loading branch information
jjanvier committed Feb 23, 2022
1 parent 8ed9516 commit ac8adec
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions handler/notify_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ package handler

import (
"embed"
"errors"
"github.com/faiface/beep"
"github.com/faiface/beep/speaker"
"github.com/faiface/beep/vorbis"
"github.com/gen2brain/beeep"
"io/fs"
"io/ioutil"
"os"
"path/filepath"
"time"
)

const title = "TDD.sh"
const icon = "assets/logo.png"
const localIcon = "tddsh-icon.png"
const bell = "assets/bell.ogg"

// assets present in assets/* are embedded in a local filesystem
Expand All @@ -30,7 +32,8 @@ func (handler NotifyHandler) HandleNotify(delay int, message string) error {
wait(delay)

iconData, _ := Assets.ReadFile(icon)
displayNotification(message, iconData)
localIconFile := getLocalIconFile(iconData)
displayNotification(message, localIconFile)

bellFile, _ := Assets.Open(bell)
playBell(bellFile)
Expand All @@ -42,15 +45,8 @@ func wait(delay int) {
time.Sleep(time.Duration(delay) * time.Second)
}

// beeep.Notify wants a path of file for the icon
// our icon file is embedded in the binary's filesystem, which means we can't access it from TDD.sh's host
// that's why we create a temporary icon file on the host
func displayNotification(message string, iconFileContent []byte) {
tmpIconFile := createTmpIconFile(iconFileContent)
defer tmpIconFile.Close()
defer os.Remove(tmpIconFile.Name())

beeep.Notify(title, message, tmpIconFile.Name())
func displayNotification(message string, icon *os.File) {
beeep.Notify(title, message, icon.Name())
}

// see the tutorial https://github.com/faiface/beep/wiki/Hello,-Beep! to understand how it works
Expand All @@ -70,9 +66,36 @@ func playBell(bell fs.File) {
<-done
}

func createTmpIconFile(iconContent []byte) *os.File {
tmpFile, _ := ioutil.TempFile(os.TempDir(), "tddshicon-")
tmpFile.Write(iconContent)
// beeep.Notify wants a path of file for the icon
// our icon file is embedded in the binary's filesystem, which means we can't access it from TDD.sh's host
// that's why we create an icon file on the host
func getLocalIconFile(iconContent []byte) *os.File {
if shouldCreateLocalIconFile() {
createLocalIconFile(iconContent)
}

file, _ := os.OpenFile(getLocalIconFilePath(), os.O_RDONLY, 0644)

return file
}

func getLocalIconFilePath() string {
path := os.TempDir() + "/" + localIcon
realPath, _ := filepath.Abs(filepath.FromSlash(path))

return realPath
}

func shouldCreateLocalIconFile() bool {
if _, err := os.Stat(getLocalIconFilePath()); errors.Is(err, os.ErrNotExist) {
return true
}

return false
}

return tmpFile
func createLocalIconFile(iconContent []byte) {
file, _ := os.OpenFile(getLocalIconFilePath(), os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
file.Write(iconContent)
defer file.Close()
}

0 comments on commit ac8adec

Please sign in to comment.