Class: Koi::Command

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

Constant Summary collapse

Commands =
[
  :init, :add, :list, :tag,
  :done, :did, :log, :status,
  :remove, :float, :sink,
  :ls, :rm, :rise, :x, :show
]
Initializers =
[:init, :add]
Special =
{"!" => :done, "?" => :status, "+" => :float}

Instance Method Summary collapse

Constructor Details

#initialize(*all) ⇒ Command

Returns a new instance of Command.



61
62
63
64
65
66
67
# File 'lib/koi.rb', line 61

def initialize *all
  cmd, args, options = all
  @command = Special[cmd] || cmd.to_sym
  @args = (args || []).map {|a| a =~ /^\d+$/ ? a.to_i : a }
  @options = options || {}
  @db = Koi.init?? Database.new(File.join(Koi.root, Path[:db])) : Database.new
end

Instance Method Details

#[](key) ⇒ Object



93
94
95
# File 'lib/koi.rb', line 93

def [] key
  @options[key]
end

#[]=(key, val) ⇒ Object



89
90
91
# File 'lib/koi.rb', line 89

def []= key, val
  @options[key] = val
end

#add(entry, *args) ⇒ Object



193
194
195
196
197
198
# File 'lib/koi.rb', line 193

def add entry, *args
  Koi.init
  target = args.find {|a| a.start_with? '@' }[1..-1] rescue nil
  tags = args.select {|a| a.start_with? '#' }
  @db << Entity.new(title: entry, tags: tags, target: target)
end

#did(entry = 0) ⇒ Object Also known as: done, fish, x



209
210
211
212
213
# File 'lib/koi.rb', line 209

def did entry = 0
  entry = @db.find(entry)
  entry.status = :completed
  entry[:completed_by] = ENV['USER']
end

#err(str) ⇒ Object



189
190
191
# File 'lib/koi.rb', line 189

def err str
  @options[:silent] ? abort : abort(str)
end

#float(entry) ⇒ Object



159
160
161
162
163
# File 'lib/koi.rb', line 159

def float entry
  entry = @db.find(entry)
  entry[:sticky] = ! entry[:sticky]
  true
end

#initObject



97
98
99
100
101
102
103
# File 'lib/koi.rb', line 97

def init
  unless Koi.init
    err "'koi' has already been initialized here"
  else
    true
  end
end

#list(entities = ) ⇒ Object Also known as: ls

List current tasks



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/koi.rb', line 127

def list entities = @db.list[0..10]
  out

  entities.reject {|e| e[:status] == :removed }.each_with_index do |e, i|
    out " [#{i}]".blue                     +
        "#{e.sticky?? " + ".bold : "   "}" +
        e[:title].underline                +
        " #{e[:tags].join(' ')}".cyan
  end.tap do |list|
    out "  there are no koi in the water".green if list.size.zero?
  end

  out
  entities
end

#logObject

Show task history



168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/koi.rb', line 168

def log
  @db.map do |entity|
    Entity::Status.map do |status|
      { title:  entity[:title],
        action: status,
        time:   entity[:"#{status}_at"].strftime("%Y/%m/%d %H:%m")
      } if entity[:"#{status}_at"]
    end.compact
  end.flatten.sort_by {|e| e[:time]}.reverse.each do |entry|
    out "#{entry[:time].blue} #{entry[:action].to_s.bold} #{entry[:title].underline}"
  end
end

#out(obj = "") ⇒ Object



181
182
183
184
185
186
187
# File 'lib/koi.rb', line 181

def out obj = ""
  if obj.is_a? Hash
    puts "#{obj[:title]} : #{obj[:status]}"
  else
    puts obj.to_s
  end unless @options[:silent]
end

#remove(entry) ⇒ Object Also known as: rm, kill

Mark task as :removed (doesn’t show up anywhere)



225
226
227
# File 'lib/koi.rb', line 225

def remove entry
  @db.find(entry).status = :removed
end

#rise(entry) ⇒ Object



151
152
153
# File 'lib/koi.rb', line 151

def rise entry
  swim entry, -1
end

#runObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/koi.rb', line 69

def run
  if Commands.include? @command
    if Koi.init? or Initializers.include? @command
      send(@command, *(@args.length == 1 ? @args.first : @args)).tap do |result|
        if result
          save
        else
          err "error running #@command"
        end
      end
    else
       err "'koi' is not initialized here, please run `koi init`"
    end
  else
    err "#{@command} is not a valid command."
  end
rescue Database::EntityNotFound
  err "the koi wasn't found"
end

#saveObject



218
219
220
# File 'lib/koi.rb', line 218

def save
  File.open(File.join(Koi.root, Path[:db]), 'w') {|f| f.write @db.to_yaml }
end

#show(tags) ⇒ Object



119
120
121
122
# File 'lib/koi.rb', line 119

def show tags
  tags = [tags].flatten
  self.list @db.select {|e| e if (tags & e[:tags]).any? }
end

#sink(entry) ⇒ Object



155
156
157
# File 'lib/koi.rb', line 155

def sink entry
  swim entry, 1
end

#status(options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/koi.rb', line 105

def status options = {}
  todo = @db.select {|e| e.new? }.size
  out "#{todo} koi in the water" unless todo.zero?

  self.list @db.list[0..5]

  @db.select  {|e| e[:status] == :completed }.
      sort_by {|e| e[:completed_at] }[0..3].reverse.each do |e|
    out " [x] ".blue + "  #{e[:title]}".green
  end.tap {|l| out if l.length > 0 }

  true
end

#swim(entry, n) ⇒ Object



144
145
146
147
148
149
# File 'lib/koi.rb', line 144

def swim entry, n
  entry = @db.find(entry)
  v = @db.index(entry) + @db.size / 3 * n
  @db.delete entry
  @db.insert([[v, 0].max, @db.size].min, entry)
end

#tag(*args) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/koi.rb', line 200

def tag *args
  entry, *tags = args
  entry = @db.find(entry)
  entry[:tags] += tags
  entry[:tags].uniq!

  true
end