-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.rb
executable file
·175 lines (147 loc) · 3.89 KB
/
rest.rb
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
#!/usr/bin/env ruby
require 'optparse'
require 'socket'
require 'fileutils'
system('logger &')
DUMMY_PATH = '/tmp/rest_dummy'
PID_PATH = '/tmp/rest.pid'
FileUtils.touch(DUMMY_PATH) unless File.exist?(DUMMY_PATH)
FileUtils.touch(PID_PATH) unless File.exist?(PID_PATH)
def get_brightness
`brightness -l`.split.last.to_f
end
def set_brightness(value)
system("brightness #{value}")
end
def dim_screen(dim_duration, brightness)
initial_brightness = get_brightness
current_brightness = initial_brightness
step = 0.01
((initial_brightness - brightness) / step).floor.to_i.times do
set_brightness(current_brightness -= step)
sleep step / 100
end
set_brightness(brightness)
sleep(dim_duration)
((initial_brightness - brightness) / step).floor.to_i.times do
set_brightness(current_brightness += step)
sleep step / 100
end
set_brightness(initial_brightness)
end
def parse_time(str)
if str[-1, 1] == 'm'
str[0..-2].to_i * 60
else
str.to_i
end
end
def start(options)
dim_duration = options[:dim_duration] || 30
sleep_duration = options[:sleep_duration] || 3600
brightness = options[:brightness] || 0.1
inactive = options[:inactive] || 15
run_in_background = options[:run_in_background] || false
if run_in_background
pid = fork do
sleep(sleep_duration)
run_dimmer(dim_duration, sleep_duration, brightness, inactive)
end
if pid
Process.detach(pid)
File.write(PID_PATH, pid)
puts "Dim screen script started in the background with pid #{pid}"
else
puts 'Failed to start dim screen script in the background'
end
else
sleep(sleep_duration)
run_dimmer(dim_duration, sleep_duration, brightness, inactive)
end
end
def run_dimmer(dim_duration, sleep_duration, brightness, inactive)
Signal.trap('TERM') do
FileUtils.rm_f(PID_PATH)
FileUtils.rm_f(DUMMY_PATH)
exit
end
loop do
last_modified = File.mtime(DUMMY_PATH)
if Time.now - last_modified > inactive
dim_screen(dim_duration, brightness)
sleep(sleep_duration)
else
sleep(1)
end
end
end
def stop
if File.exist?(PID_PATH)
pid = File.read(PID_PATH).to_i
Process.kill('TERM', pid)
FileUtils.rm_f(PID_PATH)
FileUtils.rm_f(DUMMY_PATH)
puts 'Dim screen script stopped.'
else
puts 'Dim screen script is not running.'
end
end
options = {}
option_parser = OptionParser.new do |opts|
opts.banner = 'Usage: rest.rb command [options]'
opts.on('-dDIM', '--dim=DIM', 'Set dimming duration') do |d|
d = parse_time(d)
if d <= 0 || d > 300
puts 'Dim duration must be a positive integer not exceeding 300 (5 minutes)'
exit
end
options[:dim_duration] = d
end
opts.on('-sSLEEP', '--sleep=SLEEP', 'Set sleep duration') do |s|
s = parse_time(s)
if s <= 0
puts 'Sleep duration must be a positive integer'
exit
end
options[:sleep_duration] = s
end
opts.on('-bBRIGHTNESS', '--brightness=BRIGHTNESS', 'Set dimming brightness') do |b|
b = b.to_f
if b < 0.0 || b > 1.0
puts 'Brightness must be a float between 0.0 and 1.0'
exit
end
options[:brightness] = b
end
opts.on('-iINACTIVE', '--inactive=INACTIVE', 'Set the duration of inactivity before dimming') do |i|
i = parse_time(i)
if i <= 0
puts 'Inactive duration must be a positive integer'
exit
end
options[:inactive] = i
end
opts.on('-r', '--run-in-background', 'Run in background') do
options[:run_in_background] = true
end
opts.on('-h', '--help', 'Displays Help') do
puts opts
exit
end
end
option_parser.order!
command = ARGV.shift
option_parser.order!
case command
when 'run'
start(options)
when 'stop'
stop
when 'now'
stop
dim_screen(options[:dim_duration] || 30, options[:brightness] || 0.1)
start(options)
else
puts "Invalid command. Please use 'run' to start the script and 'stop' to stop it."
puts option_parser
end