/
console.rb
213 lines (177 loc) · 5.14 KB
/
console.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
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
# frozen_string_literal: true
module DEBUGGER__
class Console
begin
raise LoadError if CONFIG[:no_reline]
require 'reline'
require_relative 'color'
include Color
def parse_input buff, commands
c, rest = get_command buff
case
when commands.keys.include?(c)
:command
when !rest && /\A\s*[a-z]*\z/ =~ c
nil
else
:ruby
end
end
def readline_setup prompt
load_history_if_not_loaded
commands = DEBUGGER__.commands
prev_completion_proc = Reline.completion_proc
prev_output_modifier_proc = Reline.output_modifier_proc
prev_prompt_proc = Reline.prompt_proc
# prompt state
state = nil # :command, :ruby, nil (unknown)
Reline.prompt_proc = -> args, *kw do
case state = parse_input(args.first, commands)
when nil, :command
[prompt]
when :ruby
[prompt.sub('rdbg'){colorize('ruby', [:RED])}]
end * args.size
end
Reline.completion_proc = -> given do
buff = Reline.line_buffer
Reline.completion_append_character= ' '
if /\s/ =~ buff # second parameters
given = File.expand_path(given + 'a').sub(/a\z/, '')
files = Dir.glob(given + '*')
if files.size == 1 && File.directory?(files.first)
Reline.completion_append_character= '/'
end
files
else
commands.keys.grep(/\A#{Regexp.escape(given)}/)
end
end
Reline.output_modifier_proc = -> buff, **kw do
c, rest = get_command buff
case state
when :command
cmd = colorize(c, [:CYAN, :UNDERLINE])
if commands[c] == c
rprompt = colorize(" # command", [:DIM])
else
rprompt = colorize(" # #{commands[c]} command", [:DIM])
end
rest = rest ? colorize_code(rest) : ''
cmd + rest + rprompt
when nil
buff
when :ruby
colorize_code(buff)
end
end unless CONFIG[:no_hint]
yield
ensure
Reline.completion_proc = prev_completion_proc
Reline.output_modifier_proc = prev_output_modifier_proc
Reline.prompt_proc = prev_prompt_proc
end
private def get_command line
case line.chomp
when /\A(\s*[a-z]+)(\s.*)?\z$/
return $1.strip, $2
else
line.strip
end
end
def readline prompt
readline_setup prompt do
Reline.readmultiline(prompt, true){ true }
end
end
def history
Reline::HISTORY
end
rescue LoadError
begin
require 'readline.so'
def readline_setup
load_history_if_not_loaded
commands = DEBUGGER__.commands
Readline.completion_proc = proc{|given|
buff = Readline.line_buffer
Readline.completion_append_character= ' '
if /\s/ =~ buff # second parameters
given = File.expand_path(given + 'a').sub(/a\z/, '')
files = Dir.glob(given + '*')
if files.size == 1 && File.directory?(files.first)
Readline.completion_append_character= '/'
end
files
else
commands.keys.grep(/\A#{given}/)
end
}
end
def readline prompt
readline_setup
Readline.readline(prompt, true)
end
def history
Readline::HISTORY
end
rescue LoadError
def readline prompt
print prompt
gets
end
def history
nil
end
end
end
def history_file
history_file = CONFIG[:history_file]
if !history_file.empty?
File.expand_path(history_file)
else
history_file
end
end
FH = "# Today's OMIKUJI: "
def read_history_file
if history && File.exist?(path = history_file)
f = (['', 'DAI-', 'CHU-', 'SHO-'].map{|e| e+'KICHI'}+['KYO']).sample
["#{FH}#{f}".dup] + File.readlines(path)
else
[]
end
end
def initialize
@init_history_lines = nil
end
def load_history_if_not_loaded
return if @init_history_lines
@init_history_lines = load_history
end
def deactivate
if history && @init_history_lines
added_records = history.to_a[@init_history_lines .. -1]
path = history_file
max = CONFIG[:save_history]
if !added_records.empty? && !path.empty?
orig_records = read_history_file
open(history_file, 'w'){|f|
(orig_records + added_records).last(max).each{|line|
if !line.start_with?(FH) && !line.strip.empty?
f.puts line.strip
end
}
}
end
end
end
def load_history
read_history_file.each{|line|
line.strip!
history << line unless line.empty?
} if history.empty?
history.count
end
end # class Console
end