Skip to content

Creating a New Theme

kyrylo edited this page Mar 26, 2013 · 21 revisions

Pry Theme runs on terminals that support 8, 16 or 256 colors. Check the repository for some examples of themes.

Let's create our own theme. Let's call it redblood. In most cases you want a theme suitable for 256 color capable terminals. Create a new theme with pry-theme edit redblood command. This command will open your text editor (defined in Pry.config.editor). In my case it is Vim.

You see something like this:

t = PryTheme.create :name => 'redblood' do
  author :name => 'curacao', :email => 'user@hostname'
  description 'redblood theme'

  # How the flip do I edit this?!
  # Help is there: https://github.com/kyrylo/pry-theme/wiki/Creating-a-New-Theme
  define_theme do
    class_
    class_variable
    comment
    constant
    error
    float
    global_variable
    inline_delimiter
    instance_variable
    integer
    keyword
    method
    predefined_constant
    symbol

    regexp do
      self_
      char
      content
      delimiter
      modifier
      escape
    end

    shell do
      self_
      char
      content
      delimiter
      escape
    end

    string do
      self_
      char
      content
      delimiter
      escape
    end
  end
end

PryTheme::ThemeList.add_theme(t)

As you can see, this is a boilerplate. Fill in your name and email. Add the description, if you want. Add :color_model option if your theme is supposed to have 8 or 16 colors:

t = PryTheme.create :name => 'redblood', :color_model => 8 do

So what about define_theme section? First of all examine Pry Theme Cheatsheet. Everything defined under define_theme is called a token color definition. Or just definition. There are subsections like regexp, string and shell, which also contain some definitions. Every definition accepts 3 parameters. For example, let's set the color of method token to red: method 'red'. OK, that looks not very pleasant. Let's change it so red would be a background color and the foreground color would be set back to the default terminal color: method :bg => 'red'.

More examples of definitions:

  • method 'white', 'green' — white text on green background
  • symbol 'white', 'green', [:bold] — bold white text on green background
  • integer [:bold] — bold text
  • comment 'gray01', [:italic, :underline] — italic underlined gray text
  • keyword :bg => 'yellow', [:bold, :italic, :underline] — bold italic underlined text on yellow background

Pry Theme supports different notations for colors. For example, you can use HEX notation, if you like: method '#AA00AA', [:italic]. Or you can use RGB notation: method '23, 105, 60' (the same as: method [23, 105, 60]). You can even use color indices: method 23. However, be careful to use only those indices that fit the limits of the color model you chose. If you edit a 16 color theme, you can't use 23 as your color value. The RGB and HEX notations convert the values to the limits automatically.

Pry Theme comes with two useful subcommands: convert and colors. The convert subcommand is useful when you want to know which color you'll be using. For example, 23 is not a very informative. We can get its name with help of pry-theme convert --term 23. The output will inform us that this color is dark_turquoise. You can use human readable color values as well: method 'dark_turquoise'. The convert subcommand can also convert HEX and RGB colors: pry-theme convert -h #990033 or pry-theme convert --rgb 32,12,0. More examples.

The colors subcommand is my favorite one. It outputs the table of colors. Just type pry-theme colors and enjoy the rainbow. Examples of usage.

So after you're done editing your theme, just exit the editor and you'll see the changes you just made. If you don't like what you see, just execute pry-theme edit redblood again. The edit subcommand is useful for incremental theme creation. More information about the subcommand.

If you want to share your Pry Theme with the community, you can send a pull request to the Pry Theme Collection repository. Check out their README for instructions.

Good luck!

P.S.: feel free to improve the wiki.

Clone this wiki locally