Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bkuhlmann/irb-kit

Extends IRB by providing additional productivity enhancements.
https://github.com/bkuhlmann/irb-kit

console extensions irb ruby utilities

Last synced: about 2 months ago
JSON representation

Extends IRB by providing additional productivity enhancements.

Awesome Lists containing this project

README

        

:toc: macro
:toclevels: 5
:figure-caption!:

:hanami_link: link:https://hanamirb.org[Hanami]
:irb_article_link: link:https://alchemists.io/articles/interactive_ruby[Interactive Ruby]
:irb_extensions_link: link:https://github.com/ruby/irb/blob/master/EXTEND_IRB.md[IRB Extensions]
:irb_link: link:https://github.com/ruby/irb[IRB]
:rails_link: link:https://rubyonrails.org[Rails]
:ruby_link: link:https://www.ruby-lang.org[Ruby]
:xdg_link: link:https://alchemists.io/projects/xdg[XDG]
:iterm_link: link:https://iterm2.com[iTerm2]

= IRB Kit

This gem is a collection of {irb_extensions_link} for enhancing {irb_link} with additional commands, helpers, prompts, and more. Add this gem to your {irb_link} configuration (i.e. `irbrc`) to get all these enhancements immediately while keeping your IRB configuration slim and readable.

toc::[]

== Features

* Provides custom commands and helpers.
* Provides dynamic prompt based on environment.

== Requirements

. {ruby_link}.
. {irb_link}.
. A solid understanding of {irb_article_link}.

== Setup

To install _with_ security, run:

[source,bash]
----
# 💡 Skip this line if you already have the public certificate installed.
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
gem install irb-kit --trust-policy HighSecurity
----

To install _without_ security, run:

[source,bash]
----
gem install irb-kit
----

You can also add the gem directly to your project:

[source,bash]
----
bundle add irb-kit
----

Once the gem is installed, you only need to require it:

[source,ruby]
----
require "irb/kit"
----

== Usage

To use, add the following to your `~/.config/irb/irbrc` configuration (or `~/.irbrc` if not using {xdg_link}).

[source,ruby]
----
begin
require "irb/kit"
rescue LoadError => error
puts "ERROR: #{error.message.capitalize}."
end
----

This ensures this gem is fully loaded in order to enhance {irb_link} further. Otherwise, an error is displayed if you are in a Ruby project that doesn't have this gem as a dependency. To customize, read on.

=== Quick Start

If you'd like all the bells and whistles fully enabled, add the following to your {irb_link} configuration (as discussed above):

[source,ruby]
----
begin
require "irb/kit"

IRB::Kit.register_commands :all
IRB::Kit.register_helpers :all

IRB.conf[:PROMPT] ||= {}

IRB.conf[:PROMPT][:DEMO] = {
PROMPT_I: "[#{IRB::Kit.prompt}]> ",
PROMPT_N: "[#{IRB::Kit.prompt}]| ",
PROMPT_C: "[#{IRB::Kit.prompt}]| ",
PROMPT_S: "[#{IRB::Kit.prompt}]%l ",
RETURN: "=> %s\n"
}

IRB.conf[:PROMPT_MODE] = :DEMO
rescue LoadError => error
puts "ERROR: #{error.message.capitalize}."
end
----

The above enables all commands and helpers and configures your prompt for {ruby_link}, {hanami_link}, and {rails_link} environments.

💡 Use of `:DEMO`, while functional, is meant to be replaced with the name of your organization or personal identity. So please ensure you replace both occurrences of `:DEMO` with your own unique identifier.

=== Commands

Custom command are provided for you by this gem. Each is listed below as found when using `help` within an {irb_link} console:

....
Kit
descendants Show class descendants.
....

To enable all of these commands, use `IRB::Kit.register_commands :all`. This is the same as using:

[source,ruby]
----
IRB::Kit.register_commands :descendants
----

Knowing this, you can disable all commands entirely, use only the ones you care about, or enable all of them at once. The following explains each helper in greater detail.

==== `descendants`

Use this command to show all descendants of a class. Example:

[source,ruby]
----
descendants IO
# File

descendants Ractor::Error
# Ractor::IsolationError
# Ractor::MovedError
# Ractor::RemoteError
# Ractor::UnsafeError
----

=== Helpers

Several custom helpers are provided for you by this gem. Each is listed below as found when using `help` within an {irb_link} console:

....
Helper methods
clip Copy input to macOS clipboard.
esource Edit the source code of a constant or method in your default editor.
paste Paste last entry from macOS clipboard.
search Search an object's methods by pattern.
....

To enable all of these helpers, use `IRB::Kit.register_helpers :all`. This is the same as using:

