Adding progress bars to the custom configuration
parent
65f75a3360
commit
8bfdf4b777
|
|
@ -18,8 +18,11 @@ module Squib
|
||||||
# @api public
|
# @api public
|
||||||
def png(opts = {})
|
def png(opts = {})
|
||||||
opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout])
|
opts = needs(opts, [:range, :files, :x, :y, :alpha, :layout])
|
||||||
opts[:range].each do |i|
|
@progress_bar.start("Loading PNGs #{location(opts)}", opts[:range].size) do |bar|
|
||||||
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha])
|
opts[:range].each do |i|
|
||||||
|
@cards[i].png(opts[:file][i], opts[:x], opts[:y], opts[:alpha])
|
||||||
|
bar.increment
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -40,8 +43,11 @@ module Squib
|
||||||
# @api public
|
# @api public
|
||||||
def svg(opts = {})
|
def svg(opts = {})
|
||||||
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout])
|
p = needs(opts,[:range, :files, :svgid, :x, :y, :width, :height, :layout])
|
||||||
p[:range].each do |i|
|
@progress_bar.start("Loading SVGs #{location(opts)}", p[:range].size) do |bar|
|
||||||
@cards[i].svg(p[:file][i], p[:id], p[:x], p[:y], p[:width], p[:height])
|
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
|
||||||
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 [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] 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 [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
|
# @return [nil] Returns nothing
|
||||||
# @api public
|
# @api public
|
||||||
def save_png(opts = {})
|
def save_png(opts = {})
|
||||||
opts = needs(opts,[:range, :creatable_dir, :prefix, :progress_bar])
|
opts = needs(opts,[:range, :creatable_dir, :prefix])
|
||||||
if opts[:progress_bar]
|
@progress_bar.start("Saving PNGs to #{opts[:dir]}/#{opts[:prefix]}*") do |bar|
|
||||||
require 'ruby-progressbar'
|
opts[:range].each do |i|
|
||||||
bar = ProgressBar.create(title: "Saving to PNGs",
|
@cards[i].save_png(i, opts[:dir], opts[:prefix])
|
||||||
starting_at: 0, total: opts[:range].size,
|
bar.increment
|
||||||
format: '%t <%B> %p%% %a')
|
end
|
||||||
end
|
|
||||||
opts[:range].each do |i|
|
|
||||||
@cards[i].save_png(i, opts[:dir], opts[:prefix])
|
|
||||||
bar.increment if opts[:progress_bar]
|
|
||||||
end
|
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.
|
# 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
|
# Setting a hint to nil or to :off will disable hints. @see samples/text.rb
|
||||||
# @example
|
# @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 [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
|
# @return [nil] Returns nothing
|
||||||
# @api public
|
# @api public
|
||||||
def hint(text: nil)
|
def hint(text: nil, progress_bar: false)
|
||||||
text = nil if text == :off
|
text = nil if text == :off
|
||||||
@text_hint = text
|
@text_hint = text
|
||||||
|
@progress_bar.enabled = progress_bar
|
||||||
end
|
end
|
||||||
|
|
||||||
# Sets various defaults for this deck. Defaults can be overriden by the commands themselves
|
# Sets various defaults for this deck. Defaults can be overriden by the commands themselves
|
||||||
|
|
|
||||||
|
|
@ -38,4 +38,13 @@ module Squib
|
||||||
:progress_bar => false
|
:progress_bar => false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Squib's configuration defaults
|
||||||
|
#
|
||||||
|
# @api public
|
||||||
|
CONFIG_DEFAULTS = {
|
||||||
|
'dpi' => 300,
|
||||||
|
'progress_bar' => false,
|
||||||
|
'hint' => nil
|
||||||
|
}
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
require 'yaml'
|
require 'yaml'
|
||||||
require 'pp'
|
require 'pp'
|
||||||
require 'squib/card'
|
require 'squib/card'
|
||||||
|
require 'squib/progress'
|
||||||
require 'squib/input_helpers'
|
require 'squib/input_helpers'
|
||||||
require 'squib/constants'
|
require 'squib/constants'
|
||||||
|
|
||||||
|
|
@ -51,6 +52,7 @@ module Squib
|
||||||
@dpi = dpi
|
@dpi = dpi
|
||||||
@font = Squib::SYSTEM_DEFAULTS[:default_font]
|
@font = Squib::SYSTEM_DEFAULTS[:default_font]
|
||||||
@cards = []
|
@cards = []
|
||||||
|
@progress_bar = Squib::Progress.new(false)
|
||||||
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
cards.times{ @cards << Squib::Card.new(self, width, height) }
|
||||||
load_config(config)
|
load_config(config)
|
||||||
load_layout(layout)
|
load_layout(layout)
|
||||||
|
|
@ -73,13 +75,19 @@ module Squib
|
||||||
@cards.each { |card| block.call(card) }
|
@cards.each { |card| block.call(card) }
|
||||||
end
|
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
|
# Load the configuration file, if exists, overriding hardcoded defaults
|
||||||
# @api private
|
# @api private
|
||||||
def load_config(file)
|
def load_config(file)
|
||||||
if File.exists? file
|
if File.exists?(file) && config = YAML.load_file(file)
|
||||||
if config = YAML.load_file(file)
|
config = Squib::CONFIG_DEFAULTS.merge(config)
|
||||||
@dpi = config['dpi'].to_i
|
@dpi = config['dpi'].to_i
|
||||||
end
|
@hint = config['hint']
|
||||||
|
@progress_bar.enabled = config['progress_bar']
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,20 +18,24 @@ module Squib
|
||||||
width = 11 * @dpi ; height = 8.5 * @dpi #TODO: allow this to be specified too
|
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))
|
cc = Cairo::Context.new(Cairo::PDFSurface.new("#{p[:dir]}/#{p[:file]}", width, height))
|
||||||
x = p[:margin] ; y = p[:margin]
|
x = p[:margin] ; y = p[:margin]
|
||||||
@cards.each_with_index do |card, i|
|
|
||||||
surface = trim(card.cairo_surface, p[:trim], @width, @height)
|
@progress_bar.start("Saving PDF to #{p[:dir]}/#{p[:file]}", p[:range].size) do |bar|
|
||||||
cc.set_source(surface, x, y)
|
@cards.each_with_index do |card, i|
|
||||||
cc.paint
|
surface = trim(card.cairo_surface, p[:trim], @width, @height)
|
||||||
x += surface.width + p[:gap]
|
cc.set_source(surface, x, y)
|
||||||
if x > (width - surface.width - p[:margin])
|
cc.paint
|
||||||
x = p[:margin]
|
bar.increment
|
||||||
y += surface.height + p[:gap]
|
x += surface.width + p[:gap]
|
||||||
if y > (height - surface.height - p[:margin])
|
if x > (width - surface.width - p[:margin])
|
||||||
x = p[:margin] ; y = p[:margin]
|
x = p[:margin]
|
||||||
cc.show_page #next page
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# :nodoc:
|
# :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
|
dpi: 300
|
||||||
|
progress_bar: true
|
||||||
|
hint: '#FF0000'
|
||||||
|
|
@ -3,4 +3,14 @@ require 'squib'
|
||||||
|
|
||||||
Squib::Deck.new(config: 'custom-config.yml') do
|
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
|
end
|
||||||
Loading…
Reference in New Issue