readline.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <readline/readline.h>
00003 #include <ruby.h>
00004 
00005 static VALUE readline_save_prompt(VALUE self)
00006 {
00007     rl_save_prompt();
00008     return Qnil;
00009 }
00010 
00011 static VALUE readline_message(VALUE self, VALUE msg)
00012 {
00013     rl_message("%s", StringValuePtr(msg));
00014     rl_redisplay();
00015     return Qnil;
00016 }
00017 
00018 static VALUE readline_print(VALUE self, VALUE msg)
00019 {
00020     int need_hack = (rl_readline_state & RL_STATE_READCMD) > 0;
00021     char *saved_line;
00022     int saved_point;
00023     if (need_hack)
00024     {
00025         saved_point = rl_point;
00026         saved_line = rl_copy_text(0, rl_end);
00027         rl_save_prompt();
00028         rl_replace_line("", 0);
00029         rl_redisplay();
00030     }
00031 
00032     printf("%s", StringValuePtr(msg));
00033 
00034     if (need_hack)
00035     {
00036         rl_restore_prompt();
00037         rl_replace_line(saved_line, 0);
00038         rl_point = saved_point;
00039         rl_redisplay();
00040         free(saved_line);
00041     }
00042     return Qnil;
00043 }
00044 
00045 extern void Init_utilrb_readline()
00046 {
00047     VALUE mReadline = rb_define_module("Readline");
00048     rb_define_singleton_method(mReadline, "save_prompt", readline_save_prompt, 0);
00049     rb_define_singleton_method(mReadline, "message", readline_message, 1);
00050     rb_define_singleton_method(mReadline, "print", readline_print, 1);
00051 }
00052 


utilrb
Author(s): Sylvain Joyeux/sylvain.joyeux@m4x.org
autogenerated on Sat Jun 8 2019 18:49:20