Class: Timelog::Command::List

Inherits:
Timelog::Command show all
Defined in:
lib/timelog/command/list.rb

Instance Attribute Summary

Attributes inherited from Timelog::Command

#arguments, #book, #session

Instance Method Summary collapse

Methods inherited from Timelog::Command

get, inherited, #initialize, known, register, #time_in_arguments

Constructor Details

This class inherits a constructor from Timelog::Command

Instance Method Details

#column_sizes_for(table) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/timelog/command/list.rb', line 52

def column_sizes_for(table)
  columns = {}
  table.each do |row|
    row.each do |key, value|
      columns[key] ||= key.size
      columns[key] = value.to_s.size if value and value.to_s.size > columns[key]
    end
  end
  columns
end

#executeObject



4
5
6
7
8
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
# File 'lib/timelog/command/list.rb', line 4

def execute
  table = book.entries.where(arguments.first || "non-archived").map do |e|
    {
      id: e.id,
      client: e.client ? book.clients.name(e.client) : "",
      project: e.project ? book.projects.name(e.client, e.project) : "",
      description: e.description,
      started: e.started.try(:strftime, "%Y-%m-%d %H:%M"),
      stopped: e.stopped.try(:strftime, "%Y-%m-%d %H:%M"),
      "active time" => "%02d:%02d:%02d" % e.active_time.values,
      "paused time" => e.paused_seconds > 0 ? "%02d:%02d:%02d" % e.paused_time.values : "",
      "total time" => "%02d:%02d:%02d" % e.total_time.values,
      status: e.status
    }
  end

  if table.size == 0
    "no entries found"
  else
    sizes = column_sizes_for table
    width = sizes.values.reduce(:+) + sizes.length * 3 + 1

    separater = ''
    width.times { separater << '-' }

    lines = [separater]
    lines << "|" + sizes.map do |name, length|
      " %-#{length}s " % name
    end.join("|") + "|"
    lines << separater

    table.each do |row|
      line = []
      row.each do |key, value|
        line << " %-#{sizes[key]}s " % value
      end
      lines << "|" + line.join("|") + "|"
    end
    lines << separater

    lines.join("\n")
  end
end

#helpObject



48
49
50
# File 'lib/timelog/command/list.rb', line 48

def help
  "usage: timelog list [<query>]"
end