Class: Bmo2::Command

Inherits:
Object
  • Object
show all
Extended by:
Color
Defined in:
lib/bmo2/command.rb

Constant Summary

Constants included from Color

Bmo2::Color::CODES

Class Method Summary collapse

Methods included from Color

colorize, included

Class Method Details

.add_item(list, name, value) ⇒ Object



140
141
142
143
144
145
# File 'lib/bmo2/command.rb', line 140

def add_item(list,name,value)
  list = List.find(list)
  list.add_item(Item.new(name,value))
  output "#{green("Bmo2")} #{cyan(name)} in #{cyan(list.name)} is #{cyan(value)}. Got it."
  save
end

.allObject



40
41
42
43
44
45
46
47
# File 'lib/bmo2/command.rb', line 40

def all
  storage.lists.each do |list|
    output "  #{list.name}"
    list.items.each do |item|
      output "    #{item.short_name}:#{item.spacer} #{item.value}"
    end
  end
end

.copy(major, minor) ⇒ Object



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

def copy(major, minor)
  unless minor
    item = storage.items.detect do |item|
      item.name == major
    end
    return output "#{cyan(major)} #{red("not found")}" unless item
  else
    list = List.find(major)
    item = list.find_item(minor)
    return output "#{cyan(minor)} #{red("not found in")} #{cyan(major)}" unless item
  end
  Platform.copy(item)
end

.create_list(name, item = nil, value = nil) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/bmo2/command.rb', line 117

def create_list(name, item = nil, value = nil)
  lists = (storage.lists << List.new(name))
  storage.lists = lists
  output "#{green("Bmo2")} Created a new list called #{cyan(name)}."
  save
  add_item(name, item, value) unless value.nil?
end

.delegate(command, major, minor) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bmo2/command.rb', line 49

def delegate(command, major, minor)
  return all               if command == 'all'
  return edit              if command == 'edit'
  return version           if command == "-v"
  return version           if command == "--version"
  return help              if command == 'help'
  return help              if command[0] == 45 || command[0] == '-' # any - dash options are pleas for help
  return echo(major,minor) if command == 'echo' || command == 'e'
  return copy(major,minor) if command == 'copy' || command == 'c'
  return open(major,minor) if command == 'open' || command == 'o'
  return random(major)     if command == 'random' || command == 'rand' || command == 'r'

  if command == 'delete' || command == 'd'
    if minor
      return delete_item(major, minor)
    else
      return delete_list(major)
    end
  end

  if storage.list_exists?(command)
    return detail_list(command) unless major
    return add_item(command,major,minor) if minor
    return add_item(command,major,stdin.read) if stdin.stat.size > 0
    return search_list_for_item(command, major)
  end

  return search_items(command) if storage.item_exists?(command) and !major

  return create_list(command, major, stdin.read) if !minor && stdin.stat.size > 0
  return create_list(command, major, minor)
end

.delete_item(list_name, name) ⇒ Object



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/bmo2/command.rb', line 147

def delete_item(list_name,name)
  if storage.list_exists?(list_name)
    list = List.find(list_name)
    if list.delete_item(name)
      output "#{green("Bmo2")} #{cyan(name)} is gone forever."
      save
    else
      output "#{cyan(name)} #{red("not found in")} #{cyan(list_name)}"
    end
  else
    output "We couldn't find that list."
  end
end

.delete_list(name) ⇒ Object



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

def delete_list(name)
  if storage.list_exists?(name)
    printf "You sure you want to delete everything in #{cyan(name)}? (y/n): "
    if $stdin.gets.chomp == 'y'
      List.delete(name)
      output "#{green("Bmo2")} Deleted all your #{cyan(name)}."
      save
    else
      output "Just kidding then."
    end
  else
    output "We couldn't find that list."
  end
end

.detail_list(name) ⇒ Object



82
83
84
85
86
87
# File 'lib/bmo2/command.rb', line 82

