hotchpotch (owner)

Revisions

gist: 63981 Download_button fork
public
Public Clone URL: git://gist.github.com/63981.git
Embed All Files: show embed
ifx #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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