Programming Freaks  | دورات ومقالات برمجيه

Please login or register.

Login with username, password and session length
Advanced search  

News:

Please Read our FAQ

Author Topic: CLI Progress Bar  (Read 530 times)

abom

  • Ruby Fr34k
  • Moderator
  • Just Joined
  • *****
  • Posts: 20
    • View Profile
    • Email
CLI Progress Bar
« on: July 15, 2009, 02:41:20 AM »
السلام عليكم
عبارة عن شريط تقدم لكن لواجه سطر الأوامر

command-line interface

Code: [Select]
# A simple command-line interface progress bar
# Author: Abdelrahman ghanem <abom.jdev@gmail.com>
#
# 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.
#
# 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.

# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

class CLIProgressBar
 
  def initialize(pchar="=")
    @pchar=pchar
    $stdout.sync=true #auto flush
  end
 
  def text=(ptext)
    @text=ptext
  end
 
  def min=(pmin=0)
    @min=pmin
  end
 
  def max=(pmax=100)
    @max=pmax
  end
 
  def value=(pvalue)
    #use \r [return] escape sequence
    ratio=((pvalue.to_f/@max)*55).round
    print "\r#{@text} #{ratio+45}% [#{@pchar*ratio}>#{"\s"*(55-ratio)}]"
    print "\n" if pvalue==@max
  end
 
end

طبعاً غير الرقم 55 براحتك
و لكن لا تنسى تغيير  الرقم 45 ب:
100-55  :D

test:

Code: [Select]
require 'cliprogress'

pb=CLIProgressBar.new
pb.text="Progress"
pb.max=100000
for i in 1..100000
  pb.value=i
end

screenshot:



أى ملاحظات أو استفسار ؟ :)
مرفق
« Last Edit: July 15, 2009, 02:47:02 AM by abom »
Logged

Striky

  • Helping Freak
  • Administrator
  • Posting Freak
  • *****
  • Posts: 252
    • View Profile
    • WWW
    • Email
Re: CLI Progress Bar
« Reply #1 on: July 17, 2009, 01:29:36 AM »
That's cool :)
Logged

Life is just a chance to grow a soul. - A. Powell
Weblog: http://ahmedyoussef.wordpress.com/