1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

[ruby/reline] Support XDG_CONFIG_HOME

In the XDG Specification, if ~/.config/readline/inputrc exists, then ~/.inputrc
should not be read, but for compatibility with GNU Readline, if ~/.inputrc
exists, then it is given priority.

https://github.com/ruby/reline/commit/97f1e7db04
This commit is contained in:
aycabta 2020-04-21 18:17:27 +09:00
parent 71f84018b7
commit 81b0b79197
2 changed files with 55 additions and 5 deletions

View file

@ -3,8 +3,6 @@ require 'pathname'
class Reline::Config
attr_reader :test_mode
DEFAULT_PATH = '~/.inputrc'
KEYSEQ_PATTERN = /\\(?:C|Control)-[A-Za-z_]|\\(?:M|Meta)-[0-9A-Za-z_]|\\(?:C|Control)-(?:M|Meta)-[A-Za-z_]|\\(?:M|Meta)-(?:C|Control)-[A-Za-z_]|\\e|\\[\\\"\'abdfnrtv]|\\\d{1,3}|\\x\h{1,2}|./
class InvalidInputrc < RuntimeError
@ -86,14 +84,28 @@ class Reline::Config
def inputrc_path
case ENV['INPUTRC']
when nil, ''
DEFAULT_PATH
else
ENV['INPUTRC']
return File.expand_path(ENV['INPUTRC'])
end
# In the XDG Specification, if ~/.config/readline/inputrc exists, then
# ~/.inputrc should not be read, but for compatibility with GNU Readline,
# if ~/.inputrc exists, then it is given priority.
path = File.expand_path('~/.inputrc')
return path if File.exist?(path)
case ENV['XDG_CONFIG_HOME']
when nil, ''
path = File.expand_path('~/.config/readline/inputrc')
return path if File.exist?(path)
else
path = File.expand_path("#{ENV['XDG_CONFIG_HOME']}/readline/inputrc")
return path if File.exist?(path)
end
end
def read(file = nil)
file ||= File.expand_path(inputrc_path)
file ||= inputrc_path
begin
if file.respond_to?(:readlines)
lines = file.readlines

View file

@ -215,4 +215,42 @@ class Reline::Config::Test < Reline::TestCase
end
ENV['INPUTRC'] = inputrc_backup
end
def test_inputrc
inputrc_backup = ENV['INPUTRC']
expected = "#{@tmpdir}/abcde"
ENV['INPUTRC'] = expected
assert_equal expected, @config.inputrc_path
ENV['INPUTRC'] = inputrc_backup
end
def test_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
nonexistence_dir = '/the_nonexistence_dir!!!!!!'
xdg_config_home = File.expand_path("#{@tmpdir}/.config/example_dir")
expected = File.expand_path("#{xdg_config_home}/readline/inputrc")
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
ENV['HOME'] = nonexistence_dir
ENV['XDG_CONFIG_HOME'] = xdg_config_home
assert_equal expected, @config.inputrc_path
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end
def test_empty_xdg_config_home
home_backup = ENV['HOME']
xdg_config_home_backup = ENV['XDG_CONFIG_HOME']
ENV['HOME'] = @tmpdir
ENV['XDG_CONFIG_HOME'] = ''
expected = File.expand_path('~/.config/readline/inputrc')
FileUtils.mkdir_p(File.dirname(expected))
FileUtils.touch(expected)
assert_equal expected, @config.inputrc_path
FileUtils.rm(expected)
ENV['XDG_CONFIG_HOME'] = xdg_config_home_backup
ENV['HOME'] = home_backup
end
end