Skip to content

Commit 43f80ff

Browse files
committed
fix hitory file path
* Set `CONFIG[:history_file]` as nil, and ignore it if it is nil. * Respect `CONFIG[:history_file]` even if the path doesn't exists. * Respect `~/.rdbg_history` if exists for compatibility. * Use `XDG_STATE_HOME` if there is no configuration about history file.
1 parent f0579e4 commit 43f80ff

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ config set no_color true
512512
* `RUBY_DEBUG_INIT_SCRIPT` (`init_script`): debug command script path loaded at first stop
513513
* `RUBY_DEBUG_COMMANDS` (`commands`): debug commands invoked at first stop. Commands should be separated by `;;`
514514
* `RUBY_DEBUG_NO_RC` (`no_rc`): ignore loading ~/.rdbgrc(.rb) (default: false)
515-
* `RUBY_DEBUG_HISTORY_FILE` (`history_file`): history file (default: ~/.rdbg_history)
515+
* `RUBY_DEBUG_HISTORY_FILE` (`history_file`): history file (default: $XDG_STATE_HOME/rdbg/history)
516516
* `RUBY_DEBUG_SAVE_HISTORY` (`save_history`): maximum save history lines (default: 10000)
517517

518518
* REMOTE

lib/debug/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module DEBUGGER__
4040
init_script: ['RUBY_DEBUG_INIT_SCRIPT', "BOOT: debug command script path loaded at first stop"],
4141
commands: ['RUBY_DEBUG_COMMANDS', "BOOT: debug commands invoked at first stop. Commands should be separated by `;;`"],
4242
no_rc: ['RUBY_DEBUG_NO_RC', "BOOT: ignore loading ~/.rdbgrc(.rb)", :bool, "false"],
43-
history_file: ['RUBY_DEBUG_HISTORY_FILE',"BOOT: history file", :string, "~/.rdbg_history"],
43+
history_file: ['RUBY_DEBUG_HISTORY_FILE',"BOOT: history file (default: $XDG_STATE_HOME/rdbg/history)", :string, nil],
4444
save_history: ['RUBY_DEBUG_SAVE_HISTORY',"BOOT: maximum save history lines", :int, "10000"],
4545

4646
# remote setting

lib/debug/console.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,26 +153,24 @@ def history
153153
end
154154

155155
def history_file
156-
path =
157-
if !CONFIG[:history_file].empty? && File.exist?(File.expand_path(CONFIG[:history_file]))
158-
CONFIG[:history_file]
159-
elsif (xdg_home = ENV['XDG_DATA_HOME'])
160-
File.join(xdg_home, 'rdbg', 'history')
161-
else
162-
'~/.rdbg_history'
163-
end
164-
165-
path = File.expand_path(path)
156+
case
157+
when (path = CONFIG[:history_file]) && !path.empty?
158+
path = File.expand_path(path)
159+
when (path = File.expand_path("~/.rdbg_history")) && File.exist?(path) # for compatibility
160+
# path
161+
else
162+
state_dir = ENV['XDG_STATE_HOME'] || File.join(Dir.home, '.local', 'state')
163+
path = File.join(File.expand_path(state_dir), 'rdbg', 'history')
164+
end
166165

167166
FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(path)
168-
169167
path
170168
end
171169

172170
FH = "# Today's OMIKUJI: "
173171

174172
def read_history_file
175-
if history && File.exist?(path = history_file)
173+
if history && File.exist?(path = history_file())
176174
f = (['', 'DAI-', 'CHU-', 'SHO-'].map{|e| e+'KICHI'}+['KYO']).sample
177175
# Read history file and scrub invalid characters to prevent encoding errors
178176
lines = File.readlines(path).map(&:scrub)
@@ -195,12 +193,12 @@ def load_history_if_not_loaded
195193
def deactivate
196194
if history && @init_history_lines
197195
added_records = history.to_a[@init_history_lines .. -1]
198-
path = history_file
196+
path = history_file()
199197
max = CONFIG[:save_history]
200198

201199
if !added_records.empty? && !path.empty?
202200
orig_records = read_history_file
203-
open(history_file, 'w'){|f|
201+
open(history_file(), 'w'){|f|
204202
(orig_records + added_records).last(max).each{|line|
205203
# Use scrub to handle encoding issues gracefully
206204
scrubbed_line = line.scrub.strip

0 commit comments

Comments
 (0)