summaryrefslogtreecommitdiff
path: root/test/reline/yamatanooroti/multiline_repl
blob: 66bcf51e1add72d87e771df844215fdbf7c9e90d (plain)
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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env ruby


require 'bundler'
Bundler.require

require 'reline'
require 'optparse'
require_relative 'termination_checker'

opt = OptionParser.new
opt.on('--dynamic-prompt') {
  Reline.prompt_proc = proc { |lines|
    lines.each_with_index.map { |l, i|
      '[%04d]> ' % i
    }
  }
}
opt.on('--broken-dynamic-prompt') {
  Reline.prompt_proc = proc { |lines|
    range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0)
    lines[range].each_with_index.map { |l, i|
      '[%04d]> ' % i
    }
  }
}
opt.on('--dynamic-prompt-returns-empty') {
  Reline.prompt_proc = proc { |l| [] }
}
opt.on('--dynamic-prompt-with-newline') {
  Reline.prompt_proc = proc { |lines|
    range = lines.size > 1 ? (0..(lines.size - 2)) : (0..0)
    lines[range].each_with_index.map { |l, i|
      '[%04d\n]> ' % i
    }
  }
}
opt.on('--broken-dynamic-prompt-assert-no-escape-sequence') {
  Reline.prompt_proc = proc { |lines|
    has_escape_sequence = lines.join.include?("\e")
    (lines.size + 1).times.map { |i|
      has_escape_sequence ? 'error>' : '[%04d]> ' % i
    }
  }
}
opt.on('--color-bold') {
  Reline.output_modifier_proc = ->(output, complete:){
    output.gsub(/./) { |c| "\e[1m#{c}\e[0m" }
  }
}
opt.on('--dynamic-prompt-show-line') {
  Reline.prompt_proc = proc { |lines|
    lines.map { |l|
      '[%4.4s]> ' % l
    }
  }
}

def assert_auto_indent_params(lines, line_index, byte_pointer, is_newline)
  raise 'Wrong lines type' unless lines.all?(String)

  line = lines[line_index]
  raise 'Wrong line_index value' unless line

  # The condition `byte_pointer <= line.bytesize` is not satisfied. Maybe bug.
  # Instead, loose constraint `byte_pointer <= line.bytesize + 1` seems to be satisfied when is_newline is false.
  return if is_newline

  raise 'byte_pointer out of bounds' unless byte_pointer <= line.bytesize + 1
  raise 'Invalid byte_pointer' unless line.byteslice(0, byte_pointer).valid_encoding?
end

opt.on('--auto-indent') {
  Reline.auto_indent_proc = lambda do |lines, line_index, byte_pointer, is_newline|
    assert_auto_indent_params(lines, line_index, byte_pointer, is_newline)
    AutoIndent.calculate_indent(lines, line_index, byte_pointer, is_newline)
  end
}
opt.on('--dialog VAL') { |v|
  Reline.add_dialog_proc(:simple_dialog, lambda {
    return nil if v.include?('nil')
    if v.include?('simple')
      contents = <<~RUBY.split("\n")
        Ruby is...
        A dynamic, open source programming
        language with a focus on simplicity
        and productivity. It has an elegant
        syntax that is natural to read and
        easy to write.
      RUBY
    elsif v.include?('long')
      contents = <<~RUBY.split("\n")
        Ruby is...
        A dynamic, open
        source programming
        language with a
        focus on simplicity
        and productivity.
        It has an elegant
        syntax that is
        natural to read
        and easy to write.
      RUBY
    elsif v.include?('fullwidth')
      contents = <<~RUBY.split("\n")
        Rubyとは...

        オープンソースの動的なプログラミン
        グ言語で、シンプルさと高い生産性を
        備えています。エレガントな文法を持
        ち、自然に読み書きができます。
      RUBY
    end
    if v.include?('scrollkey')
      dialog.trap_key = nil
      if key and key.match?(dialog.name)
        if dialog.pointer.nil?
          dialog.pointer = 0
        elsif dialog.pointer >= (contents.size - 1)
          dialog.pointer = 0
        else
          dialog.pointer += 1
        end
      end
      dialog.trap_key = [?j.ord]
      height = 4
    end
    scrollbar = false
    if v.include?('scrollbar')
      scrollbar = true
    end
    if v.include?('alt-scrollbar')
      scrollbar = true
    end
    Reline::DialogRenderInfo.new(pos: cursor_pos, contents: contents, height: height, scrollbar: scrollbar, face: :completion_dialog)
  })
  if v.include?('alt-scrollbar')
    ENV['RELINE_ALT_SCROLLBAR'] = '1'
  end
}
opt.on('--complete') {
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
    %w{String ScriptError SyntaxError Signal}.select{ |c| c.start_with?(target) }
  }
}
opt.on('--autocomplete') {
  Reline.autocompletion = true
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
    %w{String Struct Symbol ScriptError SyntaxError Signal}.select{ |c| c.start_with?(target) }
  }
}
opt.on('--autocomplete-empty') {
  Reline.autocompletion = true
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil| [] }
}
opt.on('--autocomplete-long') {
  Reline.autocompletion = true
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
    %w{
      String
      Struct
      Symbol
      StopIteration
      SystemCallError
      SystemExit
      SystemStackError
      ScriptError
      SyntaxError
      Signal
      SizedQueue
      Set
      SecureRandom
      Socket
      StringIO
      StringScanner
      Shellwords
      Syslog
      Singleton
      SDBM
    }.select{ |c| c.start_with?(target) }
  }
}
opt.on('--autocomplete-super-long') {
  Reline.autocompletion = true
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
    c = +'A'
    2000.times.map{ s = "Str_#{c}"; c.succ!; s }.select{ |c| c.start_with?(target) }
  }
}

opt.on('--autocomplete-width-long') {
  Reline.autocompletion = true
  Reline.completion_proc = lambda { |target, preposing = nil, postposing = nil|
    %w{
        remove_instance_variable
        respond_to?
        ruby2_keywords
        rand
        readline
        readlines
        require
        require_relative
        raise
        respond_to_missing?
        redo
        rescue
        retry
        return
    }.select{ |c| c.start_with?(target) }
  }
}
opt.parse!(ARGV)

begin
  stty_save = `stty -g`.chomp
rescue
end

begin
  prompt = ENV['RELINE_TEST_PROMPT'] || 'prompt> '
  puts 'Multiline REPL.'
  while code = Reline.readmultiline(prompt, true) { |code| TerminationChecker.terminated?(code) }
    case code.chomp
    when 'exit', 'quit', 'q'
      exit 0
    when ''
      # NOOP
    else
      begin
        result = eval(code)
        puts "=> #{result.inspect}"
      rescue ScriptError, StandardError => e
        puts "Traceback (most recent call last):"
        e.backtrace.reverse_each do |f|
          puts "        #{f}"
        end
        puts e.message
      end
    end
  end
rescue Interrupt
  puts '^C'
  `stty #{stty_save}` if stty_save
  exit 0
ensure
  `stty #{stty_save}` if stty_save
end
begin
  puts
rescue Errno::EIO
  # Maybe the I/O has been closed.
end