diff options
author | aycabta <aycabta@gmail.com> | 2020-04-21 18:17:27 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2020-04-24 16:45:07 +0900 |
commit | 81b0b7919718bf7f8a6bd9590651b62279977327 (patch) | |
tree | e1316670e2db7b804983ef5368a0335cce574025 | |
parent | 71f84018b7fada5c32dfc384f856b6099cc0da6e (diff) | |
download | ruby-81b0b7919718bf7f8a6bd9590651b62279977327.tar.gz |
[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
-rw-r--r-- | lib/reline/config.rb | 22 | ||||
-rw-r--r-- | test/reline/test_config.rb | 38 |
2 files changed, 55 insertions, 5 deletions
diff --git a/lib/reline/config.rb b/lib/reline/config.rb index 53b868fd2e..0e5488eefb 100644 --- a/lib/reline/config.rb +++ b/lib/reline/config.rb @@ -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 diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index cecb364f89..c1a4a46ecf 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -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 |