Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/t/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
require 'twitter'
require 't/collectable'
require 't/delete'
require 't/editor'
require 't/list'
require 't/printable'
require 't/rcfile'
Expand Down Expand Up @@ -737,10 +738,11 @@ def unfollow(user, *users)
say "Run `#{File.basename($0)} follow #{users.map{|user| "@#{user.screen_name}"}.join(' ')}` to follow again."
end

desc "update MESSAGE", "Post a Tweet."
desc "update [MESSAGE]", "Post a Tweet."
method_option "location", :aliases => "-l", :type => :string, :default => nil, :desc => "Add location information. If the optional 'latitude,longitude' parameter is not supplied, looks up location by IP address."
method_option "file", :aliases => "-f", :type => :string, :desc => "The path to an image to attach to your tweet."
def update(message)
def update(message = nil)
message = T::Editor.gets(:update) if message.nil? || message.empty?
opts = {:trim_user => true}
opts = add_location(options, opts)

Expand Down
43 changes: 43 additions & 0 deletions lib/t/editor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'tempfile'
require 'shellwords'

module T
class Editor
class << self
PREFILLS = {
:update => "\n# Enter your tweet above."
}

def gets(operation = :update)
f = tempfile(PREFILLS[operation])
edit(f.path)
f.read.gsub(/(?:^#.*$\n?)+\s*\z/, '').strip
end

def tempfile(prefill = PREFILLS[:update])
f = Tempfile.new("TWEET_MESSAGE")
f << prefill
f.rewind
f
end

def edit(path)
system Shellwords.join([editor, path])
end

def editor
editor = ENV['VISUAL']
editor ||= ENV['EDITOR']
editor ||= system_editor
end

def system_editor
if RbConfig::CONFIG['host_os'] =~ /mswin|mingw/
'notepad'
else
'vi'
end
end
end
end
end
96 changes: 96 additions & 0 deletions spec/editor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
# encoding: utf-8
require 'helper'

describe T::Editor do

context "when generating a tempfile" do
it "appends with default filler text" do
expect(T::Editor.tempfile.read).to eq("\n# Enter your tweet above.")
end

it "starts with specified filler text" do
expect(T::Editor.tempfile("# Oh yeah.").read).to eq("# Oh yeah.")
end
end

context "when editing a file" do
before(:all) do
T::Editor.stub(:edit) do |path|
File.open(path, "wb") do |f|
f.write("A tweet!!!!")
end
end
end

it "fetches your tweet content without comments" do
expect(T::Editor.gets(:update)).to eq("A tweet!!!!")
end
end

context "when fetching the editor to write in" do
context "no $VISUAL or $EDITOR set" do
before(:all) do
ENV["EDITOR"] = ENV["VISUAL"] = nil
RbConfig::CONFIG['host_os'] = "darwin12.2.0"
end

it "returns the system editor" do
expect(T::Editor.editor).to eq("vi")
end
end

context "$VISUAL is set" do
before(:all) do
ENV["EDITOR"] = nil
ENV["VISUAL"] = "/my/vim/install"
end

it "returns the system editor" do
expect(T::Editor.editor).to eq("/my/vim/install")
end
end

context "$EDITOR is set" do
before(:all) do
ENV["EDITOR"] = "/usr/bin/subl"
ENV["VISUAL"] = nil
end

it "returns the system editor" do
expect(T::Editor.editor).to eq("/usr/bin/subl")
end
end

context "$VISUAL and $EDITOR are set" do
before(:all) do
ENV["EDITOR"] = "/my/vastly/superior/editor"
ENV["VISUAL"] = "/usr/bin/emacs"
end

it "returns the system editor" do
expect(T::Editor.editor).to eq("/usr/bin/emacs")
end
end
end

context "when fetching system editor" do
context "on a mac" do
before(:all) do
RbConfig::CONFIG['host_os'] = "darwin12.2.0"
end
it "returns 'vi' on a unix machine" do
expect(T::Editor.system_editor).to eq("vi")
end
end

context "on a Windows POC" do
before(:all) do
RbConfig::CONFIG['host_os'] = "mswin"
end
it "returns 'notepad' on a windows box" do
expect(T::Editor.system_editor).to eq("notepad")
end
end
end

end