readline-ng 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -4,7 +4,7 @@
4
4
  # development environment upon cd'ing into the directory
5
5
 
6
6
  # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional.
7
- environment_id="ruby-1.9.3-p0"
7
+ environment_id="ruby-1.9.3-p125"
8
8
 
9
9
  #
10
10
  # Uncomment following line if you want options to be set only for given project.
@@ -65,7 +65,7 @@ fi
65
65
  if [[ $- == *i* ]] # check for interactive shells
66
66
  then
67
67
  echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
68
- else
68
+ else
69
69
  echo "Using: $GEM_HOME" # don't use colors in interactive shells
70
70
  fi
71
71
 
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/README CHANGED
@@ -2,3 +2,15 @@ Readline-NG is /not/ a drop in replacement for readline.
2
2
 
3
3
  It addresses a very specific need I had inside a twitter client, but
4
4
  hopefully it's of use to someone else, too.
5
+
6
+ Readline relies on being able to poll for input often, leading to a
7
+ hideously inefficient event loop, but generally
8
+
9
+ reader = ReadlineNG::Reader.new
10
+ loop do
11
+ reader.tick
12
+ reader.each_line do |line|
13
+ # Handle full line of input
14
+ reader.puts_above("user input #{line}")
15
+ end
16
+ end
@@ -2,7 +2,7 @@ module ReadlineNG
2
2
 
3
3
  VERSION_MAJOR = 0
4
4
  VERSION_MINOR = 0
5
- VERSION_PATCH = 4
5
+ VERSION_PATCH = 5
6
6
  VERSION = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_PATCH}"
7
7
 
8
8
  CONTROL_BS = "\x08"
@@ -12,8 +12,6 @@ module ReadlineNG
12
12
 
13
13
  KB_BS = "\x7F"
14
14
  KB_CR = "\x0d"
15
- KB_LEFT = "LEFT" # XXX PLACEHOLDERS
16
- KB_RIGHT = "RIGHT"
17
15
 
18
16
  BLANK = " "
19
17
 
@@ -21,39 +19,29 @@ module ReadlineNG
21
19
 
22
20
  @@initialized = false
23
21
 
24
- # TODO Arrange for the terminal to be in raw mode etc.
25
22
  # XXX This probably needs to be a singleton, having more than one doesn't
26
23
  # make a whole lot of sense, although potentially giving out rope is not a
27
24
  # terrible idea here
28
25
 
29
- attr_accessor :lines, :visible, :polling_resolution
26
+ attr_accessor :lines, :visible
30
27
 
28
+ # A third party dev can overload filter to implement their own actions
31
29
  def filter
32
30
  end
33
31
 
34
- def initialize(visible=true, opts = {})
32
+ def initialize(visible=true)
35
33
  @buf = ""
36
- @ind = 0
37
34
  @visible = visible
38
35
  @lines = []
39
- @polling_resolution = opts[:polling_resolution] || 20
40
36
  if @@initialized
41
37
  STDERR.puts "A ReadlineNG reader is already instanciated, expect weirdness"
42
38
  else
43
39
  @@initialized = true
44
40
  end
45
41
 
46
- stty_saved = `stty -g`
47
- `stty -echo raw`
42
+ setup
48
43
  at_exit do
49
- `stty #{stty_saved}`
50
- end
51
- end
52
-
53
- def sleep(n)
54
- (n * polling_resolution).times do
55
- tick
56
- sleep 1.0/polling_resolution
44
+ teardown
57
45
  end
58
46
  end
59
47
 
@@ -68,7 +56,7 @@ module ReadlineNG
68
56
 
69
57
  def tick
70
58
  t = STDIN.read_nonblock(128)
71
- process(t)
59
+ t.each_char { |c| process(c) }
72
60
  filter # Expect a 3rd party dev to override this
73
61
 
74
62
  raise Interrupt if @buf.include?(CONTROL_INT)
@@ -103,46 +91,35 @@ module ReadlineNG
103
91
  when KB_BS
104
92
  @buf.chop!
105
93
  backspace
106
- when KB_LEFT
107
- unless @ind == 0
108
- _print CONTROL_BS
109
- @ind -= 1
110
- end
111
- when KB_RIGHT
112
- unless @ind == @buf.length
113
- _print @buf[@ind]
114
- @ind += 1
115
- end
116
94
  else
117
- @buf.insert(@ind, c)
118
- @ind += 1
119
- if @ind == @buf.length
120
- _print c
121
- else
122
- # TODO Only backspace to the new character
123
- redraw
124
- end
95
+ @buf += c
96
+ _print c
125
97
  end
126
98
  end
127
99
 
128
100
  def backspace(n=1)
