-
Notifications
You must be signed in to change notification settings - Fork 0
/
brits.py
executable file
·137 lines (101 loc) · 3.38 KB
/
brits.py
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
#!/usr/bin/env python3
import os
import re
import sys
BACKLIGHT_DIR = "/sys/class/backlight"
_device = os.listdir(BACKLIGHT_DIR)[0]
MAX_BRIGHTNESS = int(
open(os.path.join(BACKLIGHT_DIR, _device, "max_brightness")).read().strip()
)
CURRENT_BRIGHTNESS = int(
open(os.path.join(BACKLIGHT_DIR, _device, "actual_brightness")).read().strip()
)
CURRENT_PERCENTAGE = round((CURRENT_BRIGHTNESS / MAX_BRIGHTNESS) * 100)
BRIGHTNESS_FILE = os.path.join(BACKLIGHT_DIR, _device, "brightness")
def set_raw(value: int):
value = max(min(value, MAX_BRIGHTNESS), 0)
with open(BRIGHTNESS_FILE, "w") as f:
f.write(str(value))
def set_inc_raw(increment: int):
set_raw(CURRENT_BRIGHTNESS + increment)
def set_dec_raw(decrement: int):
set_raw(CURRENT_BRIGHTNESS - decrement)
def set_percentage(percentage: int):
percentage = max(min(percentage, 100), 0)
value = round((percentage / 100) * MAX_BRIGHTNESS)
set_raw(value)
def set_inc_percentage(increment: int):
set_percentage(CURRENT_PERCENTAGE + increment)
def set_dec_percentage(decrement: int):
set_percentage(CURRENT_PERCENTAGE - decrement)
def parse_set_value(value: str) -> list:
match = re.match(r"(\d+)(.{0,2})", value)
value_int = int(match.group(1))
percentage, plus_minus = sorted(list(match.group(2).ljust(2, "&")))
is_percentage = percentage == "%"
is_plus = plus_minus == "+"
is_minus = plus_minus == "-"
return value_int, is_percentage, is_plus, is_minus
def set_handler(value: str):
value_int, is_percentage, is_plus, is_minus = parse_set_value(value)
if not any([is_percentage, is_plus, is_minus]):
set_raw(value_int)
if is_plus:
if is_percentage:
set_inc_percentage(value_int)
else:
set_inc_raw(value_int)
elif is_minus:
if is_percentage:
set_dec_percentage(value_int)
else:
set_dec_raw(value_int)
elif is_percentage:
set_percentage(value_int)
else:
set_raw(value_int)
def print_help():
"""
Function to print help message
"""
help_msg = """
Usage: brits <command> [options]
Commands:
get [p(ercentage)] Get the current brightness as percentage (default).
get r(aw) Get the current brightness as raw value.
set <value> Set the brightness to the specified value.
Possible values:
- 200 (raw value)
- 60% (percentage value)
- 20+ (increment raw value)
- 30- (decrement raw value)
- 5%+ (increment percentage)
- 10%- (decrement percentage)
"""
print(help_msg)
def main():
"""
Main function
Returns:
None
"""
if len(sys.argv) < 2:
print_help()
sys.exit(1)
command = sys.argv[1]
if command == "get":
if len(sys.argv) == 2 or "percentage".startswith(sys.argv[2]):
print(f"{CURRENT_PERCENTAGE}%")
elif "raw".startswith(sys.argv[2]):
print(CURRENT_BRIGHTNESS)
else:
print_help()
elif command == "set":
if len(sys.argv) != 3:
print_help()
sys.exit(1)
set_handler(sys.argv[2])
else:
print_help()
if __name__ == "__main__":
main()