josevalim-thor 0.10.22 → 0.10.23

Sign up to get free protection for your applications and to get access to all the features.
data/lib/thor.rb CHANGED
@@ -127,24 +127,21 @@ class Thor
127
127
  # script.invoke(:task, first_arg, second_arg, third_arg)
128
128
  #
129
129
  def start(given_args=ARGV, config={})
130
- config[:shell] ||= Thor::Base.shell.new
130
+ super do
131
+ meth = normalize_task_name(given_args.shift)
132
+ task = all_tasks[meth]
131
133
 
132
- meth = normalize_task_name(given_args.shift)
133
- task = all_tasks[meth]
134
+ if task
135
+ args, opts = Thor::Options.split(given_args)
136
+ config.merge!(:task_options => task.options)
137
+ else
138
+ args, opts = given_args, {}
139
+ end
134
140
 
135
- if task
136
- args, opts = Thor::Options.split(given_args)
137
- config.merge!(:task_options => task.options)
138
- else
139
- args, opts = given_args, {}
141
+ task ||= Task.dynamic(meth)
142
+ trailing = args[Range.new(arguments.size, -1)]
143
+ new(args, opts, config).invoke(task, trailing || [])
140
144
  end
141
-
142
- task ||= Task.dynamic(meth)
143
- trailing = args[Range.new(arguments.size, -1)]
144
-
145
- new(args, opts, config).invoke(task, trailing || [])
146
- rescue Thor::Error => e
147
- config[:shell].error e.message
148
145
  end
149
146
 
150
147
  # Prints help information. If a task name is given, it shows information
data/lib/thor/base.rb CHANGED
@@ -341,6 +341,19 @@ class Thor
341
341
  end
342
342
  end
343
343
 
344
+ # Default way to start generators from the command line.
345
+ #
346
+ def start(given_args=ARGV, config={}) #:nodoc:
347
+ config[:shell] ||= Thor::Base.shell.new
348
+ yield
349
+ rescue Thor::Error => e
350
+ if given_args.include?("--debug")
351
+ raise e
352
+ else
353
+ config[:shell].error e.message
354
+ end
355
+ end
356
+
344
357
  protected
345
358
 
346
359
  # Prints the class options per group. If an option does not belong to
data/lib/thor/group.rb CHANGED
@@ -22,17 +22,15 @@ class Thor::Group
22
22
  # inside the class.
23
23
  #
24
24
  def start(given_args=ARGV, config={})
25
- config[:shell] ||= Thor::Base.shell.new
25
+ super do
26
+ if Thor::HELP_MAPPINGS.include?(given_args.first)
27
+ help(config[:shell])
28
+ return
29
+ end
26
30
 
27
- if Thor::HELP_MAPPINGS.include?(given_args.first)
28
- help(config[:shell])
29
- return
31
+ args, opts = Thor::Options.split(given_args)
32
+ new(args, opts, config).invoke
30
33
  end
31
-
32
- args, opts = Thor::Options.split(given_args)
33
- new(args, opts, config).invoke
34
- rescue Thor::Error => e
35
- config[:shell].error e.message
36
34
  end
37
35
 
38
36
  # Prints help information.
data/lib/thor/shell.rb CHANGED
@@ -1,11 +1,11 @@
1
- require 'thor/shell/basic'
1
+ require 'thor/shell/color'
2
2
 
3
3
  class Thor
4
4
  module Base
5
- # Returns the shell used in all Thor classes.
5
+ # Returns the shell used in all Thor classes. Default to color one.
6
6
  #
7
7
  def self.shell
8
- @shell || Thor::Shell::Basic
8
+ @shell ||= Thor::Shell::Color
9
9
  end
10
10
 
11
11
  # Sets the shell used in all Thor classes.
@@ -3,7 +3,19 @@ require 'tempfile'
3
3
  class Thor
4
4
  module Shell
5
5
  class Basic
