Repper
Repper is a regular expression pretty printer and formatter for Ruby.
Installation
gem install repper, or add it to your Gemfile.
Usage
repper can be integrated into the REPL (e.g. IRB) through core extensions for Regexp pretty-printing, integrated into editors to format Regexps, or called manually.
There are also a few customization options.
REPL integration
Via Regexp#inspect (recommended)
require 'repper/core_ext/regexp' in your ~/.irbrc or ~/.pryrc to override Regexp#inspect and automatically use repper to display Regexps:
Via Kernel#pp
Alternatively, require 'repper/core_ext/kernel' to make the pp command give nicer output for Regexps (which will look like above by default).
Editor integration
Use vscode-repper to format Regexps in VSCode.
Using Repper manually
Repper.call(/foo/) # pretty prints the given Regexp and returns nil
Repper.render(/foo/) # returns the pretty print StringCustomization
Customizing the format
Multiple formats are available out of the box:
-
:annotatedis the default, verbose format, shown above -
:inlineadds only colorization and does not restructure the Regexp -
:structuredis like:annotated, just without annotations -
:x(or:extended) returns a lightly formatted but equivalent Regexp- this format is used for the repper executable and vscode-repper
You can change the format globally:
Repper.format = :structuredOr pick a format on a case-by-case basis:
Or create your own format:
require 'csv'
csv_format = ->(tokens, _theme) { tokens.map(&:text).to_csv }
Repper.render(/re[\p{pe}\r]$/, format: csv_format)
=> "/,re,[,\\p{pe},\\r,],$,/\n"Customizing the colors
The color theme can also be set globally or passed on call:
Repper.theme = :monokai # a nicer theme, if the terminal supports itRepper.call(/foo/, theme: nil) # render without colorsOr create your own theme - you can use all colors supported by the rainbow gem.
Repper.theme = {
group: :green,
set: :red,
default: :white,
}Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/jaynetics/repper.
License
The gem is available as open source under the terms of the MIT License.




