Class: Tsk::Main

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

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



7
8
9
10
11
12
# File 'lib/tsk.rb', line 7

def initialize
    @start = "START"
    @stop = "STOP"
    @log_file = File.expand_path('../../storage/log', __FILE__)
    @log = load_log
end

Instance Method Details

#get_last_taskObject



67
68
69
70
71
72
73
# File 'lib/tsk.rb', line 67

def get_last_task
    if @log.length > 0
        @log[@log.keys.last][-1]
    else
        false
    end
end

#helpObject



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/tsk.rb', line 104

def help
    puts "Usage: tsk [start[=<name>]|stop|status|report|clear|help]"
    puts
    puts "\tstart:\tStart a task."
    puts "\tstop:\tStop the currently running task."
    puts "\tstatus:\tDisplay the current task,  if any."
    puts "\treport:\tShows a list of all tasks and time taken for each."
    puts "\tclear:\tClear out the log file. Use with caution."
    puts "\thelp:\tDisplay this help text. Meta."
    puts
    puts "Example:\n\ttsk start \"Doing something\""
end

#load_logObject



117
118
119
# File 'lib/tsk.rb', line 117

def load_log
    YAML::load_file(@log_file) || Hash.new
end

#report(pretty = false) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tsk.rb', line 83

def report pretty=false
    @log.keys.each do |date|
        puts date
        last = {}
        @log[date].each do |entry|
            if entry[:action] == @start
                last = entry
            elsif entry[:action] == @stop
                d = date.split('-')
                t = last[:time].split(':')
                start_t = Time.new(d[0], d[1], d[2], t[0], t[1], t[2]).to_i
                t = entry[:time].split(':')
                end_t = Time.new(d[0], d[1], d[2], t[0], t[1], t[2]).to_i
                puts "\t" + last[:name]
                puts "\t" + NumberHelper.duration(end_t - start_t)
                puts
            end
        end
    end
end

#run(args) ⇒ Object



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

def run args
    @args = args
    case @args[0]
    when "start", "sta"
        start @args[1]
    when "stop", "sto"
        stop
    when "status", "st"
        status
    when "report", "r"
        do_pretty = @args[1] == "--pretty" || @args[1] == "-p"
        report do_pretty
    when "clear"
        puts "This will remove all logged time."
        print "Are you sure you want to do this? "
        print "(anything but \"yes\" will cancel) "
        yes_no = $stdin.gets.chomp
        if yes_no == 'yes'
            print "Wiping log... "
            File.open(@log_file, 'w+') { |f| f.write('') }
            print "Done.\n"
        else
            puts "Your log was not wiped."
        end
    else
        help
    end
end

#start(name) ⇒ Object



43
44
45
46
47
48
# File 'lib/tsk.rb', line 43

def start name
    name ||= "Unnamed task"
    stop true
    puts "Starting task: #{name}"
    write_to_log @start, name
end

#statusObject



59
60
61
62
63
64
65
# File 'lib/tsk.rb', line 59

def status
    if task_running?
        puts "Task that is running:\n\t#{get_last_task[:name]}"
    else
        puts "No running task."
    end
end

#stop(silent = false) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/tsk.rb', line 50

def stop silent=false
    if task_running?
        puts "Stopping last task."
        write_to_log @stop
    else
        puts "No task running. Nothing was done." unless silent
    end
end

#task_running?Boolean

Returns:

  • (Boolean)


75
76
77
78
79
80
81
# File 'lib/tsk.rb', line 75

def task_running?
    if get_last_task
        get_last_task[:action] == @start
    else
        false
    end
end

#write_to_log(action, task = nil) ⇒ Object



121
122
123
124
125
126
127
128
# File 'lib/tsk.rb', line 121

def write_to_log action, task=nil
    time = Time.now
    data = {:action => action, :time => time.strftime('%T'), :name => task}
    date = time.strftime '%F'
    @log[date] ||= []
    @log[date] << data
    File.open(@log_file, 'w+') { |f| f.write(YAML::dump(@log)) }
end