tty-editor 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +47 -9
- data/examples/choices.rb +9 -0
- data/examples/env.rb +7 -0
- data/lib/tty-editor.rb +1 -2
- data/lib/tty/editor.rb +78 -46
- data/lib/tty/editor/version.rb +1 -1
- data/tty-editor.gemspec +2 -2
- metadata +8 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f635e6b8b169d9aa4acb44bba6bc733e5cf6304
|
4
|
+
data.tar.gz: fb78c565c78344b4b213f5ba650e630b411ea0c6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 273927959a7bcbd0af32c5227ee83f903215fc274184c219fb4284692508cdcac65e872aaf4c1ac73714e2745c9605628110b2ac2327c6c6cada01893f01d3d4
|
7
|
+
data.tar.gz: b2d1ab5052f8310d45694bfaa2a592a0833963e6ec5f5961755087c43c1e76ee36c5fd39386f194e740346ccd9dd5d678895f0cf165f1aea54597993442bac21
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,21 @@
|
|
1
1
|
# Change log
|
2
2
|
|
3
|
+
## [v0.2.0] - 2017-03-26
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Add support for Windows by using notepad editor
|
7
|
+
* Add :content option to Editor#open to distinguish between opening
|
8
|
+
a file with/without content
|
9
|
+
|
10
|
+
### Changed
|
11
|
+
* Change to prioritise $VISUAL & $EDITOR env variables
|
12
|
+
* Change to stop generating tempfiles when open without filename or content
|
13
|
+
* Change loading paths
|
14
|
+
* Change dependencies
|
15
|
+
|
16
|
+
### Fixed
|
17
|
+
* Fix windows shell escaping
|
18
|
+
|
3
19
|
## [v0.1.2] - 2017-02-06
|
4
20
|
|
5
21
|
### Fixed
|
@@ -14,6 +30,7 @@
|
|
14
30
|
|
15
31
|
* Initial implementation and release
|
16
32
|
|
33
|
+
[v0.2.0]: https://github.com/piotrmurach/tty-editor/compare/v0.1.2...v0.2.0
|
17
34
|
[v0.1.2]: https://github.com/piotrmurach/tty-editor/compare/v0.1.1...v0.1.2
|
18
35
|
[v0.1.1]: https://github.com/piotrmurach/tty-editor/compare/v0.1.0...v0.1.1
|
19
36
|
[v0.1.0]: https://github.com/piotrmurach/tty-editor/compare/v0.1.0
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
# TTY::Editor [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter]
|
2
|
+
|
2
3
|
[![Gem Version](https://badge.fury.io/rb/tty-editor.svg)][gem]
|
3
4
|
[![Build Status](https://secure.travis-ci.org/piotrmurach/tty-editor.svg?branch=master)][travis]
|
4
5
|
[![Build status](https://ci.appveyor.com/api/projects/status/yw4guy16meq5wkee?svg=true)][appveyor]
|
@@ -36,32 +37,69 @@ Or install it yourself as:
|
|
36
37
|
|
37
38
|
## Usage
|
38
39
|
|
39
|
-
|
40
|
+
To edit a file in default editor:
|
40
41
|
|
41
42
|
```ruby
|
42
|
-
TTY::Editor.open
|
43
|
-
TTY::Editor.open('hello.rb') # opens editor with the file contents
|
44
|
-
TTY::Editor.open('some text') # opens editor with text
|
43
|
+
TTY::Editor.open('hello.rb')
|
45
44
|
```
|
46
45
|
|
47
|
-
|
46
|
+
To edit content in a default editor:
|
48
47
|
|
49
48
|
```ruby
|
49
|
+
TTY::Editor.open(content: "some text")
|
50
|
+
```
|
51
|
+
|
52
|
+
You can also set your preferred editor command:
|
50
53
|
|
54
|
+
```ruby
|
51
55
|
TTY::Editor.open('hello.rb', command: :vim)
|
52
56
|
```
|
53
57
|
|
58
|
+
Also, the `VISUAL` or `EDITOR` shell environment variables take precedencee when auto detecting available editors.
|
59
|
+
|
60
|
+
## Interface
|
61
|
+
|
62
|
+
### open
|
63
|
+
|
64
|
+
If you wish to open editor with no file or content do:
|
65
|
+
|
66
|
+
```ruby
|
67
|
+
TTY::Editor.open
|
68
|
+
```
|
69
|
+
|
70
|
+
When editor successfully opens file or content then `true` is returned.
|
71
|
+
|
72
|
+
If the editor cannot be opened, a `TTY::Editor::CommandInvocation` error is raised.
|
73
|
+
|
74
|
+
In order to open text content inside an editor do:
|
75
|
+
|
76
|
+
```ruby
|
77
|
+
TTY::Editor.open(content: 'text')
|
78
|
+
```
|
79
|
+
|
80
|
+
You can also provide filename that will be created with specified content before editor is opened:
|
81
|
+
|
82
|
+
```ruby
|
83
|
+
TTY::Editor.open('new.rb', content: 'text')
|
84
|
+
```
|
85
|
+
|
86
|
+
If you open a filename with already existing content then new content gets appended at the end of the file.
|
87
|
+
|
88
|
+
### :env
|
89
|
+
|
54
90
|
Use `:env` key to forward environment variables to the editor.
|
55
91
|
|
56
92
|
```ruby
|
57
|
-
TTY::Editor.open('hello.rb', env: {
|
93
|
+
TTY::Editor.open('hello.rb', env: {"FOO" => "bar"})
|
58
94
|
```
|
59
95
|
|
60
|
-
|
96
|
+
### :command
|
61
97
|
|
62
|
-
|
98
|
+
You can force to always use a specific editor by passing `:command` option:
|
63
99
|
|
64
|
-
|
100
|
+
```ruby
|
101
|
+
TTY::Editor.open('hello.rb', command: :vim)
|
102
|
+
```
|
65
103
|
|
66
104
|
## Development
|
67
105
|
|
data/examples/choices.rb
ADDED
data/examples/env.rb
ADDED
data/lib/tty-editor.rb
CHANGED
data/lib/tty/editor.rb
CHANGED
@@ -3,12 +3,15 @@
|
|
3
3
|
require 'tty-prompt'
|
4
4
|
require 'tty-which'
|
5
5
|
require 'tempfile'
|
6
|
+
require 'fileutils'
|
6
7
|
require 'shellwords'
|
7
8
|
|
9
|
+
require_relative 'editor/version'
|
10
|
+
|
8
11
|
module TTY
|
9
12
|
# A class responsible for launching an editor
|
10
13
|
#
|
11
|
-
# @api
|
14
|
+
# @api public
|
12
15
|
class Editor
|
13
16
|
# Raised when command cannot be invoked
|
14
17
|
class CommandInvocationError < RuntimeError; end
|
@@ -25,14 +28,31 @@ module TTY
|
|
25
28
|
TTY::Which.exist?(cmd)
|
26
29
|
end
|
27
30
|
|
31
|
+
# Check if Windowz
|
32
|
+
#
|
33
|
+
# @return [Boolean]
|
34
|
+
#
|
35
|
+
# @api public
|
36
|
+
def self.windows?
|
37
|
+
::File::ALT_SEPARATOR == "\\"
|
38
|
+
end
|
39
|
+
|
40
|
+
# Check editor from environment variables
|
41
|
+
#
|
42
|
+
# @return [Array[String]]
|
43
|
+
#
|
44
|
+
# @api public
|
45
|
+
def self.from_env
|
46
|
+
[ENV['VISUAL'], ENV['EDITOR']].compact
|
47
|
+
end
|
48
|
+
|
28
49
|
# List possible executable for editor command
|
29
50
|
#
|
30
51
|
# @return [Array[String]]
|
31
52
|
#
|
32
|
-
# @api
|
53
|
+
# @api public
|
33
54
|
def self.executables
|
34
|
-
[
|
35
|
-
'vim', 'vi', 'emacs', 'nano', 'nano-tiny', 'pico', 'mate -w'].compact
|
55
|
+
['vim', 'vi', 'emacs', 'nano', 'nano-tiny', 'pico', 'mate -w']
|
36
56
|
end
|
37
57
|
|
38
58
|
# Find available command
|
@@ -44,8 +64,15 @@ module TTY
|
|
44
64
|
#
|
45
65
|
# @api public
|
46
66
|
def self.available(*commands)
|
47
|
-
commands
|
48
|
-
|
67
|
+
return commands unless commands.empty?
|
68
|
+
|
69
|
+
if !from_env.all?(&:empty?)
|
70
|
+
[from_env.find { |e| !e.empty? }]
|
71
|
+
elsif windows?
|
72
|
+
['notepad']
|
73
|
+
else
|
74
|
+
executables.uniq.select(&method(:exist?))
|
75
|
+
end
|
49
76
|
end
|
50
77
|
|
51
78
|
# Open file in system editor
|
@@ -64,33 +91,49 @@ module TTY
|
|
64
91
|
|
65
92
|
yield(editor) if block_given?
|
66
93
|
|
67
|
-
editor.
|
94
|
+
editor.open
|
68
95
|
end
|
69
96
|
|
70
97
|
# Initialize an Editor
|
71
98
|
#
|
72
99
|
# @param [String] file
|
100
|
+
# @param [Hash[Symbol]] options
|
101
|
+
# @option options [Hash] :command
|
102
|
+
# the editor command to use, by default auto detects
|
103
|
+
# @option options [Hash] :env
|
104
|
+
# environment variables to forward to the editor
|
73
105
|
#
|
74
106
|
# @api public
|
75
|
-
def initialize(
|
107
|
+
def initialize(*args, **options)
|
108
|
+
@filename = args.unshift.first
|
76
109
|
@env = options.fetch(:env) { {} }
|
77
110
|
@command = options[:command]
|
78
|
-
@filename
|
111
|
+
if @filename
|
112
|
+
if ::File.exist?(@filename) && !::FileTest.file?(@filename)
|
113
|
+
raise ArgumentError, "Don't know how to handle `#{@filename}`. " \
|
114
|
+
"Please provida a file path or content"
|
115
|
+
elsif ::File.exist?(@filename) && !options[:content].to_s.empty?
|
116
|
+
::File.open(@filename, 'a') { |f| f.write(options[:content]) }
|
117
|
+
elsif !::File.exist?(@filename)
|
118
|
+
::File.write(@filename, options[:content])
|
119
|
+
end
|
120
|
+
elsif options[:content]
|
121
|
+
@filename = tempfile_path(options[:content])
|
122
|
+
end
|
79
123
|
end
|
80
124
|
|
81
|
-
#
|
125
|
+
# Read or update environment vars
|
82
126
|
#
|
83
|
-
# @
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
def file_or_temp_path(filename)
|
88
|
-
::FileTest.file?(filename) ? filename : tempfile_path(filename)
|
127
|
+
# @api public
|
128
|
+
def env(value = (not_set = true))
|
129
|
+
return @env if not_set
|
130
|
+
@env = value
|
89
131
|
end
|
90
132
|
|
91
133
|
# Finds command using a configured command(s) or detected shell commands.
|
92
134
|
#
|
93
135
|
# @param [Array[String]] commands
|
136
|
+
# the optional command to use, by default auto detecting
|
94
137
|
#
|
95
138
|
# @raise [TTY::CommandInvocationError]
|
96
139
|
#
|
@@ -98,43 +141,32 @@ module TTY
|
|
98
141
|
#
|
99
142
|
# @api public
|
100
143
|
def command(*commands)
|
101
|
-
if @command && commands.empty?
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
'Could not find editor to use. Please specify $VISUAL or $EDITOR'
|
108
|
-
else
|
109
|
-
exec = if execs.size > 1
|
110
|
-
prompt = TTY::Prompt.new
|
111
|
-
prompt.enum_select('Select an editor?', execs)
|
112
|
-
else
|
113
|
-
execs[0]
|
114
|
-
end
|
115
|
-
@command = TTY::Which.which(exec)
|
116
|
-
end
|
144
|
+
return @command if @command && commands.empty?
|
145
|
+
|
146
|
+
execs = self.class.available(*commands)
|
147
|
+
if execs.empty?
|
148
|
+
raise EditorNotFoundError,
|
149
|
+
'Could not find editor to use. Please specify $VISUAL or $EDITOR'
|
117
150
|
end
|
151
|
+
exec = choose_exec_from(execs)
|
152
|
+
@command = TTY::Which.which(exec.to_s)
|
118
153
|
end
|
119
154
|
|
120
|
-
#
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
155
|
+
# @api private
|
156
|
+
def choose_exec_from(execs)
|
157
|
+
if execs.size > 1
|
158
|
+
prompt = TTY::Prompt.new
|
159
|
+
prompt.enum_select('Select an editor?', execs)
|
160
|
+
else
|
161
|
+
execs[0]
|
162
|
+
end
|
127
163
|
end
|
128
164
|
|
129
165
|
# Escape file path
|
130
166
|
#
|
131
167
|
# @api private
|
132
168
|
def escape_file
|
133
|
-
|
134
|
-
@filename.gsub(/\//, '\\')
|
135
|
-
else
|
136
|
-
Shellwords.shellescape(@filename)
|
137
|
-
end
|
169
|
+
Shellwords.shellescape(@filename)
|
138
170
|
end
|
139
171
|
|
140
172
|
# Build command path to invoke
|
@@ -167,8 +199,8 @@ module TTY
|
|
167
199
|
# @raise [TTY::CommandInvocationError]
|
168
200
|
#
|
169
201
|
# @api private
|
170
|
-
def
|
171
|
-
status = system(*Shellwords.split(command_path))
|
202
|
+
def open
|
203
|
+
status = system(env, *Shellwords.split(command_path))
|
172
204
|
return status if status
|
173
205
|
fail CommandInvocationError,
|
174
206
|
"`#{command_path}` failed with status: #{$? ? $?.exitstatus : nil}"
|
data/lib/tty/editor/version.rb
CHANGED
data/tty-editor.gemspec
CHANGED
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
20
|
spec.require_paths = ["lib"]
|
21
21
|
|
22
|
-
spec.add_dependency 'tty-prompt', '~> 0.
|
23
|
-
spec.add_dependency 'tty-which', '~> 0.
|
22
|
+
spec.add_dependency 'tty-prompt', '~> 0.12.0'
|
23
|
+
spec.add_dependency 'tty-which', '~> 0.3.0'
|
24
24
|
|
25
25
|
spec.add_development_dependency 'bundler', '>= 1.5.0', '< 2.0'
|
26
26
|
spec.add_development_dependency 'rake', '~> 10.0'
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tty-editor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Murach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-03-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: tty-prompt
|
@@ -16,28 +16,28 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.
|
19
|
+
version: 0.12.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.
|
26
|
+
version: 0.12.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: tty-which
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.3.0
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.3.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -106,7 +106,9 @@ files:
|
|
106
106
|
- bin/console
|
107
107
|
- bin/setup
|
108
108
|
- examples/basic.rb
|
109
|
+
- examples/choices.rb
|
109
110
|
- examples/empty.rb
|
111
|
+
- examples/env.rb
|
110
112
|
- examples/tempfile.rb
|
111
113
|
- lib/tty-editor.rb
|
112
114
|
- lib/tty/editor.rb
|