Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/jaynetics/repper

Regexp pretty printer and formatter for Ruby
https://github.com/jaynetics/repper

code-formatter formatter pretty-printer regexp regular-expression ruby

Last synced: 3 months ago
JSON representation

Regexp pretty printer and formatter for Ruby

Awesome Lists containing this project

README

        

Rapper necklace with dollar pendant

# Repper

[![Gem Version](https://badge.fury.io/rb/repper.svg)](http://badge.fury.io/rb/repper)
[![Build Status](https://github.com/jaynetics/repper/actions/workflows/main.yml/badge.svg)](https://github.com/jaynetics/repper/actions)
[![Coverage](https://codecov.io/gh/jaynetics/repper/branch/main/graph/badge.svg?token=4C6EKINE4B)](https://codecov.io/gh/jaynetics/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:

screenshot1

#### 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](https://github.com/jaynetics/vscode-repper) to format Regexps in VSCode.

![vscode-repper](https://user-images.githubusercontent.com/10758879/170892739-e2f408f2-e239-4b13-8d28-c14fb7a9dbb9.gif)

### Using Repper manually

```ruby
Repper.call(/foo/) # pretty prints the given Regexp and returns nil
Repper.render(/foo/) # returns the pretty print String
```

### Customization

#### Customizing the format

Multiple formats are available out of the box:

- `:annotated` is the default, verbose format, shown above
- `:inline` adds only colorization and does not restructure the Regexp
- `:structured` is 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](https://github.com/jaynetics/vscode-repper)

You can change the format globally:

```ruby
Repper.format = :structured
```

Or pick a format on a case-by-case basis:

screenshot2

Or create your own format:

```ruby
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:

```ruby
Repper.theme = :monokai # a nicer theme, if the terminal supports it
```

screenshot3

```ruby
Repper.call(/foo/, theme: nil) # render without colors
```

Or create your own theme - you can use all colors supported by the [`rainbow` gem](https://github.com/sickill/rainbow).

```ruby
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](https://opensource.org/licenses/MIT).