Skip to content

Commit

Permalink
allow configuration of text options in loltext plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
matthutchinson committed Jul 17, 2015
1 parent 6bd311b commit 8427236
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 37 deletions.
29 changes: 23 additions & 6 deletions features/lolcommits.feature
Original file line number Diff line number Diff line change
Expand Up @@ -114,30 +114,47 @@ Feature: Basic UI functionality
When I successfully run `lolcommits --plugins`
Then the output should contain a list of plugins

Scenario: Configuring plugin
Scenario: Configuring loltext plugin
Given I am in a git repo named "config-test"
When I run `lolcommits --config` interactively
And I wait for output to contain "Name of plugin to configure:"
Then I type "loltext"
And I wait for output to contain "enabled:"
Then I type "true"
And I wait for output to contain "font"
Then I type "my-font.ttf"
And I wait for output to contain "size"
Then I type "32"
And I wait for output to contain "position"
Then I type "SouthEast"
And I wait for output to contain "color"
Then I type "red"
And I wait for output to contain "stroke color"
Then I type "white"
And I wait for output to contain "sha text"
Then I type ""
Then I type ""
Then I type ""
Then I type ""
Then I type ""
Then the output should contain "Successfully configured plugin: loltext"
And the output should contain a list of plugins
And a file named "~/.lolcommits/config-test/config.yml" should exist
And the output should contain a list of plugins
And a file named "~/.lolcommits/config-test/config.yml" should exist
When I successfully run `lolcommits --show-config`
Then the output should match /loltext:\s+enabled: true/
And the output should match /:message:\n\s+:font: my-font\.ttf\n\s+:size: 32\n\s+:position: SouthEast\n\s+:color: red/

Scenario: Configuring plugin in test mode affects test loldir not repo loldir
Scenario: Configuring loltext plugin in test mode affects test loldir not repo loldir
Given I am in a git repo named "testmode-config-test"
When I run `lolcommits --config --test` interactively
And I wait for output to contain "Name of plugin to configure:"
Then I type "loltext"
And I wait for output to contain "enabled:"
Then I type "true"
Then I type "false"
Then the output should contain "Successfully configured plugin: loltext"
And a file named "~/.lolcommits/test/config.yml" should exist
When I successfully run `lolcommits --test --show-config`
Then the output should match /loltext:\s+enabled: true/
Then the output should match /loltext:\s+enabled: false/

Scenario: test capture should work regardless of whether in a git repo
Given I am in a directory named "nothingtoseehere"
Expand Down
2 changes: 1 addition & 1 deletion lib/lolcommits/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def save(config)
end

def to_s
"#{configuration_file}\n#{read_configuration.to_yaml}"
read_configuration.to_yaml.to_s
end

# class methods
Expand Down
9 changes: 7 additions & 2 deletions lib/lolcommits/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ def configure_options!
print "#{option}: "
val = parse_user_input(STDIN.gets.strip)
# check enabled option isn't a String
if (option == 'enabled') && val.is_a?(String)
puts "Aborting - please enable with 'true' or 'false'"
if (option == 'enabled') && ![true, false].include?(val)
puts "Aborting - please respond with 'true' or 'false'"
exit 1
else
acc.merge(option => val)
Expand All @@ -60,10 +60,15 @@ def configure_options!
end

def parse_user_input(str)
# cater for bools, strings, ints and blanks
if 'true'.casecmp(str) == 0
true
elsif 'false'.casecmp(str) == 0
false
elsif str =~ /^[0-9]+$/
str.to_i
elsif str.strip.empty?
nil
else
str
end
Expand Down
119 changes: 91 additions & 28 deletions lib/lolcommits/plugins/loltext.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,58 @@
# -*- encoding : utf-8 -*-
module Lolcommits
class Loltext < Plugin
def initialize(runner)
super
@font_location = runner ? runner.font : nil
def self.name
'loltext'
end

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'
puts ' * colors can be hex #FC0 value or a string \'white\''
puts '------------------------------------------------------'

options[:message] = configure_text_options(:message)
options[:sha] = configure_text_options(:sha)
end
options
end

# TODO: consider this type of configuration prompting in the base Plugin
# class, working with hash of defaults
def configure_text_options(type)
print "#{type} text:\n"
defaults = configure_options_defaults[type]
defaults.keys.reduce({}) do |acc, option|
print " #{option.to_s.gsub('_', ' ')} (#{defaults[option]}): "
val = parse_user_input(STDIN.gets.strip)
acc.merge(option => val)
end
end

def configure_options_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

# enabled by default (if no configuration exists)
Expand All @@ -12,43 +61,57 @@ def enabled?
end

def run_postcapture
font_location = @font_location || File.join(Configuration::LOLCOMMITS_ROOT,
'vendor',
'fonts',
'Impact.ttf')

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

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

image.combine_options do |c|
c.gravity 'SouthWest'
c.fill 'white'
c.stroke 'black'
c.strokewidth '2'
c.pointsize(runner.animate? ? '24' : '48')
c.interline_spacing '-9'
c.font font_location
c.annotate '0', clean_msg(runner.message)
c.stroke text_option(type, :stroke_color)
c.fill text_option(type, :color)
c.gravity position_transform(text_option(type, :position))
c.pointsize runner.animate? ? '24' : text_option(type, :size)
c.font text_option(type, :font)
c.annotate '0', string
end
end

image.combine_options do |c|
c.gravity 'NorthEast'
c.fill 'white'
c.stroke 'black'
c.strokewidth '2'
c.pointsize(runner.animate? ? '21' : '32')
c.font font_location
c.annotate '0', runner.sha
def text_option(type, option)
default_option = configure_options_defaults[type][option]
if configuration[type]
configuration[type][option] || default_option
else
default_option
end

debug "Writing changed file to #{runner.main_image}"
image.write runner.main_image
end

def self.name
'loltext'
private

# explode psuedo-names for text positions
def position_transform(position)
case position
when 'NE'
'NorthEast'
when 'NW'
'NorthWest'
when 'SE'
'SouthEast'
when 'SW'
'SouthWest'
end
end

private
def default_font_path
File.join(Configuration::LOLCOMMITS_ROOT, 'vendor', 'fonts', 'Impact.ttf')
end

# do whatever is required to commit message to get it clean and ready for imagemagick
def clean_msg(text)
Expand Down

0 comments on commit 8427236

Please sign in to comment.