From bf040f6d1c8c7bc33d5268db42bc2ac9016e2198 Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 24 Mar 2020 18:35:27 +0530 Subject: Add background package This rewrite has better comments and returns better error messages. --- background/download.go | 48 +++++++++++++++++++++++++++++++++++ background/set_darwin.go | 25 ++++++++++++++++++ background/set_unix.go | 66 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 139 insertions(+) create mode 100644 background/download.go create mode 100644 background/set_darwin.go create mode 100644 background/set_unix.go diff --git a/background/download.go b/background/download.go new file mode 100644 index 0000000..50f22c5 --- /dev/null +++ b/background/download.go @@ -0,0 +1,48 @@ +package background + +import ( + "fmt" + "io" + "net/http" + "os" +) + +// Download takes path and url as input and downloads the data to a +// file, returning an error if there is one. +func Download(file string, url string) error { + o, err := os.Create(file) + if err != nil { + err = fmt.Errorf("%s%s\n%s", + "download.go: failed to create file: ", file, + err.Error()) + return err + } + defer o.Close() + + res, err := http.Get(url) + if err != nil { + err = fmt.Errorf("%s%s\n%s", + "download.go: failed to get response from ", url, + err.Error()) + return err + } + defer res.Body.Close() + + // Return an error on unexpected response code. + if res.StatusCode != http.StatusOK { + err = fmt.Errorf("Unexpected Response: %s", + res.Status) + return err + } + + // This will not copy everything to memory but will save to + // disk as it progresses, ideal for big files or low memory + // environments. + _, err = io.Copy(o, res.Body) + if err != nil { + err = fmt.Errorf("%s\n%s", + "download.go: failed to copy body to file", + err.Error()) + } + return err +} diff --git a/background/set_darwin.go b/background/set_darwin.go new file mode 100644 index 0000000..43e86b7 --- /dev/null +++ b/background/set_darwin.go @@ -0,0 +1,25 @@ +// +build darwin + +package background + +import ( + "fmt" + "os/exec" + "strconv" +) + +// SetFromFile takes a string as an input, it must be absolute path to +// the background. Checks are not made to check if the path exists or +// it is actually an image, that must be verified before passing it to +// SetFromFile. SetFromFile will exit returning in error if there is +// any. +func SetFromFile(path string) error { + err := exec.Command("osascript", "-e", + `tell application "System Events" to tell every desktop to set picture to `+strconv.Quote(path)).Run() + if err != nil { + err = fmt.Errorf("%s\n%s", + "set_darwin.go: failed to set background", + err.Error()) + } + return err +} diff --git a/background/set_unix.go b/background/set_unix.go new file mode 100644 index 0000000..e782a92 --- /dev/null +++ b/background/set_unix.go @@ -0,0 +1,66 @@ +// +build linux netbsd openbsd freebsd dragonfly + +package background + +import ( + "fmt" + "os" + "os/exec" +) + +// SetFromFile takes a string as an input, it must be absolute path to +// the background. Checks are not made to check if the path exists or +// it is actually an image, that must be verified before passing it to +// SetFromFile. SetFromFile will exit returning in error if there is +// any. +func SetFromFile(path string) error { + var err error + switch os.Getenv("XDG_CURRENT_DESKTOP") { + case "GNOME", "Unity", "Pantheon": + // GNOME, Unity & Pantheon support setting background + // from gsettings & have the same key. + + // gsettings takes path in format of a uri + path = fmt.Sprintf("%s%s", "file://", path) + + err = exec.Command("gsettings", + "set org.gnome.desktop.background picture-uri", path).Run() + if err != nil { + err = fmt.Errorf("%s\n%s", + "set_unix.go: failed to set background with gsettings", + err.Error()) + } + return err + + case "LXDE": + // Background on LXDE can be set with pcmanfm (default + // file manager). + err = exec.Command("pcmanfm", "-w", path).Run() + if err != nil { + err = fmt.Errorf("%s\n%s", + "set_unix.go: failed to set background with pcmanfm", + err.Error()) + } + return err + + default: + // If WM/DE doesn't have a case then feh is used to + // set the background. This is tested to work on WMs + // similar to i3wm. + feh, err := exec.LookPath("feh") + if err != nil { + err = fmt.Errorf("%s\n%s", + "set_unix.go: feh not found in $PATH", + err.Error()) + return err + } + + err = exec.Command(feh, "--bg-fill", path).Run() + if err != nil { + err = fmt.Errorf("%s\n%s", + "set_unix.go: failed to set background with feh", + err.Error()) + } + return err + } +} -- cgit 1.4.1-2-gfad0