Skip to content

Commit

Permalink
Change to search default editors when command and env variables check…
Browse files Browse the repository at this point in the history
…s fail

References #3
  • Loading branch information
piotrmurach committed May 22, 2021
1 parent 61b7f1e commit dee630a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [v0.7.0] - unreleased

### Changed
* Change to search default editors when command and env variables checks fail

### Fixed
* Fix Editor#exist? to correctly check command with an absolute path

Expand Down
31 changes: 22 additions & 9 deletions lib/tty/editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,37 @@ def self.from_env

# Find available text editors
#
# @param [Array[String]] commands
# @param [Array<String>] commands
# the commands to use intstead of defaults
#
# @return [Array[String]]
# @return [Array<String>]
# the existing editor commands
#
# @api public
def self.available(*commands)
execs = if !commands.empty?
commands.map(&:to_s)
elsif from_env.any?
[from_env.first]
else
EXECUTABLES
end
if commands.any?
execs = search_executables(commands.map(&:to_s))
return execs unless execs.empty?
end

if from_env.any?
execs = search_executables(from_env)
return execs unless execs.empty?
end

search_executables(EXECUTABLES)
end

# Search for existing executables
#
# @return [Array<String>]
#
# @api private
def self.search_executables(execs)
execs.compact.map(&:strip).reject(&:empty?).uniq
.select { |exec| exist?(exec.split.first) }
end
private_class_method :search_executables

# Open file in system editor
#
Expand Down
20 changes: 19 additions & 1 deletion spec/unit/available_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,31 @@

it "uses custom editor" do
allow(described_class).to receive(:exist?).and_return(true)
allow(described_class).to receive(:exist?).with("custom-ed").and_return(true)

expect(described_class.available("custom")).to eql(["custom"])
expect(described_class.available("custom-ed")).to eql(["custom-ed"])
end

it "sets custom editor commands with symbols" do
allow(described_class).to receive(:exist?).and_return(true)

expect(described_class.available(:vim, :emacs)).to eql(%w[vim emacs])
end

it "checks EDITOR and VISUAL when a custom command doesn't exist" do
allow(ENV).to receive(:[]).with("VISUAL").and_return("env-editor")
allow(described_class).to receive(:exist?).with("custom-ed").and_return(false)
allow(described_class).to receive(:exist?).with("env-editor").and_return(true)

expect(described_class.available("custom-ed")).to eq(["env-editor"])
end

it "checks default editors when custom and ENV commands don't exist" do
allow(ENV).to receive(:[]).with("VISUAL").and_return("env-editor")
allow(described_class).to receive(:exist?).and_return(false)
allow(described_class).to receive(:exist?).with("vim").and_return(true)
allow(described_class).to receive(:exist?).with("emacs").and_return(true)

expect(described_class.available("comst-ed")).to eq(%w[vim emacs])
end
end

0 comments on commit dee630a

Please sign in to comment.