GNU readline with suggested text

Discussion in 'Ruby' started by Tim Heaney, Oct 9, 2005.

  1. Tim Heaney

    Tim Heaney Guest

    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
    Tim Heaney, Oct 9, 2005
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter

    1 day gnu, whole life gnu?

    Peter, Jan 10, 2005, in forum: Java
    Replies:
    3
    Views:
    327
    John C. Bollinger
    Jan 10, 2005
  2. Peter
    Replies:
    17
    Views:
    595
    Chris Smith
    Jan 13, 2005
  3. Markus Elfring
    Replies:
    2
    Views:
    359
    Markus Elfring
    Feb 23, 2005
  4. Jason
    Replies:
    1
    Views:
    331
    Jack Klein
    Oct 18, 2003
  5. Replies:
    3
    Views:
    631
    Thomas Lotze
    Aug 2, 2005
Loading...

Share This Page