Class: TTY::System::Editor

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/system/editor.rb

Overview

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)


18
19
20
# File 'lib/tty/system/editor.rb', line 18

def initialize(file)
  @file = file
end

Instance Attribute Details

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

Parameters:

  • commands (Array[String])

Returns:

  • (String)


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.

Parameters:

  • commands (Array[String])

Returns:

  • (String)


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

.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])


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

Parameters:

  • file (String)

    the name of the file

Returns:

  • (Object)

Raises:



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

#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)


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

#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



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