Skip to content

Instantly share code, notes, and snippets.

@hotchpotch
Created February 13, 2009 16:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hotchpotch/63981 to your computer and use it in GitHub Desktop.
Save hotchpotch/63981 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'readline'
require 'socket'
host = ARGV.shift || 'localhost'
port = ARGV.shift || 4444
history_file = nil
begin
history_file = File.expand_path("~/.ifx.history")
rescue ArgumentError
history_file = "ifx-history.txt"
end
max_history_size = 1000
if defined?(Readline::HISTORY)
history_file = File.expand_path(history_file)
if File.exist?(history_file)
lines = IO.readlines(history_file).collect {|line| line.chomp}
Readline::HISTORY.push(*lines)
end
at_exit do
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-[max_history_size, lines.size].min, max_history_size]
File::open(history_file, File::WRONLY|File::CREAT|File::TRUNC) do |output|
output.puts(lines)
end
end
end
if Readline.respond_to?("basic_word_break_characters=")
Readline.basic_word_break_characters= " \t\n\"\\'`><=;|&{("
end
Readline.completion_append_character = nil
def wait_buffer(line, socket, wait = 3)
socket.puts line
buffer = ""
now = Time.now
while !((Time.now - now) > wait)
while IO.select([socket], [], [], 0.05)
break if socket.eof?
buffer << socket.readpartial(4096)
end
break if !buffer.empty?
sleep 0.1
end
buffer
end
begin
TCPSocket.open(host, port) do |socket|
Readline.completion_proc = Proc.new {|input|
targets = input.split('=').last.scan(/[\.\w]+/).last.split('.')
targets << '' if input[-1..-1] == '.'
case targets.length
when 0
[]
when 1
target = 'this'
name = targets.pop
else
name = targets.pop
target = targets.join('.')
end
js = <<-EOF
(function(target, name) {
var keys = [key for (key in target)];
if (name) {
return keys.filter(function(key) key.indexOf(name) == 0);
} else {
return keys;
}
})(#{target}, '#{name}');
EOF
res = wait_buffer(js, socket).chomp.split(",")
if (targets.length > 0)
res.map! {|key| target + '.' + key }
end
res
}
while line = Readline.readline("firefox> ", true)
begin
if line.chomp.length
buffer = wait_buffer(line, socket)
puts buffer unless buffer.empty?
end
rescue Errno::EPIPE
break
end
end
end
rescue SystemCallError
puts "Is UxU server running? (localhost:#{port})"
exit(1)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment