class Boom::Command

Public Class Methods

add_item(list,name,value) click to toggle source

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.

# File lib/boom/command.rb, line 268
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
all() click to toggle source

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

Returns nothing.

# File lib/boom/command.rb, line 75
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) click to toggle source

Public: Copies to clipboard the Item's value without printing to screen

Returns nothing

# File lib/boom/command.rb, line 199
def copy(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
  Platform.copy(item)
end
create_list(name, item = nil, value = nil) click to toggle source

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.

# File lib/boom/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) click to toggle source

Public: allows main access to most commands.

Returns output based on method calls.

# File lib/boom/command.rb, line 87
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 we're operating on a List
  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) click to toggle source

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.

# File lib/boom/command.rb, line 285
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) click to toggle source

Public: remove a named List.

name - the String name of the List.

Example

Commands.delete_list("snippets")

Returns nothing.

# File lib/boom/command.rb, line 242
def delete_list(name)
  if storage.list_exists?(name)
    printf "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
  else
    output "We couldn't find that list."
  end
end
detail_list(name) click to toggle source

Public: prints all Items over a List.

name - the List object to iterate over

Returns nothing.

# File lib/boom/command.rb, line 126
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) click to toggle source

Public: echoes only the Item's value without copying

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

Returns nothing

# File lib/boom/command.rb, line 182
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
edit() click to toggle source

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

Returns nothing.

# File lib/boom/command.rb, line 348
def edit
  output "#{cyan("Boom!")} #{Platform.edit(storage.json_file)}"
end
execute(*args) click to toggle source

Public: executes a command.

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

arguments or as many as three.
# File lib/boom/command.rb, line 27
def execute(*args)
  command = args.shift
  major   = args.shift
  minor   = args.empty? ? nil : args.join(' ')

  return overview unless command
  delegate(command, major, minor)
end
help() click to toggle source

Public: prints all the commands of boom.

Returns nothing.

# File lib/boom/command.rb, line 355
def help
  text = %{
    - boom: help ---------------------------------------------------

    boom                          display high-level overview
    boom all                      show all items in all lists
    boom edit                     edit the boom JSON file in $EDITOR
    boom help                     this help text

    boom <list>                   create a new list
    boom <list>                   show items for a list
    boom delete <list>            deletes a list

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

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

  output text
end
open(major, minor) click to toggle source

Public: opens the Item.

Returns nothing.

# File lib/boom/command.rb, line 136
def open(major, minor)
  if storage.list_exists?(major)
    list = List.find(major)
    if minor
      item = storage.items.detect { |item| item.name == minor }
      if item
        output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
      else
        output "Couldn't find #{yellow(minor)}."
      end
    else
      list.items.each { |item| Platform.open(item) }
      output "#{cyan("Boom!")} We just opened all of #{yellow(major)} for you."
    end
  else
    item = storage.items.detect { |item| item.name == major }
    if item
      output "#{cyan("Boom!")} We just opened #{yellow(Platform.open(item))} for you."
    else
      output "Couldn't find #{yellow(major)}."
    end
  end
end
output(s) click to toggle source

Public: prints any given string.

s = String output

Prints to STDOUT and returns. This method exists to standardize output and for easy mocking or overriding.

# File lib/boom/command.rb, line 42
def output(s)
  puts(s)
end
overview() click to toggle source

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

Returns nothing.

# File lib/boom/command.rb, line 58
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
random(major) click to toggle source

Public: Opens a random item

Returns nothing.

# File lib/boom/command.rb, line 163
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
save() click to toggle source

Public: save in-memory data to disk.

Returns whether or not data was saved.

# File lib/boom/command.rb, line 334
def save
  storage.save
end
search_items(name) click to toggle source

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.

# File lib/boom/command.rb, line 305
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) click to toggle source

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.

# File lib/boom/command.rb, line 320
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
stdin() click to toggle source

Public: gets $stdin.

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

# File lib/boom/command.rb, line 50
def stdin
  $stdin
end
storage() click to toggle source

Public: accesses the in-memory JSON representation.

Returns a Storage instance.

# File lib/boom/command.rb, line 19
def storage
  Boom.storage
end
version() click to toggle source

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

Returns a String identifying the version number.

# File lib/boom/command.rb, line 341
def version
  output "You're running boom #{Boom::VERSION}. Congratulations!"
end