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.



176
177
178
# File 'lib/debug/console.rb', line 176

def initialize
  @init_history_lines = nil
end

Instance Method Details

#deactivateObject



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/debug/console.rb', line 186

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

#history_fileObject



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

def history_file
  history_file = CONFIG[:history_file]

  if !history_file.empty?
    File.expand_path(history_file)
  else
    history_file
  end
end

#load_historyObject



205
206
207
208
209
210
211
# File 'lib/debug/console.rb', line 205

def load_history
  read_history_file.each{|line|
    line.strip!
    history << line unless line.empty?
  } if history.empty?
  history.count
end

#load_history_if_not_loadedObject



180
181
182
183
184
# File 'lib/debug/console.rb', line 180

def load_history_if_not_loaded
  return if @init_history_lines

  @init_history_lines = load_history
end

#read_history_fileObject



167
168
169
170
171
172
173
174
# File 'lib/debug/console.rb', line 167

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