Class: ReadlineNG::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/readline-ng.rb

Constant Summary collapse

@@initialized =
false

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(visible = true, opts = {}) ⇒ Reader

Returns a new instance of Reader.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/readline-ng.rb', line 41

def initialize(visible=true, opts = {})
  @buf = ""
  @chunked = ChunkedString.new
  @index = 0
  @visible = visible
  @lines = []
  @polling_resolution = opts[:polling_resolution] || 20
  if @@initialized
    STDERR.puts "A ReadlineNG reader is already instanciated, expect weirdness"
  else
    @@initialized = true
  end

  setup
  at_exit do
    teardown
  end
end

Instance Attribute Details

#linesObject

XXX This probably needs to be a singleton, having more than one doesn’t make a whole lot of sense, although potentially giving out rope is not a terrible idea here



35
36
37
# File 'lib/readline-ng.rb', line 35

def lines
  @lines
end

#polling_resolutionObject

XXX This probably needs to be a singleton, having more than one doesn’t make a whole lot of sense, although potentially giving out rope is not a terrible idea here



35
36
37
# File 'lib/readline-ng.rb', line 35

def polling_resolution
  @polling_resolution
end

#visibleObject

XXX This probably needs to be a singleton, having more than one doesn’t make a whole lot of sense, although potentially giving out rope is not a terrible idea here



35
36
37
# File 'lib/readline-ng.rb', line 35

def visible
  @visible
end

Instance Method Details

#each_lineObject



88
89
90
# File 'lib/readline-ng.rb', line 88

def each_line
  yield @lines.shift while @lines.any?
end

#filterObject

A third party dev can overload filter to implement their own actions



38
39
# File 'lib/readline-ng.rb', line 38

def filter
end

#get_lineObject



92
93
94
95
96
# File 'lib/readline-ng.rb', line 92

def get_line
  # Blocks!
  tick until lines.any?
  line
end

#lineObject



84
85
86
# File 'lib/readline-ng.rb', line 84

def line
  @lines.shift
end

#puts_above(string) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/readline-ng.rb', line 67

def puts_above(string)
  if visible
    backspace(@buf.length)
    _print string.gsub("\n", "\n\r")
    _puts CONTROL_CR
    _print @buf
  end
end

#tickObject



76
77
78
79
80
81
82
# File 'lib/readline-ng.rb', line 76

def tick
  @chunked << STDIN.read_nonblock(128)
  @chunked.each_chunk { |c| process(c) }
  filter # Expect a 3rd party dev to override this
rescue Errno::EAGAIN
  nil
end

#wait(n) ⇒ Object



60
61
62
63
64
65
# File 'lib/readline-ng.rb', line 60

def wait(n)
  (n * polling_resolution).times do
    tick
    sleep 1.0/polling_resolution
  end
end