Class: TTY::System::Editor
- Inherits:
-
Object
- Object
- TTY::System::Editor
- Defined in:
- lib/tty/system/editor.rb
Overview
A class responsible for launching an editor
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
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.
-
#initialize(file) ⇒ Editor
constructor
Initialize an Editor.
-
#invoke ⇒ Object
private
Inovke editor command in a shell.
Constructor Details
#initialize(file) ⇒ Editor
Initialize an Editor
18 19 20 |
# File 'lib/tty/system/editor.rb', line 18 def initialize(file) @file = file end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
11 12 13 |
# File 'lib/tty/system/editor.rb', line 11 def file @file end |
Class Method Details
.available(*commands) ⇒ String
Find available command
38 39 40 41 |
# File 'lib/tty/system/editor.rb', line 38 def self.available(*commands) commands = commands.empty? ? self.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.
50 51 52 53 54 55 56 |
# File 'lib/tty/system/editor.rb', line 50 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
27 28 29 |
# File 'lib/tty/system/editor.rb', line 27 def self.executables [ ENV['VISUAL'], ENV['EDITOR'], 'vi', 'emacs' ] end |
.open(file) ⇒ Object
Open file in system editor
68 69 70 71 72 73 74 75 |
# File 'lib/tty/system/editor.rb', line 68 def self.open(file) unless self.command raise CommandInvocationError, "Please export $VISUAL or $EDITOR" exit 1 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
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tty/system/editor.rb', line 82 def build escaped_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 "#{Editor.command} #{escaped_file}" 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
99 100 101 102 103 104 105 106 107 |
# File 'lib/tty/system/editor.rb', line 99 def invoke command_invocation = build status = system(*Shellwords.split(command_invocation)) unless status raise CommandInvocationError, "`#{command_invocation}` failed with status: #{$? ? $?.exitstatus : nil}" exit status end end |