Prev: [ANN] 2010 GoRuCo Videos Up.
Next: Net::SCP download from windows to linux and encrypt password
From: R.. Kumar on 29 Jun 2010 01:31 Does Highline or Thor/Facets/ActiveSupport etc have a method for when you want user to edit multiline text using the $EDITOR value? Commander has but it does not work with vim. So i wrote a method, if anyone has time please give it a look and give me feedback. Is it correct to pass back a nil if user does not edit, or should I pass a blank String. Thanks. (File attached). http://gist.github.com/456809 Also, following is the method "commander" has. It is supposed to work with TextMate but does not with Vim. It does open TextEdit in the background, but does not seem to put the text in it. def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate' IO.popen(editor.to_s, 'w+') do |pipe| pipe.puts input.to_s unless input.nil? pipe.close_write pipe.read end end I'd like to know why the developer of commander used this instead of a "system" command. In what way is this superior ? Why does it fail with vim. Attachments: http://www.ruby-forum.com/attachment/4822/edit.rb -- Posted via http://www.ruby-forum.com/.
From: Caleb Clausen on 5 Jul 2010 17:39 On 6/28/10, R.. Kumar <sentinel1879(a)gmail.com> wrote: > Does Highline or Thor/Facets/ActiveSupport etc have a method for when > you want user to edit multiline text using the $EDITOR value? Ok, this was a week ago now, but no one else has responded. > Commander has but it does not work with vim. [snip] > Also, following is the method "commander" has. It is supposed to work > with TextMate but does not with Vim. It does open TextEdit in the > background, but does not seem to put the text in it. > > def ask_editor input = nil, editor = ENV['EDITOR'] || 'mate' > IO.popen(editor.to_s, 'w+') do |pipe| > pipe.puts input.to_s unless input.nil? > pipe.close_write > pipe.read > end > end This code is wrong. EDITOR is supposed to be set to a text-mode editor, which runs inside a text terminal environment. If the user has a graphical editor he wants to use, such as gvim, gnotepad, or textmate, he should set the VISUAL environment variable instead. ask_editor or equivalent code should look for VISUAL first and then EDITOR only if VISUAL is not set. A gui editor needs to be called in a different way than a text-mode editor is called. A gui editor needs no controlling text-mode terminal, whereas a text-mode editor requires one. A gui editor should be called asynchronously whereas a text-mode editor must be called synchronously. A gui editor doesn't (really) use stdin/stdout/stderr whereas a text-mode editor must use them. The code above is designed to be used with a gui editor, even tho it consults the EDITOR environment variable. It starts the editor process asynchronously. It captures the subprocess's stdin and uses it for a different purpose, which prevents vim from ever getting any input from the user. These factors sccount for the problems you observed, I believe. ENV['EDITOR'] in that method should be replaced with ENV['VISUAL']. (And it should be expanded to use EDITOR if that is set as well...) > So i wrote a method, if > anyone has time please give it a look and give me feedback. Is it > correct to pass back a nil if user does not edit, or should I pass a > blank String. > > Thanks. (File attached). > http://gist.github.com/456809 > I'd like to know why the developer of commander used this instead of a > "system" command. In what way is this superior ? Why does it fail with > vim. > > Attachments: > http://www.ruby-forum.com/attachment/4822/edit.rb Your code looks pretty good to me. You really ought to check for VISUAL as well, as I outlined above. If VISUAL is set, do something like what the ask_editor code you posted above is doing. Except I don't think you should assume you can use stdin and stdout to communicate with a gui editor; that sounds too textmate specific to me. Working with temp files like you have for the EDITOR case is the most portable and general plan. Some other minor nits: 1) quote all words on the command line, to handle spaces in filenames better. Better yet, break the command line up into words, or rather don't join them together in one line. So, replace system("#{ed} #{temp.path}") with system(ed, temp.path) 2) I'd just read the whole file in one big slirp instead of line by line. (Unless you think you're user is really going to write a whole lot of stuff.... in which case you need to rewrite anyway.)
From: R.. Kumar 1.9.1 OSX on 6 Jul 2010 03:46 R.. Kumar 1.9.1 OSX wrote: > > However, since "mail -s" requires both input, as well as some command > line parameters (title and emailid), i was not clear whether this method > would work. > > thx. This works: def pipe_output (str) pipeto = %q{mail -s "a title" myemail } if pipeto != nil # i was taking pipeto from a hash, so checking proc = IO.popen(pipeto, "w+") proc.puts str proc.close_write #puts proc.gets end end str = "this is some data to go in mail" pipe_output str Not sure which is cleaner and more reliable though, this way or: cmd = %q{cat file.txt | mail -s "title" email} system(cmd) -- Posted via http://www.ruby-forum.com/.
From: Caleb Clausen on 6 Jul 2010 09:30 On 7/6/10, R.. Kumar 1.9.1 OSX <sentinel1879(a)gmail.com> wrote: > R.. Kumar 1.9.1 OSX wrote: > >> >> However, since "mail -s" requires both input, as well as some command >> line parameters (title and emailid), i was not clear whether this method >> would work. >> >> thx. > > This works: > > def pipe_output (str) > > pipeto = %q{mail -s "a title" myemail } > if pipeto != nil # i was taking pipeto from a hash, so checking > proc = IO.popen(pipeto, "w+") > proc.puts str > proc.close_write > #puts proc.gets > end > end > > str = "this is some data to go in mail" > pipe_output str > > Not sure which is cleaner and more reliable though, this way or: > > cmd = %q{cat file.txt | mail -s "title" email} > system(cmd) Either way should work fine, but I would go with popen, personally. I think it's a little cleaner semantically; might be a tad more portable too.
|
Pages: 1 Prev: [ANN] 2010 GoRuCo Videos Up. Next: Net::SCP download from windows to linux and encrypt password |