Skip to content

Commit

Permalink
Rework help system (again) to reduce channel noise.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jan 16, 2013
1 parent 38f59a5 commit 5f7962e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
19 changes: 12 additions & 7 deletions plugin/help_cinch_plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ module TurbotPlugins
class Help
include Cinch::Plugin

PluginHandler.add_plugin(self)

def self.help
PluginCommand.new('.help', 'This help display.')
end

set :prefix, PREFIX

match /help/, method: :help
match /help$/, method: :help
def help(m)
m.reply pretty_help.to_s
m.reply "Hi, I'm turbot. I know how to respond to many commands (I also accept pull requests for more). For more information on any of them execute .help <command name>."
m.reply PluginHandler.matchers.join(", ")
end

def pretty_help
plugin_commands = PluginHandler.plugins.inject([]) {|m,p| m += Array(p.help) }
rows = plugin_commands.collect {|c| [c.matchers,c.description]}

Terminal::Table.new :headings => ['Matchers', 'Description'], :rows => rows
match /help (.+)/, method: :help_command
def help_command(m, matcher)
m.reply PluginHandler.command(matcher).display_row.join(' - ')
end
end
end
16 changes: 16 additions & 0 deletions plugin_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ def self.plugins
def self.add_plugin(plugin)
self.plugins << plugin
end

def self.commands
plugins.inject([]) {|m,p| m += Array(p.help) }
end

def self.matchers
commands.collect{|c| c.matchers}
end

def self.command(matcher)
commands.detect{|c| c.matchers =~ /#{matcher}/}
end
end

class PluginCommand
Expand All @@ -15,4 +27,8 @@ def initialize(matchers, description)
self.matchers = matchers
self.description = description
end

def display_row
[matchers, description]
end
end
11 changes: 9 additions & 2 deletions spec/help_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ def self.help

context "#help" do
it "should print the plugins help message." do

m.should_receive(:reply).with("+-------------+----------------------------------+\n| Matchers | Description |\n+-------------+----------------------------------+\n| .nextmeetup | Get the next meetup information. |\n+-------------+----------------------------------+")
m.should_receive(:reply).with("Hi, I'm turbot. I know how to respond to many commands (I accept pull requests for more).")
m.should_receive(:reply).with(".nextmeetup")

plugin.help(m)
end
end

context "#help_command" do
it "should print the detailed help for the given matcher." do
m.should_receive(:reply).with(".nextmeetup - Get the next meetup information.")
plugin.help_command(m, '.nextmeetup')
end
end
end

0 comments on commit 5f7962e

Please sign in to comment.