def detail_list(name)
  list = List.find(name)
  list.items.sort{ |x,y| x.name <=> y.name }.each do |item|
    output "    #{item.short_name}:#{item.spacer} #{item.value}"
  end
end

.echo(major, minor) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bmo2/command.rb', line 89

def echo(major, minor)
  unless minor
    item = storage.items.detect do |item|
      item.name == major
    end
    return output "#{cyan(major)} #{red("not found")}" unless item
  else
    list = List.find(major)
    item = list.find_item(minor)
    return output "#{cyan(minor)} #{red("not found in")} #{cyan(major)}" unless item
  end
  output item.value
end

.editObject



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

def edit
  output "#{green("Bmo2")} #{Platform.edit(storage.json_file)}"
end

.execute(*args) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/bmo2/command.rb', line 10

def execute(*args)
  command = args.shift
  major   = args.shift
  minor   = args.empty? ? nil : args.join(' ')

  return overview unless command
  delegate(command, major, minor)
end

.helpObject



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bmo2/command.rb', line 192

def help
  text = %{\e[32m----------------------------------------------------------------------------\e[0m
    bmo2                          display high-level overview
    bmo2 all                      show all items in all lists
    bmo2 help                     this help text
    bmo2 <list>                   create/show a list
    bmo2 delete <list>            deletes a list

    bmo2 <list> <name> <value>    create a new list item
    bmo2 <name>                   copy item's value to clipboard
    bmo2 <list> <name>            copy item's value to clipboard
    bmo2 echo <name>              echo the item's value without copying
    bmo2 echo <list> <name>       echo the item's value without copying
    bmo2 copy <name>              copy the item's value without echo
    bmo2 copy <list> <name>       copy the item's value without echo
    bmo2 delete <list> <name>     deletes an item

    all other documentation is located at:
    https://github.com/bmo2/bmo
  }.gsub(/^ {8}/, '')
  output text
end

.output(s) ⇒ Object



19
20
21
# File 'lib/bmo2/command.rb', line 19

def output(s)
  puts(s)
end

.overviewObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bmo2/command.rb', line 27

def overview
  storage.lists.each do |list|
    output "  #{list.name} (#{list.items.size})"
  end
  s =  "\e[32mYou don't have anything yet! To start out, create a new list:\e[0m"
  s << "\n$ bmo2 <list-name>"
  s << "\n\e[32mAnd then add something to your list!\e[0m"
  s << "\n$ bmo2 <list-name> <item-name> <item-value>"
  s << "\n\e[32mYou can then grab your new item:\e[0m"
  s << "\n$ bmo2 <item-name>"
  output s if storage.lists.size == 0
end

.saveObject



180
181
182
# File 'lib/bmo2/command.rb', line 180

def save
  storage.save
end

.search_items(name) ⇒ Object



161
162
163
164
165
166
167
# File 'lib/bmo2/command.rb', line 161

def search_items(name)
  item = storage.items.detect do |item|
    item.name == name
  end

  output "#{green("Bmo2")} We just copied #{cyan(Platform.copy(item))} to your clipboard."
end

.search_list_for_item(list_name, item_name) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/bmo2/command.rb', line 169

def search_list_for_item(list_name, item_name)
  list = List.find(list_name)
  item = list.find_item(item_name)

  if item
    output "#{green("Bmo2")} We just copied #{cyan(Platform.copy(item))} to your clipboard."
  else
    output "#{cyan(item_name)} #{red("not found in")} #{cyan(list_name)}"
  end
end

.stdinObject



23
24
25
# File 'lib/bmo2/command.rb', line 23

def stdin
  $stdin
end

.storageObject



6
7
8
# File 'lib/bmo2/command.rb', line 6

def storage
  Bmo2.storage
end

.versionObject



184
185
186
# File 'lib/bmo2/command.rb', line 184

def version
  output "You're running bmo2 #{Bmo::VERSION}. Congratulations!"
end