Class: Birdwatcher::Console

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/birdwatcher/console.rb

Constant Summary collapse

DEFAULT_AUTO_COMPLETION_STRINGS =
[].freeze
DB_MIGRATIONS_PATH =
File.expand_path("../../../db/migrations", __FILE__).freeze
LINE_SEPARATOR =
("=" * 80).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



12
13
14
# File 'lib/birdwatcher/console.rb', line 12

def initialize
  @output_mutex = Mutex.new
end

Instance Attribute Details

#current_moduleObject

Returns the value of attribute current_module.



9
10
11
# File 'lib/birdwatcher/console.rb', line 9

def current_module
  @current_module
end

#current_workspaceObject

Returns the value of attribute current_workspace.



9
10
11
# File 'lib/birdwatcher/console.rb', line 9

def current_workspace
  @current_workspace
end

#databaseObject (readonly)

Returns the value of attribute database.



10
11
12
# File 'lib/birdwatcher/console.rb', line 10

def database
  @database
end

Instance Method Details

#auto_completion_stringsObject



43
44
45
46
47
48
49
50
# File 'lib/birdwatcher/console.rb', line 43

def auto_completion_strings
  if !@auto_completion_strings
    @auto_completion_strings = DEFAULT_AUTO_COMPLETION_STRINGS
    commands.each { |c| @auto_completion_strings += c.auto_completion_strings }
    @auto_completion_strings += Birdwatcher::Module.module_paths
  end
  @auto_completion_strings
end

#error(message) ⇒ Object



83
84
85
# File 'lib/birdwatcher/console.rb', line 83

def error(message)
  output "[-] ".bold.light_red + message
end

#fatal(message) ⇒ Object



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

def fatal(message)
  output "[-]".white.bold.on_red + " #{message}"
end

#handle_input(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/birdwatcher/console.rb', line 30

def handle_input(input)
  input.strip!
  command_name, argument_line = input.split(" ", 2).map(&:strip)
  command_name.downcase
  commands.each do |command|
    next unless command.has_name?(command_name)
    command.new.execute(argument_line)
    return true
  end
  error("Unknown command: #{command_name.bold}")
  false
end

#info(message) ⇒ Object



69
70
71
# File 'lib/birdwatcher/console.rb', line 69

def info(message)
  output "[+] ".bold.light_blue + message
end

#klout_clientObject



102
103
104
105
106
107
# File 'lib/birdwatcher/console.rb', line 102

def klout_client
  if !@klout_clients
    @klout_clients = create_klout_clients!
  end
  @klout_clients.sample
end

#line_separatorObject



65
66
67
# File 'lib/birdwatcher/console.rb', line 65

def line_separator
  output LINE_SEPARATOR
end

#newlineObject



61
62
63
# File 'lib/birdwatcher/console.rb', line 61

def newline
  with_output_mutex { puts }
end

#output(data, newline = true) ⇒ Object



52
53
54
55
# File 'lib/birdwatcher/console.rb', line 52

def output(data, newline = true)
  data = "#{data}\n" if newline
  with_output_mutex { print data }
end

#output_formatted(*args) ⇒ Object



57
58
59
# File 'lib/birdwatcher/console.rb', line 57

def output_formatted(*args)
  with_output_mutex { printf(*args) }
end

#start!Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/birdwatcher/console.rb', line 16

def start!
  print_banner
  bootstrap!
  Readline.completion_proc = proc do |s|
    expanded_s = File.expand_path(s)
    Birdwatcher::Console.instance.auto_completion_strings.grep(/\A#{Regexp.escape(s)}/) + Dir["#{expanded_s}*"].grep(/^#{Regexp.escape(expanded_s)}/)
  end
  Readline.completion_append_character = ""
  while input = Readline.readline(prompt_line, true)
    input = input.to_s.strip
    handle_input(input) unless input.empty?
  end
end

#task(message, fatal = false, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/birdwatcher/console.rb', line 73

def task(message, fatal = false, &block)
  output("[+] ".bold.light_blue + message, false)
  yield block
  output " done".bold.light_green
rescue => e
  output " failed".bold.light_red
  error "#{e.class}: ".bold + e.message
  exit(1) if fatal
end

#twitter_clientObject



95
96
97
98
99
100
# File 'lib/birdwatcher/console.rb', line 95

def twitter_client
  if !@twitter_clients
    @twitter_clients = create_twitter_clients!
  end
  @twitter_clients.sample
end

#warn(message) ⇒ Object



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

def warn(message)
  output "[!] ".bold.light_yellow + message
end