diff --git a/lib/springpad.rb b/lib/springpad.rb index 37951f4..5826fc9 100644 --- a/lib/springpad.rb +++ b/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 diff --git a/lib/springpad/cli.rb b/lib/springpad/cli.rb new file mode 100644 index 0000000..a9b5c34 --- /dev/null +++ b/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 diff --git a/test/springpad/cli_test.rb b/test/springpad/cli_test.rb new file mode 100644 index 0000000..cbc697b --- /dev/null +++ b/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