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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/lolcommits/plugins/loltext.rb', line 24

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_defaultsObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lolcommits/plugins/loltext.rb', line 72

def config_defaults
  {
    :message => {
      :font     => DEFAULT_FONT_PATH,
      :size     => 48,
      :position => 'SW',
      :color    => 'white',
      :stroke_color => 'black'
    },
    :sha => {
      :font     => DEFAULT_FONT_PATH,
      :size     => 32,
      :position => 'NE',
      :color    => 'white',
      :stroke_color => 'black'
    }
  }
end

#config_option(type, option) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/lolcommits/plugins/loltext.rb', line 91

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/lolcommits/plugins/loltext.rb', line 39

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) ⇒ Object

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



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/lolcommits/plugins/loltext.rb', line 60

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?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
# File 'lib/lolcommits/plugins/loltext.rb', line 15

def run_postcapture
  debug 'Annotating image via MiniMagick'
  image = MiniMagick::Image.open(runner.main_image)
  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