What Will Give Me Something Like Ruby Readline with a Default Value

Pre-Filled Prompt in Ruby

You can do it with RbReadline:

require 'rubygems'
require 'rb-readline'

module RbReadline
def self.prefill_prompt(str)
@rl_prefill = str
@rl_startup_hook = :rl_prefill_hook
end

def self.rl_prefill_hook
rl_insert_text @rl_prefill if @rl_prefill
@rl_startup_hook = nil
end
end

RbReadline.prefill_prompt("Previous query")
str = Readline.readline("Enter query: ", true)

puts "You entered: #{str}"

Present editable input to a user with ruby

The -e flag for read is a bash extension. You're using the sh shell (or something emulating sh), which has the read command but doesn't have that flag.

The Readline module provides terminal input, with editing, from within Ruby:

require "readline"
while buf = Readline.readline("> ", true)
p buf
end

It also has history and completion features.

Print editable to console in Ruby

There are similar questions here and here

However, the solutions there don't seem to work as expected, so it looks this is ruby version or platform dependent?

For example, this does not work for me, but also does not throw an error.

require "readline"

filename = Readline.insert_text("untitled.txt").readline("Enter a filename:")
print filename

But since it looks much better, and should work according to the documentation for ruby >= 2, I am leaving it there for now.

The following works on my system (ruby 2.3.1, OS X)

require "readline"
require 'rb-readline'

module RbReadline
def self.prefill_prompt(str)
@rl_prefill = str
@rl_startup_hook = :rl_prefill_hook
end

def self.rl_prefill_hook
rl_insert_text @rl_prefill if @rl_prefill
@rl_startup_hook = nil
end
end

RbReadline.prefill_prompt("untitled.txt")
str = Readline.readline("Enter a filename:", true)

puts "You entered: #{str}"

Ruby readline fails if process started with arguments

Have you tried while (l = $stdin.gets.chomp!)? Otherwise Kernel#gets reads from ARGV.

How can I do readline arguments completion?

After thinking a while, the solution was very simple:

comp = proc do |s| 
if Readline.line_buffer =~ /^.* /
COLLECT.grep( /^#{Regexp.escape(s)}/ )
else
COMMANDS.grep( /^#{Regexp.escape(s)}/ )
end
end

Now I just need to turn it into something more flexible/usable.

How to read lines of a file in Ruby

I believe my answer covers your new concerns about handling any type of line endings since both "\r\n" and "\r" are converted to Linux standard "\n" before parsing the lines.

To support the "\r" EOL character along with the regular "\n", and "\r\n" from Windows, here's what I would do:

line_num=0
text=File.open('xxx.txt').read
text.gsub!(/\r\n?/, "\n")
text.each_line do |line|
print "#{line_num += 1} #{line}"
end

Of course this could be a bad idea on very large files since it means loading the whole file into memory.

How to allow users to edit given string via $stdin in ruby

begin
system("stty raw -echo")
print (acc = "Edit me: ")
loop.each_with_object(acc) do |_,acc|
sym = $stdin.getc
case sym.ord
when 13 # carriage return
break acc
when 127 # backspace
print "\e[1D \e[1D"
acc.slice!(acc.length - 1) if acc.length > 0
else # regular symbol
print sym
acc << sym
end
end
ensure
system("stty -raw echo")
puts
puts "\e[0mEntered: |#{acc}|"
end

Here you go. More info on terminal control sequences. Also, ANSI terminal codes.



Related Topics



Leave a reply



Submit