Class: TTY::System::Editor Private

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Editor

Initialize an Editor

Parameters:

  • file (String)


20
21
22
# File 'lib/tty/system/editor.rb', line 20

def initialize(file)
  @file    = file
end

Instance Attribute Details

#fileObject (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

Parameters:

  • commands (Array[String])

Returns:

  • (String)


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.

Parameters:

  • commands (Array[String])

Returns:

  • (String)


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

.executablesArray[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

Returns:

  • (Array[String])


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

Parameters:

  • file (String)

    the name of the file

Returns:

  • (Object)

Raises:



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

#buildString

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

Returns:

  • (String)


83
84
85
# File 'lib/tty/system/editor.rb', line 83

def build
  "#{Editor.command} #{escape_file}"
end

#escape_fileObject

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

#invokeObject

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