Class: Todd::Util

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

Class Method Summary collapse

Class Method Details

.format_bundle(bundle, formatting = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/todd/util.rb', line 51

def self.format_bundle bundle, formatting = nil
  formatting = Base[:output_format] unless formatting
  case formatting
    when "table"
      needs 'terminal-table/import'
      return self.format_to_table bundle
    when "minimal"
      return self.format_minimal bundle
    when "ruby_obj"
      return PP.pp(bundle,'')
    when "json"
      needs 'json'
      return bundle.to_json
    else
      puts "ERROR: Cannot format bundle to #{formatting}"
      exit
  end
end

.format_minimal(stack, ret_string = true) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/todd/util.rb', line 105

def self.format_minimal stack, ret_string = true
  stack = [stack] unless stack.class == [].class
  rows = []
  default_category = Base[:default_category]

  while item = stack.pop
    case item[:type]
      when :todolist
        return "Nothing to list" if item[:categories] == []
        stack = item[:categories]
        stack.sort! do |x,y|
          case default_category
            when x[:name]
              ret = 1
            when y[:name]
              ret = -1
            else
              ret = x[:name] <=> y[:name]
          end
          ret
        end
      when :category
        return "" if item[:task] == []
        rows << "+ #{item[:name]}" unless item[:name] == Base[:default_category]
        rows += self.format_minimal item[:tasks], false
        rows << " " unless stack.empty?
      when :task
        rows << [
          item[:id],
          item[:active] ? 'x' : ' ',
          self.to_minimal_time_s(item[:total_time]),
          item[:title]
        ].map { |e|
          "[%s]" % e
        } * " " + (item[:active] ? " [%s]" % self.to_minimal_time_s(item[:session_time]) : '')
      else
        puts "ERROR: Bundle type #{bundle[:type]} not recognized"
        exit
    end
  end

  return rows unless ret_string

  rows * "\n"
end

.format_to_table(stack, ret_table = true) ⇒ Object



70
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
99
100
101
102
103
# File 'lib/todd/util.rb', line 70

def self.format_to_table stack, ret_table = true
  stack = [stack] unless stack.class == [].class
  rows = []
  header = ["ID", "Task", "Session Time", "Total Time"]

  while item = stack.pop
    case item[:type]
      when :todolist
        return "" if item[:categories] == []
        stack =  item[:categories]
      when :category
        return "" if item[:task] == []
        rows << ['Category:',item[:name],'','']
        rows << :separator
        rows += self.format_to_table item[:tasks], false
        rows << :separator unless stack.empty?
      when :task
        running = item[:active]
        rows << [
          item[:id],
          item[:title],
          running ? self.time_period_to_s(item[:session_time]) : 'Not Running',
          item[:total_time].to_i > 0.1 ? self.time_period_to_s(item[:total_time].to_i) : 'Never Run'
        ]
      else
        puts "ERROR: Bundle type #{bundle[:type]} not recognized"
        exit
    end
  end

  return rows unless ret_table

  table(header, *rows)
end

.needs(file) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/todd/util.rb', line 3

def self.needs file
  begin
    require file
  rescue LoadError => e
    warn "Cannot find module %s, perhaps you need to install it?" % file
    exit
  end
end

.time_period_to_s(time_period) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/todd/util.rb', line 12

def self.time_period_to_s time_period
  time_period = time_period.to_i

  if time_period < 1
    return "< 1 sec"
  end
 
  out_str = ''
  interval_array = [ [:weeks, 604800], [:days, 86400], [:hours, 3600], [:mins, 60], [:secs, 1] ]
  interval_array.each do |sub|
    if time_period >= sub[1] then
      time_val, time_period = time_period.divmod( sub[1] )
      time_val == 1 ? name = sub[0].to_s.singularize : name = sub[0].to_s
      ( sub[0] != :mins ? out_str += ", " : out_str += " and " ) if out_str != ''
      out_str += time_val.to_s + " #{name}"
    end
  end

  out_str
end

.to_minimal_time_s(time_period) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/todd/util.rb', line 33

def self.to_minimal_time_s time_period
  time_period = time_period.to_i

  out_str = ''
  interval_array = [3600, 60, 1]
  interval_array.each do |sub|
    if time_period >= sub then
      time_val, time_period = time_period.divmod(sub)
      out_str += "%02d" % time_val
    else
      out_str += "00"
    end
    out_str += ":" if sub != interval_array[-1]
  end

  out_str
end