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

Instance Method Summary collapse

Constructor Details

#initialize(*all) ⇒ Command

Returns a new instance of Command.



57
58
59
60
61
62
63
64
65
# File 'lib/koi.rb', line 57

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: "''", cyan: '@@', green: '!!', yellow: '^').clear(:default)
end

Instance Method Details

#[](key) ⇒ Object



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

def [] key
  @options[key]
end

#[]=(key, val) ⇒ Object



87
88
89
# File 'lib/koi.rb', line 87

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

#add(entry, *args) ⇒ Object



170
171
172
173
174
175
# File 'lib/koi.rb', line 170

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



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

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

#err(str) ⇒ Object



166
167
168
# File 'lib/koi.rb', line 166

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

#floatObject



136
137
# File 'lib/koi.rb', line 136

def float
end

#initObject



95
96
97
98
99
100
101
# File 'lib/koi.rb', line 95

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

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

List current tasks



122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/koi.rb', line 122

def list count = 10, index = -1
  out
  
  @db.select {|e| e.new? }[0..count].each do |e|
    out "#[#{index += 1}]# ''#{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



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/koi.rb', line 145

def log
  @db.map do |entity|
    Entity::Status.map do |status|
      { title: entity[:title],
        action: status,
        time: entity[:"#{status}_at"]
      } 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



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

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)



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

def remove entry
  entry.status = :removed
end

#runObject



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

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

#saveObject



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

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

#sinkObject



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

def sink
end

#statusObject



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

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

  self.list 5

  out "recently fished (#{@db.select {|e| e.completed? }.size})"

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

  out
  true
end

#tag(entry, tags) ⇒ Object



177
178
179
# File 'lib/koi.rb', line 177

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