diff options
author | 0xAB <0xAB@protonmail.com> | 2017-09-02 13:54:14 +0100 |
---|---|---|
committer | 0xAB <0xAB@protonmail.com> | 2017-09-02 13:54:14 +0100 |
commit | ef09ca1b6cb8b3d2a4bc6f74200d721c6769971a (patch) | |
tree | d54deac6e2b80aafc7aa6593a88f01560c16f445 | |
parent | 8fc102086674f56ffe6609bce21088c22c91920e (diff) | |
download | pry-ef09ca1b6cb8b3d2a4bc6f74200d721c6769971a.tar.gz |
hide prompt aliases that the terminal cannot display
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | lib/pry/commands/list_prompts.rb | 2 | ||||
-rw-r--r-- | lib/pry/helpers/text.rb | 16 | ||||
-rw-r--r-- | lib/pry/prompt.rb | 16 | ||||
-rw-r--r-- | lib/pry/pry_instance.rb | 1 |
5 files changed, 31 insertions, 8 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a0e099d8..b1f2fae9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ ### HEAD -* Add `Pry::Helpers::Text#displayable_character?`, and `Pry::Helpers::Text#snowman_ready?`. +* Add `Pry::Helpers::Text#{displayable_character?,displayable_string?,snowman_ready?}`. * `Pry::Helpers::Text#default` is deprecated in favor of `Pry::Helpers::Colors#strip_color`. * Coloring functions such as `black()` etc respect `_pry_.color` automatically, see [#1637](https://github.com/pry/pry/pull/1637). -* Add `Pry#helpers`, see [#1635](https://github.com/pry/pry/pull/1635) +* Add `Pry#helpers`, see [#1635](https://github.com/pry/pry/pull/1635). Aliased as `Pry#h`. * Add `Pry::Helpers::Colors`, and a new `#paint` function. * Add clear-screen command ([#1630](https://github.com/pry/pry/pull/1630)) * Fix [#1636](https://github.com/pry/pry/issues/1636) diff --git a/lib/pry/commands/list_prompts.rb b/lib/pry/commands/list_prompts.rb index 317dc7aa..6c426fdf 100644 --- a/lib/pry/commands/list_prompts.rb +++ b/lib/pry/commands/list_prompts.rb @@ -13,7 +13,7 @@ class Pry::Command::ListPrompts < Pry::ClassCommand output.puts heading("Available prompts") + "\n\n" all_prompts.each do |prompt| next if prompt.alias? - aliases = Pry::Prompt.aliases_for(prompt.name) + aliases = Pry::Prompt.aliases_for(prompt.name, _pry_) output.write "Name: #{text.bold(prompt.name)}" output.puts selected_prompt?([prompt].concat(aliases)) ? text.green(" [active]") : "" output.puts "Aliases: #{aliases.map {|s| text.bold(s.name) }.join(',')}" if aliases.any? diff --git a/lib/pry/helpers/text.rb b/lib/pry/helpers/text.rb index d4b9fc13..0ad53aa9 100644 --- a/lib/pry/helpers/text.rb +++ b/lib/pry/helpers/text.rb @@ -69,12 +69,22 @@ class Pry # to deal with unicode characters (like MS Windows+cmd.exe). # def displayable_character?(char) + displayable_string? char.to_s[0] + end + + # + # Same as {#displayable_character?} but operates on entire String instead of single + # character. + # + # @see #displayable_character? + # + def displayable_string?(str) require 'open3' if not defined?(Open3) - Open3.popen3 DISPLAY_CMD % {ruby: RbConfig.ruby, char: char.to_s[0]} do |_, stdout, stderr| - SNOWMAN == stdout.gets.chomp + Open3.popen3 DISPLAY_CMD % {ruby: RbConfig.ruby, str: str} do |_, stdout, _| + str == stdout.gets.chomp end end - DISPLAY_CMD = %q("%{ruby}" -e 'print "%{char}"').freeze + DISPLAY_CMD = %q("%{ruby}" -e 'print "%{str}"').freeze SNOWMAN = "☃".freeze private_constant :DISPLAY_CMD, :SNOWMAN diff --git a/lib/pry/prompt.rb b/lib/pry/prompt.rb index ea71b346..632aaf3c 100644 --- a/lib/pry/prompt.rb +++ b/lib/pry/prompt.rb @@ -37,14 +37,26 @@ module Pry::Prompt end # + # @note + # When '_pry_' is in scope or explicitly passed as a second argument, aliases that cannot + # be displayed in a terminal are not returned by this method. See + # {Pry::Helpers::Text#displayable_string?}. + # # @param [String] prompt # The name of a prompt. # + # @param [Pry] pry + # An instance of `Pry`. + # # @return [Array<PromptInfo>] # Returns an array of aliases for _prompt_, as {PromptInfo} objects. # - def aliases_for(prompt) - all_prompts.select{|prompt_info| prompt_info.alias_for == prompt.to_s} + def aliases_for(prompt_name, pry=defined?(_pry_) && _pry_) + prompt_name = prompt_name.to_s + all_prompts.select do |prompt_info| + prompt_info.alias_for == prompt_name and + (pry ? pry.h.displayable_string?(prompt_info.name) : true) + end end # diff --git a/lib/pry/pry_instance.rb b/lib/pry/pry_instance.rb index 24d88e15..b48bd9ed 100644 --- a/lib/pry/pry_instance.rb +++ b/lib/pry/pry_instance.rb @@ -105,6 +105,7 @@ class Pry end }.call end + alias_method :h, :helpers # The current prompt. # This is the prompt at the top of the prompt stack. |