Class: CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/cli.rb

Class Method Summary collapse

Class Method Details

.runObject



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
# File 'lib/cli.rb', line 9

def self.run

    OptionParser.new do |parser| 
        parser.banner = "Usage: totaltime [options]"

        parser.on("-h", "--help", "Show this help message") do ||
            puts parser
            exit
        end
    end.parse!

    puts "Press [Enter] on an empty line to confirm".light_black
    totalDuration = Duration.new

    lineUp = "\033[1A"
    lineClear = "\033[K"

    prompt = "> ".light_black
    promptOverride = lineUp + "\r" + lineClear + prompt

    begin
        while line = Readline.readline(prompt, true)
            if line.empty?
                print lineUp
                break
            end

            duration = Duration.try_convert(line)
            
            if duration != nil
                totalDuration += duration
                puts promptOverride + duration.to_s.green
            elsif
                invalidText = (line + " [Invalid]").red
                puts promptOverride + invalidText
            end
        end
    rescue Interrupt => e
        puts "[Interrupt]".red
    end

    puts "Total time: ".light_black + totalDuration.to_s.cyan
end