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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/djinni.rb', line 7
def grant_wish(input, djinni_env = {})
return "" if (input.nil? || input.empty?)
djinni_env["djinni"] = self
djinni_env["djinni_history"] = @history
djinni_env["djinni_wishes"] = @wishes
space = 4
case input[-1]
when "\x03" puts if (@interactive)
return ""
when "\x04" return "\x04"
when "\x7F", "\b" return input[0..-3]
when "\f" system("clear") if (@interactive)
return input[0..-2]
when "\t" input = input[0..-2]
if (input.include?(" "))
name, args = input.split(" ", 2)
return input if (!@wishes.has_key?(name))
begin
wish = @wishes[name]
djinni_env["djinni_input"] = name
completions, replace, append = wish.tab_complete(
args,
djinni_env
)
rescue SystemExit => e
raise e
rescue Exception => e
puts
puts e.message
ensure
append ||= " "
completions ||= Hash.new
replace ||= ""
end
return input if (completions.empty?)
if (completions.length == 1)
return input.gsub(
/#{replace}$/,
"#{completions.first.first}#{append}"
)
end
max = completions.keys.max_by(&:length).length
nlfill = ""
nlfill = " " * (max + space) if ((max + space) > 0)
width = @width - (max + space) - 2
puts
completions.each do |item, desc|
desc ||= ""
fill = ""
if ((max + space - item.length) > 0)
fill = " " * (max + space - item.length)
end
lines = desc.scan(
/\S.{0,#{width}}\S(?=\s|$)|\S+/
)
if (lines.empty?)
puts item
else
start = lines.delete_at(0)
puts "#{item}#{fill}#{start}"
lines.each do |line|
puts "#{nlfill}#{line}"
end
end
end
longest = longest_common_substring(completions.keys)
return input if (longest.empty?)
return input.gsub(/#{replace}$/, longest)
else
wishes = @wishes.select do |aliaz, w|
aliaz.start_with?(input)
end
return input if (wishes.empty?)
if (wishes.length == 1)
return "#{wishes.first.first} "
end
max = wishes.keys.max_by(&:length).length
nlfill = ""
nlfill = " " * (max + space) if ((max + space) > 0)
width = @width - (max + space) - 2
puts
wishes.sort do |a, b|
a.first.downcase <=> b.first.downcase
end.each do |aliaz, w|
fill = ""
if ((max + space - aliaz.length) > 0)
fill = " " * (max + space - aliaz.length)
end
lines = w.description.scan(
/\S.{0,#{width}}\S(?=\s|$)|\S+/
)
if (lines.empty?)
puts aliaz
else
start = lines.delete_at(0)
puts "#{aliaz}#{fill}#{start}"
lines.each do |line|
puts "#{nlfill}#{line}"
end
end
end
return longest_common_substring(wishes.keys)
end
when /\r|\n/ input.strip!
puts if (@interactive)
return "" if (input.empty?)
name, args = input.split(" ", 2)
args ||= ""
@wishes.select do |aliaz, w|
aliaz == name
end.each do |aliaz, w|
begin
djinni_env["djinni_input"] = name
w.execute(args, djinni_env)
rescue SystemExit => e
raise e
rescue Exception => e
puts e.message
end
if (w.class.to_s != "Djinni::Wish::History")
store_history(input)
end
return ""
end
store_history(input)
return nil
when "\e" code = "\e"
t = Thread.new do
code += STDIN.getch + STDIN.getch
end
t.join(0.001) if (t)
t.kill if (t)
case code
when /\e[\[O]A/ return "" if (@history.empty?)
@hist_index = @history.size if (@hist_index.nil?)
@hist_index = 1 if (@hist_index == 0)
@hist_index -= 1
return @history[@hist_index]
when /\e[\[O]B/ @hist_index = @history.size if (@hist_index.nil?)
@hist_index += 1
if (@hist_index < @history.size)
return @history[@hist_index]
else
@hist_index = @history.size
return ""
end
when /\e[\[O]C/ return input[0..-2]
when /\e[\[O]D/ return input[0..-2]
else
return input[0..-2]
end
else
return input
end
end
|