129
- print CONTROL_BS*n,BLANK*n,CONTROL_BS*n
101
+ _print CONTROL_BS*n,BLANK*n,CONTROL_BS*n
130
102
  end
131
103
 
132
- def redraw
133
- backspace(@buf.length)
134
- _print(@buf)
104
+ def _print(*c)
105
+ print *c if visible
135
106
  end
136
107
 
137
- def _print(c)
138
- print c if visible
108
+ def _puts(*c)
109
+ puts *c if visible
139
110
  end
140
111
 
141
- def _puts(c)
142
- puts c if visible
112
+ def setup
113
+ @stty_saved = `stty -g`
114
+ `stty -echo raw`
115
+ end
116
+
117
+ def teardown
118
+ `stty #{@stty_saved}`
143
119
  end
144
120
 
145
121
 
122
+
146
123
  end
147
124
  end
148
125
 
@@ -11,6 +11,8 @@ Gem::Specification.new do |s|
11
11
  s.summary = "Essentially, readline++"
12
12
  s.description = s.summary
13
13
 
14
+ s.add_development_dependency 'rspec'
15
+
14
16
  s.files = `git ls-files`.split("\n")
15
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
18
  s.require_paths = ["lib"]
@@ -0,0 +1,62 @@
1
+ require 'readline-ng'
2
+ require 'mocha'
3
+
4
+ # Hook to reset internal state between runs
5
+ module ReadlineNG
6
+ class Reader
7
+ def self.initialized=(v)
8
+ @@initialized = v
9
+ end
10
+ end
11
+ end
12
+
13
+ describe ReadlineNG do
14
+
15
+ before(:each) do
16
+ @reader = ReadlineNG::Reader.new
17
+ @reader.visible = false
18
+ end
19
+
20
+ after(:each) do
21
+ @reader.send(:teardown)
22
+ ReadlineNG::Reader.initialized = false
23
+ end
24
+
25
+ it "should only return full lines of input" do
26
+ STDIN.stub(:read_nonblock).and_return("in", "put", "\r")
27
+ @reader.tick # gets "in"
28
+ @reader.lines.should be_empty
29
+ @reader.tick # gets "put"
30
+ @reader.lines.should be_empty
31
+ @reader.tick # gets "\r"
32
+ @reader.line.should == "input"
33
+ end
34
+
35
+ it "should handle newlines in amongst inputs" do
36
+ STDIN.stub(:read_nonblock).and_return("in", "put\racros", "slines\r...")
37
+ @reader.tick # gets "in"
38
+ @reader.lines.should be_empty
39
+ @reader.tick # gets "put\racros"
40
+ @reader.tick # gets "slines\r..."
41
+ @reader.lines.should == ["input", "acrosslines"]
42
+ end
43
+
44
+ it "should call a filter method if defined" do
45
+ STDIN.stub(:read_nonblock).and_return("in", "put")
46
+ @reader.expects(:filter).twice
47
+ @reader.tick
48
+ @reader.tick
49
+ end
50
+
51
+ it "should let the user backspace out errors" do
52
+ STDIN.stub(:read_nonblock).and_return("in", "put", "\x7F"*3, "tro\r")
53
+ @reader.get_line.should == "intro"
54
+ end
55
+
56
+ it "should not fall over trying to backspace an empty buffer" do
57
+ STDIN.stub(:read_nonblock).and_return("in", "\x7F"*8, "input\r")
58
+ @reader.get_line.should == "input"
59
+ end
60
+
61
+ end
62
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: readline-ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,19 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-02-19 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2012-02-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &7211240 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *7211240
14
25
  description: Essentially, readline++
15
26
  email:
16
27
  - richo@psych0tik.net
@@ -19,11 +30,11 @@ extensions: []
19
30
  extra_rdoc_files: []
20
31
  files:
21
32
  - .rvmrc
33
+ - Gemfile
22
34
  - README
23
35
  - lib/readline-ng.rb
24
36
  - readline-ng.gemspec
25
- - test/newline.rb
26
- - test/test_helper.rb
37
+ - spec/readline-ng_spec.rb
27
38
  homepage: http://github.com/richoH/readline-ng
28
39
  licenses: []
29
40
  post_install_message:
@@ -44,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
44
55
  version: '0'
45
56
  requirements: []
46
57
  rubyforge_project:
47
- rubygems_version: 1.8.10
58
+ rubygems_version: 1.8.16
48
59
  signing_key:
49
60
  specification_version: 3
50
61
  summary: Essentially, readline++
@@ -1,11 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require './lib/readline-ng'
3
-
4
- puts "Should output"
5
- puts "line1\nline2"
6
- puts ""
7
-
8
- reader = ReadlineNG::Reader.new
9
-
10
- reader.puts_above("line1\nline2")
11
-
File without changes