ruby - Adding readline to sqlite3 -
i tried create script add readline abilities sqlite3 commands history , moving cursor. came far:
#!/usr/bin/env ruby require 'pty' require 'expect' require 'readline' pty.spawn("sqlite3") |reader, writer, pid| reader.expect("sqlite> ") writer.puts(readline.readline("sqlite> ", true)) end
but have no idea how print output of command entered. appreciate on this. thanks!
i had play after finding this tutorial.
require 'pty' require 'expect' require 'readline' pty.spawn("sqlite3") |reader, writer, pid| cmd = readline.readline("prompt> ", true); reader.expect(/^sqlite\>/) { |r| writer.printf("%s\n",cmd) } puts reader.expect(/^sqlite\>/)[0] end
so in nutshell, example, need add this:
puts reader.expect("sqlite> ")[0]
playing bit more added little loop asks commands, sends them sqllite3 , sends result:
require 'pty' require 'expect' require 'readline' pty.spawn("sqlite3") |reader, writer, pid| reader.expect(/^sqlite\>/) # read till prompt. loop { cmd = readline.readline("prompt> ", true); # command. writer.printf("%s\n",cmd) # send it. r = reader.expect(/^sqlite\>/) # result r[0].lines.to_a[0..-2].each |l| # turn array without last line. unless l.match(/^ /) # ignore indented lines puts l # show result user end end end
the r[0].lines.to_a[0..-2].join
black magic remove last line.
Comments
Post a Comment