Module: Readline::Callback

Extended by:
FFI::Library
Included in:
Readline
Defined in:
lib/readline/callback.rb

Overview

An alternate, callback-based interface to Readline for use in a larger event loop.

Used FFI for access to the Readline C library. The Readline module (Ruby core) is extended with this module.

See cnswww.cns.cwru.edu/php/chet/readline/readline.html#SEC41

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.editline?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/readline/callback.rb', line 15

def self.editline?
  @__is_editline__ ||= (Readline::VERSION rescue nil).to_s[/editline/i]
end

Instance Method Details

#callback_handler_install(prompt = nil) {|String| ... } ⇒ Object

Set up the terminal for readline I/O and display the initial expanded value of prompt. Save the value of ‘block` to call when a complete line of input has been entered.

A reference to the handler is saved in an instance variable so that it will not be garbage collected. Subsequent calls to #handler_install will displace this reference. A call to #handler_remove will remove the reference.

argument, called when a complete line of input has been entered.

Parameters:

  • prompt (String) (defaults to: nil)

Yields:

  • (String)

    a handler taking the text of the line as the sole

Raises:

  • (ArgumentError)


62
63
64
65
66
# File 'lib/readline/callback.rb', line 62

def callback_handler_install(prompt = nil, &block)
  raise ArgumentError, 'block is required' unless block
  @rl_callback_handler = block
  rl_callback_handler_install(prompt, block)
end

#callback_handler_removeObject

Restore the terminal to its initial state and remove the line handler. This may be called from within a callback as well as independently. If the handler installed by #handler_install does not exit the program, either this function or the function referred to by the value of rl_deprep_term_function (not sure what that translates to in Ruby) should be called before the program exits to reset the terminal settings.



88
89
90
91
# File 'lib/readline/callback.rb', line 88

def callback_handler_remove
  rl_callback_handler_remove
  @rl_callback_handler = nil
end

#callback_read_charObject

When keyboard input is available (determined by, e.g. calling select on $stdin), this method should be called. If that character completes the line, the block registered by #callback_handler_install will be called. Before calling the handler function, the terminal settings are reset to the values they had before calling #callback_handler_install. If the handler function returns, the terminal settings are modified for Readline’s use again. EOF is indicated by calling handler with a NULL line.



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

def callback_read_char
  rl_callback_read_char
end

#line_bufferObject



36
37
38
# File 'lib/readline/callback.rb', line 36

def line_buffer
  ::Readline::Callback.rl_line_buffer
end

#set_prompt(*args) ⇒ Object



42
43
44
# File 'lib/readline/callback.rb', line 42

def set_prompt(*args)
  # noop; rl_set_prompt isn't exported by EditLine
end