Project

General

Profile

Actions

Feature #13604

open

Exposing alternative interface of readline

Added by graywolf (Gray Wolf) almost 7 years ago. Updated about 4 years ago.

Status:
Assigned
Target version:
-
[ruby-core:81417]

Description

GNU Readline has multiple modes of operation. At the moment, the readline extension only supports typical, Readline.readline mode. However, there is also alternative callback-based interface which is also useful.

require_relative 'readline'

PROMPT = "rltest$ "

$running = true
$sigwinch_received = false

Readline.handler_install(PROMPT, add_hist: true) do |line|
	# Can use ^D (stty eof) or `exit' to exit.
	if !line || line == "exit"
		puts unless line
		puts "exit"
		Readline.handler_remove
		$running = false
	else
		puts "input line: #{line}"
	end
end

Signal.trap('SIGWINCH') { $sigwinch_received = true }

while $running do
	rs = IO.select([$stdin])
	if $sigwinch_received
		Readline.resize_terminal
		$sigwinch_received = false
	end
	Readline.read_char if r = rs[0]
end

puts "rltest: Event loop has exited"

Patch adding support for this is attached. This is my first try at contributing to ruby, so please tell me what I did wrong (I'm sure something, C is not my strong language).


Files

readline_alternative_interface.diff (7.22 KB) readline_alternative_interface.diff Patch graywolf (Gray Wolf), 05/27/2017 10:00 AM
readline_alternative_interface_example.rb (606 Bytes) readline_alternative_interface_example.rb How it can be used? graywolf (Gray Wolf), 05/27/2017 10:00 AM
readline_alternative_interface_update1.diff (7.08 KB) readline_alternative_interface_update1.diff improved version graywolf (Gray Wolf), 05/29/2017 06:34 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0