GNU readline with suggested text

T

Tim Heaney

Say I am using GNU readline to get a line of input like so

require 'readline'
line = Readline.readline("Enter name: ")
puts "Hello " + line

and I want to put some suggested text on the line to be edited, rather
than start with a blank line. GNU readline has something called
rl_startup_hook, which I might use like so in C

int insert_my_line ()
{
return rl_insert_text(getenv("LOGNAME"));
}

int main ()
{
char* line;
rl_startup_hook = insert_my_line;
if ( (line = readline("Enter your name: ")) == NULL) {
fprintf(stderr, "Cannot get line of input!\n");
return 1;
}
printf("Hello %s\n", line);
return 0;
}

In Perl, this appears as an optional second argument to readline

$line = $term->readline("Enter your name: ", $ENV{LOGNAME});

In Python, I use set_startup_hook much like I do in C

readline.set_startup_hook(lambda: readline.insert_text(os.getenv("LOGNAME")))
line = raw_input("Enter your name: ")

I can't work out what to do in Ruby. Like Perl, Ruby has an optional
second argument to readline, but it toggles history. This startup hook
doesn't seem to be available to me. Am I just missing it or is there
something else I'm meant to do?

Thanks,

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top