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



86
87
88
# File 'lib/readline-ng.rb', line 86

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



90
91
92
93
94
# File 'lib/readline-ng.rb', line 90

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

#lineObject



82
83
84
# File 'lib/readline-ng.rb', line 82

def line
  @lines.shift
end

#puts_above(string) ⇒ Object



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

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

#tickObject



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

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
# File 'lib/readline-ng.rb', line 60

def wait(n)
  to_read, _, _ = IO.select([STDIN], [], [], n)
  tick unless to_read.empty?
end