class Lolcommits::Plugin::Loltext

Constants

DEFAULT_FONT_PATH

Public Class Methods

runner_order() click to toggle source

Returns position(s) of when this plugin should run during the capture process. We want to add text to the image after capturing, but before capture is ready for sharing.

@return [Array] the position(s) (:post_capture)

# File lib/lolcommits/plugin/loltext.rb, line 19
def self.runner_order
  [:post_capture]
end

Public Instance Methods

configure_options!() click to toggle source

Prompts the user to configure text options.

@return [Hash] a hash of configured plugin options

Calls superclass method
# File lib/lolcommits/plugin/loltext.rb, line 50
def configure_options!
  puts '---------------------------------------------------------------'
  puts '  LolText options '
  puts ''
  puts '  * any blank options will use the (default)'
  puts '  * always use the full absolute path to fonts'
  puts '  * valid text positions are NE, NW, SE, SW, S, C (centered)'
  puts '  * colors can be hex #FC0 value or a string \'white\''
  puts '      - use `none` for no stroke color'
  puts '  * overlay fills your image with a random color'
  puts '      - set one or more overlay_colors with a comma seperator'
  puts '      - overlay_percent (0-100) sets the fill colorize strength'
  puts '---------------------------------------------------------------'

  super
end
enabled?() click to toggle source

Returns true/false indicating if the plugin is enabled or not. Enabled by default (if no configuration exists)

@return [Boolean] true/false indicating if plugin is enabled

Calls superclass method
# File lib/lolcommits/plugin/loltext.rb, line 29
def enabled?
  configuration.empty? || super
end
run_post_capture() click to toggle source

Post-capture hook, runs after lolcommits captures a snapshot.

Annotate runner overlay with message, sha, optional border, and semi-transparent colored background (based on config)

# File lib/lolcommits/plugin/loltext.rb, line 74
def run_post_capture
  debug "creating annotation overlay with MiniMagick"
  if config_option(:overlay, :enabled)
    color = config_option(:overlay, :overlay_colors).split(',').map(&:strip).sample
    overlay_percent = config_option(:overlay, :overlay_percent).to_f
    debug "adding colorized overlay (#{color} at #{overlay_percent}% opacity)"
    overlay_percent = (255 * (overlay_percent/100)).round

    runner.overlay.combine_options do |c|
      c.fill "#{color}#{sprintf('%02X',overlay_percent)}"
      c.draw "color 0,0 reset"
    end
  end

  if config_option(:border, :enabled)
    debug "adding border (#{config_option(:border, :size)}px #{config_option(:border, :color)})"
    # mogrify doesn't allow compose, format forces use of convert
    runner.overlay.format("PNG32") do |c|
      c.compose "Copy"
      c.shave config_option(:border, :size)
      c.bordercolor config_option(:border, :color)
      c.border config_option(:border, :size)
    end
  end

  annotate(runner.overlay, :message, clean_msg(runner.message))
  annotate(runner.overlay, :sha, runner.sha)
end
valid_configuration?() click to toggle source

Returns true/false indicating if the plugin has been correctly configured.

Valid by default (if no configuration exists)

@return [Boolean] true/false indicating if plugin is correct configured

Calls superclass method
# File lib/lolcommits/plugin/loltext.rb, line 41
def valid_configuration?
  configuration.empty? || super
end

Private Instance Methods

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

  transformed_position = position_transform(config_option(type, :position))
  annotate_location = '0'

  padded_annotation = 10
  if config_option(:border, :enabled)
    padded_annotation = padded_annotation + config_option(:border, :size)
  end

  # move all positions off the edge of the image
  case transformed_position
  when "South"
    annotate_location = "+0+#{padded_annotation}"
  when "NorthWest", "SouthWest", "NorthEast", "SouthEast"
    annotate_location = "+#{padded_annotation}+#{padded_annotation}"
  end

  string.upcase! if config_option(type, :uppercase)

  image.combine_options do |c|
    c.strokewidth 2
    c.interline_spacing(-(config_option(type, :size) / 5))
    c.stroke config_option(type, :stroke_color)
    c.fill config_option(type, :color)
    c.gravity transformed_position
    c.pointsize config_option(type, :size)
    c.font config_option(type, :font)
    c.annotate annotate_location, string
  end
rescue => error
  debug("mogrify annotate returned non-zero status: #{error.message}")
end
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/plugin/loltext.rb, line 196
def clean_msg(text)
  wrapped_text = word_wrap text
  escape_quotes wrapped_text
  escape_ats wrapped_text
end
default_options() click to toggle source

default text styling and positions

# File lib/lolcommits/plugin/loltext.rb, line 141
def default_options
  {
    message: {
      color: 'white',
      font: DEFAULT_FONT_PATH,
      position: 'SW',
      size: 48,
      stroke_color: 'black',
      uppercase: false
    },
    sha: {
      color: 'white',
      font: DEFAULT_FONT_PATH,
      position: 'NE',
      size: 32,
      stroke_color: 'black',
      uppercase: false
    },
    overlay: {
      enabled: false,
      overlay_colors: [
        '#2e4970', '#674685', '#ca242f', '#1e7882', '#2884ae', '#4ba000',
        '#187296', '#7e231f', '#017d9f', '#e52d7b', '#0f5eaa', '#e40087',
        '#5566ac', '#ed8833', '#f8991c', '#408c93', '#ba9109'
      ].join(','),
      overlay_percent: 50
    },
    border: {
      enabled: false,
      color: 'white',
      size: 10,
    }
  }
end
escape_ats(text) click to toggle source
# File lib/lolcommits/plugin/loltext.rb, line 207
def escape_ats(text)
  text.gsub(/@/, '\@')
end
escape_quotes(text) click to toggle source

conversion for quotation marks to avoid shell interpretation

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

explode psuedo-names for text positioning

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

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

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