<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: progress code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 12 Oct 2008 09:42:27 GMT</pubDate>
    <description>DZone Snippets: progress code</description>
    <item>
      <title>Command-line progress indicators</title>
      <link>http://snippets.dzone.com/posts/show/3760</link>
      <description>... just some proof-of-concept snippets ... &lt;br /&gt;&lt;br /&gt;inspired by:  http://www.ruby-forum.com/topic/87404&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;# move cursor to beginning of line&lt;br /&gt;cr = "\r"           &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;# ANSI escape code to clear line from cursor to end of line&lt;br /&gt;# "\e" is an alternative to "\033"&lt;br /&gt;# cf. http://en.wikipedia.org/wiki/ANSI_escape_code&lt;br /&gt;&lt;br /&gt;clear = "\e[0K"     &lt;br /&gt;&lt;br /&gt;# reset lines&lt;br /&gt;reset = cr + clear&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#-------------------------------- Example 1 --------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(1..100).each do |i| &lt;br /&gt;  print "#{reset}#{i}%"&lt;br /&gt;  sleep(0.08)&lt;br /&gt;  $stdout.flush&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;print "#{reset}"     # clear current line&lt;br /&gt;&lt;br /&gt;$stdout.flush&lt;br /&gt;puts "done"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#-------------------------------- Example 2 --------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;chars = [ "|", "/", "-", "\\" ]&lt;br /&gt;&lt;br /&gt;# 7 turns on reverse video mode, 31 red , ...&lt;br /&gt;n = 31&lt;br /&gt;&lt;br /&gt;str = "#{reset}\e[#{n};1m"   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(1..100).each do |i| &lt;br /&gt;&lt;br /&gt;   case i&lt;br /&gt;      when   0..10    then print "#{str}#{chars[0]}"&lt;br /&gt;      when  10..20    then print "#{str}#{chars[1]}"&lt;br /&gt;      when  20..30    then print "#{str}#{chars[2]}"&lt;br /&gt;      when  30..40    then print "#{str}#{chars[3]}"&lt;br /&gt;      when  40..50    then print "#{str}#{chars[0]}"&lt;br /&gt;      when  50..60    then print "#{str}#{chars[1]}"&lt;br /&gt;      when  60..70    then print "#{str}#{chars[2]}"&lt;br /&gt;      when  70..80    then print "#{str}#{chars[3]}"&lt;br /&gt;      when  80..90    then print "#{str}#{chars[0]}"&lt;br /&gt;      when  90..100   then print "#{str}#{chars[1]}"&lt;br /&gt;   end&lt;br /&gt;&lt;br /&gt;   sleep(0.1)&lt;br /&gt;   $stdout.flush&lt;br /&gt;&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;print "\e[0m"&lt;br /&gt;print "#{reset}" &lt;br /&gt;&lt;br /&gt;$stdout.flush&lt;br /&gt;puts "done"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;#-------------------------------- Example 3 --------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MAX = 80 &lt;br /&gt;&lt;br /&gt;$stdout.sync = true     # alternative to $stdout.flush below&lt;br /&gt;&lt;br /&gt;10.times do&lt;br /&gt;   foo_string = Time.now.to_s&lt;br /&gt;   s = foo_string[0..MAX].center(MAX)   # or rjust or ljust &lt;br /&gt;   print cr + s &lt;br /&gt;   #$stdout.flush &lt;br /&gt;   sleep(1.1)&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;print "\e[0m" &lt;br /&gt;print "#{reset}" &lt;br /&gt;&lt;br /&gt;$stdout.flush&lt;br /&gt;puts "done"&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 04 Apr 2007 18:22:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3760</guid>
      <author>ntk ()</author>
    </item>
    <item>
      <title>Progress Indicator Helper</title>
      <link>http://snippets.dzone.com/posts/show/3520</link>
      <description>Use the optional options param to pass custom show or hide functions. Provides functions show/hide.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function ProgressIndicator(element, options) {&lt;br /&gt;        var element = $(element);&lt;br /&gt;        var my_options = {show:Element.show, hide:Element.hide};&lt;br /&gt;        Object.extend(my_options, options || {});&lt;br /&gt;        this.show = function() { my_options.show(element) }&lt;br /&gt;        this.hide = function() { my_options.hide(element) }&lt;br /&gt;        this.hide();&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;code&gt;&lt;br /&gt;var p = new ProgressIndicator($("my_element"));&lt;br /&gt;p.show();&lt;br /&gt;&lt;br /&gt;var q = new ProgressIndicator("my_other_element",&lt;br /&gt;                             {show: Effect.Appear, &lt;br /&gt;                              hide:Effect.Fade});&lt;br /&gt;q.show();&lt;br /&gt;$("my_other_element").onclick = q.hide;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Requires prototype for a few things.</description>
      <pubDate>Wed, 14 Feb 2007 03:17:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3520</guid>
      <author>burnto (Brent Fitzgerald)</author>
    </item>
  </channel>
</rss>
