Skip to content

Commit

Permalink
Add inactive option
Browse files Browse the repository at this point in the history
  • Loading branch information
Vagab committed May 29, 2023
1 parent 8ffe640 commit 11a1238
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ def start(options)
dim_duration = options[:dim_duration] || 10
sleep_duration = options[:sleep_duration] || 3600
brightness = options[:brightness] || 0.1
inactive = options[:inactive] || 30
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)
run_dimmer(dim_duration, sleep_duration, brightness, inactive)
end

if pid
Expand All @@ -69,11 +70,11 @@ def start(options)
end
else
sleep(sleep_duration)
run_dimmer(dim_duration, sleep_duration, brightness)
run_dimmer(dim_duration, sleep_duration, brightness, inactive)
end
end

def run_dimmer(dim_duration, sleep_duration, brightness)
def run_dimmer(dim_duration, sleep_duration, brightness, inactive)
Signal.trap('TERM') do
FileUtils.rm_f(PID_PATH)
FileUtils.rm_f(DUMMY_PATH)
Expand All @@ -83,7 +84,7 @@ def run_dimmer(dim_duration, sleep_duration, brightness)
loop do
last_modified = File.mtime(DUMMY_PATH)

if Time.now - last_modified > 300
if Time.now - last_modified > inactive
dim_screen(dim_duration, brightness)
sleep(sleep_duration)
else
Expand Down Expand Up @@ -136,6 +137,15 @@ def stop
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
Expand Down

0 comments on commit 11a1238

Please sign in to comment.