



(4 ratings)
I went milling about for source for progress bars that would be suitably embedded within a Ruby on Rails application I'm working on and came up with nada. Sure, sure there are lots of bars that have to do with live uploading and whatnot ; but nothing that grabbed me with the interface I was looking for (call function, return data).
Like cheating on a lover, I went looking into PHP territory and found a really quite wonderful and straightforward example here by Bård (Bob) Johannessen.
It only took about 20 minutes to adapt to a halfway decent ruby class and now I'm rollin'. Here are some output examples:
Here's the source:
#!/bin/env ruby18
# copyright 2005, Jordan Husney <jordan@husney.com>
# (http://jordan.husney.com/main.html)
#
# derived directly from the following source and protected under
# the GNU GENERAL PUBLIC LICENSE, Version 2, June 1991
#
# http://db.org/demo/2003/02/17/progress-bar/?view=source
#
# php+gd dynamic progress bar image script.
# copyright 2003, B. Johannessen <bob@db.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version, and provided that the above
# copyright and permission notice is included with all distributed
# copies of this or derived software.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# A copy of the GNU General Public License is available from the GNU
# website at the following URL: http://www.gnu.org/licenses/gpl.txt
require 'rubygems'
require 'gd2'
class RubyProgressbar
include GD2
IMAGE_DIR = "./images/"
IMAGE_STYLES = [ 'winxp', 'osx', 'led', 'solaris' ]
public
def initialize
@image = nil
end
def create(style, width, completed, total)
# Check arugments:
raise ArgumentError, "style '#{style}' unknown" if
not IMAGE_STYLES.include?(style)
raise ArgumentError, "width must be between 64 and 1280 pixels" if
width < 64 || width > 1280
raise ArgumentError,
"completed must be greater than 0 and less than total" if
completed < 0 || completed > total
raise ArgumentError, "total must be greater than 0" if
total < 1
# Open images according to defined style:
begin
bg = Image.import(IMAGE_DIR + style + '-bg.png')
fill = Image.import(IMAGE_DIR + style + '-fill.png')
bg_cap = Image.import(IMAGE_DIR + style + '-bg-cap.png')
fill_cap = Image.import(IMAGE_DIR + style + '-fill-cap.png')
rescue Exception => e
raise "Unable to load required images for style #{style}!"
end
# calculate the fill width:
fill_width = (((width - bg_cap.width) * completed) / total).round -
fill_cap.width
# create the new image, and copy the fragments into it:
image = Image::TrueColor.new(width, bg.height)
image.copy_from(bg, 0, 0, 0, 0, bg.w, (width - bg_cap.w))
image.copy_from(bg_cap, (width - bg_cap.w), 0, 0, 0, bg_cap.w, bg_cap.h)
image.copy_from(fill, 0, 0, 0, 0, fill_width, fill.h)
image.copy_from(fill_cap, fill_width, 0, 0, 0, fill_cap.w, fill_cap.h)
@image = image
# return success
return true
end
def destroy
@image = nil
end
def gif()
raise Exception, "image not created" if @image.nil?
return @image.gif()
end
def jpeg(compression = nil)
raise Exception, "image not created" if @image.nil?
return @image.jpeg(compression)
end
def png(compression = nil)
raise Exception, "image not created" if @image.nil?
return @image.png(compression)
end
end
It can be used thusly:
require 'ruby_progressbar'
bar = RubyProgressbar.new
# OSX style, 320px wide, 3 of 10 units completed on the bar:
bar.create("osx", 320, 3, 10)
f = File.new("output.png", "w")
f.write(bar.png)
f.close
20 Random Tutorials from the same category :
The Scalability of Ruby
Sorting the addressbook RUBY
Writing iterators In RUBY
What can ruby arrays do?
Difference of Gtk+ and Ruby/Gtk
Iterators In RUBY
Example: Addressbook RUBY
Printing the addressbook RUBY
Arrays In RUBY
Implementing AddressBook In RUBY
Progress Bars with GD2 and Ruby
More features IN RUBY
Hashes In RUBY
Classes and methods in RUBY













