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
HISTORY_FILE_NAME =
".birdwatcher_history".freeze
HISTORY_FILE_LOCATION =
File.join(Dir.home, HISTORY_FILE_NAME).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



14
15
16
17
# File 'lib/birdwatcher/console.rb', line 14

def initialize
  @output_mutex = Mutex.new
  @spool_mutex  = Mutex.new
end

Instance Attribute Details

#current_moduleObject

Returns the value of attribute current_module.



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

def current_module
  @current_module
end

#current_workspaceObject

Returns the value of attribute current_workspace.



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

def current_workspace
  @current_workspace
end

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

#spoolObject

Returns the value of attribute spool.



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

def spool
  @spool
end

Instance Method Details

#auto_completion_stringsObject



50
51
52
53
54
55
56
57
# File 'lib/birdwatcher/console.rb', line 50

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

#confirm(question) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/birdwatcher/console.rb', line 104

def confirm(question)
  question = "#{question} (y/n) "
  save_to_spool(question)
  if HighLine.agree("#{question}")
    save_to_spool("y\n")
    true
  else
    save_to_spool("n\n")
    false
  end
end

#error(message) ⇒ Object



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

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

#fatal(message) ⇒ Object



100
101
102
# File 'lib/birdwatcher/console.rb', line 100

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

#handle_input(input) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/birdwatcher/console.rb', line 35

def handle_input(input)
  input.strip!
  save_command_to_history(input)
  save_to_spool("#{input}\n")
  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



77
78
79
# File 'lib/birdwatcher/console.rb', line 77

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

#klout_clientObject



129
130
131
132
133
134
# File 'lib/birdwatcher/console.rb', line 129

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

#line_separatorObject



73
74
75
# File 'lib/birdwatcher/console.rb', line 73

def line_separator
  output LINE_SEPARATOR
end

#newlineObject



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

def newline
  output ""
end

#output(data, newline = true) ⇒ Object



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

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

#output_formatted(*args) ⇒ Object



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

def output_formatted(*args)
  output(sprintf(*args), false)
end

#page_text(text) ⇒ Object



116
117
118
119
120
# File 'lib/birdwatcher/console.rb', line 116

def page_text(text)
  save_to_spool(text)
  ::TTY::Pager::SystemPager.new.page(text)
rescue Errno::EPIPE
end

#start!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/birdwatcher/console.rb', line 19

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 = ""
  load_command_history
  while input = Readline.readline(prompt_line, true)
    save_to_spool(prompt_line)
    input = input.to_s.strip
    handle_input(input) unless input.empty?
  end
end

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



81
82
83
84
85
86
87
88
89
90
# File 'lib/birdwatcher/console.rb', line 81

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
  e.backtrace.each { |l| error l } if debugging_enabled?
  exit(1) if fatal
end

#twitter_clientObject



122
123
124
125
126
127
# File 'lib/birdwatcher/console.rb', line 122

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

#warn(message) ⇒ Object



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

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