Adding progress bars to the custom configuration
parent
65f75a3360
commit
8bfdf4b777
|
|
@ -18,8 +18,11 @@ module Squib
|
|||
# @api public
|
||||
def png(opts = {})
|
||||
opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout])
|
||||
opts[:range].each do |i|
|
||||
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha])
|
||||
@progress_bar.start("Loading PNGs #{location(opts)}", opts[:range].size) do |bar|
|
||||
opts[:range].each do |i|
|
||||
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha])
|
||||
bar.increment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -40,8 +43,11 @@ module Squib
|
|||
# @api public
|
||||
def svg(opts = {})
|
||||
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout])
|
||||
p[:range].each do |i|
|
||||
@cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height])
|
||||
@progress_bar.start("Loading SVGs #{location(opts)}", p[:range].size) do |bar|
|
||||
p[:range].each do |i|
|
||||
@cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height])
|
||||
bar.increment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -24,20 +24,15 @@ module Squib
|
|||
# @option opts [Enumerable] range (:all) the range of cards over which this will be rendered. See {file:API.md#label-Specifying+Ranges Specifying Ranges}
|
||||
# @option opts [String] dir (_output) the directory for the output to be sent to. Will be created if it doesn't exist.
|
||||
# @option opts [String] prefix (card_) the prefix of the file name to be printed.
|
||||
# @option opts [Boolean] progress_bar (false) if true, show a progress bar on stdout while saving
|
||||
# @return [nil] Returns nothing
|
||||
# @api public
|
||||
def save_png(opts = {})
|
||||
opts = needs(opts,[:range, :creatable_dir, :prefix, :progress_bar])
|
||||
if opts[:progress_bar]
|
||||
require 'ruby-progressbar'
|
||||
bar = ProgressBar.create(title: "Saving to PNGs",
|
||||
starting_at: 0, total: opts[:range].size,
|
||||
format: '%t <%B> %p%% %a')
|
||||
end
|
||||
opts[:range].each do |i|
|
||||
@cards[i].save_png(i, opts[:dir], opts[:prefix])
|
||||
bar.increment if opts[:progress_bar]
|
||||
opts = needs(opts,[:range, :creatable_dir, :prefix])
|
||||
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*") do |bar|
|
||||
opts[:range].each do |i|
|
||||
@cards[i].save_png(i, opts[:dir], opts[:prefix])
|
||||
bar.increment
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -6,14 +6,17 @@ module Squib
|
|||
# Text hints are rectangles around where the text will be laid out. They are intended to be temporary.
|
||||
# Setting a hint to nil or to :off will disable hints. @see samples/text.rb
|
||||
# @example
|
||||
# hint text:cyan
|
||||
# hint text: :cyan
|
||||
# hint text: :cyan, progress_bar: true
|
||||
#
|
||||
# @param [String] text the color of the text hint. To turn off use nil or :off. @see API.md
|
||||
# @param [Boolean] progress_bar enable progress bars on long-running operations
|
||||
# @return [nil] Returns nothing
|
||||
# @api public
|
||||
def hint(text: nil)
|
||||
def hint(text: nil, progress_bar: false)
|
||||
text = nil if text == :off
|
||||
@text_hint = text
|
||||
@progress_bar.enabled = progress_bar
|
||||
end
|
||||
|
||||
# Sets various defaults for this deck. Defaults can be overriden by the commands themselves
|
||||
|
|
|
|||
|
|
@ -38,4 +38,13 @@ module Squib
|
|||
:progress_bar => false
|
||||
}
|
||||
|
||||
# Squib's configuration defaults
|
||||
#
|
||||
# @api public
|
||||
CONFIG_DEFAULTS = {
|
||||
'dpi' => 300,
|
||||
'progress_bar' => false,
|
||||
'hint' => nil
|
||||
}
|
||||
|
||||
end
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
require 'yaml'
|
||||
require 'pp'
|
||||
require 'squib/card'
|
||||
require 'squib/progress'
|
||||
require 'squib/input_helpers'
|
||||
require 'squib/constants'
|
||||
|
||||
|
|
@ -51,6 +52,7 @@ module Squib
|
|||
@dpi = dpi
|
||||
@font = Squib::SYSTEM_DEFAULTS[:default_font]
|
||||
@cards = []
|
||||
@progress_bar = Squib::Progress.new(false)
|
||||
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
||||
load_config(config)
|
||||
load_layout(layout)
|
||||
|
|
@ -73,13 +75,19 @@ module Squib
|
|||
@cards.each { |card| block.call(card) }
|
||||
end
|
||||
|
||||
# Shows a descriptive place of the location
|
||||
def location(opts)
|
||||
opts[:layout] || (" @ #{opts[:x]},#{opts[:y]}")
|
||||
end
|
||||
|
||||
# Load the configuration file, if exists, overriding hardcoded defaults
|
||||
# @api private
|
||||
def load_config(file)
|
||||
if File.exists? file
|
||||
if config = YAML.load_file(file)
|
||||
@dpi = config['dpi'].to_i
|
||||
end
|
||||
if File.exists?(file) && config = YAML.load_file(file)
|
||||
config = Squib::CONFIG_DEFAULTS.merge(config)
|
||||
@dpi = config['dpi'].to_i
|
||||
@hint = config['hint']
|
||||
@progress_bar.enabled = config['progress_bar']
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -18,20 +18,24 @@ module Squib
|
|||
width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
|
||||
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height))
|
||||
x = p[:margin] ; y = p[:margin]
|
||||
@cards.each_with_index do |card, i|
|
||||
surface = trim(card.cairo_surface, p[:trim], @width, @height)
|
||||
cc.set_source(surface, x, y)
|
||||
cc.paint
|
||||
x += surface.width + p[:gap]
|
||||
if x > (width - surface.width - p[:margin])
|
||||
x = p[:margin]
|
||||
y += surface.height + p[:gap]
|
||||
if y > (height - surface.height - p[:margin])
|
||||
x = p[:margin] ; y = p[:margin]
|
||||
cc.show_page #next page
|
||||
|
||||
@progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
|
||||
@cards.each_with_index do |card, i|
|
||||
surface = trim(card.cairo_surface, p[:trim], @width, @height)
|
||||
cc.set_source(surface, x, y)
|
||||
cc.paint
|
||||
bar.increment
|
||||
x += surface.width + p[:gap]
|
||||
if x > (width - surface.width - p[:margin])
|
||||
x = p[:margin]
|
||||
y += surface.height + p[:gap]
|
||||
if y > (height - surface.height - p[:margin])
|
||||
x = p[:margin] ; y = p[:margin]
|
||||
cc.show_page #next page
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# :nodoc:
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
require 'ruby-progressbar'
|
||||
|
||||
module Squib
|
||||
# A facade that handles (or doesn't) the progress bar on the console
|
||||
#
|
||||
# :nodoc:
|
||||
# @api private
|
||||
class Progress
|
||||
attr_accessor :enabled
|
||||
|
||||
def initialize(enabled)
|
||||
@enabled = enabled
|
||||
end
|
||||
|
||||
def start(title="", total=100, &block)
|
||||
if @enabled
|
||||
@bar = ProgressBar.create(title: title, total: total, format: '%t <%B> %p%% %a')
|
||||
yield(@bar)
|
||||
@bar.finish
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
|
@ -1 +1,3 @@
|
|||
dpi: 300
|
||||
progress_bar: true
|
||||
hint: '#FF0000'
|
||||
|
|
@ -3,4 +3,14 @@ require 'squib'
|
|||
|
||||
Squib::Deck.new(config: 'custom-config.yml') do
|
||||
|
||||
# Hints are turned on in the config file
|
||||
text str: "The Title", x: 0, y: 78, width: 750,
|
||||
font: 'Arial 72', align: :center
|
||||
|
||||
# Progress bars are shown for these commands
|
||||
png file: 'shiny-purse.png', x: 620, y: 75
|
||||
svg file: 'spanner.svg', x: 620, y: 218
|
||||
save_png prefix: 'custom-config_'
|
||||
save_pdf file: "custom-config-out.pdf"
|
||||
|
||||
end
|
||||
Loading…
Reference in New Issue