-
-
Save rfinz/e87e09240a3187f605cd to your computer and use it in GitHub Desktop.
Revisions
-
siers revised this gist
Dec 20, 2015 . 1 changed file with 11 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,8 @@ def die(str) def usage die "Usage: nap { for <float>[<h|m|s>] | until <date(1) parsable datestr> | cycles [<int>] }" + "\ne.g. nap for 3h\n nap until 10AM\n nap cycles 4\n " + "nap cycles 4+10 # 10 minutes to brush teeth" end def hourminutes(hours) @@ -48,21 +49,26 @@ def kind_until(time) end def kind_cycles(n = nil, &block) # Print time, if n = nil. # n = '1' -> sleep one cycle + 14 minutes # n = '1+20' -> sleep one cylce + 14 minutes + 20 minutes to brush teeth gen = proc do |i, j| cycle = 3600 * 3 / 2 tts = 14 * 60 # a joke on Time To Live (Time.now + (cycle * i + tts + (j || 0) * 60)).strftime('%H:%M') end enough = 6 times = (1..enough).map { |i| gen.call i } if n m, o = n.split('+').map(&:to_i) unless (1..enough).include? m yield "# only cycles 1 through #{ enough } are meant to be used" end kind_until(gen.call(m, o), &block) else yield "# according to http://sleepyti.me" yield "# you should wake up at:" -
siers revised this gist
Dec 20, 2015 . 1 changed file with 39 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,13 +1,15 @@ #!/usr/bin/env ruby require 'time' def die(str) $stderr.puts str exit end def usage die "Usage: nap { for <float>[<h|m|s>] | until <date(1) parsable datestr> | cycles [<int>] }" + "\ne.g. nap for 3h\n nap until 10AM\n nap cycles 4" end def hourminutes(hours) @@ -23,7 +25,7 @@ def kind_for(time) yield "# date format bogus" unless time.match(/^\d+(\.\d+)?([hms])?$/) "sudo rtcwake -m mem -t -s #{ time.to_f.*(prefix).to_i }" end def successfully(args) @@ -42,14 +44,42 @@ def kind_until(time) yield "# %s hours" % hourminutes(hours) if (0.40..60).include? hours yield "# #{ date }" "sudo rtcwake -m mem -t #{ secs }" end def kind_cycles(n = nil, &block) gen = proc do |i| cycle = 3600 * 3 / 2 tts = 14 * 60 # a joke on Time To Live (Time.now + (cycle * i + tts)).strftime('%H:%M') end enough = 6 times = (1..enough).map { |i| gen.call i } if n unless (1..enough).include? (m = n.to_i) yield "# only cycles 1 through #{ enough } are meant to be used" end kind_until(gen.call(m), &block) else yield "# according to http://sleepyti.me" yield "# you should wake up at:" yield("# " + times.join(', ')) end end kind = ARGV[0] timedescr = ARGV[1..-1] if ARGV.length == 2 usage if !kind.match(/^(until|for|cycles)$/) elsif ARGV.length == 1 usage if kind != 'cycles' else usage end cmd = send(:"kind_#{ kind }", *timedescr) { |info| $stderr.puts info } puts cmd if cmd -
siers revised this gist
Dec 10, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ def hourminutes(hours) wholes = hours.to_i minutes = (hours - hours.to_i) * 60 "#{ wholes }:#{ ("0" + minutes.to_i.to_s)[-2..-1] }" end def kind_for(time) @@ -52,4 +52,4 @@ def kind_until(time) cmd = send(:"kind_#{ kind }", timedescr) { |info| $stderr.puts info } puts "sudo rtcwake -m mem #{ cmd }" -
siers revised this gist
Dec 8, 2015 . 1 changed file with 0 additions and 29 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,29 +0,0 @@ -
siers revised this gist
Dec 8, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -52,4 +52,4 @@ def kind_until(time) cmd = send(:"kind_#{ kind }", timedescr) { |info| $stderr.puts info } puts "sudo rtcwake -m mem #{ cmd }" -
siers revised this gist
Dec 8, 2015 . No changes.There are no files selected for viewing
-
siers revised this gist
Dec 8, 2015 . 1 changed file with 29 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,29 @@ % # My typical usecase: % nap until 10am; mpc toggle % nap Usage: nap { for <float>[<h|m|s>] | until <date(1) parsable datestr> } e.g. nap for 3h nap until 10AM % nap for 5.3m sudo rtcwake -m mem -s 318 % date Tue Dec 8 14:30:45 EET 2015 % nap until '10am' # Tue Dec 8 10:00:00 EET 2015 sudo rtcwake -m mem -t 1449561600 % # Error handling: % nap for '5.5.m' # date format bogus sudo rtcwake -m mem -s 330 % nap until 'gibberish' date: invalid date ‘gibberish’ -
siers revised this gist
Dec 8, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -21,7 +21,7 @@ def kind_for(time) aliases = {'m' => 60, 'h' => 3600, 's' => 1} prefix = aliases[(time.match('[hms]$') || ['s'])[0]] yield "# date format bogus" unless time.match(/^\d+(\.\d+)?([hms])?$/) "-s #{ time.to_f.*(prefix).to_i }" end @@ -52,4 +52,4 @@ def kind_until(time) cmd = send(:"kind_#{ kind }", timedescr) { |info| $stderr.puts info } puts "sudo rtcwake -m mem #{ cmd }" -
siers created this gist
Dec 8, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ #!/usr/bin/env ruby def die(str) $stderr.puts str exit end def usage die "Usage: nap { for <float>[<h|m|s>] | until <date(1) parsable datestr> }" + "\ne.g. nap for 3h\n nap until 10AM" end def hourminutes(hours) wholes = hours.to_i minutes = (hours - hours.to_i) * 60 "#{ wholes }:#{ minutes.to_i }" end def kind_for(time) aliases = {'m' => 60, 'h' => 3600, 's' => 1} prefix = aliases[(time.match('[hms]$') || ['s'])[0]] yield "# date format bogus" unless time.match(/^\d+(\.d+)?([hms])?$/) "-s #{ time.to_f.*(prefix).to_i }" end def successfully(args) errors, pipe = IO.pipe IO.popen(args, err: pipe) do |p| p.read end.tap do pipe.close die errors.read unless $?.success? end end def kind_until(time) date = successfully(%w{date -d} + [time]) secs = successfully(%w{date -d} + [time] + %w{+%s}).to_i hours = (((secs.to_i - Time.now.to_i) / 36) / 100.0) yield "# %s hours" % hourminutes(hours) if (0.40..60).include? hours yield "# #{ date }" "-t #{ secs }" end kind = ARGV[0] timedescr = ARGV[1] usage if ARGV.length == 0 || !kind.match(/^(until|for)$/) cmd = send(:"kind_#{ kind }", timedescr) { |info| $stderr.puts info } puts "sudo rtcwake -m mem #{ cmd }"