@@ -116,52 +116,30 @@ def clear_reline
116
116
117
117
def load_history_file ( context )
118
118
history_file = context [ :history_file ]
119
- case context [ :input_library ]
120
- when :readline
121
- return unless readline_available?
122
-
123
- clear_readline
124
- if File . exist? ( history_file )
125
- File . readlines ( history_file ) . each do |e |
126
- ::Readline ::HISTORY << safe_undump ( e . chomp )
127
- end
128
- end
129
- when :reline
130
- return unless reline_available?
131
-
132
- clear_reline
133
- if File . exist? ( history_file )
134
- File . readlines ( history_file ) . each do |e |
135
- ::Reline ::HISTORY << safe_undump ( e . chomp )
119
+ history = context [ :input_library ] == :reline ? ::Reline ::HISTORY : ::Readline ::HISTORY
120
+
121
+ if File . exist? ( history_file )
122
+ File . open ( history_file , 'r' ) do |f |
123
+ f . each do |line |
124
+ chomped_line = line . chomp
125
+ if context [ :input_library ] == :reline && history . last &.end_with? ( "\\ " )
126
+ history . last . delete_suffix! ( "\\ " )
127
+ history . last << "\n " << chomped_line
128
+ else
129
+ history << chomped_line
130
+ end
136
131
end
137
132
end
138
133
end
139
134
end
140
135
141
136
def store_history_file ( context )
142
- cmds = [ ]
143
137
history_file = context [ :history_file ]
138
+ history = context [ :input_library ] == :reline ? ::Reline ::HISTORY : ::Readline ::HISTORY
144
139
145
- case context [ :input_library ]
146
- when :readline
147
- return unless readline_available?
148
-
149
- history_diff = ::Readline ::HISTORY . length < MAX_HISTORY ? ::Readline ::HISTORY . length : MAX_HISTORY
150
- history_diff . times do
151
- entry = ::Readline ::HISTORY . pop . dump
152
- cmds . push ( entry ) unless entry . nil?
153
- end
154
- when :reline
155
- return unless reline_available?
156
-
157
- history_diff = ::Reline ::HISTORY . length < MAX_HISTORY ? ::Reline ::HISTORY . length : MAX_HISTORY
158
- history_diff . times do
159
- entry = ::Reline ::HISTORY . pop . dump
160
- cmds . push ( entry ) unless entry . nil?
161
- end
162
- end
140
+ history_to_save = history . map { |line | line . scrub . split ( "\n " ) . join ( "\\ \n " ) }
163
141
164
- write_history_file ( history_file , cmds )
142
+ write_history_file ( history_file , history_to_save )
165
143
end
166
144
167
145
def switch_context ( new_context , old_context = nil )
@@ -190,7 +168,7 @@ def write_history_file(history_file, cmds)
190
168
cmds = event [ :cmds ]
191
169
192
170
File . open ( history_file , 'wb+' ) do |f |
193
- f . puts ( cmds . reverse )
171
+ f . puts ( cmds )
194
172
end
195
173
196
174
rescue => e
@@ -205,16 +183,6 @@ def write_history_file(history_file, cmds)
205
183
@write_queue << event
206
184
@remaining_work << event
207
185
end
208
-
209
- # @param [String] dumped A string that has been previously dumped
210
- # @return [String] A string that is undumped if possible or the input if it can't be undumped.
211
- def safe_undump ( dumped )
212
- begin
213
- dumped . undump
214
- rescue ::RuntimeError => _e
215
- dumped
216
- end
217
- end
218
186
end
219
187
220
188
end
0 commit comments