-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
206 lines (168 loc) · 5.29 KB
/
main.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"os"
"os/exec"
"strconv"
"strings"
"time"
)
// I use the stack of constants for clarity
const (
JSON_PARAM_LENGTH = 2
LEFT_BOX = "╟"
RIGHT_BOX = "╢"
ASCII_RED = "\x1b[31m"
ASCII_LIGHT_BLUE = "\x1b[94m"
ASCII_RESET = "\x1b[0m"
ASCII_CYAN = "\x1b[36m"
EMOJI = "┌( ಠ‿ಠ)┘"
TAG_FETCH_URL = "https://api.github.com/repos/michalspano/wlpr/tags"
)
/* TODO: fade in/out the wallpaper
- currently not supported by osascript;
- linked article: https://discussions.apple.com/thread/250539801 */
func main() {
var SCRIPT_SRC, IMG_DIR string // initial path buffers
var conf map[string]string // buffer to store the map
var showFooter bool = true // show the message by default
/* Optional flags
-nm: display no ending message
-v: display the current version at remote GitHub reposiroty
We parse the optional flags before the execution of the program */
args := os.Args[1:]
if len(args) == 1 {
switch args[0] {
case "-nm", "--no-message":
showFooter = false
case "-v", "--version":
fmt.Println(fetchCurrentVersion())
return
default:
raiseError("Invalid flag")
}
}
HOME, _ := os.UserHomeDir() // get the $HOME directory
confPath := HOME + "/.wlpr.json"
// open the config file
file, err := openFile(confPath)
if err != nil {
raiseError("No config file found, run './init'")
}
// read the config file
data, err := readFile(file)
if err != nil {
raiseError("Error reading config file")
}
// unmarshal the json
json.Unmarshal(data, &conf)
if len(conf) != JSON_PARAM_LENGTH {
raiseError("Config file is invalid, run ./init")
}
// evaluate the JSON parsed paths
IMG_DIR, SCRIPT_SRC = conf["src_path"], conf["root"]+"scripts/"
/* We check whether `IMG_DIR` exits, if not, we warnt the user.
We dont' need to chcek if `SCRIPT_SCR` exits, because it's
been created by the init script. */
if _, err := os.Stat(IMG_DIR); os.IsNotExist(err) {
raiseError("Directory " + IMG_DIR + " does not exist")
}
// get the current wallpaper using osascript
currentWallpaper, err := getCurrentWallpaper(SCRIPT_SRC + "current_wallpaper.scpt")
if err != nil {
raiseError("Error getting current wallpaper")
}
buff := getFiles(IMG_DIR)
rand.Seed(time.Now().UnixNano()) // seed the random generator
// append a random picture from the selected directory
imgPath := IMG_DIR + buff[rand.Intn(len(buff))]
// we would like to change the wallpaper, such that it won't be the current one
for imgPath == currentWallpaper {
imgPath = IMG_DIR + buff[rand.Intn(len(buff))]
}
// set the wallpaper
exec.Command("/bin/bash", "-c", SCRIPT_SRC+"setter.scpt"+" "+imgPath).Run()
// display the footer (indicates success) [default behavior]
if showFooter {
displayFooter()
}
}
// raise a formatted error to the console and abort the program
func raiseError(errorMessage string) {
fmt.Printf("%s%s%s\n", ASCII_RED, errorMessage, ASCII_RESET)
os.Exit(1)
}
// open a file and catch errors
func openFile(path string) (*os.File, error) {
return os.Open(path)
}
func readFile(file *os.File) ([]byte, error) {
return ioutil.ReadAll(file)
}
// get the possible pictures in the directory
func getFiles(path string) []string {
var buff []string
files, _ := ioutil.ReadDir(path)
for _, f := range files {
if f.IsDir() || f.Name()[0] == '.' { // omit dirs and dotfiles
continue
}
buff = append(buff, f.Name())
}
return buff
}
func getCurrentWallpaper(script string) (string, error) {
cmd := exec.Command("/bin/bash", "-c", script)
out, err := cmd.Output()
if err != nil {
return "", err
}
return string(out[:len(out)-1]), nil
}
func getTerminalWidth() (int, error) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, _ := cmd.Output()
// obtain an array with: [width height]
dims := strings.Split(string(out[:len(out)-1]), " ")
// return the height (second element) parsed as an int
return strconv.Atoi(dims[1])
}
func displayFooter() {
// predefined footer buffers
startMsg, endMsg := "Changed successfully!", LEFT_BOX+" wlpr "+RIGHT_BOX
// print the first part of the line
fmt.Printf("%s%s %s%s", ASCII_CYAN, EMOJI, ASCII_RESET, startMsg)
width, err := getTerminalWidth()
if err != nil {
raiseError("Error getting terminal size")
}
// the number of (visual) pixels used by the predefined buffers
stdinLen := len(startMsg) + len(EMOJI) - 10
/* The number of pixels filled with empty spaces, such that the
ending part of the footer will be displayed to the complete right
of the width of the terminal session */
relativeLen := width - stdinLen - len(endMsg) + 2 // +2 extra padding
for i := 0; i < relativeLen; i++ {
fmt.Printf(" ")
}
// display the ending part; add new line
fmt.Printf("%s%s%s\n", ASCII_LIGHT_BLUE, endMsg, ASCII_RESET)
}
// fetch the applicaitons current tag via GitHub
func fetchCurrentVersion() string {
var tags []map[string]string // form: [{string: string}]
response, _ := http.Get(TAG_FETCH_URL)
body, err := ioutil.ReadAll(response.Body)
if err != nil {
raiseError("Error fetching current version")
}
json.Unmarshal(body, &tags) // unmarshal the json
/* we assume that the latest tag is defined at the first index,
given by the key-value "name" in the dictionary */
return tags[0]["name"]
}