tp 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/keyboard.rb CHANGED
@@ -1,7 +1,63 @@
1
1
  module Keyboard
2
2
  extend self
3
3
 
4
- def wait_for_enter
5
- $stdin.gets
4
+ def read
5
+ begin
6
+ old_stty = `stty -g`
7
+ system "stty raw -echo"
8
+
9
+ chr = STDIN.getc.chr
10
+
11
+ if chr == "\e"
12
+ extra_thread = Thread.new {
13
+ chr = chr + STDIN.getc.chr
14
+ chr = chr + STDIN.getc.chr
15
+ }
16
+
17
+ extra_thread.join 0.00001
18
+ extra_thread.kill
19
+ end
20
+ ensure
21
+ system "stty #{old_stty}"
22
+ end
23
+
24
+ symbolize chr
25
+ end
26
+
27
+ def wait_for_return
28
+ loop do
29
+ result = read
30
+
31
+ return result if result == :return
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def symbolize(character)
38
+ case character
39
+ when " "
40
+ :space
41
+ when "\t"
42
+ :tab
43
+ when "\r", "\n"
44
+ :return
45
+ when "\e"
46
+ :escape
47
+ when "\e[A"
48
+ :up
49
+ when "\e[B"
50
+ :down
51
+ when "\e[D"
52
+ :left
53
+ when "\e[C"
54
+ :right
55
+ when "\177"
56
+ :backspace
57
+ when "\004"
58
+ :delete
59
+ else
60
+ character
61
+ end
6
62
  end
7
63
  end
data/lib/screen.rb CHANGED
@@ -9,6 +9,13 @@ module Screen
9
9
  `tput lines`.to_i
10
10
  end
11
11
 
12
+ def suggest(x, y)
13
+ y.times do |n|
14
+ print "\n" if n.nonzero?
15
+ print "\u2588" * x
16
+ end
17
+ end
18
+
12
19
  def width
13
20
  `tput cols`.to_i
14
21
  end
data/lib/tp/presenter.rb CHANGED
@@ -7,10 +7,22 @@ module TP
7
7
  end
8
8
 
9
9
  def present
10
- slides.each do |slide|
10
+ Screen.suggest 30, 8
11
+ Keyboard.wait_for_return
12
+
13
+ loop do
11
14
  Screen.clear!
12
- show_slide slide
13
- Keyboard.wait_for_enter
15
+
16
+ show_slide slide_deck.current or break
17
+
18
+ case Keyboard.read
19
+ when :right, :down, :space, :return, 'l', 'k'
20
+ slide_deck.next
21
+ when :left, :up, :backspace, 'h', 'j'
22
+ slide_deck.previous
23
+ when 'q'
24
+ break
25
+ end
14
26
  end
15
27
 
16
28
  Screen.clear!
@@ -24,7 +36,13 @@ module TP
24
36
  result.map { |string| Slide.new string }
25
37
  end
26
38
 
39
+ def slide_deck
40
+ @slide_deck ||= SlideDeck.new slides
41
+ end
42
+
27
43
  def show_slide(slide)
44
+ return unless slide
45
+
28
46
  buffer = slide.header.center Screen.width
29
47
 
30
48
  if slide.body
@@ -38,6 +56,8 @@ module TP
38
56
  end
39
57
 
40
58
  print buffer
59
+
60
+ true
41
61
  end
42
62
 
43
63
  def bullet
@@ -0,0 +1,30 @@
1
+ module TP
2
+ class SlideDeck
3
+ attr_accessor :cursor, :slides
4
+
5
+ def initialize(slides)
6
+ self.cursor = 0
7
+ self.slides = slides
8
+ end
9
+
10
+ def [](index)
11
+ slides[index]
12
+ end
13
+
14
+ def current
15
+ slides[cursor]
16
+ end
17
+
18
+ def next
19
+ self.cursor += 1
20
+
21
+ current
22
+ end
23
+
24
+ def previous
25
+ self.cursor -= 1
26
+
27
+ current
28
+ end
29
+ end
30
+ end
data/lib/tp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module TP
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/tp.rb CHANGED
@@ -3,5 +3,6 @@ require 'screen'
3
3
 
4
4
  require 'tp/presenter'
5
5
  require 'tp/slide'
6
+ require 'tp/slide_deck'
6
7
  require 'tp/version'
7
8
 
@@ -3,13 +3,4 @@ require 'keyboard'
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Keyboard do
6
- describe ".wait_for_enter" do
7
- subject(:wait_for_enter) { klass.wait_for_enter }
8
-
9
- it "waits for enter" do
10
- $stdin.should_receive :gets
11
-
12
- wait_for_enter
13
- end
14
- end
15
6
  end
@@ -19,13 +19,14 @@ describe TP::Presenter do
19
19
  }
20
20
 
21
21
  before :each do
22
- Screen.stub width: 20, height: 20
22
+ Keyboard.stub read: :return
23
+ Screen.stub width: 20, height: 20, suggest: nil
23
24
  end
24
25
 
25
26
  describe "#present" do
26
27
  it "works" do
27
- Keyboard.should_receive(:wait_for_enter).exactly(3).times
28
- Screen.should_receive(:clear!).exactly(4).times
28
+ Keyboard.should_receive(:wait_for_return).exactly(1).times
29
+ Screen.should_receive(:clear!).exactly(5).times
29
30
 
30
31
  presenter.should_receive(:print).exactly(3).times
31
32
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -97,6 +97,7 @@ files:
97
97
  - lib/tp.rb
98
98
  - lib/tp/presenter.rb
99
99
  - lib/tp/slide.rb
100
+ - lib/tp/slide_deck.rb
100
101
  - lib/tp/version.rb
101
102
  - spec/lib/keyboard_spec.rb
102
103
  - spec/lib/screen_spec.rb