[source,ruby]
----
IRB::Kit.register_helpers(*%i[clip esource paste search])
----

Knowing this, you can disable all helpers entirely, use only the ones you care about, or enable all of them at once. The following explains each helper in greater detail.

==== `clip`

Use this helper to copy output into the macOS clipboard. Example:

[source,ruby]
----
clip (1..3).to_a
# 1
# 2
# 3

clip 1, 2, 3
# 1
# 2
# 3

clip Object.new
# #
----

This helper accepts any number of arguments. Each is delimited by a new line for pasting into another application.

==== `esource`

Use this helper, short for _edit source_, to print and edit the source of a constant or method in your default editor. This assumes your have the `EDITOR` environment variable set and configured to use your favorite editor. If not, you'll get an error requiring your to update your environment accordingly. Here are a few usage examples:

[source,ruby]
----
esource IRB, :start
# Editing: irb-1.13.2/lib/irb.rb:893...

esource IRB, "start"
# Editing: irb-1.13.2/lib/irb.rb:893...

esource "IRB::IRBRC_EXT"
# Editing: irb-1.13.2/lib/irb/init.rb:407...

esource :RUBY_DESCRIPTION
# ERROR (invalid path): ruby.
----

The first argument is the constant or object you want to search for or target. The second argument is the object's method you want to find the source code location for. You can also use a symbol or string for the method.

==== `paste`

Use this helper to paste the last entry from your macOS clipboard into your console. For example, assuming the text `This is a demonstration` is in your clipboard, you'd see the following:

[source,ruby]
----
paste
# "This is a demonstration"
----

This helper takes no arguments.

==== `search`

Use this helper to search for methods on an object. Example:

[source,ruby]
----
search Module, "protected"
# protected_instance_methods
# protected_method_defined?
# protected_methods

search Module, /_defined/
# const_defined?
# class_variable_defined?
# method_defined?
# public_method_defined?
# private_method_defined?
# protected_method_defined?
# instance_variable_defined?
----

This helper takes two arguments. The first is the object you want to search on and the second argument is the string or regular expression of the methods to search for.

=== Prompt

A dynamic prompt, based on environment, is provided for you. The code -- as shown above -- for configuring IRB to make use of this custom prompt is:

[source,ruby]
----
IRB.conf[:PROMPT][:DEMO] = {
PROMPT_I: "[#{IRB::Kit.prompt}]> ",
PROMPT_N: "[#{IRB::Kit.prompt}]| ",
PROMPT_C: "[#{IRB::Kit.prompt}]| ",
PROMPT_S: "[#{IRB::Kit.prompt}]%l ",
RETURN: "=> %s\n"
}

IRB.conf[:PROMPT_MODE] = :DEMO
----

You only need to swap out the `:DEMO` key with a key that identifies you as you see fit.

At the moment, the prompt dynamically detects the following environments:

* {ruby_link}
* {hanami_link}
* {rails_link}

Additionally, when working with the {hanami_link} and/or {rails_link} frameworks, environment information will be color coded as follows:

* *Non-Production*: Displays as green for any environment other than production.
* *Production*: Displays as red but only for a production environment.

The following screenshots demonstrate what the prompt looks like in different environments:

*Ruby (with Git)*

image::https://alchemists.io/images/projects/irb-kit/screenshots/prompt-with_git.png[Screenshot,651,150,role=focal_point]

*Ruby (without Git)*

image::https://alchemists.io/images/projects/irb-kit/screenshots/prompt-without_git.png[Screenshot,400,146,role=focal_point]

*Hanami (development)*

image::https://alchemists.io/images/projects/irb-kit/screenshots/prompt-hanami-development.png[Screenshot,690,146,role=focal_point]

*Hanami (production)*

image::https://alchemists.io/images/projects/irb-kit/screenshots/prompt-hanami-production.png[Screenshot,910,169,role=focal_point]

*Rails*

image::https://alchemists.io/images/projects/irb-kit/screenshots/prompt-rails.png[Screenshot,756,170,role=focal_point]

== Development

To contribute, run:

[source,bash]
----
git clone https://github.com/bkuhlmann/irb-kit
cd irb-kit
bin/setup
----

You can also use the IRB console for direct access to all objects:

[source,bash]
----
bin/console
----

== Tests

To test, run:

[source,bash]
----
bin/rake
----

== link:https://alchemists.io/policies/license[License]

== link:https://alchemists.io/policies/security[Security]

== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]

== link:https://alchemists.io/policies/contributions[Contributions]

== link:https://alchemists.io/projects/irb-kit/versions[Versions]

== link:https://alchemists.io/community[Community]

== Credits

* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].