Class: Lolcommits::Loltext

Inherits:
Plugin
  • Object
show all
Defined in:
lib/lolcommits/plugins/loltext.rb

Constant Summary collapse

DEFAULT_FONT_PATH =
File.join(Configuration::LOLCOMMITS_ROOT, 'vendor', 'fonts', 'Impact.ttf')

Instance Attribute Summary

Attributes inherited from Plugin

#options, #runner

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#configuration, #configured?, #debug, #execute_postcapture, #execute_precapture, #initialize, #log_error, #parse_user_input, #puts, #run_precapture, runner_order, #valid_configuration?

Constructor Details

This class inherits a constructor from Lolcommits::Plugin

Class Method Details

.nameObject



6
7
8
# File 'lib/lolcommits/plugins/loltext.rb', line 6

def self.name
  'loltext'
end

Instance Method Details

#annotate(image, type, string) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lolcommits/plugins/loltext.rb', line 31

def annotate(image, type, string)
  debug("annotating #{type} to image with #{string}")

  transformed_position = position_transform(config_option(type, :position))
  annotate_location = '0'
  if transformed_position == 'South'
    annotate_location = '+0+20' # Move South gravity off the edge of the image.
  end

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

  image.combine_options do |c|
    c.strokewidth runner.animate? ? '1' : '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 runner.animate? ? (config_option(type, :size) / 2) : config_option(type, :size)
    c.font config_option(type, :font)
    c.annotate annotate_location, string
  end
end

#config_defaultsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/lolcommits/plugins/loltext.rb', line 101

def config_defaults
  {
    :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'
      ],
      :overlay_percent => 50
    }
  }
end

#config_option(type, option) ⇒ Object



131
132
133
134
135
136
137
138
# File 'lib/lolcommits/plugins/loltext.rb', line 131

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!Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 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 '  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 '---------------------------------------------------------------'

    options[:message] = configure_sub_options(:message)
    options[:sha]     = configure_sub_options(:sha)
    options[:overlay] = configure_sub_options(:overlay)
  end
  options
end

#configure_sub_options(type) ⇒ Object

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lolcommits/plugins/loltext.rb', line 80

def configure_sub_options(type)
  print "#{type}:\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|
    # if we have an enabled key set to false, abort asking for any more options
    if acc.key?(:enabled) && acc[:enabled] != true
      acc
    else
      print "  #{opt.to_s.tr('_', ' ')} (#{defaults[opt]}): "
      val = parse_user_input(STDIN.gets.chomp.strip)
      # handle array options (comma seperated string)
      if defaults[opt].is_a?(Array) && !val.nil?
        val = val.split(',').map(&:strip).delete_if(&:empty?)
      end
      acc.merge(opt => val)
    end
  end
end

#enabled?Boolean

enabled by default (if no configuration exists)

Returns:

  • (Boolean)


11
12
13
# File 'lib/lolcommits/plugins/loltext.rb', line 11

def enabled?
  !configured? || super
end

#run_postcaptureObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/lolcommits/plugins/loltext.rb', line 15

def run_postcapture
  debug 'Annotating image via MiniMagick'
  image = MiniMagick::Image.open(runner.main_image)
  if config_option(:overlay, :enabled)
    image.combine_options do |c|
      c.fill config_option(:overlay, :overlay_colors).sample
      c.colorize config_option(:overlay, :overlay_percent)
    end
  end

  annotate(image, :message, clean_msg(runner.message))
  annotate(image, :sha, runner.sha)
  debug "Writing changed file to #{runner.main_image}"
  image.write runner.main_image
end