ppl 1.5.1 → 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,10 +3,12 @@ class Ppl::Application::Input
3
3
 
4
4
  attr_accessor :arguments
5
5
  attr_accessor :options
6
+ attr_accessor :stdin
6
7
 
7
8
  def initialize(arguments=[])
8
9
  @arguments = arguments
9
10
  @options = {}
11
+ @stdin = $stdin
10
12
  end
11
13
 
12
14
  end
@@ -16,20 +16,54 @@ class Ppl::Command::Shell < Ppl::Application::Command
16
16
 
17
17
  def execute(input, output)
18
18
  begin
19
- while line = Readline.readline("ppl> ", true)
20
- break if line == "exit"
21
- break if line == false
22
- Kernel.system "#{$0} #{line}"
23
- end
19
+ shell(input, output)
24
20
  rescue SystemExit, Interrupt
25
- output.line("")
26
- return false
21
+ terminate_gracefully(input, output)
22
+ false
23
+ end
24
+ end
25
+
26
+
27
+ private
28
+
29
+ def shell(input, output)
30
+ welcome_user(input, output)
31
+ while line = read_line(input)
32
+ break if ["exit", false].include?(line)
33
+ process_line(line)
34
+ end
35
+ terminate_gracefully(input, output)
36
+ true
37
+ end
38
+
39
+ def welcome_user(input, output)
40
+ if input.stdin.tty?
41
+ output.line("ppl #{Ppl::Version} (type \"exit\" to leave)")
27
42
  end
43
+ end
44
+
45
+ def read_line(input)
46
+ prompt = determine_prompt(input.stdin)
47
+ line = Readline.readline(prompt, true)
48
+ end
49
+
50
+ def process_line(line)
51
+ command = "#{$0} #{line}"
52
+ Kernel.system(command)
53
+ end
54
+
55
+ def determine_prompt(io)
56
+ if io.tty?
57
+ "ppl> "
58
+ else
59
+ ""
60
+ end
61
+ end
28
62
 
29
- if $stdin.tty?
63
+ def terminate_gracefully(input, output)
64
+ if input.stdin.tty?
30
65
  output.line("")
31
66
  end
32
- return true
33
67
  end
34
68
 
35
69
  end
data/lib/ppl.rb CHANGED
@@ -1,7 +1,7 @@
1
1
 
2
2
  module Ppl
3
3
 
4
- Version = "1.5.1"
4
+ Version = "1.5.2"
5
5
 
6
6
  module Adapter
7
7
  end
data/ppl.gemspec CHANGED
@@ -2,8 +2,8 @@
2
2
  Gem::Specification.new do |spec|
3
3
 
4
4
  spec.name = "ppl"
5
- spec.version = "1.5.1"
6
- spec.date = "2012-12-20"
5
+ spec.version = "1.5.2"
6
+ spec.date = "2012-12-23"
7
7
 
8
8
  spec.required_ruby_version = ">= 1.9.3"
9
9
 
@@ -14,18 +14,34 @@ describe Ppl::Command::Shell do
14
14
  end
15
15
 
16
16
  describe "#execute" do
17
- before(:each) do
18
- @output.should_receive(:line)
19
- end
20
17
  it "should read a line of input from stdin" do
21
18
  Readline.should_receive(:readline)
19
+ @command.stub(:welcome_user)
20
+ @command.stub(:terminate_gracefully)
22
21
  @command.execute(@input, @output).should eq true
23
22
  end
24
23
  end
25
24
 
26
25
  describe "#execute" do
26
+
27
27
  before(:each) do
28
- @output.should_receive(:line)
28
+ @input.stdin = double(IO)
29
+ @input.stdin.stub(:tty?) { true }
30
+ end
31
+
32
+ it "should not show a prompt if stdin isn't a tty" do
33
+ @input.stdin.stub(:tty?) { false }
34
+ Readline.should_receive(:readline).with("", true)
35
+ @command.stub(:welcome_user)
36
+ @command.execute(@input, @output)
37
+ end
38
+
39
+ it "should show a prompt if stdin is a tty" do
40
+ @input.stdin.stub(:tty?) { true }
41
+ Readline.should_receive(:readline).with("ppl> ", true)
42
+ @command.stub(:welcome_user)
43
+ @command.stub(:terminate_gracefully)
44
+ @command.execute(@input, @output)
29
45
  end
30
46
 
31
47
  it "should make a system call with the input from stdin" do
@@ -34,14 +50,52 @@ describe Ppl::Command::Shell do
34
50
  Kernel.should_receive(:system) do |command|
35
51
  command.should include "email fred"
36
52
  end
53
+ @command.stub(:welcome_user)
54
+ @command.stub(:terminate_gracefully)
37
55
  @command.execute(@input, @output).should eq true
38
56
  end
39
57
 
40
58
  it "should exit on ctrl+c" do
41
59
  Readline.should_receive(:readline).and_raise(Interrupt)
60
+ @command.stub(:welcome_user)
61
+ @command.stub(:terminate_gracefully)
42
62
  @command.execute(@input, @output).should eq false
43
63
  end
44
64
 
65
+ it "should print a final newline on EOF so the user's prompt looks nice" do
66
+ @input.stdin.stub(:tty?) { true }
67
+ @output.should_receive(:line).with("")
68
+ Readline.should_receive(:readline).and_return(false)
69
+ @command.stub(:welcome_user)
70
+ @command.execute(@input, @output)
71
+ end
72
+
73
+ it "should print a final newline on error so the user's prompt looks nice" do
74
+ @input.stdin.stub(:tty?) { true }
75
+ @output.should_receive(:line).with("")
76
+ @command.stub(:shell).and_raise(Interrupt)
77
+ @command.execute(@input, @output)
78
+ end
79
+
80
+ it "should not print a final newline on EOF if stdin isn't a tty" do
81
+ @input.stdin.stub(:tty?) { false }
82
+ @output.should_not_receive(:line).with("")
83
+ Readline.should_receive(:readline).and_return(false)
84
+ @command.stub(:welcome_user)
85
+ @command.execute(@input, @output)
86
+ end
87
+
88
+ it "should print a welcome message on the tty" do
89
+ @input.stdin.stub(:tty?) { true }
90
+ @output.should_receive(:line) do |line|
91
+ line.should include "ppl"
92
+ line.should include Ppl::Version
93
+ end
94
+ Readline.should_receive(:readline).and_return(false)
95
+ @command.stub(:terminate_gracefully)
96
+ @command.execute(@input, @output)
97
+ end
98
+
45
99
  end
46
100
 
47
101
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ppl
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.5.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-20 00:00:00.000000000 Z
12
+ date: 2012-12-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: inifile
16
- requirement: &14299780 !ruby/object:Gem::Requirement
16
+ requirement: &16217320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - =
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.0.2
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *14299780
24
+ version_requirements: *16217320
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rugged
27
- requirement: &14298340 !ruby/object:Gem::Requirement
27
+ requirement: &16216640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - =
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 0.17.0.b6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *14298340
35
+ version_requirements: *16216640
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: vpim
38
- requirement: &14297560 !ruby/object:Gem::Requirement
38
+ requirement: &16215860 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - =
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: '0.695'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *14297560
46
+ version_requirements: *16215860
47
47
  description: CLI Address Book
48
48
  email: henry@henrysmith.org
49
49
  executables: