-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from expez/keyboard
Add support for keyboard events
- Loading branch information
Showing
13 changed files
with
142 additions
and
12 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
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,21 @@ | ||
class CommandFactory | ||
|
||
def initialize(terminal) | ||
@terminal = terminal | ||
commands = File.join(File.dirname(__FILE__), "commands") + "/*" | ||
Dir[commands].each { |file| eval "require '#{file}'" } | ||
end | ||
|
||
# Instantiates the command object corresponding to `key`. | ||
# @param key [Symbol] the key the user pressed on the keyboard. | ||
# @return [Command] the command the user wishes to execute. | ||
def command(key) | ||
case key | ||
when :q | ||
QuitCommand.new(@terminal) | ||
else | ||
NoOpCommand.new | ||
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,9 @@ | ||
class NoOpCommand | ||
|
||
def execute | ||
end | ||
|
||
def unexecute | ||
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,15 @@ | ||
require 'terminal' | ||
|
||
class QuitCommand | ||
|
||
def initialize terminal | ||
@terminal = terminal | ||
end | ||
|
||
def execute | ||
@terminal.shutdown | ||
end | ||
|
||
def unexecute | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
require 'terminal' | ||
require 'command_factory' | ||
|
||
terminal = Terminal.new | ||
begin | ||
terminal.draw | ||
sleep(10) | ||
ensure | ||
terminal.shutdown | ||
unless ENV["HEADLESS"] | ||
begin | ||
terminal = Terminal.new | ||
command_factory = CommandFactory.new terminal | ||
while true do | ||
terminal.draw | ||
key = terminal.get_user_input | ||
cmd = command_factory.cmd key | ||
cmd.execute | ||
end | ||
rescue | ||
terminal.shutdown | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
|
||
describe CommandFactory do | ||
|
||
subject(:factory) { CommandFactory.new nil } | ||
let(:command) { factory.command(:foo) } | ||
|
||
context "creates commands which:" do | ||
subject { command } | ||
it_behaves_like "a command" | ||
end | ||
|
||
let(:key) { :q } | ||
|
||
describe "instantiates commands matching the user's input." do | ||
context "the returned command" do | ||
subject { factory.command(key) } | ||
it { should be_a(QuitCommand) } | ||
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,20 @@ | ||
require 'spec_helper' | ||
|
||
describe QuitCommand do | ||
|
||
|
||
let(:terminal) { double('terminal', shutdown: true ) } | ||
let(:command) { QuitCommand.new terminal } | ||
|
||
subject{ command } | ||
it_behaves_like "a command" | ||
|
||
describe "after #execute" do | ||
context "the terminal" do | ||
before { command.execute } | ||
subject { terminal } | ||
|
||
it { should have_received :shutdown } | ||
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,6 @@ | ||
shared_examples "a command" do | ||
|
||
it { should respond_to :execute } | ||
it { should respond_to :unexecute } | ||
|
||
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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
require 'rspec' | ||
require 'rspec-spies' | ||
|
||
require './lib/hatter.rb' | ||
require './lib/configuration.rb' | ||
require './lib/views/view.rb' | ||
ENV["HEADLESS"] = "ya, don't GUI for testing." | ||
|
||
require './lib/hatter' | ||
require './lib/configuration' | ||
require './lib/views/view' | ||
require './lib/command_factory' | ||
require './lib/commands/quit' | ||
require './spec/commands/shared_examples_for_commands' |