class Builtins::Countdown

Constants

USAGE_STRING

Public Class Methods

description() click to toggle source
# File lib/builtins/countdown.rb, line 13
def description
        "Like `sleep`, but displays time remaining in terminal."
end

Public Instance Methods

run() click to toggle source
# File lib/builtins/countdown.rb, line 18
def run
        check_args

        timer_task.execute

        while timer_task.running?
                sleep(1)
                timer_task.shutdown if task_complete?
        end
        Output.out("\rCountdown complete after #{sleep_seconds}s.")
end

Private Instance Methods

check_arg_count() click to toggle source
# File lib/builtins/countdown.rb, line 37
def check_arg_count
        raise Builtin::ArgumentError, USAGE_STRING unless args.length == 1
end
check_arg_is_positive_int() click to toggle source
# File lib/builtins/countdown.rb, line 41
def check_arg_is_positive_int
        raise Builtin::ArgumentError, USAGE_STRING unless sleep_seconds.positive?
# raised when the arg is not an int
rescue ::ArgumentError
        raise Builtin::ArgumentError, USAGE_STRING
end
check_args() click to toggle source
# File lib/builtins/countdown.rb, line 32
def check_args
        check_arg_count
        check_arg_is_positive_int
end
seconds_left() click to toggle source
# File lib/builtins/countdown.rb, line 70
def seconds_left
        Integer(task_end_time - Time.now + 1)
end
sleep_seconds() click to toggle source
# File lib/builtins/countdown.rb, line 54
def sleep_seconds
        Integer(args.first)
end
task_complete?() click to toggle source
# File lib/builtins/countdown.rb, line 66
def task_complete?
        Time.now > task_end_time
end
task_end_time() click to toggle source
# File lib/builtins/countdown.rb, line 62
def task_end_time
        @task_end_time ||= task_start_time + sleep_seconds
end
task_start_time() click to toggle source
# File lib/builtins/countdown.rb, line 58
def task_start_time
        @task_start_time ||= Time.now
end
timer_task() click to toggle source
# File lib/builtins/countdown.rb, line 48
def timer_task
        @timer_task ||= Concurrent::TimerTask.new(run_now: true, execution_interval: 1) do
                Output.print("\r      \r#{seconds_left}")
        end
end