-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrepl.cr
47 lines (42 loc) · 1.15 KB
/
repl.cr
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
require "fancyline"
class Repl
def initialize(history : String, &process : String -> _)
fancy = Fancyline.new
Repl.load_history(fancy, history)
while input = fancy.readline("$ ") # Ask the user for input
begin
if input.starts_with?("%load")
filepath = input.lchop("%load").strip
File.each_line(filepath) { |line|
Repl.eval_and_print process, line
}
else
Repl.eval_and_print process, input
end
rescue ex : Exception
puts "error: #{ex}"
end
end
rescue ex : Fancyline::Interrupt
puts "Shutting down..."
ensure
Repl.write_history(fancy, history)
end
def self.eval_and_print(process, input)
output = process.call input
puts output if output
end
def self.load_history(fancy, history)
if File.exists? history
puts " Reading history from #{history}"
File.open(history, "r") do |io|
fancy.history.load io
end
end
end
def self.write_history(fancy, history)
File.open(history, "w") do |io| # So open it writable
fancy.not_nil!.history.save io # And save. That's it.
end
end
end