Module: Timecard

Defined in:
lib/timecard.rb

Constant Summary collapse

DEBUG =
false

Class Method Summary collapse

Class Method Details

.ask_taskObject



26
27
28
29
30
31
32
33
# File 'lib/timecard.rb', line 26

def ask_task
  $stderr.print "What will you work on now? "

  @task_name = $stdin.readline.chomp
  @tasks << {:name => @task_name, :time_pairs => []}

  :time_loop
end

.dump_tasksObject



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/timecard.rb', line 103

def dump_tasks
  if @tasks.any?
    puts "\n"
    i = 0
    @tasks.each do |task|
      i += 1
      puts "Task ##{i}: #{task[:name]} #{format_seconds(sum_time_pairs(task))}"
      task[:time_pairs].each { |pair| puts "  " + pair.inspect + " " + format_seconds(pair.last - pair.first) }
    end
  end
end

.finish_and_quitObject



96
97
98
99
100
101
# File 'lib/timecard.rb', line 96

def finish_and_quit
  $stderr.print clearline
  $stderr.puts ">#{format_seconds(sum_time_pairs(@tasks.last))} #{@tasks.last[:name]}"
  dump_tasks
  exit(0)
end

.finish_taskObject



90
91
92
93
94
# File 'lib/timecard.rb', line 90

def finish_task
  $stderr.print clearline
  $stderr.puts ">#{format_seconds(sum_time_pairs(@tasks.last))} #{@tasks.last[:name]}"
  return :ask_task
end

.invokeObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/timecard.rb', line 5

def invoke
  $stderr.puts "=== Welcome to Ruby Timecard! ==="

  @tasks = []
  @new_input = nil

  Signal.trap("INT") do
    dump_tasks
    exit(0)
  end

  state_loop(:ask_task)
end

.pause_taskObject



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/timecard.rb', line 77

def pause_task
  $stderr.print "#{clearline}*#{format_seconds(sum_time_pairs(@tasks.last))} * #{@task_name} (paused, any key to resume)"

  await_input

  loop do
    next_state = process_input { |input| :time_loop }
    return next_state if next_state

    sleep(0.1)
  end
end

.state_loop(state) ⇒ Object



19
20
21
22
23
24
# File 'lib/timecard.rb', line 19

def state_loop(state)
  loop do
    $stderr.puts "Entering state #{state}..." if DEBUG
    state = send(state)
  end
end

.time_loopObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/timecard.rb', line 35

def time_loop
  @start_time = @last_time = Time.now

  await_input

  loop do
    if (Time.now - @last_time) > 5.0
      $stderr.puts "#{clearline}Detected Sleep Auto-Pausing for #{format_seconds(Time.now - @last_time)}"
      @tasks.last[:time_pairs] << [@start_time, @last_time]
      @start_time = Time.now
    end

    next_state = process_input { |input| time_loop_transition(input) }
    return next_state if next_state

    elapsed = Time.now - @start_time
    $stderr.print "#{clearline}*#{format_seconds(sum_time_pairs(@tasks.last) + elapsed)} * #{@task_name} (p: pause   f: next task   q: finish & quit)"
    @last_time = Time.now
    sleep(0.5)
  end
end

.time_loop_transition(command) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/timecard.rb', line 57

def time_loop_transition(command)
  valid_command = case command
  when 'p'
    :pause_task
  when 'f'
    :finish_task
  when 'q'
    :finish_and_quit
  end

  if ! valid_command
    await_input
  else
    @tasks.last[:time_pairs] << [@start_time, Time.now]
    @start_time = nil
  end

  valid_command
end