Skip to content

Commit

Permalink
Merge pull request #12 from expez/keyboard
Browse files Browse the repository at this point in the history
Add support for keyboard events
  • Loading branch information
jfredett committed Mar 6, 2013
2 parents 5d44c8b + ba6ae36 commit 3f3990f
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .rvmrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
# Only full ruby name is supported here, for short names use:
# echo "rvm use 1.9.3" > .rvmrc
environment_id="rbx-head@hatter"
environment_id="ruby-head@hatter"

# Uncomment the following lines if you want to verify rvm version per project
# rvmrc_rvm_version="1.18.3 (stable)" # 1.10.1 seams as a safe start
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ source 'http://rubygems.org'

group :development, :test do
gem 'rspec'
gem 'rspec-spies'
gem 'pry'
gem 'pry-doc'
gem 'rake'
gem 'mail'
gem 'rb_termbox'
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ GEM
method_source (~> 0.8)
slop (~> 3.4)
win32console (~> 1.3)
pry-doc (0.4.4)
pry (>= 0.9.9.6)
yard (~> 0.8.1)
rake (10.0.3)
rb_termbox (0.1.0)
ffi
Expand All @@ -43,11 +46,14 @@ GEM
rspec-expectations (2.12.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.12.2)
rspec-spies (2.1.3)
rspec (~> 2.0)
slop (3.4.3)
treetop (1.4.12)
polyglot
polyglot (>= 0.3.1)
win32console (1.3.2-x86-mingw32)
yard (0.8.5.2)

PLATFORMS
ruby
Expand All @@ -58,6 +64,8 @@ DEPENDENCIES
configtoolkit
mail
pry
pry-doc
rake
rb_termbox
rspec
rspec-spies
21 changes: 21 additions & 0 deletions lib/command_factory.rb
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
9 changes: 9 additions & 0 deletions lib/commands/no_op.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class NoOpCommand

def execute
end

def unexecute
end

end
15 changes: 15 additions & 0 deletions lib/commands/quit.rb
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
4 changes: 2 additions & 2 deletions lib/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def initialize(config_file = CONFIG_FILE)
end

class Colors < ConfigToolkit::BaseConfig
add_required_param(:foreground, String)
add_required_param(:background, String)
add_required_param(:foreground, String)
add_required_param(:background, String)
end

add_required_param(:maildir, String)
Expand Down
20 changes: 14 additions & 6 deletions lib/hatter.rb
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
14 changes: 14 additions & 0 deletions lib/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ def initialize
initialize_views
end

# Updates the terminal view by redrawing all currently active views.
def draw
Termbox.tb_clear
@views.each {|view| view.draw}
Termbox.tb_present
end

# Shuts off the terminal in a safe manner and then exits the ruby process.
def shutdown
Termbox.tb_shutdown
exit
end

# Blocks until the user presses a key
# @return [Symbol] the key pressed as a symbol
def get_user_input
Termbox.tb_poll_event(@event)
ascii_to_symbol @event[:ch]
end

private
Expand All @@ -36,6 +46,10 @@ def initialize_views
FolderView.new(folder_view_location)]
end

def ascii_to_symbol(i)
i.chr.to_sym
end

def initialize_termbox
libtermbox_path = Configuration.instance.termbox_library_path
Termbox.initialize_library libtermbox_path
Expand Down
21 changes: 21 additions & 0 deletions spec/command_factory_spec.rb
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
20 changes: 20 additions & 0 deletions spec/commands/quit_spec.rb
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
6 changes: 6 additions & 0 deletions spec/commands/shared_examples_for_commands.rb
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
12 changes: 9 additions & 3 deletions spec/spec_helper.rb
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'

0 comments on commit 3f3990f

Please sign in to comment.