class Lolcommits::Loltext

Constants

DEFAULT_FONT_PATH
DEFAULT_LOGO_PATH

Public Class Methods

name() click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 6
def self.name
  'loltext'
end

Public Instance Methods

annotate(image, type, string) click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 28
def annotate(image, type, string)
  debug("annotating #{type} text to image")

  image.combine_options do |c|
    c.strokewidth '2'
    c.interline_spacing '-9'
    c.stroke config_option(type, :stroke_color)
    c.fill config_option(type, :color)
    c.gravity position_transform(config_option(type, :position))
    c.pointsize runner.animate? ? 24 : config_option(type, :size)
    c.font config_option(type, :font)
    c.annotate '0', string
  end
end
config_defaults() click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 87
def config_defaults
  {
    :message => {
      :font     => DEFAULT_FONT_PATH,
      :size     => 48,
      :position => 'SW',
      :color    => 'white',
      :stroke_color => 'black'
    }
  }
end
config_option(type, option) click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 99
def config_option(type, option)
  default_option = config_defaults[type][option]
  if configuration[type]
    configuration[type][option] || default_option
  else
    default_option
  end
end
configure_options!() click to toggle source
Calls superclass method Lolcommits::Plugin#configure_options!
# File lib/lolcommits/plugins/loltext.rb, line 54
def configure_options!
  options = super
  # ask user to configure text options when enabling
  if options['enabled']
    puts '------------------------------------------------------'
    puts '  Text options '
    puts
    puts '  * blank options use the (default)'
    puts '  * use full absolute path to fonts'
    puts '  * valid positions are NE, NW, SE, SW, C (centered)'
    puts '  * colors can be hex #FC0 value or a string \'white\''
    puts '------------------------------------------------------'

    options[:message] = configure_sub_options(:message)
    options[:sha]     = configure_sub_options(:sha)
  end
  options
end
configure_sub_options(type) click to toggle source

TODO: consider this type of configuration prompting in the base Plugin class, working with hash of defaults

# File lib/lolcommits/plugins/loltext.rb, line 75
def configure_sub_options(type)
  print "#{type} text:\n"
  defaults = config_defaults[type]

  # sort option keys since different `Hash#keys` varys across Ruby versions
  defaults.keys.sort_by(&:to_s).reduce({}) do |acc, opt|
    print "  #{opt.to_s.tr('_', ' ')} (#{defaults[opt]}): "
    val = parse_user_input(STDIN.gets.strip)
    acc.merge(opt => val)
  end
end
enabled?() click to toggle source

enabled by default (if no configuration exists)

Calls superclass method Lolcommits::Plugin#enabled?
# File lib/lolcommits/plugins/loltext.rb, line 11
def enabled?
  # Added by Felix
  # we have to access the configuration for the snapgit plugin here
  config = runner.config.read_configuration if runner
  return false unless config["snapgit"]["show_commit_messages"]
  !configured? || super
end
run_postcapture() click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 19
def run_postcapture
  debug 'Annotating image via MiniMagick'
  image = MiniMagick::Image.open(runner.main_image)
  annotate(image, :message, clean_msg(runner.message))
  image = add_logo(image, DEFAULT_LOGO_PATH)
  debug "Writing changed file to #{runner.main_image}"
  image.write runner.main_image
end

Private Instance Methods

clean_msg(text) click to toggle source

do whatever is required to commit message to get it clean and ready for imagemagick

# File lib/lolcommits/plugins/loltext.rb, line 127
def clean_msg(text)
  wrapped_text = word_wrap text
  escape_quotes wrapped_text
  escape_ats wrapped_text
end
escape_ats(text) click to toggle source
# File lib/lolcommits/plugins/loltext.rb, line 139
def escape_ats(text)
  text.gsub(/@/, '\@')
end
escape_quotes(text) click to toggle source

conversion for quotation marks to avoid shell interpretation does not seem to be a safe way to escape cross-platform?

# File lib/lolcommits/plugins/loltext.rb, line 135
def escape_quotes(text)
  text.gsub(/"/, "''")
end
position_transform(position) click to toggle source

explode psuedo-names for text position

# File lib/lolcommits/plugins/loltext.rb, line 111
def position_transform(position)
  case position
  when 'NE'
    'NorthEast'
  when 'NW'
    'NorthWest'
  when 'SE'
    'SouthEast'
  when 'SW'
    'SouthWest'
  when 'C'
    'Center'
  end
end
word_wrap(text, col = 20) click to toggle source

convenience method for word wrapping based on github.com/cmdrkeene/memegen/blob/master/lib/meme_generator.rb

# File lib/lolcommits/plugins/loltext.rb, line 145
def word_wrap(text, col = 20)
  wrapped = text.gsub(/(.{1,#{col + 4}})(\s+|\Z)/, "\\1\n")
  wrapped.chomp!
end