class DEBUGGER__::Console

def readline_setup prompt

def readline_setup prompt
  load_history_if_not_loaded
  commands = DEBUGGER__.commands
  prev_completion_proc = Reline.completion_proc
  prev_output_modifier_proc = Reline.output_modifier_proc
  prev_prompt_proc = Reline.prompt_proc
  # prompt state
  state = nil # :command, :ruby, nil (unknown)
  Reline.prompt_proc = -> args, *kw do
    case state = parse_input(args.first, commands)
    when nil, :command
      [prompt, prompt]
    when :ruby
      [prompt.sub('rdbg'){colorize('ruby', [:RED])}] * 2
    end
  end
  Reline.completion_proc = -> given do
    buff = Reline.line_buffer
    Reline.completion_append_character= ' '
    if /\s/ =~ buff # second parameters
      given = File.expand_path(given + 'a').sub(/a\z/, '')
      files = Dir.glob(given + '*')
      if files.size == 1 && File.directory?(files.first)
        Reline.completion_append_character= '/'
      end
      files
    else
      commands.keys.grep(/\A#{Regexp.escape(given)}/)
    end
  end
  Reline.output_modifier_proc = -> buff, **kw do
    c, rest = get_command buff
    case state
    when :command
      cmd = colorize(c, [:CYAN, :UNDERLINE])
      if commands[c] == c
        rprompt = colorize("    # command", [:DIM])
      else
        rprompt = colorize("    # #{commands[c]} command", [:DIM])
      end
      rest = rest ? colorize_code(rest) : ''
      cmd + rest + rprompt
    when nil
      buff
    when :ruby
      colorize_code(buff.chomp)
    end
  end unless CONFIG[:no_hint]
  yield
ensure
  Reline.completion_proc = prev_completion_proc
  Reline.output_modifier_proc = prev_output_modifier_proc
  Reline.prompt_proc = prev_prompt_proc
end