Skip to content

Instantly share code, notes, and snippets.

@kazz187
Created December 5, 2013 20:23
Show Gist options
  • Save kazz187/7813206 to your computer and use it in GitHub Desktop.
Save kazz187/7813206 to your computer and use it in GitHub Desktop.
readline supports startup_hook.
require 'readline'
require 'fiddle/import'
module Readline
module LIBREADLINE
extend Fiddle::Importer
# このパスどうしよ
dlload '/usr/local/opt/readline/lib/libreadline.6.2.dylib'
RL_STARTUP_HOOK = import_symbol 'rl_startup_hook'
end
def self.set_startup_hook(&blk)
ptr = LIBREADLINE::RL_STARTUP_HOOK
cf = LIBREADLINE::bind('int startup_hook_callback()') { blk.call }
ptr[0, Fiddle::SIZEOF_VOIDP] = [cf.to_i].pack(Fiddle::SIZEOF_VOIDP == 4 ? 'I!' : 'L!')
end
end
loop do
Readline.set_startup_hook do
Readline.insert_text 'default text'
0
end
buf = Readline.readline '> ', true
puts buf
end
@kazz187
Copy link
Author

kazz187 commented Dec 5, 2013

と言うか、 startup_hook くらい最初からサポートしてて欲しい

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment