Skip to content

Instantly share code, notes, and snippets.

@rkumar
Created June 29, 2010 04:55
Show Gist options
  • Save rkumar/456809 to your computer and use it in GitHub Desktop.
Save rkumar/456809 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
# R Kumar
# 2010-06-29 10:24
require 'fileutils'
require 'tempfile'
# edits given text using EDITOR
# @param [String] text to edit
# @return [String, nil] edited string, or nil if no change
def edit_text text
ed = ENV['EDITOR'] || "vim"
temp = Tempfile.new "tmp"
puts temp
File.open(temp,"w"){ |f| f.write text }
mtime = File.mtime(temp.path)
system("#{ed} #{temp.path}")
newmtime = File.mtime(temp.path)
newstr = nil
if mtime < newmtime
# check timestamp, if updated ..
newstr = ""
File.open(temp,"r"){ |f| f.each {|r| newstr << r } }
#puts "I got: #{newstr}"
else
#puts "user quit without saving"
end
return newstr
end
puts "Enter a few lines and press Ctrl-d"
str = $stdin.read
puts
puts edit_text str if str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment