mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
prompt: make it possible to set name dynamically
Fixes #1738 (Possible to make prompt_name dynamic?) The user-facing API is the following: ```rb Pry.config.prompt_name = Pry.lazy { rand(100) } [1] 80(main)> [2] 87(main)> [3] 30(main)> ```
This commit is contained in:
parent
cb837419ff
commit
ace907b1b1
4 changed files with 71 additions and 4 deletions
|
@ -1,5 +1,31 @@
|
||||||
class Pry
|
class Pry
|
||||||
|
# The Pry config.
|
||||||
|
# @api public
|
||||||
class Config < Pry::BasicObject
|
class Config < Pry::BasicObject
|
||||||
|
# Wraps a block so it can have a name.
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# proc1 = proc {}
|
||||||
|
# proc2 = Pry::Config::Lazy.new(&proc {})
|
||||||
|
#
|
||||||
|
# proc1.is_a?(Pry::Config::Lazy)
|
||||||
|
# #=> false
|
||||||
|
# proc2.is_a?(Pry::Config::Lazy)
|
||||||
|
# #=> true
|
||||||
|
#
|
||||||
|
# @api private
|
||||||
|
# @since v0.12.0
|
||||||
|
class Lazy
|
||||||
|
def initialize(&block)
|
||||||
|
@block = block
|
||||||
|
end
|
||||||
|
|
||||||
|
# @return [Object]
|
||||||
|
def call
|
||||||
|
@block.call
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
include Behavior
|
include Behavior
|
||||||
|
|
||||||
def self.shortcuts
|
def self.shortcuts
|
||||||
|
|
|
@ -31,7 +31,7 @@ class Pry
|
||||||
format(
|
format(
|
||||||
DEFAULT_TEMPLATE,
|
DEFAULT_TEMPLATE,
|
||||||
in_count: _pry_.input_ring.count,
|
in_count: _pry_.input_ring.count,
|
||||||
name: _pry_.config.prompt_name,
|
name: prompt_name(_pry_.config.prompt_name),
|
||||||
context: Pry.view_clip(context),
|
context: Pry.view_clip(context),
|
||||||
nesting: (nesting > 0 ? ":#{nesting}" : ''),
|
nesting: (nesting > 0 ? ":#{nesting}" : ''),
|
||||||
separator: separator
|
separator: separator
|
||||||
|
@ -44,7 +44,7 @@ class Pry
|
||||||
proc do |context, _nesting, _pry_|
|
proc do |context, _nesting, _pry_|
|
||||||
format(
|
format(
|
||||||
SHELL_TEMPLATE,
|
SHELL_TEMPLATE,
|
||||||
name: _pry_.config.prompt_name,
|
name: prompt_name(_pry_.config.prompt_name),
|
||||||
context: Pry.view_clip(context),
|
context: Pry.view_clip(context),
|
||||||
pwd: Dir.pwd,
|
pwd: Dir.pwd,
|
||||||
separator: separator
|
separator: separator
|
||||||
|
@ -59,12 +59,18 @@ class Pry
|
||||||
format(
|
format(
|
||||||
NAV_TEMPLATE,
|
NAV_TEMPLATE,
|
||||||
in_count: _pry_.input_ring.count,
|
in_count: _pry_.input_ring.count,
|
||||||
name: _pry_.config.prompt_name,
|
name: prompt_name(_pry_.config.prompt_name),
|
||||||
tree: tree.join(' / '),
|
tree: tree.join(' / '),
|
||||||
stack_size: _pry_.binding_stack.size - 1
|
stack_size: _pry_.binding_stack.size - 1
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def prompt_name(name)
|
||||||
|
return name unless name.is_a?(Pry::Config::Lazy)
|
||||||
|
|
||||||
|
name.call
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# The default Pry prompt, which includes the context and nesting level.
|
# The default Pry prompt, which includes the context and nesting level.
|
||||||
|
|
|
@ -406,6 +406,24 @@ Readline version #{Readline::VERSION} detected - will not auto_resize! correctly
|
||||||
ensure
|
ensure
|
||||||
Thread.current[:pry_critical_section] -= 1
|
Thread.current[:pry_critical_section] -= 1
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Wraps a block in a named block called `Pry::Config::Lazy`. This is used for
|
||||||
|
# dynamic config values, which are calculated every time
|
||||||
|
# {Pry::Config::Lazy#call} is called.
|
||||||
|
#
|
||||||
|
# @example
|
||||||
|
# # pryrc
|
||||||
|
# Pry.config.prompt_name = Pry.lazy { rand(100) }
|
||||||
|
#
|
||||||
|
# # Session
|
||||||
|
# [1] 96(main)>
|
||||||
|
# [2] 19(main)>
|
||||||
|
# [3] 80(main)>
|
||||||
|
#
|
||||||
|
# @return [#call]
|
||||||
|
def self.lazy(&block)
|
||||||
|
Pry::Config::Lazy.new(&block)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Pry.init
|
Pry.init
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
require_relative 'helper'
|
require_relative 'helper'
|
||||||
|
|
||||||
describe "Prompts" do
|
describe Pry::Prompt do
|
||||||
describe "one-parameter prompt proc" do
|
describe "one-parameter prompt proc" do
|
||||||
it 'should get full config object' do
|
it 'should get full config object' do
|
||||||
config = nil
|
config = nil
|
||||||
|
@ -67,4 +67,21 @@ describe "Prompts" do
|
||||||
expect(p1.is_a?(Pry)).to eq true
|
expect(p1.is_a?(Pry)).to eq true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "can compute prompt name dynamically" do
|
||||||
|
config = nil
|
||||||
|
redirect_pry_io(InputTester.new("def hello", "exit-all")) do
|
||||||
|
Pry.start(self, prompt: proc { |v| config = v })
|
||||||
|
end
|
||||||
|
|
||||||
|
enum = Enumerator.new do |y|
|
||||||
|
count = 100
|
||||||
|
loop { y << count += 1 }
|
||||||
|
end
|
||||||
|
config._pry_.config.prompt_name = Pry.lazy { enum.next }
|
||||||
|
|
||||||
|
proc = subject::DEFAULT.first
|
||||||
|
expect(proc.call(Object.new, 1, config._pry_)).to eq('[1] 101(#<Object>):1> ')
|
||||||
|
expect(proc.call(Object.new, 1, config._pry_)).to eq('[1] 102(#<Object>):1> ')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue