Class: Boom::Command

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

Constant Summary

Constants included from Color

Boom::Color::CODES

Class Method Summary collapse

Methods included from Output

output

Methods included from Color

colorize, included

Class Method Details

.add_item(list, name, value) ⇒ Object

Public: add a new Item to a list.

list - the String name of the List to associate with this Item name - the String name of the Item value - the String value of the Item

Example

Commands.add_item("snippets","sig","- @holman")

Returns the newly created Item.



264
265
266
267
268
269
# File 'lib/kaboom/command.rb', line 264

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

.allObject

Public: prints the detailed view of all your Lists and all their Items.

Returns nothing.



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

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

.c(t) ⇒ Object



356
357
358
# File 'lib/kaboom/command.rb', line 356

def c t
  cyan t
end

.check_if_remote(args) ⇒ Object



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

def check_if_remote args
  command = args.shift

  if command == "remote"
    Boom.use_remote
    command = args.shift
  end
  command
end

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

Public: add a new List.

name - the String name of the List. item - the String name of the Item value - the String value of Item

Example

Commands.list_create("snippets")
Commands.list_create("hotness", "item", "value")

Returns the newly created List and creates an item when asked.



225
226
227
228
229
230
231
# File 'lib/kaboom/command.rb', line 225

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

.delegate(command, major, minor) ⇒ Object

Public: allows main access to most commands.

Returns output based on method calls.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/kaboom/command.rb', line 92

def delegate(command, major, minor)
  return all               if command == 'all'
  return edit              if command == 'edit'
  return switch(major)     if command == 'switch'
  return show_storage      if command == 'storage'
  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 open(major,minor) if command == 'open' || command == 'o'
  return random(major)     if command == 'random' || command == 'rand' || command == 'r'

  # if we're operating on a List
  list = command
  if storage.list_exists?(list)
    return delete_list(list) if major == 'delete'
    return detail_list(list) unless major
    unless minor == 'delete'
      return add_item(list,major,minor) if minor
      return add_item(list,major,stdin.read) if stdin.stat.size > 0
      return search_list_for_item(list, major)
    end
  end

  if minor == 'delete' and storage.item_exists?(major)
    return delete_item(command, major)
  end

  return search_items(command) if storage.item_exists?(command)

  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

Public: remove a named Item.

list_name - the String name of the List. name - the String name of the Item.

Example

Commands.delete_item("a-list-name", "an-item-name")

Returns nothing.



281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/kaboom/command.rb', line 281

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

.delete_list(name) ⇒ Object

Public: remove a named List.

name - the String name of the List.

Example

Commands.delete_list("snippets")

Returns nothing.



242
243
244
245
246
247
248
249
250
251
# File 'lib/kaboom/command.rb', line 242

def delete_list(name)
  output "You sure you want to delete everything in #{yellow(name)}? (y/n):"
  if $stdin.gets.chomp == 'y'
    List.delete(name)
    output "#{cyan("Boom!")} Deleted all your #{yellow(name)}."
    save
  else
    output "Just kidding then."
  end
end

.detail_list(name) ⇒ Object

Public: prints all Items over a List.

name - the List object to iterate over

Returns nothing.



151
152
153
154
155
156
# File 'lib/kaboom/command.rb', line 151

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

Public: echoes only the Item’s value without copying

item_name - the String term to search for in all Item names

Returns nothing



199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/kaboom/command.rb', line 199

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

.editObject

Public: launches JSON file in an editor for you to edit manually.

Returns nothing.



344
345
346
347
348
349
350
# File 'lib/kaboom/command.rb', line 344

def edit
  if storage.respond_to?("json_file")
    output "#{cyan("Boom!")} #{Platform.edit(storage.json_file)}"
  else
    output "This storage backend does not store #{cyan("Boom!")} data on your computer"
  end
end

.execute(*args) ⇒ Object

Public: executes a command.

args - The actual commands to operate on. Can be as few as zero

arguments or as many as three.


28
29
30
31
32
33
34
35
36
# File 'lib/kaboom/command.rb', line 28

def execute(*args)
  command = check_if_remote args

  major   = args.shift
  minor   = args.empty? ? nil : args.join(' ')

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

.helpObject

Public: prints all the commands of boom.

Returns nothing.



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/kaboom/command.rb', line 367

def help
  text = %{
    #{r "BOOM snippets"} ___________________________________________________

    boom                                 display high-level overview
    boom #{y "all"}                             show all items in all lists
    boom #{y "edit"}                            edit the boom JSON file in $EDITOR
    boom #{y "help"}                            this help text
    boom #{y "storage"}                         shows which storage backend you're using
    boom #{y "switch"} #{c "<storage>"}                switches to a different storage backend

    boom #{c "<list>"}                          create a new list
    boom #{c "<list>"}                          show items for a list
    boom #{c "<list>"} #{y "delete"}                   deletes a list

    boom #{c "<list> <name> <value>"}           create a new list item
    boom #{c "<name>"}                          copy item's value to clipboard
    boom #{c "<list> <name>"}                   copy item's value to clipboard
    boom #{y "open"} #{c "<name>"}                     open item's url in browser
    boom #{y "open"} #{c "<list> <name>"}              open all item's url in browser for a list
    boom #{y "random"}                          open a random item's url in browser
    boom #{y "random"} #{c "<list>"}                   open a random item's url for a list in browser
    boom #{y "echo"} #{c "<name>"}                     echo the item's value without copying
    boom #{y "echo"} #{c "<list> <name>"}              echo the item's value without copying
    boom #{c "<list> <name>"} #{y "delete"}            deletes an item

    #{red "KABOOM sharing"} ___________________________________________________

    boom remote #{y "<any command above>"}      using the #{y "~/.boom.remote.conf"} it
    kaboom      #{y "<any command above>"}      connects to an alternative backend
                                         meaning you can pipe to a remote
                                         backend storage

    e.g. to pipe config from a local boom to a remote boom do:

    kaboom #{c("config ackrc")} < boom #{c "config ackrc"}

    ___________________________________________________________________
    all other documentation is located at:
      https://github.com/markburns/kaboom
  }.gsub(/^ {8}/, '') # strip the first eight spaces of every line

  output text
end

.open(key, value) ⇒ Object

Public: opens the Item.

Returns nothing.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kaboom/command.rb', line 161

def open(key, value)
  if storage.list_exists?(key)
    list = List.find(key)
    if value
      item = storage.items.detect { |item| item.name == value }
      output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
    else
      list.items.each { |item| Platform.open(item) }
      output "#{cyan("Boom!")} We just opened all of #{yellow(key)} for you."
    end
  else
    item = storage.items.detect { |item| item.name == key }
    output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
  end
end

.overviewObject

Public: prints a tidy overview of your Lists in descending order of number of Items.

Returns nothing.



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kaboom/command.rb', line 63

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

.r(t) ⇒ Object



360
361
362
# File 'lib/kaboom/command.rb', line 360

def r t
  red t
end

.random(major) ⇒ Object

Public: Opens a random item

Returns nothing.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/kaboom/command.rb', line 180

def random(major)
  if major.nil?
    index = rand(storage.items.size)
    item = storage.items[index]
  elsif storage.list_exists?(major)
    list = List.find(major)
    index = rand(list.items.size)
    item = list.items[index]
  else
    output "We couldn't find that list."
  end
  open(item.name, nil) unless item.nil?
end

.saveObject

Public: save in-memory data to disk.

Returns whether or not data was saved.



330
331
332
# File 'lib/kaboom/command.rb', line 330

def save
  storage.save
end

.search_items(name) ⇒ Object

Public: search for an Item in all lists by name. Drops the corresponding entry into your clipboard.

name - the String term to search for in all Item names

Returns the matching Item.



301
302
303
304
305
306
307
# File 'lib/kaboom/command.rb', line 301

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

  output "#{cyan("Boom!")} We just copied #{yellow(Platform.copy(item))} to your clipboard."
end

.search_list_for_item(list_name, item_name) ⇒ Object

Public: search for an Item in a particular list by name. Drops the corresponding entry into your clipboard if found.

list_name - the String name of the List in which to scope the search item_name - the String term to search for in all Item names

Returns the matching Item if found.



316
317
318
319
320
321
322
323
324
325
# File 'lib/kaboom/command.rb', line 316

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

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

.show_storageObject

Public: shows the current user’s storage.

Returns nothing.



130
131
132
# File 'lib/kaboom/command.rb', line 130

def show_storage
  output "You're currently using #{Boom.config.attributes['backend']}."
end

.stdinObject

Public: gets $stdin.

Returns the $stdin object. This method exists to help with easy mocking or overriding.



55
56
57
# File 'lib/kaboom/command.rb', line 55

def stdin
  $stdin
end

.storageObject

Public: accesses the in-memory JSON representation.

Returns a Storage instance.



20
21
22
# File 'lib/kaboom/command.rb', line 20

def storage
  Boom.storage
end

.switch(backend) ⇒ Object

Public: switch to a new backend.

backend - the String of the backend desired

Returns nothing.



139
140
141
142
143
144
# File 'lib/kaboom/command.rb', line 139

def switch(backend)
  Storage.backend = backend
  output "We've switched you over to #{backend}."
rescue NameError
  output "We couldn't find that storage engine. Check the name and try again."
end

.versionObject

Public: the version of boom that you’re currently running.

Returns a String identifying the version number.



337
338
339
# File 'lib/kaboom/command.rb', line 337

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

.y(t) ⇒ Object



352
353
354
# File 'lib/kaboom/command.rb', line 352

def y t
  yellow t
end