~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/irbtkw.rbw

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-05-21 14:00:19 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20070521140019-ui4zd0v80duktssk
Tags: 1.9.0+20070521-1
new upstream snapshot. (2006-07-21)  (Closes: #414856, #388344)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
#
 
3
#  irbtkw.rb : IRB console with Ruby/Tk
 
4
#
 
5
#                                 by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
 
6
#
 
7
release = '2006/11/06'
 
8
 
 
9
require 'tk'
 
10
begin
 
11
  require 'tktextio'
 
12
rescue LoadError
 
13
  require File.join(File.dirname(File.expand_path(__FILE__)), 'tktextio.rb')
 
14
end
 
15
 
 
16
require 'irb'
 
17
 
 
18
# console setup
 
19
top = TkToplevel.new(:title=>'IRB console')
 
20
top.protocol(:WM_DELETE_WINDOW){ Tk.exit }
 
21
 
 
22
console = TkTextIO.new(top, :mode=>:console, 
 
23
                       :width=>80).pack(:side=>:left, 
 
24
                                        :expand=>true, :fill=>:both)
 
25
console.yscrollbar(TkScrollbar.new(top, :width=>10).pack(:before=>console, 
 
26
                                                         :side=>:right, 
 
27
                                                         :expand=>false, 
 
28
                                                         :fill=>:y))
 
29
irb_thread = nil
 
30
ev_loop = Thread.new{
 
31
  Tk.mainloop
 
32
  irb_thread.kill if irb_thread
 
33
}
 
34
 
 
35
# window position control
 
36
root = Tk.root
 
37
 
 
38
r_x = root.winfo_rootx
 
39
r_y = root.winfo_rooty
 
40
r_w = root.winfo_width
 
41
 
 
42
t_x = top.winfo_rootx
 
43
t_y = top.winfo_rooty
 
44
t_w = top.winfo_width
 
45
 
 
46
delta = 10
 
47
 
 
48
ratio = 0.8
 
49
s_w = (ratio * root.winfo_screenwidth).to_i
 
50
 
 
51
if r_x < t_x
 
52
  r_x, t_x = t_x, r_x
 
53
end
 
54
if t_x + t_w + r_w + delta < s_w
 
55
  r_x = t_x + t_w + delta
 
56
elsif t_w + r_w + delta < s_w
 
57
  r_x = s_w - r_w
 
58
  t_x = r_x - t_w
 
59
else
 
60
  r_x = s_w - r_w
 
61
  t_x = 0
 
62
end
 
63
 
 
64
root.geometry("+#{r_x}+#{r_y}")
 
65
top.geometry("+#{t_x}+#{t_y}")
 
66
 
 
67
root.raise
 
68
console.focus
 
69
 
 
70
# I/O setup
 
71
$stdin  = console
 
72
$stdout = console
 
73
$stderr = console
 
74
 
 
75
# dummy for rubyw.exe on Windows
 
76
def STDIN.tty?
 
77
  true
 
78
end
 
79
 
 
80
# IRB setup
 
81
IRB.init_config(nil)
 
82
IRB.conf[:USE_READLINE] = false
 
83
IRB.init_error
 
84
irb = IRB::Irb.new
 
85
IRB.conf[:MAIN_CONTEXT] = irb.context
 
86
 
 
87
class IRB::StdioInputMethod
 
88
  def gets
 
89
    prompt = "\n" << @prompt
 
90
    $stdin.instance_eval{
 
91
      flush
 
92
      @prompt = prompt
 
93
      _set_console_line
 
94
      @prompt = nil
 
95
      _see_pos
 
96
    }
 
97
 
 
98
    @line[@line_no += 1] = $stdin.gets
 
99
  end
 
100
end
 
101
 
 
102
# IRB start
 
103
$stdout.print("*** IRB console on Ruby/Tk (#{release})  ")
 
104
irb_thread = Thread.new{
 
105
  catch(:IRB_EXIT){
 
106
    loop {
 
107
      begin
 
108
        irb.eval_input
 
109
      rescue Exception
 
110
      end
 
111
    }
 
112
  }
 
113
}
 
114
 
 
115
console.bind('Control-c'){
 
116
  console.insert('end', "^C\n")
 
117
  irb_thread.raise RubyLex::TerminateLineInput
 
118
}
 
119
 
 
120
irb_thread.join
 
121
 
 
122
# exit
 
123
ev_thread.kill
 
124
Tk.exit