Properly encode hyperlinks with special characters

This also works on Windows now, since absolute paths include a colon after the
drive letter, the `file:` URI needs to start with a triple slash.
This commit is contained in:
Claudio Bley 2021-01-14 21:57:43 +01:00
parent 76f148cc55
commit 0ad630da4c
4 changed files with 12 additions and 4 deletions

View file

@ -61,6 +61,7 @@ Gem::Specification.new do |spec|
spec.post_install_message = POST_INSTALL_MESSAGE
spec.add_runtime_dependency 'addressable', '~> 2.7'
spec.add_runtime_dependency 'clocale', '~> 0'
spec.add_runtime_dependency 'filesize', '~> 0'
spec.add_runtime_dependency 'manpages', '~> 0'

View file

@ -8,6 +8,7 @@ require 'io/console'
require 'rainbow/ext/string'
require 'clocale'
require 'unicode/display_width'
require 'addressable/uri'
require 'colorls/core'
require 'colorls/fileinfo'

View file

@ -377,8 +377,8 @@ module ColorLS
end
def make_link(path, name)
href = "file://#{File.absolute_path(path)}/#{name}"
"\033]8;;#{href}\007#{name}\033]8;;\007"
uri = Addressable::URI.convert_path(File.absolute_path(File.join(path, name)))
"\033]8;;#{uri}\007#{name}\033]8;;\007"
end
end
end

View file

@ -291,9 +291,15 @@ RSpec.describe ColorLS::Flags do
context 'with --hyperlink flag' do
let(:args) { ['--hyperlink', FIXTURES] }
href = "file://#{File.absolute_path(FIXTURES)}/a.txt"
href = if File::ALT_SEPARATOR.nil?
"file://#{File.absolute_path(FIXTURES)}/a.txt"
else
"file:///#{File.absolute_path(FIXTURES)}/a.txt"
end
it { expect { subject }.to output(include(href)).to_stdout }
pattern = File.fnmatch('cat', 'CAT', File::FNM_SYSCASE) ? /#{href}/i : /#{href}/
it { expect { subject }.to output(match(pattern)).to_stdout }
end
context 'symlinked directory' do