forked from guard/guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is the initial coolline based interactor, that makes use of Ruby 1.9.3 io/console library.
- Loading branch information
Showing
9 changed files
with
290 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Guard | ||
|
||
# Module for providing word completion to an interactor. | ||
# | ||
module Completion | ||
|
||
COMPLETION_ACTIONS = %w[help reload exit pause notification] | ||
|
||
# Auto complete the given word. | ||
# | ||
# @param [String] word the partial word | ||
# @return [Array<String>] the matching words | ||
# | ||
def auto_complete(word) | ||
completion_list.grep(/^#{ Regexp.escape(word) }/) | ||
end | ||
|
||
# Get the auto completion list. | ||
# | ||
# @return [Array<String>] the list of words | ||
# | ||
def completion_list | ||
groups = ::Guard.groups.map { |group| group.name.to_s } | ||
guards = ::Guard.guards.map { |guard| guard.class.to_s.downcase.sub('guard::', '') } | ||
|
||
COMPLETION_ACTIONS + groups + guards - ['default'] | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'guard/interactors/completion' | ||
|
||
module Guard | ||
|
||
# Interactor that uses coolline for getting the user input. | ||
# This enables history support and auto-completion, | ||
# | ||
class CoollineInteractor < Interactor | ||
include ::Guard::Completion | ||
|
||
# Template method for checking if the Interactor is | ||
# available in the current environment? | ||
# | ||
# @param [Boolean] silent true if no error messages should be shown | ||
# @return [Boolean] the availability status | ||
# | ||
def self.available?(silent = false) | ||
if RbConfig::CONFIG['RUBY_PROGRAM_VERSION'] == '1.9.3' | ||
require 'coolline' | ||
true | ||
else | ||
::Guard::UI.error 'The :coolline interactor runs only on Ruby 1.9.3.' unless silent | ||
false | ||
end | ||
|
||
rescue | ||
::Guard::UI.error "Please add \"gem 'coolline'\" to your Gemfile and run Guard with \"bundle exec\"." unless silent | ||
false | ||
end | ||
|
||
# Read a line from stdin with Readline. | ||
# | ||
def read_line | ||
coolline = Coolline.new do |cool| | ||
cool.transform_proc = proc do | ||
cool.line | ||
end | ||
|
||
cool.completion_proc = proc do | ||
word = cool.completed_word | ||
auto_complete(word) | ||
end | ||
end | ||
|
||
while line = coolline.readline(prompt) | ||
process_input(line) | ||
end | ||
end | ||
|
||
# The current interactor prompt | ||
# | ||
# @return [String] the prompt to show | ||
# | ||
def prompt | ||
::Guard.listener.paused? ? 'p> ' : '>> ' | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
require 'spec_helper' | ||
require 'guard/interactors/completion' | ||
|
||
describe Guard::Completion do | ||
subject { Guard::ReadlineInteractor.new } | ||
|
||
describe '#auto_complete' do | ||
it 'returns the matching list of words' do | ||
subject.should_receive(:completion_list).any_number_of_times.and_return %w[help reload exit pause notification backend frontend foo foobar] | ||
subject.auto_complete('f').should =~ ['frontend', 'foo', 'foobar'] | ||
subject.auto_complete('foo').should =~ ['foo', 'foobar'] | ||
subject.auto_complete('he').should =~ ['help'] | ||
subject.auto_complete('re').should =~ ['reload'] | ||
end | ||
end | ||
|
||
describe "#completion_list" do | ||
before(:all) do | ||
class Guard::Foo < Guard::Guard; end | ||
class Guard::FooBar < Guard::Guard; end | ||
end | ||
|
||
before(:each) do | ||
guard = ::Guard | ||
guard.setup_guards | ||
guard.setup_groups | ||
@backend_group = guard.add_group(:backend) | ||
@frontend_group = guard.add_group(:frontend) | ||
@foo_guard = guard.add_guard(:foo, [], [], { :group => :backend }) | ||
@foo_bar_guard = guard.add_guard('foo-bar', [], [], { :group => :frontend }) | ||
end | ||
|
||
after(:all) do | ||
::Guard.instance_eval do | ||
remove_const(:Foo) | ||
remove_const(:FooBar) | ||
end | ||
end | ||
|
||
it 'creates the list of string to auto complete' do | ||
subject.completion_list.should =~ %w[help reload exit pause notification backend frontend foo foobar] | ||
end | ||
|
||
it 'does not include the default scope' do | ||
subject.completion_list.should_not include('default') | ||
end | ||
end | ||
end |
Oops, something went wrong.