当我将以下行放入~/.pryrc
时,它无法正常工作:
Pry.config.prompt_name = Time.now.to_s
每个提示都等于Pry发布的时间。
每次显示提示时(每次通话后),如何使用当前时间戳更新提示?
答案 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]