| 1 |
#!/usr/bin/env ruby |
| 2 |
|
| 3 |
|
| 4 |
require 'bundler' |
| 5 |
Bundler.require |
| 6 |
|
| 7 |
require 'reline' |
| 8 |
require 'optparse' |
| 9 |
require_relative 'termination_checker' |
| 10 |
|
| 11 |
opt = OptionParser.new |
| 12 |
opt.on('--dynamic-prompt') { |
| 13 |
Reline.prompt_proc = proc { |lines| |
| 14 |
lines.each_with_index.map { |l, i| |
| 15 |
"\e[1m[%04d]>\e[m " % i |
| 16 |
} |
| 17 |
} |
| 18 |
} |
| 19 |
opt.on('--broken-dynamic-prompt') { |
| 20 |
Reline.prompt_proc = proc { |lines| |
| 21 |
range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0) |
| 22 |
lines[range].each_with_index.map { |l, i| |
| 23 |
'[%04d]> ' % i |
| 24 |
} |
| 25 |
} |
| 26 |
} |
| 27 |
opt.on('--dynamic-prompt-returns-empty') { |
| 28 |
Reline.prompt_proc = proc { |l| [] } |
| 29 |
} |
| 30 |
opt.on('--dynamic-prompt-with-newline') { |
| 31 |
Reline.prompt_proc = proc { |lines| |
| 32 |
range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0) |
| 33 |
lines[range].each_with_index.map { |l, i| |
| 34 |
'[%04d\n]> ' % i |
| 35 |
} |
| 36 |
} |
| 37 |
} |
| 38 |
opt.on('--broken-dynamic-prompt-assert-no-escape-sequence') { |
| 39 |
Reline.prompt_proc = proc { |lines| |
| 40 |
has_escape_sequence = lines.join.include?("\e") |
| 41 |
(lines.size + 1).times.map { |i| |
| 42 |
has_escape_sequence ? 'error>' : '[%04d]> ' % i |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
opt.on('--color-bold') { |
| 47 |
Reline.output_modifier_proc = ->(output, complete:){ |
| 48 |
output.gsub(/./) { |c| "\e[1m#{c}\e[0m" } |
| 49 |
} |
| 50 |
} |
| 51 |
opt.on('--dynamic-prompt-show-line') { |
| 52 |
Reline.prompt_proc = proc { |lines| |
| 53 |
lines.map { |l| |
| 54 |
'[%4.4s]> ' % l |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
def assert_auto_indent_params(lines, line_index, byte_pointer, is_newline) |
| 60 |
raise 'Wrong lines type' unless lines.all?(String) |
| 61 |
|
| 62 |
line = lines[line_index] |
| 63 |
raise 'Wrong line_index value' unless line |
| 64 |
|
| 65 |
# The condition `byte_pointer <= line.bytesize` is not satisfied. Maybe bug. |
| 66 |
# Instead, loose constraint `byte_pointer <= line.bytesize + 1` seems to be satisfied when is_newline is false. |
| 67 |
return if is_newline |
| 68 |
|
| 69 |
raise 'byte_pointer out of bounds' unless byte_pointer <= line.bytesize + 1 |
| 70 |
raise 'Invalid byte_pointer' unless line.byteslice(0, byte_pointer).valid_encoding? |
| 71 |
end |
| 72 |
|
| 73 |
opt.on('--auto-indent') { |
| 74 |
Reline.auto_indent_proc = lambda do |lines, line_index, byte_pointer, is_newline| |
| 75 |
assert_auto_indent_params(lines, line_index, byte_pointer, is_newline) |
| 76 |
AutoIndent.calculate_indent(lines, line_index, byte_pointer, is_newline) |
| 77 |
end |
| 78 |
} |
| 79 |
opt.on('--dialog VAL') { |v| |
| 80 |
Reline.add_dialog_proc(:simple_dialog, lambda { |
| 81 |
return nil if v.include?('nil') |
| 82 |
if v.include?('simple') |
| 83 |
contents = <<~RUBY.split("\n") |
| 84 |
Ruby is... |
| 85 |
A dynamic, open source programming |
| 86 |
language with a focus on simplicity |
| 87 |
and productivity. It has an elegant |
| 88 |
syntax that is natural to read and |
| 89 |
easy to write. |
| 90 |
RUBY |
| 91 |
elsif v.include?('long') |
| 92 |
contents = <<~RUBY.split("\n") |
| 93 |
Ruby is... |
| 94 |
A dynamic, open |
| 95 |
source programming |
| 96 |
language with a |
| 97 |
focus on simplicity |
| 98 |
and productivity. |
| 99 |
It has an elegant |
| 100 |
syntax that is |
| 101 |
natural to read |
| 102 |
and easy to write. |
| 103 |
RUBY |
| 104 |
elsif v.include?('fullwidth') |
| 105 |
contents = <<~RUBY.split("\n") |
| 106 |
Rubyとは... |
| 107 |
|
| 108 |
オープンソースの動的なプログラミン |
| 109 |
グ言語で、シンプルさと高い生産性を |
| 110 |
備えています。エレガントな文法を持 |
| 111 |
ち、自然に読み書きができます。 |
| 112 |
RUBY |
| 113 |
end |
| 114 |
if v.include?('scrollkey') |
| 115 |
dialog.trap_key = nil |
| 116 |
if key and key.match?(dialog.name) |
| 117 |
if dialog.pointer.nil? |
| 118 |
dialog.pointer = 0 |
| 119 |
elsif dialog.pointer >= (contents.size - 1) |
| 120 |
dialog.pointer = 0 |
| 121 |
else |
| 122 |
dialog.pointer += 1 |
| 123 |
end |
| 124 |
end |
| 125 |
dialog.trap_key = [?j.ord] |
| 126 |
height = 4 |
| 127 |
end |
| 128 |
scrollbar = false |
| 129 |
if v.include?('scrollbar') |
| 130 |
scrollbar = true |
| 131 |
end |
| 132 |
if v.include?('alt-scrollbar') |
| 133 |
scrollbar = true |
| 134 |
end |
| 135 |
Reline::DialogRenderInfo.new(pos: cursor_pos, contents: contents, height: height, scrollbar: scrollbar, face: :completion_dialog) |
| 136 |
}) |
| 137 |
if v.include?('alt-scrollbar') |
| 138 |
ENV['RELINE_ALT_SCROLLBAR'] = '1' |
| 139 |
end |
| 140 |
} |
| 141 |
opt.on('--complete') { |
| 142 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 143 |
%w{String ScriptError SyntaxError Signal}.select{ |c| c.start_with?(target) } |
| 144 |
} |
| 145 |
} |
| 146 |
opt.on('--complete-menu-with-perfect-match') { |
| 147 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 148 |
%w{abs abs2}.select{ |c| c.start_with?(target) } |
| 149 |
} |
| 150 |
} |
| 151 |
opt.on('--autocomplete') { |
| 152 |
Reline.autocompletion = true |
| 153 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 154 |
%w{String Struct Symbol ScriptError SyntaxError Signal}.select{ |c| c.start_with?(target) } |
| 155 |
} |
| 156 |
} |
| 157 |
opt.on('--autocomplete-empty') { |
| 158 |
Reline.autocompletion = true |
| 159 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| [] } |
| 160 |
} |
| 161 |
opt.on('--autocomplete-long') { |
| 162 |
Reline.autocompletion = true |
| 163 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 164 |
%w{ |
| 165 |
String |
| 166 |
Struct |
| 167 |
Symbol |
| 168 |
StopIteration |
| 169 |
SystemCallError |
| 170 |
SystemExit |
| 171 |
SystemStackError |
| 172 |
ScriptError |
| 173 |
SyntaxError |
| 174 |
Signal |
| 175 |
SizedQueue |
| 176 |
Set |
| 177 |
SecureRandom |
| 178 |
Socket |
| 179 |
StringIO |
| 180 |
StringScanner |
| 181 |
Shellwords |
| 182 |
Syslog |
| 183 |
Singleton |
| 184 |
SDBM |
| 185 |
}.select{ |c| c.start_with?(target) } |
| 186 |
} |
| 187 |
} |
| 188 |
opt.on('--autocomplete-super-long') { |
| 189 |
Reline.autocompletion = true |
| 190 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 191 |
c = +'A' |
| 192 |
2000.times.map{ s = "Str_#{c}"; c.succ!; s }.select{ |c| c.start_with?(target) } |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
opt.on('--autocomplete-width-long') { |
| 197 |
Reline.autocompletion = true |
| 198 |
Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| |
| 199 |
%w{ |
| 200 |
remove_instance_variable |
| 201 |
respond_to? |
| 202 |
ruby2_keywords |
| 203 |
rand |
| 204 |
readline |
| 205 |
readlines |
| 206 |
require |
| 207 |
require_relative |
| 208 |
raise |
| 209 |
respond_to_missing? |
| 210 |
redo |
| 211 |
rescue |
| 212 |
retry |
| 213 |
return |
| 214 |
}.select{ |c| c.start_with?(target) } |
| 215 |
} |
| 216 |
} |
| 217 |
opt.parse!(ARGV) |
| 218 |
|
| 219 |
begin |
| 220 |
stty_save = `stty -g`.chomp |
| 221 |
rescue |
| 222 |
end |
| 223 |
|
| 224 |
begin |
| 225 |
prompt = ENV['RELINE_TEST_PROMPT'] || "\e[1mprompt>\e[m " |
| 226 |
puts 'Multiline REPL.' |
| 227 |
while code = Reline.readmultiline(prompt, true) { |code| TerminationChecker.terminated?(code) } |
| 228 |
case code.chomp |
| 229 |
when 'exit', 'quit', 'q' |
| 230 |
exit 0 |
| 231 |
when '' |
| 232 |
# NOOP |
| 233 |
else |
| 234 |
begin |
| 235 |
result = eval(code) |
| 236 |
puts "=> #{result.inspect}" |
| 237 |
rescue ScriptError, StandardError => e |
| 238 |
puts "Traceback (most recent call last):" |
| 239 |
e.backtrace.reverse_each do |f| |
| 240 |
puts " #{f}" |
| 241 |
end |
| 242 |
puts e.message |
| 243 |
end |
| 244 |
end |
| 245 |
end |
| 246 |
rescue Interrupt |
| 247 |
puts '^C' |
| 248 |
`stty #{stty_save}` if stty_save |
| 249 |
exit 0 |
| 250 |
ensure |
| 251 |
`stty #{stty_save}` if stty_save |
| 252 |
end |
| 253 |
begin |
| 254 |
puts |
| 255 |
rescue Errno::EIO |
| 256 |
# Maybe the I/O has been closed. |
| 257 |
end |