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



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

def get_last_task_name
  line = ''
  IO.popen("tail -n 1 #{TRACK_FILE}") { |f| line = f.gets }
  m = line.match '^(.+)\t\d+(\t\d+)?'
  m[1]
end

.get_todo(nb) ⇒ Object

Return the todo associated to the given number



67
68
69
# File 'lib/swatch.rb', line 67

def get_todo (nb)
  IO.readlines(TODO_FILE)[nb-1]
end

.running_task?Boolean

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

Returns:

  • (Boolean)


6
7
8
9
10
11
12
13
14
15
# File 'lib/swatch.rb', line 6

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/swatch.rb', line 38

def task_in (task)
  # don't go here if ARGV is null !
  if task.strip.empty?
    puts "No task specified"
    exit
  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}"
end

.task_in_todo(nb) ⇒ Object

Going in a task with a todo from todo.txt



72
73
74
75
76
77
78
79
# File 'lib/swatch.rb', line 72

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



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

def task_out
  if running_task?
    puts "Stop task: " + get_last_task_name
    f = File.open(TRACK_FILE, "a")
    f.print "\t#{Time.now.to_i}\n"
    f.close
  else
    puts "There is no task running"
  end
end