Class: DEBUGGER__::Console

Inherits:
Object show all
Defined in:
lib/debug/console.rb

Constant Summary collapse

FH =
"# Today's OMIKUJI: "

Instance Method Summary collapse

Constructor Details

#initializeConsole

Returns a new instance of Console.



183
184
185
# File 'lib/debug/console.rb', line 183

def initialize
  @init_history_lines = nil
end

Instance Method Details

#deactivateObject



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/debug/console.rb', line 193

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|
          # Use scrub to handle encoding issues gracefully
          scrubbed_line = line.scrub.strip
          if !line.start_with?(FH) && !scrubbed_line.empty?
            f.puts scrubbed_line
          end
        }
      }
    end
  end
end

#history_fileObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/debug/console.rb', line 155

def history_file
  case
  when (path = CONFIG[:history_file]) && !path.empty?
    path = File.expand_path(path)
  when (path = File.expand_path("~/.rdbg_history")) && File.exist?(path) # for compatibility
    # path
  else
    state_dir = ENV['XDG_STATE_HOME'] || File.join(Dir.home, '.local', 'state')
    path = File.join(File.expand_path(state_dir), 'rdbg', 'history')
  end

  FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(path)
  path
end

#load_historyObject



214
215
216
217
218
219
220
221
222
# File 'lib/debug/console.rb', line 214

def load_history
  read_history_file.each{|line|
    # Use scrub to handle encoding issues gracefully, then strip
    line.scrub!
    line.strip!
    history << line unless line.empty?
  } if history.empty?
  history.count
end

#load_history_if_not_loadedObject



187
188
189
190
191
# File 'lib/debug/console.rb', line 187

def load_history_if_not_loaded
  return if @init_history_lines

  @init_history_lines = load_history
end

#read_history_fileObject



172
173
174
175
176
177
178
179
180
181
# File 'lib/debug/console.rb', line 172

def read_history_file
  if history && File.exist?(path = history_file())
    f = (['', 'DAI-', 'CHU-', 'SHO-'].map{|e| e+'KICHI'}+['KYO']).sample
    # Read history file and scrub invalid characters to prevent encoding errors
    lines = File.readlines(path).map(&:scrub)
    ["#{FH}#{f}".dup] + lines
  else
    []
  end
end