# vim: ft=ruby:noml:
# frozen_string_literal: true

# useful pry features
#   usage:
#     pry> help
#     pry> ls String
#     pry> show-doc String#downcase
#   shell commands are prepended with a dot:
#     pry> .ls
#   shell mode list path in prompt:
#     pry> shell-mode
#     pry :~ $
#   save an unnamed object to an object:
#     pry> [1, 2, 3]
#     pry> x = _
#     pry> x
#     [1, 2, 3]
#   list rails models:
#     pry> show-models
#   show rails routes:
#     pry> show-routes

irbrc_path = File.expand_path('~/.config/irb/irbrc')
if File.exist?(irbrc_path)
  begin
    load irbrc_path
  rescue Exception
    warn "Could not load: #{ irbrc_path }"
  end
end

if Pry::Prompt[:rails]
  Pry.config.prompt = Pry::Prompt[:rails]
end

if ENV.include?('XDG_DATA_HOME')
  Pry.config.history.file = "#{ENV['XDG_DATA_HOME']}/pry/history"
else
  Pry.config.history.file = "~/.local/share/pry/history"
end

Pry::Prompt.add(
  :colored, 'Colored version of :default.'
) do |context, nesting, pry_instance, sep|
  text = format(
    "%<name>s(%<context>s)%<nesting>s%<separator>s",
    name: pry_instance.config.prompt_name,
    context: Pry.view_clip(context),
    nesting: (nesting > 0 ? ":#{nesting}" : ''),
    separator: sep
  )
  if pry_instance.config.color
    text = Pry::Helpers::Text.magenta(text)
  end
  "[#{pry_instance.input_ring.count}] #{text} "
end

Pry.config.prompt = Pry::Prompt[:colored]

if defined?(PryDebugger)
  Pry.commands.alias_command 'c', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'f', 'finish'
end

Pry.commands.block_command(/!(\d+)/, "Replay a line of history", :listing => "!hist") do |line|
  run "history --replay #{line}"
end

Pry.commands.block_command /s\/(.*?)\/(.*?)/ do |source, dest|
  eval_string.gsub!(/#{source}/) { dest }
  run "show-input"
end

# require "pry-doc"
# require "pry-byebug"
# require "amazing_print"
# require "hirb"
#
# # enable Hirb for nicer object display, especially table-based objects
# Hirb.enable
# old_print = Pry.config.print
# Pry.config.print = proc do |*args|
#   Hirb::View.view_or_page_output(args[1]) || old_print.call(*args)
# end
#
# # change prompt color from gray to blue:green
# Pry.config.prompt = Pry::Prompt.new(
#   "Pry",
#   "Rails Console",
#   [
#     proc { |context, nesting, pry_instance|
#       project_name = defined?(Rails) ? Rails.application.class.module_parent.name.underscore.dasherize : "ruby"
#       "[#{pry_instance.input_ring.size}]\001\e[0;34m\002#{project_name}\001\e[0m\002(\001\e[1;32m\002#{Pry.view_clip(context)}\001\e[0m\002)#{":#{nesting}" unless nesting.zero?}> "
#     }
#   ]
# )
#
# # change string color from ugly red to nicer violet
# # full list of default colors used by pry:
# #   see: https://github.com/rubychan/coderay/blob/master/lib/coderay/encoders/terminal.rb
# CodeRay.scan("example", :ruby).term
#
# TERM_TOKEN_COLORS = {
#   string: {
#     self: "\e[35m",
#     modifier: "\e[1;35m",
#     char: "\e[1;35m",
#     delimiter: "\e[1;35m",
#     escape: "\e[1;34m"
#   }
# }
#
# module CodeRay
#   module Encoders
#     class Terminal < Encoder
#       # override old colors
#       TERM_TOKEN_COLORS.each_pair do |key, value|
#         TOKEN_COLORS[key] = value
#       end
#     end
#   end
# end
#
# # setup aliases
# Pry::Commands.alias_command "c", "continue"
# Pry::Commands.alias_command "f", "finish"
# Pry::Commands.alias_command "n", "next"
# Pry::Commands.alias_command "s", "step"
# Pry::Commands.alias_command "src", "show-source"
# Pry::Commands.alias_command "doc", "show-doc"
