class Bundler::Thor::Shell::Basic
属性
公共实例方法
向用户提问并接收响应。
如果指定了默认值,它将呈现给用户,并允许他们使用空响应选择该值。当提供有限的答案时,此选项将被忽略。
如果要求限制正确的响应,则可以传入一个可接受的答案数组。如果未提供其中一个答案,则会向他们显示一条消息,说明必须提供其中一个答案,并重新询问该问题。
如果要求提供敏感信息,则可以将 :echo 选项设置为 false,以屏蔽来自 $stdin 的用户输入。
如果所需的输入是路径,则将 path 选项设置为 true。这将启用对支持 Readline 的系统上的相对于当前工作目录的文件路径的制表符补全。
示例¶ ↑
ask("What is your name?") ask("What is the planet furthest from the sun?", :default => "Neptune") ask("What is your favorite Neopolitan flavor?", :limited_to => ["strawberry", "chocolate", "vanilla"]) ask("What is your password?", :echo => false) ask("Where should the file be saved?", :path => true)
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 80 def ask(statement, *args) options = args.last.is_a?(Hash) ? args.pop : {} color = args.first if options[:limited_to] ask_filtered(statement, color, options) else ask_simply(statement, color, options) end end
如果在执行过程中出现错误,则调用此方法。这由 Bundler::Thor 内部使用,不应在脚本内部使用。如果出现错误,您始终可以引发异常。如果您引发 Bundler::Thor::Error
,它将被捕获并包装在下面的方法中。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 251 def error(statement) stderr.puts statement end
处理文件冲突,如果应该覆盖文件则返回 true,否则返回 false。如果给出了块,则使用该块的响应作为差异的内容。
参数¶ ↑
- destination<String>
-
解决冲突的目标文件
- block<Proc>
-
一个可选的块,返回用于差异和合并的值
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 207 def file_collision(destination) return true if @always_force options = block_given? ? "[Ynaqdhm]" : "[Ynaqh]" loop do answer = ask( %[Overwrite #{destination}? (enter "h" for help) #{options}], add_to_history: false ) case answer when nil say "" return true when is?(:yes), is?(:force), "" return true when is?(:no), is?(:skip) return false when is?(:always) return @always_force = true when is?(:quit) say "Aborting..." raise SystemExit when is?(:diff) show_diff(destination, yield) if block_given? say "Retrying..." when is?(:merge) if block_given? && !merge_tool.empty? merge(destination, yield) return nil end say "Please specify merge tool to `THOR_MERGE` env." else say file_collision_help(block_given?) end end end
在执行块时设置输出填充,并将其重置。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 43 def indent(count = 1) orig_padding = padding self.padding = padding + count yield self.padding = orig_padding end
静音给定块内的所有内容
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 22 def mute @mute = true yield ensure @mute = false end
检查 base 是否被静音
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 31 def mute? @mute end
向用户提问,如果用户回答 “n” 或 “no”,则返回 true。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 156 def no?(statement, color = nil) !!(ask(statement, color, add_to_history: false) =~ is?(:no)) end
设置输出填充,不允许小于零的值。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 37 def padding=(value) @padding = [0, value].max end
打印表格。
参数¶ ↑
Array[Array[String, String, …]]
选项¶ ↑
- indent<Integer>
-
按 indent 值缩进第一列。
- colwidth<Integer>
-
强制第一列为 colwidth 个空格宽。
- borders<Boolean>
-
添加 ascii 边框。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 180 def print_table(array, options = {}) # rubocop:disable Metrics/MethodLength printer = TablePrinter.new(stdout, options) printer.print(array) end
告诉(打印)用户一些信息。如果句子以空格或制表符结尾,则不附加新行(print + flush)。否则直接传递给 puts(行为来自 Highline)。
示例¶ ↑
say("I know you knew that.")
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 98 def say(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) return if quiet? buffer = prepare_message(message, *color) buffer << "\n" if force_new_line && !message.to_s.end_with?("\n") stdout.print(buffer) stdout.flush end
告诉(打印)用户一个错误。如果句子以空格或制表符结尾,则不附加新行(print + flush)。否则直接传递给 puts(行为来自 Highline)。
示例¶ ↑
say_error("error: something went wrong")
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 115 def say_error(message = "", color = nil, force_new_line = (message.to_s !~ /( |\t)\Z/)) return if quiet? buffer = prepare_message(message, *color) buffer << "\n" if force_new_line && !message.to_s.end_with?("\n") stderr.print(buffer) stderr.flush end
使用给定的颜色说出状态并附加消息。由于此方法经常被操作使用,因此允许在 log_status 中提供 nil 或 false,避免显示消息。如果在 log_status 中给出了 Symbol,则将其用作颜色。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 130 def say_status(status, message, log_status = true) return if quiet? || log_status == false spaces = " " * (padding + 1) status = status.to_s.rjust(12) margin = " " * status.length + spaces color = log_status.is_a?(Symbol) ? log_status : :green status = set_color status, color, true if color message = message.to_s.chomp.gsub(/(?<!\A)^/, margin) buffer = "#{status}#{spaces}#{message}\n" stdout.print(buffer) stdout.flush end
向用户提问,如果用户回答 “y” 或 “yes”,则返回 true。
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 149 def yes?(statement, color = nil) !!(ask(statement, color, add_to_history: false) =~ is?(:yes)) end
受保护的实例方法
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 362 def answer_match(possibilities, answer, case_insensitive) if case_insensitive possibilities.detect{ |possibility| possibility.downcase == answer.downcase } else possibilities.detect{ |possibility| possibility == answer } end end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 349 def ask_filtered(statement, color, options) answer_set = options[:limited_to] case_insensitive = options.fetch(:case_insensitive, false) correct_answer = nil until correct_answer answers = answer_set.join(", ") answer = ask_simply("#{statement} [#{answers}]", color, options) correct_answer = answer_match(answer_set, answer, case_insensitive) say("Your response must be one of: [#{answers}]. Please try again.") unless correct_answer end correct_answer end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 332 def ask_simply(statement, color, options) default = options[:default] message = [statement, ("(#{default})" if default), nil].uniq.join(" ") message = prepare_message(message, *color) result = Bundler::Thor::LineEditor.readline(message, options) return unless result result = result.strip if default && result == "" default else result end end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 269 def can_display_colors? false end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 273 def lookup_color(color) return color unless color.is_a?(Symbol) self.class.const_get(color.to_s.upcase) end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 264 def prepare_message(message, *color) spaces = " " * padding spaces + set_color(message.to_s, *color) end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 282 def stderr $stderr end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 278 def stdout $stdout end
# File bundler/vendor/thor/lib/thor/shell/basic.rb, line 328 def unix? Terminal.unix? end