-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
170 lines (130 loc) · 3.1 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
package main
import (
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
"github.com/urfave/cli"
)
var brightness = "/sys/class/backlight/acpi_video0/brightness"
var maxbrightness = "/sys/class/backlight/acpi_video0/max_brightness"
var (
err error
percentage int
max int
value int
br int
)
//IsRoot checks if user is running command as root
func IsRoot() bool {
return os.Geteuid() == 0
}
//AdjustBrightness limited to values between 40 and 1400
func AdjustBrightness(origin, modification int) int {
switch {
case origin+modification < 1:
return 1
case origin+modification > 15:
return 15
default:
return origin + modification
}
}
//Inc function opens brightness file, reads, and increments by 50, if possible
func Inc(c *cli.Context) error {
br, err = ReadFile(brightness)
if err != nil {
return err
}
return WriteFile(AdjustBrightness(br, 1))
}
//Dec function opens brightness file, reads, and decrements by 50, if possible
func Dec(c *cli.Context) error {
br, err = ReadFile(brightness)
if err != nil {
return err
}
return WriteFile(AdjustBrightness(br, -1))
}
//Set write to brightness file a value corresponding to the percentage given by the user
func Set(c *cli.Context) error {
if c.NArg() > 0 {
arg := c.Args()[0]
percentage, err = strconv.Atoi(arg)
if err != nil {
fmt.Println("Percentage has to be an integer")
return fmt.Errorf("Percentage has to be an integer")
}
switch {
case percentage < 10 || percentage > 100:
fmt.Println("Percentage has to be between 20 and 100")
return fmt.Errorf("Percentage has to be between 20 and 100")
default:
max, err = ReadFile(maxbrightness)
if err != nil {
fmt.Println("failed to find max brightness")
return fmt.Errorf("failed to find max brightness")
}
value = ((max - 1) * percentage) / 100
}
}
return WriteFile(value)
}
//ReadFile reads a file containing only integers
func ReadFile(filename string) (int, error) {
content, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Println("error")
return -1, err
}
return strconv.Atoi(strings.TrimSpace(string(content)))
}
//WriteFile writes an integer to brightness file
func WriteFile(number int) error {
f, err := os.OpenFile(brightness, os.O_WRONLY|os.O_TRUNC, 0444)
if err != nil {
return err
}
n, err := f.Write([]byte(strconv.Itoa(number)))
if err == nil && n < len(([]byte(string(number)))) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}
func main() {
if !IsRoot() {
fmt.Println("Got root?")
os.Exit(1)
}
app := cli.NewApp()
app.Name = "i-brightness"
app.Usage = "change screen brightness!"
app.Commands = []cli.Command{
{
Name: "set",
Aliases: []string{"s"},
Usage: "set brightness manually",
Action: Set,
},
{
Name: "inc",
Aliases: []string{"i"},
Usage: "increase brightness",
Action: Inc,
},
{
Name: "dec",
Aliases: []string{"d"},
Usage: "decrease brightness",
Action: Dec,
},
}
app.Version = "0.0.1"
app.Author = "Seomis : psimoes@campus.fct.unl.pt"
app.Run(os.Args)
}