Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

CLI#edit edits a file and returns the contents

  • Loading branch information...
commit 84a6f4d44fac0ba938d8dd045eff798e1072e1a1 1 parent 68ae61c
@txus authored
View
1  lib/springpad.rb
@@ -1,6 +1,7 @@
require_relative "springpad/version"
require_relative "springpad/api"
require_relative "springpad/blocks"
+require_relative "springpad/cli"
module Springpad
end
View
15 lib/springpad/cli.rb
@@ -0,0 +1,15 @@
+require 'tempfile'
+
+module Springpad
+ class CLI
+ # Public: Edits a file with $EDITOR and returns the contents.
+ #
+ # Returns an Array with the first line and an array of the other lines.
+ def edit
+ temp_file = Tempfile.new('block')
+ system("$EDITOR #{temp_file.path}")
+ contents = temp_file.read.chomp.split("\n").reject(&:empty?).map(&:strip)
+ [contents.first, contents[1..-1]]
+ end
+ end
+end
View
24 test/springpad/cli_test.rb
@@ -0,0 +1,24 @@
+require 'test_helper'
+
+module Springpad
+ describe CLI do
+ describe "#edit" do
+ it 'edits a file and returns the contents' do
+ cli = CLI.new
+
+ Tempfile.stubs(:new).returns file = stub_everything
+ file.stubs(:path).returns "/tmp/something"
+ file.stubs(:read).returns """Name of the task \n\n This is a description of the task\nThis is more body for the task\n"
+ cli.expects(:system).with("$EDITOR /tmp/something")
+
+ cli.edit.must_equal [
+ "Name of the task",
+ [
+ "This is a description of the task",
+ "This is more body for the task",
+ ]
+ ]
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.