如何将我的Pry提示设置为当前时间戳?

时间:2014-02-28 01:37:57

标签: debugging configuration pry pry-rails

当我将以下行放入~/.pryrc时,它无法正常工作:

Pry.config.prompt_name = Time.now.to_s

每个提示都等于Pry发布的时间。

每次显示提示时(每次通话后),如何使用当前时间戳更新提示?

2 个答案:

答案 0 :(得分:1)

您需要使用prompt而不是prompt_name

Pry.config.prompt = Proc.new { |output, value| Time.now.to_s[0..-6] } 

答案 1 :(得分:1)

对于任何觉得有用的人,我都通过以下方式将时间戳记添加到.pryrc

def rails_prompt
  # Maybe this is only running as `pry` an not `rails console`, so check first
  return '' unless defined? Rails

  app_env =
    if Rails.env.production?
      puts "\n\e[1m\e[41mWARNING: YOU ARE USING RAILS CONSOLE IN PRODUCTION!\n" \
            "Changing data can cause serious data loss.\n" \
            "Make sure you know what you're doing.\e[0m\e[22m\n\n"
      "\e[31m#{Rails.env[0...4]}\e[0m" # red
    else
      "\e[32m#{Rails.env[0...4]}\e[0m" # green
    end
  "(\e[1m#{app_env}\e[22m)" # bold
end

now = proc { Time.new.strftime('%T%Z') } # or the time format you need it

# For older pry versions
def_proc = proc { |target_self, nest_level, pry|
  "[#{pry.input_array.size}][#{now.call}] "\
  "(#{Pry.view_clip(target_self)})"\
  "#{":#{nest_level}" unless nest_level.zero?}#{rails_prompt}"
}
Pry.config.prompt = [
  proc { |t, n, p| "#{def_proc.call(t, n, p)}> " },
  proc { |t, n, p| "#{def_proc.call(t, n, p)}* " }
]

# For newer versions
Pry::Prompt.add(:default_with_time, 'The same default, but with timestamp') do
  |context, nesting, pry_instance, sep|
  format(
    '[%<in_count>s][%<timestamp>s] %<name>s(%<context>s)%<rails_prompt>s%<nesting>s%<separator>s ',
    in_count: pry_instance.input_ring.count,
    timestamp: now.call,
    name: pry_instance.config.prompt_name,
    context: Pry.view_clip(context),
    rails_prompt: rails_prompt,
    nesting: (nesting.positive? ? ":#{nesting}" : ''),
    separator: sep
  )
end
Pry.config.prompt = Pry::Prompt[:default_with_time][:value]

然后您将收到如下提示: enter image description here