Module: PandaPal::ConsoleHelpers

Extended by:
ConsoleHelpers
Included in:
ConsoleHelpers
Defined in:
lib/panda_pal/helpers/console_helpers.rb

Defined Under Namespace

Classes: CodeBuilder

Constant Summary collapse

COLORS =
{
  "black" => 0,
  "red" => 1,
  "green" => 2,
  "yellow" => 3,
  "blue" => 4,
  "purple" => 5,
  "magenta" => 5,
  "cyan" => 6,
  "white" => 7
}.freeze

Instance Method Summary collapse

Instance Method Details

#open_editor(file_path) ⇒ Object



76
77
78
79
80
81
82
# 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

#open_string_editor(string, file: nil, name: nil, require_save: true) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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

#pandapalrcObject



27
28
29
30
# 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

#prompt(prompt = "", default: nil) ⇒ Object



32
33
34
35
36
37
38
39
# 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

#prompt_options(options, prompt = "", default: nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#prompt_pry_retry(prompt = "Retry?", default: false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# 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

#prompt_yes_no(prompt = "", default: true) ⇒ Object



57
58
59
60
61
62
# 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