6
- attr_accessor :base
6
+ attr_accessor :base, :padding
7
+
8
+ # Initialize base and padding to nil.
9
+ #
10
+ def initialize #:nodoc:
11
+ @base, @padding = nil, 0
12
+ end
13
+
14
+ # Do not allow padding to be less than zero.
15
+ #
16
+ def padding=(value) #:nodoc:
17
+ @padding = [0, value].max
18
+ end
7
19
 
8
20
  # Ask something to the user and receives a response.
9
21
  #
@@ -22,14 +34,16 @@ class Thor
22
34
  # ==== Example
23
35
  # say("I know you knew that.")
24
36
  #
25
- def say(statement="", color=nil, force_new_line=false)
26
- statement = statement.to_s
37
+ def say(message="", color=nil, force_new_line=false)
38
+ message = message.to_s
39
+ new_line = force_new_line || !(message[-1, 1] == " " || message[-1, 1] == "\t")
40
+ message = set_color(message, color) if color
27
41
 
28
- if !force_new_line && (statement[-1, 1] == " " || statement[-1, 1] == "\t")
29
- $stdout.print(statement)
30
- $stdout.flush
42
+ if new_line
43
+ $stdout.puts(message)
31
44
  else
32
- $stdout.puts(statement)
45
+ $stdout.print(message)
46
+ $stdout.flush
33
47
  end
34
48
  end
35
49
 
@@ -38,11 +52,14 @@ class Thor
38
52
  # in log_status, avoiding the message from being shown. If a Symbol is
39
53
  # given in log_status, it's used as the color.
40
54
  #
41
- def say_status(status, message, log_status=true)
42
- return if base && base.options[:quiet]
43
-
44
- color = log_status.is_a?(Symbol) ? log_status : :green
45
- say "#{status.to_s.rjust(12)} #{message}", color, true if log_status
55
+ def say_status(status, message, log_status=true) #:nodoc:
56
+ return if quiet? || log_status == false
57
+ spaces = " " * (padding + 1)
58
+ color = log_status.is_a?(Symbol) ? log_status : :green
59
+
60
+ status = status.to_s.rjust(12)
61
+ status = set_color status, color, true if color
62
+ say "#{status}#{spaces}#{message}", nil, true
46
63
  end
47
64
 
48
65
  # Make a question the to user and returns true if the user replies "y" or
@@ -160,6 +177,10 @@ class Thor
160
177
 
161
178
  protected
162
179
 
180
+ def set_color(string, color, bold=false)
181
+ string
182
+ end
183
+
163
184
  def is?(value)
164
185
  value = value.to_s
165
186
 
@@ -182,6 +203,8 @@ HELP
182
203
  end
183
204
 
184
205
  def show_diff(destination, content)
206
+ diff_cmd = ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
207
+
185
208
  Tempfile.open(File.basename(destination), File.dirname(destination)) do |temp|
186
209
  temp.write content
187
210
  temp.rewind
@@ -189,8 +212,8 @@ HELP
189
212
  end
190
213
  end
191
214
 
192
- def diff_cmd
193
- ENV['THOR_DIFF'] || ENV['RAILS_DIFF'] || 'diff -u'
215
+ def quiet?
216
+ base && base.options[:quiet]
194
217
  end
195
218
 
196
219
  end
