Class: TTY::System::Editor Private
- Inherits:
-
Object
- Object
- TTY::System::Editor
- Defined in:
- lib/tty/system/editor.rb
Overview
This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.
A class responsible for launching an editor
Instance Attribute Summary collapse
- #file ⇒ Object readonly private
Class Method Summary collapse
-
.available(*commands) ⇒ String
Find available command.
-
.command(*commands) ⇒ String
Finds command using a configured command(s) or detected shell commands.
-
.executables ⇒ Array[String]
private
List possible executable for editor command.
-
.open(file) ⇒ Object
Open file in system editor.
Instance Method Summary collapse
-
#build ⇒ String
private
Build invocation command for editor.
-
#escape_file ⇒ Object
private
Escape file path.
-
#initialize(file) ⇒ Editor
constructor
Initialize an Editor.
-
#invoke ⇒ Object
private
Inovke editor command in a shell.
Constructor Details
#initialize(file) ⇒ Editor
Initialize an Editor
20 21 22 |
# File 'lib/tty/system/editor.rb', line 20 def initialize(file) @file = file end |
Instance Attribute Details
#file ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
11 12 13 |
# File 'lib/tty/system/editor.rb', line 11 def file @file end |
Class Method Details
.available(*commands) ⇒ String
Find available command
40 41 42 43 |
# File 'lib/tty/system/editor.rb', line 40 def self.available(*commands) commands = commands.empty? ? executables : commands commands.compact.uniq.find { |cmd| System.exists?(cmd) } end |
.command(*commands) ⇒ String
Finds command using a configured command(s) or detected shell commands.
52 53 54 55 56 57 58 |
# File 'lib/tty/system/editor.rb', line 52 def self.command(*commands) @command = if @command && commands.empty? @command else available(*commands) end end |
.executables ⇒ Array[String]
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
List possible executable for editor command
29 30 31 |
# File 'lib/tty/system/editor.rb', line 29 def self.executables [ENV['VISUAL'], ENV['EDITOR'], 'vi', 'emacs'] end |
.open(file) ⇒ Object
Open file in system editor
70 71 72 73 74 75 76 |
# File 'lib/tty/system/editor.rb', line 70 def self.open(file) unless command fail CommandInvocationError, 'Please export $VISUAL or $EDITOR' end new(file).invoke end |
Instance Method Details
#build ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Build invocation command for editor
83 84 85 |
# File 'lib/tty/system/editor.rb', line 83 def build "#{Editor.command} #{escape_file}" end |
#escape_file ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Escape file path
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/tty/system/editor.rb', line 90 def escape_file if System.unix? # Escape file string so it can be safely used in a Bourne shell Shellwords.shellescape(file) elsif System.windows? file.gsub(/\//, '\\') else file end end |
#invoke ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Inovke editor command in a shell
106 107 108 109 110 111 |
# File 'lib/tty/system/editor.rb', line 106 def invoke command_invocation = build status = system(*Shellwords.split(command_invocation)) return status if status fail CommandInvocationError, "`#{command_invocation}` failed with status: #{$? ? $?.exitstatus : nil}" end |