interactive_editor 0.0.6 → 0.0.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +5 -1
- data/interactive_editor.gemspec +2 -2
- data/lib/interactive_editor.rb +38 -16
- metadata +6 -6
data/README.md
CHANGED
@@ -14,7 +14,7 @@ Put the following in your .irbrc:
|
|
14
14
|
|
15
15
|
Then, from within irb:
|
16
16
|
|
17
|
-
$ irb
|
17
|
+
$ irb # or ripl
|
18
18
|
> vi # (use vi w/ temp file)
|
19
19
|
> vi 'filename.rb' # (open filename.rb in vi)
|
20
20
|
> ed # (use EDITOR env variable)
|
@@ -27,6 +27,10 @@ To try it out without installing the gem:
|
|
27
27
|
$ cd interactive_editor
|
28
28
|
$ rake console
|
29
29
|
|
30
|
+
interactive_editor also works with the IRB alternative [ripl][].
|
31
|
+
|
30
32
|
## Credits
|
31
33
|
|
32
34
|
Giles Bowkett, Greg Brown, and several audience members from Giles' Ruby East presentation: [Use vi or any text editor from within IRB](http://gilesbowkett.blogspot.com/2007/10/use-vi-or-any-text-editor-from-within.html).
|
35
|
+
|
36
|
+
[ripl]: https://github.com/cldwalker/ripl
|
data/interactive_editor.gemspec
CHANGED
@@ -7,8 +7,8 @@ Gem::Specification.new do |s|
|
|
7
7
|
## If your rubyforge_project name is different, then edit it and comment out
|
8
8
|
## the sub! line in the Rakefile
|
9
9
|
s.name = 'interactive_editor'
|
10
|
-
s.version = '0.0.
|
11
|
-
s.date = '
|
10
|
+
s.version = '0.0.7'
|
11
|
+
s.date = '2011-03-11'
|
12
12
|
s.rubyforge_project = 'interactive_editor'
|
13
13
|
|
14
14
|
## Make sure your summary is short. The description may be as long
|
data/lib/interactive_editor.rb
CHANGED
@@ -6,9 +6,10 @@ require 'irb'
|
|
6
6
|
require 'fileutils'
|
7
7
|
require 'tempfile'
|
8
8
|
require 'shellwords'
|
9
|
+
require 'yaml'
|
9
10
|
|
10
11
|
class InteractiveEditor
|
11
|
-
VERSION = '0.0.
|
12
|
+
VERSION = '0.0.7'
|
12
13
|
|
13
14
|
attr_accessor :editor
|
14
15
|
|
@@ -16,30 +17,47 @@ class InteractiveEditor
|
|
16
17
|
@editor = editor.to_s
|
17
18
|
end
|
18
19
|
|
19
|
-
def edit(file=nil)
|
20
|
-
|
21
|
-
|
22
|
-
|
20
|
+
def edit(object, file=nil)
|
21
|
+
object = object == TOPLEVEL_BINDING.eval('self') ? nil : object
|
22
|
+
|
23
|
+
current_file = if file
|
24
|
+
FileUtils.touch(file) unless File.exist?(file)
|
25
|
+
File.new(file)
|
26
|
+
else
|
27
|
+
if @file && File.exist?(@file.path) && !object
|
28
|
+
@file
|
23
29
|
else
|
24
|
-
|
30
|
+
Tempfile.new( object ? ["yobj_tempfile", ".yml"] : ["irb_tempfile", ".rb"] )
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if object
|
35
|
+
File.open( current_file, 'w' ) { |f| f << object.to_yaml }
|
36
|
+
else
|
37
|
+
@file = current_file
|
38
|
+
mtime = File.stat(@file.path).mtime
|
25
39
|
end
|
26
|
-
mtime = File.stat(@file.path).mtime
|
27
40
|
|
28
|
-
args = Shellwords.shellwords(@editor) #parse @editor as arguments could be
|
29
|
-
args <<
|
30
|
-
|
41
|
+
args = Shellwords.shellwords(@editor) #parse @editor as arguments could be complex
|
42
|
+
args << current_file.path
|
43
|
+
current_file.close rescue nil
|
44
|
+
Exec.system(*args)
|
31
45
|
|
32
|
-
|
46
|
+
if object
|
47
|
+
File.exists?(current_file.path) ? YAML.load_file(current_file.path) : object
|
48
|
+
elsif mtime < File.stat(@file.path).mtime
|
49
|
+
execute
|
50
|
+
end
|
33
51
|
end
|
34
52
|
|
35
53
|
def execute
|
36
54
|
eval(IO.read(@file.path), TOPLEVEL_BINDING)
|
37
55
|
end
|
38
56
|
|
39
|
-
def self.edit(editor, file=nil)
|
57
|
+
def self.edit(editor, self_, file=nil)
|
40
58
|
#maybe serialise last file to disk, for recovery
|
41
59
|
(IRB.conf[:interactive_editors] ||=
|
42
|
-
Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(file)
|
60
|
+
Hash.new { |h,k| h[k] = InteractiveEditor.new(k) })[editor].edit(self_, file)
|
43
61
|
end
|
44
62
|
|
45
63
|
module Exec
|
@@ -68,16 +86,20 @@ class InteractiveEditor
|
|
68
86
|
:emacs => nil,
|
69
87
|
:nano => nil,
|
70
88
|
:mate => 'mate -w',
|
71
|
-
:mvim => 'mvim -g -f
|
89
|
+
:mvim => 'mvim -g -f' + case ENV['TERM_PROGRAM']
|
90
|
+
when 'iTerm.app'; ' -c "au VimLeave * !open -a iTerm"'
|
91
|
+
when 'Apple_Terminal'; ' -c "au VimLeave * !open -a Terminal"'
|
92
|
+
else '' #don't do tricky things if we don't know the Term
|
93
|
+
end
|
72
94
|
}.each do |k,v|
|
73
95
|
define_method(k) do |*args|
|
74
|
-
|
96
|
+
InteractiveEditor.edit(v || k, self, *args)
|
75
97
|
end
|
76
98
|
end
|
77
99
|
|
78
100
|
def ed(*args)
|
79
101
|
if ENV['EDITOR'].to_s.size > 0
|
80
|
-
InteractiveEditor.edit(ENV['EDITOR'], *args)
|
102
|
+
InteractiveEditor.edit(ENV['EDITOR'], self, *args)
|
81
103
|
else
|
82
104
|
raise "You need to set the EDITOR environment variable first"
|
83
105
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interactive_editor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 17
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 7
|
10
|
+
version: 0.0.7
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jan Berkel
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date:
|
18
|
+
date: 2011-03-11 00:00:00 +01:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -79,7 +79,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
79
79
|
requirements: []
|
80
80
|
|
81
81
|
rubyforge_project: interactive_editor
|
82
|
-
rubygems_version: 1.
|
82
|
+
rubygems_version: 1.4.2
|
83
83
|
signing_key:
|
84
84
|
specification_version: 2
|
85
85
|
summary: Interactive editing in irb.
|