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

#italic(text) ⇒ Object



27
28
29
# File 'lib/panda_pal/helpers/console_helpers.rb', line 27

def italic(text)
  "\033[3m#{text}\033[0m"
end

#open_editor(file_path) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/panda_pal/helpers/console_helpers.rb', line 87

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/panda_pal/helpers/console_helpers.rb', line 95

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



31
32
33
34
# File 'lib/panda_pal/helpers/console_helpers.rb', line 31

def pandapalrc
  # TODO Consider searching app and parent dirs before ~/
  @pandapalrc ||= YAML.load(File.read(File.expand_path("~/pandapalrc.yml"))) rescue {}
end

#pause(prompt = "Press Enter to continue") ⇒ Object



80
81
82
83
84
85
# File 'lib/panda_pal/helpers/console_helpers.rb', line 80

def pause(prompt = "Press Enter to continue")
  styled_prompt = "\033[3;90m#{prompt}\033[0m"  # grey and italic
  puts styled_prompt
  STDIN.gets
  print "\033[A\033[2K\033[A\033[2K"  # move up and clear both lines
end

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



36
37
38
39
40
41
42
43
# File 'lib/panda_pal/helpers/console_helpers.rb', line 36

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/panda_pal/helpers/console_helpers.rb', line 45

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



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/panda_pal/helpers/console_helpers.rb', line 68

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



61
62
63
64
65
66
# File 'lib/panda_pal/helpers/console_helpers.rb', line 61

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