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
]
Initializers =
[:init, :add]
Special =
{"!" => :done, "?" => :status}

Instance Method Summary collapse

Constructor Details

#initialize(*all) ⇒ Command

Returns a new instance of Command.



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

def initialize *all
  cmd, param, args, options = all
  @command = Special[cmd] || cmd.to_sym
  @args = [args || []].flatten
  @param = param =~ /^\d+$/ ? param.to_i : param
  @options = options || {}
  @db = Koi.init?? Database.new(File.join(Koi.root, Path[:db])) : Database.new
  @mut = Mutter.new(blue: '#', underline: "''", red: '++', cyan: '@@', green: '!!', yellow: '^').clear(:default)
end

Instance Method Details

#[](key) ⇒ Object



96
97
98
# File 'lib/koi.rb', line 96

def [] key
  @options[key]
end

#[]=(key, val) ⇒ Object



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

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

#add(entry, *args) ⇒ Object



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

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



197
198
199
200
# File 'lib/koi.rb', line 197

def did entry = 0
  entry.status = :completed
  entry[:completed_by] = ENV['USER']
end

#err(str) ⇒ Object



182
183
184
# File 'lib/koi.rb', line 182

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

#float(entry) ⇒ Object



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

def float entry
  entry[:sticky] = ! entry[:sticky]
  true
end

#initObject



100
101
102
103
104
105
106
# File 'lib/koi.rb', line 100

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

#list(count = 10, index = -1) ⇒ Object Also known as: ls

List current tasks



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/koi.rb', line 125

def list count = 10, index = -1
  out

  @db.list[0..count].each do |e|
    out " #[#{index += 1}]##{e.sticky?? "++ + ++" : "   "}''#{e[:title]}'' @@#{e[:tags].join(' ')}@@" unless e[:status] == :removed
  end.tap do |list|
    out "  !!nothing left to do!!" if list.size.zero?
  end

  out
  true
end

#logObject

Show task history



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/koi.rb', line 161

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]}# ^#{entry[:action]}^ ''#{entry[:title]}''"
  end
end

#out(obj = "") ⇒ Object



174
175
176
177
178
179
180
# File 'lib/koi.rb', line 174

def out obj = ""
  if obj.is_a? Hash
    puts "#{obj[:title]} : #{obj[:status]}"
  else
    @mut.say 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)



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

def remove entry
  entry.status = :removed
end

#rise(entry) ⇒ Object



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

def rise entry
  swim entry, -1
end

#runObject



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

def run
  if Commands.include? @command
    if Koi.init? or Initializers.include? @command
      if !@param or @command == :add or @param = @db.find(@param)
        @param ||= @db.last if [:float, :sink, :rm, :tag, :done].include? @command
        if send(@command, *[@param, *@args].compact.flatten)
          save
        else
          err "error running #@command"
        end
      else
        err "task wasn't found"
      end
    else
       err "'koi' is not initialized here, please run `koi init`"
    end
  else
    err "#{@command} is not a valid command."
  end
end

#saveObject



204
205
206
# File 'lib/koi.rb', line 204

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

#sink(entry) ⇒ Object



149
150
151
# File 'lib/koi.rb', line 149

def sink entry
  swim entry, 1
end

#statusObject



108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/koi.rb', line 108

def status
  out "in the water (#{@db.select {|e| e.new? }.size})"

  self.list 5

  @db.select  {|e| e[:status] == :completed }.
      sort_by {|e| e[:completed_at] }[0..3].reverse.each do |e|
    out " #[x]#   !!#{e[:title]}!!"
  end

  out
  true
end

#swim(entry, n) ⇒ Object



139
140
141
142
143
# File 'lib/koi.rb', line 139

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

#tag(entry, tags) ⇒ Object



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

def tag entry, tags
  entry[:tags] << tags
end