Class: Birdwatcher::Console
- Inherits:
-
Object
- Object
- Birdwatcher::Console
- Includes:
- Singleton
- Defined in:
- lib/birdwatcher/console.rb
Constant Summary collapse
- DEFAULT_AUTO_COMPLETION_STRINGS =
[].freeze
- DB_MIGRATIONS_PATH =
File.("../../../db/migrations", __FILE__).freeze
- LINE_SEPARATOR =
("=" * 80).freeze
Instance Attribute Summary collapse
-
#current_module ⇒ Object
Returns the value of attribute current_module.
-
#current_workspace ⇒ Object
Returns the value of attribute current_workspace.
-
#database ⇒ Object
readonly
Returns the value of attribute database.
Instance Method Summary collapse
- #auto_completion_strings ⇒ Object
- #error(message) ⇒ Object
- #fatal(message) ⇒ Object
- #handle_input(input) ⇒ Object
- #info(message) ⇒ Object
-
#initialize ⇒ Console
constructor
A new instance of Console.
- #klout_client ⇒ Object
- #line_separator ⇒ Object
- #newline ⇒ Object
- #output(data, newline = true) ⇒ Object
- #output_formatted(*args) ⇒ Object
- #start! ⇒ Object
- #task(message, fatal = false, &block) ⇒ Object
- #twitter_client ⇒ Object
- #warn(message) ⇒ Object
Constructor Details
#initialize ⇒ Console
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_module ⇒ Object
Returns the value of attribute current_module.
9 10 11 |
# File 'lib/birdwatcher/console.rb', line 9 def current_module @current_module end |
#current_workspace ⇒ Object
Returns the value of attribute current_workspace.
9 10 11 |
# File 'lib/birdwatcher/console.rb', line 9 def current_workspace @current_workspace end |
#database ⇒ Object (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_strings ⇒ Object
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() output "[-] ".bold.light_red + end |
#fatal(message) ⇒ Object
91 92 93 |
# File 'lib/birdwatcher/console.rb', line 91 def fatal() output "[-]".white.bold.on_red + " #{}" 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() output "[+] ".bold.light_blue + end |
#klout_client ⇒ Object
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_separator ⇒ Object
65 66 67 |
# File 'lib/birdwatcher/console.rb', line 65 def line_separator output LINE_SEPARATOR end |
#newline ⇒ Object
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! bootstrap! Readline.completion_proc = proc do |s| = File.(s) Birdwatcher::Console.instance.auto_completion_strings.grep(/\A#{Regexp.escape(s)}/) + Dir["#{}*"].grep(/^#{Regexp.escape()}/) 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(, fatal = false, &block) output("[+] ".bold.light_blue + , false) yield block output " done".bold.light_green rescue => e output " failed".bold.light_red error "#{e.class}: ".bold + e. exit(1) if fatal end |
#twitter_client ⇒ Object
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() output "[!] ".bold.light_yellow + end |