Module: Swatch

Defined in:
lib/swatch.rb

Class Method Summary collapse

Class Method Details

.get_last_task_nameObject

Return the name of the last task



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/swatch.rb', line 40

def get_last_task_name
  line = ''
  IO.popen("tail -n 1 #{TRACK_FILE}") { |f| line = f.gets }
  if line == nil
    return false
  end

  line = line.split "\t"
  if running_task?
    line.pop
  else
    line.pop 2
  end
  line.join "\t"
end

.get_last_task_stimeObject

Return the start time of a running task



29
30
31
32
33
34
35
36
37
# File 'lib/swatch.rb', line 29

def get_last_task_stime
  line = ''
  IO.popen("tail -n 1 #{TRACK_FILE}") { |f| line = f.gets }
  if line == nil
    return false
  end

  line.split("\t").pop
end

.get_todo(nb) ⇒ Object

Return the todo associated to the given number



101
102
103
104
105
106
107
108
# File 'lib/swatch.rb', line 101

def get_todo (nb)
  # TODO: parse todo to remove the priority
  line = IO.readlines(TODO_FILE)[nb-1]
  if line.match '^\(([A-Z])\)'
    line.slice!(0..4)
  end
  line
end

.running_task?Boolean

Test if there is a task currently running. Return true if there is

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/swatch.rb', line 17

def running_task?
  line = ''
  IO.popen("tail -n 1 #{TRACK_FILE}") { |f| line = f.gets }
  #puts line
  if(line != nil && (!line.match '^.+\t\d+\t\d+$'))
    true
  else
    false
  end
end

.task_in(task) ⇒ Object

Start a task



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/swatch.rb', line 71

def task_in (task)
  # don't go here if ARGV is null !
  if task.strip.empty?
    puts "No task specified"
    return false
  end

  # if there is a task running, we get out of it
  if running_task?
    #puts "There is a task running, getting out of this one"
    task_out
  end

  if(!File.exist?(TRACK_FILE))
    #puts "Create a new task file"
    out = File.new(TRACK_FILE, "w")
  else
    #puts "Use #{TRACK_FILE}"
    out = File.open(TRACK_FILE, "a")
  end

  #print the task in the file
  out.print "#{task}\t#{Time.now.to_i}"
  out.close

  puts "Start task: #{task}"
  return true
end

.task_in_todo(nb) ⇒ Object

Going in a task with a todo from todo.txt



111
112
113
114
115
116
117
118
# File 'lib/swatch.rb', line 111

def task_in_todo (nb)
   t = get_todo (nb)
  if t
    task_in t.chomp
  else
    puts "No task specified"
  end
end

.task_outObject

Go out of the current task running



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/swatch.rb', line 57

def task_out
  if running_task?
    puts "Stop task: " + get_last_task_name + ", #{(Time.now.to_i - get_last_task_stime.to_i).duration}"
    open(TRACK_FILE, "a"){|f|
      f.print "\t#{Time.now.to_i}\n"
    }
    return true
  else
    puts "There is no task running"
    return false
  end
end