Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xyproto committed Feb 16, 2019
1 parent 8edc26c commit d39153c
Show file tree
Hide file tree
Showing 18 changed files with 42 additions and 1,513 deletions.
28 changes: 16 additions & 12 deletions cmd/settimed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"path/filepath"
"strings"

"github.com/xyproto/gnometimed"
"github.com/xyproto/monitor"
"github.com/xyproto/simpletimed"
)

func exists(path string) bool {
Expand All @@ -29,7 +31,7 @@ func main() {
filename := collectionName
switch filepath.Ext(filename) {
case ".stw":
stw, err := monitor.ParseSTW(filename)
stw, err := simpletimed.ParseSTW(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -38,12 +40,12 @@ func main() {
fmt.Printf("Using: %s\n", stw.Path)
}
// Start endless event loop
if err := stw.EventLoop(verbose); err != nil {
if err := stw.EventLoop(verbose, monitor.SetWallpaperVerbose); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
case ".xml":
gtw, err := monitor.ParseXML(filename)
gtw, err := gnometimed.ParseXML(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
Expand All @@ -52,7 +54,7 @@ func main() {
fmt.Printf("Using: %s\n", gtw.Path)
}
// Start endless event loop
if err := gtw.EventLoop(verbose); err != nil {
if err := gtw.EventLoop(verbose, monitor.SetWallpaperVerbose); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
Expand Down Expand Up @@ -93,24 +95,26 @@ func main() {
os.Exit(1)
}

var eventLoop func(bool) error
if len(simpleTimedWallpapers) == 1 {
stw := simpleTimedWallpapers[0]
if verbose {
fmt.Printf("Using: %s\n", stw.Path)
}
eventLoop = stw.EventLoop
// Start endless event loop
if err := stw.EventLoop(verbose, monitor.SetWallpaperVerbose); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
} else if len(gnomeTimedWallpapers) == 1 {
gtw := gnomeTimedWallpapers[0]
if verbose {
fmt.Printf("Using: %s\n", gtw.Path)
}
eventLoop = gtw.EventLoop
}
// Start endless event loop
if err := eventLoop(verbose); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
// Start endless event loop
if err := gtw.EventLoop(verbose, monitor.SetWallpaperVerbose); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

}
4 changes: 2 additions & 2 deletions cmd/xml2stw/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/xyproto/monitor"
"github.com/xyproto/gnometimed"
)

func main() {
Expand All @@ -14,7 +14,7 @@ func main() {
}
filename := os.Args[1]

s, err := monitor.GnomeFileToSimpleString(filename)
s, err := gnometimed.GnomeFileToSimpleString(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s\n", err)
os.Exit(1)
Expand Down
44 changes: 23 additions & 21 deletions collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package monitor

import (
"github.com/stretchr/powerwalk"
"github.com/xyproto/gnometimed"
"github.com/xyproto/simpletimed"
"image/jpeg"
"image/png"
"os"
Expand All @@ -23,12 +25,12 @@ var (
)

type SearchResults struct {
wallpapers sync.Map // stores the full path -> *Wallpaper struct, for png + jpeg files
gnomeWallpapers sync.Map // stores the full path -> *GnomeTimedWallpaper struct, for xml files
simpleTimedWallpapers sync.Map // stores the full path -> *SimpleTimedWallpaper struct, for stw files
sortedWallpapers []*Wallpaper // holds sorted wallpapers
sortedGnomeTimedWallpapers []*GnomeTimedWallpaper // holds sorted Gnome Timed Wallpapers
sortedSimpleTimedWallpapers []*SimpleTimedWallpaper // holds sorted Simple Timed Wallpapers
wallpapers sync.Map // stores the full path -> *Wallpaper struct, for png + jpeg files
gnomeWallpapers sync.Map // stores the full path -> *gnometimed.Wallpaper struct, for xml files
simpleTimedWallpapers sync.Map // stores the full path -> *simpletimed.Wallpaper struct, for stw files
sortedWallpapers []*Wallpaper // holds sorted wallpapers
sortedGnomeTimedWallpapers []*gnometimed.Wallpaper // holds sorted Gnome Timed Wallpapers
sortedSimpleTimedWallpapers []*simpletimed.Wallpaper // holds sorted Simple Timed Wallpapers
}

var (
Expand All @@ -47,8 +49,8 @@ func NewSearchResults() *SearchResults {
gnomeWallpapers: sync.Map{},
simpleTimedWallpapers: sync.Map{},
sortedWallpapers: []*Wallpaper{},
sortedGnomeTimedWallpapers: []*GnomeTimedWallpaper{},
sortedSimpleTimedWallpapers: []*SimpleTimedWallpaper{},
sortedGnomeTimedWallpapers: []*gnometimed.Wallpaper{},
sortedSimpleTimedWallpapers: []*simpletimed.Wallpaper{},
}
}

Expand Down Expand Up @@ -134,13 +136,13 @@ func (sr *SearchResults) visit(path string, f os.FileInfo, err error) error {
// TODO: Consider supporting XPM and/or XBM wallpapers in the future
return nil
case ".stw": // Simple Timed Wallpaper
stw, err := ParseSTW(path)
stw, err := simpletimed.ParseSTW(path)
if err != nil {
return err
}
sr.simpleTimedWallpapers.Store(path, stw)
case ".xml":
gw, err := ParseXML(path)
gw, err := gnometimed.ParseXML(path)
if err != nil {
return err
}
Expand Down Expand Up @@ -173,9 +175,9 @@ func (sr *SearchResults) sortWallpapers() {

// sortGnomeTimedWallpapers sorts the Found gnome Timed Wallpapers
func (sr *SearchResults) sortGnomeTimedWallpapers() {
var collected []*GnomeTimedWallpaper
var collected []*gnometimed.Wallpaper
sr.gnomeWallpapers.Range(func(_, value interface{}) bool {
gw, ok := value.(*GnomeTimedWallpaper)
gw, ok := value.(*gnometimed.Wallpaper)
if !ok {
// internal error
panic("a value in the gnomeWallpapers map is not a pointer to a GnomeTimedWallpaper struct")
Expand All @@ -192,12 +194,12 @@ func (sr *SearchResults) sortGnomeTimedWallpapers() {

// sortSimpleTimedWallpapers sorts the found Simple Timed Wallpapers
func (sr *SearchResults) sortSimpleTimedWallpapers() {
var collected []*SimpleTimedWallpaper
var collected []*simpletimed.Wallpaper
sr.simpleTimedWallpapers.Range(func(_, value interface{}) bool {
stw, ok := value.(*SimpleTimedWallpaper)
stw, ok := value.(*simpletimed.Wallpaper)
if !ok {
// internal error
panic("a value in the simpleTimedWallpapers map is not a pointer to a SimpleTimedWallpaper struct")
panic("a value in the simpleTimedWallpapers map is not a pointer to a simpletimed.Wallpaper struct")
}
collected = append(collected, stw)
return true
Expand Down Expand Up @@ -247,12 +249,12 @@ func (sr *SearchResults) Wallpapers() []*Wallpaper {
}

// GnomeTimedWallpapers returns a sorted slice of all found gnome timed wallpapers
func (sr *SearchResults) GnomeTimedWallpapers() []*GnomeTimedWallpaper {
func (sr *SearchResults) GnomeTimedWallpapers() []*gnometimed.Wallpaper {
return sr.sortedGnomeTimedWallpapers
}

// SimpleTimedWallpapers returns a sorted slice of all found simple timed wallpapers
func (sr *SearchResults) SimpleTimedWallpapers() []*SimpleTimedWallpaper {
func (sr *SearchResults) SimpleTimedWallpapers() []*simpletimed.Wallpaper {
return sr.sortedSimpleTimedWallpapers
}

Expand All @@ -268,8 +270,8 @@ func (sr *SearchResults) WallpapersByName(name string) []*Wallpaper {
}

// GnomeTimedWallpapersByName will return gnome timed wallpapers that match with the collection name
func (sr *SearchResults) GnomeTimedWallpapersByName(name string) []*GnomeTimedWallpaper {
var collection []*GnomeTimedWallpaper
func (sr *SearchResults) GnomeTimedWallpapersByName(name string) []*gnometimed.Wallpaper {
var collection []*gnometimed.Wallpaper
for _, gw := range sr.sortedGnomeTimedWallpapers {
if gw.Name == name {
collection = append(collection, gw)
Expand All @@ -279,8 +281,8 @@ func (sr *SearchResults) GnomeTimedWallpapersByName(name string) []*GnomeTimedWa
}

// SimpleTimedWallpapersByName will return simple timed wallpapers that match with the collection name
func (sr *SearchResults) SimpleTimedWallpapersByName(name string) []*SimpleTimedWallpaper {
var collection []*SimpleTimedWallpaper
func (sr *SearchResults) SimpleTimedWallpapersByName(name string) []*simpletimed.Wallpaper {
var collection []*simpletimed.Wallpaper
for _, stw := range sr.sortedSimpleTimedWallpapers {
if stw.Name == name {
collection = append(collection, stw)
Expand Down
114 changes: 0 additions & 114 deletions convert.go

This file was deleted.

63 changes: 0 additions & 63 deletions gnometimed.go

This file was deleted.

Loading

0 comments on commit d39153c

Please sign in to comment.