@@ -0,0 +1,106 @@
1
+ require 'thor/shell/basic'
2
+
3
+ class Thor
4
+ module Shell
5
+ # Set color in the output. Got color values from HighLine.
6
+ #
7
+ class Color < Basic
8
+ # Embed in a String to clear all previous ANSI sequences.
9
+ CLEAR = "\e[0m"
10
+ # The start of an ANSI bold sequence.
11
+ BOLD = "\e[1m"
12
+
13
+ # Set the terminal's foreground ANSI color to black.
14
+ BLACK = "\e[30m"
15
+ # Set the terminal's foreground ANSI color to red.
16
+ RED = "\e[31m"
17
+ # Set the terminal's foreground ANSI color to green.
18
+ GREEN = "\e[32m"
19
+ # Set the terminal's foreground ANSI color to yellow.
20
+ YELLOW = "\e[33m"
21
+ # Set the terminal's foreground ANSI color to blue.
22
+ BLUE = "\e[34m"
23
+ # Set the terminal's foreground ANSI color to magenta.
24
+ MAGENTA = "\e[35m"
25
+ # Set the terminal's foreground ANSI color to cyan.
26
+ CYAN = "\e[36m"
27
+ # Set the terminal's foreground ANSI color to white.
28
+ WHITE = "\e[37m"
29
+
30
+ # Set the terminal's background ANSI color to black.
31
+ ON_BLACK = "\e[40m"
32
+ # Set the terminal's background ANSI color to red.
33
+ ON_RED = "\e[41m"
34
+ # Set the terminal's background ANSI color to green.
35
+ ON_GREEN = "\e[42m"
36
+ # Set the terminal's background ANSI color to yellow.
37
+ ON_YELLOW = "\e[43m"
38
+ # Set the terminal's background ANSI color to blue.
39
+ ON_BLUE = "\e[44m"
40
+ # Set the terminal's background ANSI color to magenta.
41
+ ON_MAGENTA = "\e[45m"
42
+ # Set the terminal's background ANSI color to cyan.
43
+ ON_CYAN = "\e[46m"
44
+ # Set the terminal's background ANSI color to white.
45
+ ON_WHITE = "\e[47m"
46
+
47
+ protected
48
+
49
+ # Set color by using a string or one of the defined constants. Based
50
+ # on Highline implementation. CLEAR is automatically be embedded to
51
+ # the end of the returned String.
52
+ #
53
+ def set_color(string, color, bold=false)
54
+ color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
55
+ bold = bold ? BOLD : ""
56
+ "#{bold}#{color}#{string}#{CLEAR}"
57
+ end
58
+
59
+ # Overwrite show_diff to show diff with colors if Diff::LCS is
60
+ # available.
61
+ #
62
+ def show_diff(destination, content)
63
+ if diff_lcs_loaded?
64
+ actual = File.read(destination).to_s.split("\n")
65
+ content = content.to_s.split("\n")
66
+
67
+ Diff::LCS.sdiff(actual, content).each do |diff|
68
+ output_diff_line(diff)
69
+ end
70
+ else
71
+ super
72
+ end
73
+ end
74
+
75
+ def output_diff_line(diff)
76
+ case diff.action
77
+ when '-'
78
+ say "- #{diff.old_element.chomp}", :red
79
+ when '+'
80
+ say "+ #{diff.new_element.chomp}", :green
81
+ when '!'
82
+ say "- #{diff.old_element.chomp}", :red
83
+ say "+ #{diff.new_element.chomp}", :green
84
+ else
85
+ say " #{diff.old_element.chomp}"
86
+ end
87
+ end
88
+
89
+ # Check if Diff::LCS is loaded. If it is, use it to create pretty output
90
+ # for diff.
91
+ #
92
+ def diff_lcs_loaded?
93
+ return true if defined?(Diff::LCS)
94
+ return @diff_lcs_loaded unless @diff_lcs_loaded.nil?
95
+
96
+ @diff_lcs_loaded = begin
97
+ require 'diff/lcs'
98
+ true
99
+ rescue LoadError
100
+ false
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: josevalim-thor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.22
4
+ version: 0.10.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-07-01 00:00:00 -07:00
12
+ date: 2009-07-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -51,6 +51,7 @@ files:
51
51
  - lib/thor/parser.rb
52
52
  - lib/thor/shell
53
53
  - lib/thor/shell/basic.rb
54
+ - lib/thor/shell/color.rb
54
55
  - lib/thor/invocation.rb
55
56
  - lib/thor/parser
56
57
  - lib/thor/parser/argument.rb