forked from xyproto/wallutils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
res.go
101 lines (90 loc) · 2.37 KB
/
res.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package wallutils
import (
"errors"
"fmt"
"path/filepath"
"strconv"
"strings"
)
// Res is a structure containing width and height
type Res struct {
w, h uint
}
// NewRes creates a new resolution structure
func NewRes(w, h uint) *Res {
return &Res{w, h}
}
func (r *Res) String() string {
return fmt.Sprintf("%dx%d", r.w, r.h)
}
// W is the width
func (r *Res) W() uint {
return r.w
}
// H is the height
func (r *Res) H() uint {
return r.h
}
// Distance returns the distance between two resolutions (Euclidean distance)
func Distance(a, b *Res) int {
return abs(int(b.w)-int(a.w)) + abs(int(b.h)-int(a.h))
}
// AverageResolution returns the average resolution for all connected monitors.
func AverageResolution() (*Res, error) {
monitors, err := Monitors()
if err != nil {
return nil, err
}
var ws, hs uint
for _, mon := range monitors {
ws += mon.Width
hs += mon.Height
}
ws /= uint(len(monitors))
hs /= uint(len(monitors))
return NewRes(ws, hs), nil
}
// Parses a string on the form "1234x1234"
func ParseSize(widthHeight string) (uint, uint, error) {
fields := strings.SplitN(strings.ToLower(widthHeight), "x", 2)
w, err := strconv.Atoi(fields[0])
if err != nil {
return 0, 0, err
}
h, err := strconv.Atoi(fields[1])
if err != nil {
return 0, 0, err
}
return uint(w), uint(h), nil
}
// FilenameToRes extracts width and height from a filename on the form: "asdf_123x123.xyz",
// or filenames that are just on the form "123x123.xyz".
func FilenameToRes(filename string) (*Res, error) {
size := firstname(filepath.Base(filename))
if strings.Contains(size, "_") {
parts := strings.Split(size, "_")
size = parts[len(parts)-1]
}
if !strings.Contains(size, "x") {
return nil, errors.New("does not contain width x height: " + filename)
}
width, height, err := ParseSize(size)
if err != nil {
return nil, fmt.Errorf("does not contain width x height: %s: %s", filename, err)
}
return &Res{uint(width), uint(height)}, nil
}
// ExtractResolutions extracts Res structs from a slice of filenames
// All the filenames must be on the form *_WIDTHxHEIGHT.ext,
// where WIDTH and HEIGHT are numbers.
func ExtractResolutions(filenames []string) ([]*Res, error) {
var resolutions []*Res
for _, filename := range filenames {
res, err := FilenameToRes(filename)
if err != nil {
return resolutions, err
}
resolutions = append(resolutions, res)
}
return resolutions, nil
}