Skip to content

Instantly share code, notes, and snippets.

@rfinz
Forked from siers/nap.rb
Created January 12, 2016 22:22
Show Gist options
  • Save rfinz/e87e09240a3187f605cd to your computer and use it in GitHub Desktop.
Save rfinz/e87e09240a3187f605cd to your computer and use it in GitHub Desktop.

Revisions

  1. @siers siers revised this gist Dec 20, 2015. 1 changed file with 11 additions and 5 deletions.
    16 changes: 11 additions & 5 deletions nap.rb
    Original 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"
    "\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)
    gen = proc do |i|
    # 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)).strftime('%H:%M')
    (Time.now + (cycle * i + tts + (j || 0) * 60)).strftime('%H:%M')
    end

    enough = 6
    times = (1..enough).map { |i| gen.call i }

    if n
    unless (1..enough).include? (m = n.to_i)
    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), &block)
    kind_until(gen.call(m, o), &block)
    else
    yield "# according to http://sleepyti.me"
    yield "# you should wake up at:"
  2. @siers siers revised this gist Dec 20, 2015. 1 changed file with 39 additions and 9 deletions.
    48 changes: 39 additions & 9 deletions nap.rb
    Original 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> }" +
    "\ne.g. nap for 3h\n nap until 10AM"
    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])?$/)

    "-s #{ time.to_f.*(prefix).to_i }"
    "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 }"

    "-t #{ secs }"
    "sudo rtcwake -m mem -t #{ secs }"
    end

    kind = ARGV[0]
    timedescr = ARGV[1]
    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

    usage if ARGV.length == 0 || !kind.match(/^(until|for)$/)
    kind_until(gen.call(m), &block)
    else
    yield "# according to http://sleepyti.me"
    yield "# you should wake up at:"
    yield("# " + times.join(', '))
    end
    end

    cmd = send(:"kind_#{ kind }", timedescr) { |info| $stderr.puts info }
    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

    puts "sudo rtcwake -m mem #{ cmd }"
    cmd = send(:"kind_#{ kind }", *timedescr) { |info| $stderr.puts info }
    puts cmd if cmd
  3. @siers siers revised this gist Dec 10, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nap.rb
    100644 → 100755
    Original 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 }:#{ minutes.to_i }"
    "#{ 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 }"
    puts "sudo rtcwake -m mem #{ cmd }"
  4. @siers siers revised this gist Dec 8, 2015. 1 changed file with 0 additions and 29 deletions.
    29 changes: 0 additions & 29 deletions README
    Original file line number Diff line number Diff line change
    @@ -1,29 +0,0 @@
    % # 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’
  5. @siers siers revised this gist Dec 8, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion nap.rb
    Original 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 }"
    puts "sudo rtcwake -m mem #{ cmd }"
  6. @siers siers revised this gist Dec 8, 2015. No changes.
  7. @siers siers revised this gist Dec 8, 2015. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions README
    Original 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’
  8. @siers siers revised this gist Dec 8, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions nap.rb
    Original 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])?$/)
    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 }"
    puts "sudo rtcwake -m mem #{ cmd }"
  9. @siers siers created this gist Dec 8, 2015.
    55 changes: 55 additions & 0 deletions nap.rb
    Original 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 }"