module PandaPal::ConsoleHelpers
Constants
- COLORS
Public Instance Methods
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 76 def open_editor(file_path) raise "EDITOR environment variable not set" unless ENV["EDITOR"].present? args = Shellwords.shellwords(ENV['EDITOR']) args << file_path Kernel::system(*args) end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 84 def open_string_editor(string, file: nil, name: nil, require_save: true) file_obj = file.present? ? File.new(file) : Tempfile.new([File.basename(name, File.extname(name)), File.extname(name)]) File.open(file_obj.path, 'w') { |f| f.write(string) } mtime = File.stat(file_obj.path).mtime path = file_obj.path file_obj.close rescue nil open_editor(path) return :aborted unless !require_save || mtime < File.stat(file_obj.path).mtime File.read(path) end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 27 def pandapalrc # TODO Consider searching app and parent dirs before ~/ @pandapalrc ||= YAML.load(File.read(File.expand_path("~/pandapalrc.yml"))) rescue {} end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 32 def prompt(prompt = "", default: nil) prompt = prompt + " (#{default})" if default.present? puts prompt print "> " v = STDIN.gets.chomp.downcase return default if v == "" v end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 41 def prompt_options(options, prompt = "", default: nil) options = options.map(&:to_s) prompt = prompt + " (" + options.map { |o| o == default ? o.capitalize : o }.join("/") + ")" i = 0 loop do puts prompt print "> " i += 1 v = STDIN.gets.chomp.downcase return v if options.include?(v) return default if v == "" return nil if i > 3 puts "Invalid Input." end end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 64 def prompt_pry_retry(prompt = "Retry?", default: false) default = default ? "y" : "n" unless default.is_a?(String) result = prompt_options(["y", "n", "pry"], prompt, default: default ? "y" : "n") if result == "pry" binding.pry return true end return true if result == "y" return false if result == "n" result end
Source
# File lib/panda_pal/helpers/console_helpers.rb, line 57 def prompt_yes_no(prompt = "", default: true) result = prompt_options(["y", "n"], prompt, default: default ? "y" : "n") return true if result == "y" return false if result == "n" result end