lib/pry-theme.rb in pry-theme-0.0.9 vs lib/pry-theme.rb in pry-theme-0.0.10

- old
+ new

@@ -4,20 +4,19 @@ require 'pry-theme/palette' require 'pry-theme/theme' require 'pry-theme/when_started_hook' require 'pry-theme/uninstaller' -require 'pry' require 'yaml' module PryTheme # The root path for PryTheme source codes. ROOT = File.expand_path(File.dirname(__FILE__)) # The root path for PryTheme examples. - EXAMPLES_ROOT = File.join(ROOT, "..", "examples") + EXAMPLES_ROOT = File.join(ROOT, "..", "themes") # The root path for the directory with configuration files for OS you're using. CONFIG_DIR = case RbConfig::CONFIG["host_os"] when /mingw|mswin/ File.join(ENV["APPDATA"], "pry-theme") @@ -49,10 +48,14 @@ begin theme = Theme.new(theme_name) rescue NoThemeError => no_theme_error warn no_theme_error return + rescue ThemeDescriptionError => long_descr + Pry.output.puts long_descr + Pry.output.puts "Using #{DEFAULT_THEME_NAME} theme." + return end palette = Palette.new(theme.color_depth) scheme = {} @@ -98,40 +101,34 @@ # Matches "yellow (bu) on red" or "on red". ( \s? on\s ( - [a-z]+(0[1-9])? + \w+(0[1-9])? ) )? \z /x if color m = color.match(color_pattern) - color_fg = if $2 - c = palette.colors.find do |color| - color.human == $2.to_sym - end + color_fg = find_color($2, palette) { |c| c.term } - if c - c.term - else - raise NoColorError - end - end - formatting = if $5 formatting = $5.each_char.map do |ch| Formatting::ATTRIBUTES[ch] end end - color_bg = if $7 - Formatting::BACKGROUNDS[$7] + color_bg = find_color($7, palette) do |c| + if palette.color_depth == 256 + "48;5;#{c.term}" + else + Formatting::BACKGROUNDS[c.human.to_s] + end end # Uh oh :( notation = if !color_fg "38;0" @@ -147,15 +144,29 @@ # use default color. Not handling this situation results in CodeRay's # error ("can't convert nil into String" stuff). "38;0;0" end rescue NoColorError => e - Pry.output.puts "#{e}: wrong color value: `#{$2}`. Typo?" + Pry.output.puts "#{e}: wrong color value: `#{color}`. Typo?" end def self.install_gem_hooks Gem.post_uninstall do |u| Uninstaller.run(u) if u.spec.name == "pry-theme" + end + end + + def self.find_color(color, palette, &block) + if color + c = palette.colors.find do |palette_color| + palette_color.human == color.to_sym + end + + if c + block.call(c) + else + raise NoColorError + end end end end