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.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/readline-ng.rb', line 35

def initialize(visible=true, opts = {})
  @buf = ""
  @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



29
30
31
# File 'lib/readline-ng.rb', line 29

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



29
30
31
# File 'lib/readline-ng.rb', line 29

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



29
30
31
# File 'lib/readline-ng.rb', line 29

def visible
  @visible
end

Instance Method Details

#each_lineObject



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

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

#filterObject

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



32
33
# File 'lib/readline-ng.rb', line 32

def filter
end

#get_lineObject



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

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

#lineObject



77
78
79
# File 'lib/readline-ng.rb', line 77

def line
  @lines.shift
end

#puts_above(string) ⇒ Object



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

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

#tickObject



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

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

#wait(n) ⇒ Object



53
54
55
56
57
58
# File 'lib/readline-ng.rb', line 53

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