Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Command-line progress indicators

... just some proof-of-concept snippets ...

inspired by: http://www.ruby-forum.com/topic/87404



# move cursor to beginning of line
cr = "\r"           


# ANSI escape code to clear line from cursor to end of line
# "\e" is an alternative to "\033"
# cf. http://en.wikipedia.org/wiki/ANSI_escape_code

clear = "\e[0K"     

# reset lines
reset = cr + clear



#-------------------------------- Example 1 --------------------------------


(1..100).each do |i| 
  print "#{reset}#{i}%"
  sleep(0.08)
  $stdout.flush
end

print "#{reset}"     # clear current line

$stdout.flush
puts "done"



#-------------------------------- Example 2 --------------------------------


chars = [ "|", "/", "-", "\\" ]

# 7 turns on reverse video mode, 31 red , ...
n = 31

str = "#{reset}\e[#{n};1m"   


(1..100).each do |i| 

   case i
      when   0..10    then print "#{str}#{chars[0]}"
      when  10..20    then print "#{str}#{chars[1]}"
      when  20..30    then print "#{str}#{chars[2]}"
      when  30..40    then print "#{str}#{chars[3]}"
      when  40..50    then print "#{str}#{chars[0]}"
      when  50..60    then print "#{str}#{chars[1]}"
      when  60..70    then print "#{str}#{chars[2]}"
      when  70..80    then print "#{str}#{chars[3]}"
      when  80..90    then print "#{str}#{chars[0]}"
      when  90..100   then print "#{str}#{chars[1]}"
   end

   sleep(0.1)
   $stdout.flush

end

print "\e[0m"
print "#{reset}" 

$stdout.flush
puts "done"



#-------------------------------- Example 3 --------------------------------


MAX = 80 

$stdout.sync = true     # alternative to $stdout.flush below

10.times do
   foo_string = Time.now.to_s
   s = foo_string[0..MAX].center(MAX)   # or rjust or ljust 
   print cr + s 
   #$stdout.flush 
   sleep(1.1)
end

print "\e[0m" 
print "#{reset}" 

$stdout.flush
puts "done"


Progress Indicator Helper

Use the optional options param to pass custom show or hide functions. Provides functions show/hide.
function ProgressIndicator(element, options) {
        var element = $(element);
        var my_options = {show:Element.show, hide:Element.hide};
        Object.extend(my_options, options || {});
        this.show = function() { my_options.show(element) }
        this.hide = function() { my_options.hide(element) }
        this.hide();
}

Example
var p = new ProgressIndicator($("my_element"));
p.show();

var q = new ProgressIndicator("my_other_element",
                             {show: Effect.Appear, 
                              hide:Effect.Fade});
q.show();
$("my_other_element").onclick = q.hide;


Requires prototype for a few things.
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS