-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtkrepl.rb
111 lines (88 loc) · 1.53 KB
/
tkrepl.rb
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
104
105
106
107
108
109
110
111
require 'tk'
require_relative 'tkkeys'
class TkRepl < TkText
attr_accessor :repl
def initialize(container, status)
super(container)
setup_bindings
@anchor = cursor
@status = status
@exit_proc = lambda {}
end
def on_repl_exit(&block)
@exit_proc = block
end
def handle_repl_exit
instance_eval(&@exit_proc)
end
def cursor
index('insert')
end
def line
get @anchor, cursor
end
def brk
Tk.callback_break
end
def clear
delete(@anchor, 'end')
end
def print(obj)
insert('end', obj.to_s)
@anchor = cursor
end
def puts(obj)
if !obj.end_with? "\n"
obj += "\n"
end
print obj
end
def tty?
true
end
def flush
end
def cmp_anchor
(@anchor.split(".") <=> cursor.split("."))
end
def after_anchor?
cmp_anchor == -1
end
def before_anchor?
cmp_anchor == 1
end
def at_anchor?
cmp_anchor == 0
end
def indented?
get(@anchor - 2, @anchor) == " "
end
def history(dir)
if (s = repl.history(dir))
clear
insert 'end', s
end
end
def auto_dedent
x = line
return unless indented?
return unless ['end', ']', '}'].include? x
@anchor = @anchor - 2
clear
insert('end', x)
end
def can_process_commandline
unless repl
@status.text = "repl not ready"
return false
end
return false if not after_anchor?
return true
end
def process_commandline
repl.process_commandline(line)
end
def connect(repl)
@repl = repl
end
end