免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
1234
最近访问板块 发新帖
楼主: iamlimeng
打印 上一主题 下一主题

几种常用的显示程序运行进度的小方法 [复制链接]

论坛徽章:
0
31 [报告]
发表于 2012-03-29 07:35 |只看该作者
学习学习..很好喔

论坛徽章:
0
32 [报告]
发表于 2012-04-07 18:24 |只看该作者
吵架也有这么高的水平

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
33 [报告]
发表于 2012-04-08 22:33 |只看该作者
学习学习

论坛徽章:
7
戌狗
日期:2013-12-15 20:43:38技术图书徽章
日期:2014-03-05 01:33:12技术图书徽章
日期:2014-03-15 20:31:17未羊
日期:2014-03-25 23:48:20丑牛
日期:2014-04-07 22:37:44巳蛇
日期:2014-04-11 21:58:0915-16赛季CBA联赛之青岛
日期:2016-03-17 20:36:13
34 [报告]
发表于 2013-05-09 14:00 |只看该作者
太感谢 iamlimeng 了!
贴个:
  1. #!/usr/bin/perl
  2. sub pbar {
  3.     my ( $i, $length, $total, $type ) = ( 1, @_ );
  4.     $type = '>' if !$type or length $type != 1;
  5.     sub {
  6.         my $now = int $length * $i / $total;
  7.         print "\r[", $type x $now, $" x ( $length - $now ), "] ",
  8.           sprintf( "%3d", int 100 * $i / $total ), '%',
  9.           $i++ == $total ? "\tDONE!" : "\t";
  10.       }
  11. }

  12. my @a   = 1 .. 100;
  13. my $all = scalar @a;
  14. my $bar = pbar( 36, $all, '#' );
  15. for (@a) {
  16.     $bar->();
  17.     my $r = (rand) / 10;
  18.     `sleep $r`;
  19. }
复制代码

论坛徽章:
0
35 [报告]
发表于 2013-05-09 21:49 |只看该作者
收藏了,谢谢

论坛徽章:
0
36 [报告]
发表于 2013-07-25 16:46 |只看该作者
请问一下显示进度的脚本 如何和 程序运行 进度匹配的?

论坛徽章:
3
未羊
日期:2013-11-18 15:17:06酉鸡
日期:2013-12-06 17:07:16天蝎座
日期:2014-06-11 12:37:07
37 [报告]
发表于 2013-07-25 16:50 |只看该作者
有意思。。

论坛徽章:
3
摩羯座
日期:2013-09-04 12:01:36申猴
日期:2013-10-23 12:12:23CU十二周年纪念徽章
日期:2013-10-24 15:41:34
38 [报告]
发表于 2013-07-25 22:07 |只看该作者
这样也行哈哈! 启发啊,我把我的脚本改成了时钟 哈哈!
  1. use strict;
  2. use AnyEvent;

  3. my $cv = AnyEvent->condvar;
  4. $| = 1;
  5. my $w; $w = AnyEvent->timer(
  6.         after        => 1,
  7.         interval        => 1,
  8.         cb => sub {
  9.                 my $timeshow;
  10.                 $timeshow = &nowtime("1","1","1","1","1","1","1");
  11.                 print "\b"x length($timeshow)."$timeshow";
  12.                 $timeshow;

  13.         }
  14. );  
  15. $cv->recv;


  16. sub nowtime
  17. {       
  18.         my @need_return = @_;
  19.         my %month = (
  20.                 "Jan" => "01","Feb" => "02","Mar" => "03","Apr" => "04",
  21.                 "May" => "05","Jun" => "06","Jul" => "07","Aug" => "08",
  22.                 "Sep" => "09","Oct" => "10","Nov" => "11","Dec" => "12",
  23.         );
  24.        
  25.         my $local_time = localtime();
  26.         my @date = split /\s+/,$local_time;
  27.         $date[2] = sprintf "%02d",$date[2];
  28.         my @time = split /:/,$date[3];
  29.         my @now_time = ($date[4],$month{$date[1]},$date[2],$time[0],$time[1],$time[2],$date[0]);
  30.        
  31.         my $i = 0;
  32.         for(@need_return){
  33.                 if( $_ eq "" ){
  34.                         $i++;
  35.                         return 0 if( $i == 7 );
  36.                         next;       
  37.                 }
  38.                 if( $_ == 1 ){
  39.                         #有一个参数是 1 说明要返回具体时间
  40.                         my $return_time;
  41.                         $return_time = $return_time . $date[4] if ( $need_return[0] ne "" );                        #年
  42.                         $return_time = $return_time . "-" if ( $need_return[0] ne "" && $need_return[1] ne "");        #-
  43.                         $return_time = $return_time . $month{$date[1]} if ( $need_return[1] ne "" );        #月
  44.                         $return_time = $return_time . "-" if ( $need_return[1] ne "" && $need_return[2] ne "");        #-
  45.                         $return_time = $return_time . $date[2] if ( $need_return[2] ne "" );                        #日
  46.                         $return_time = $return_time . " " if ( $need_return[2] ne "" && $need_return[3] ne "");        #空格
  47.                         $return_time = $return_time . $time[0] if ( $need_return[3] ne "" );                        #时
  48.                         $return_time = $return_time . ":" if ( $need_return[3] ne "" && $need_return[4] ne "");        #:
  49.                         $return_time = $return_time . $time[1] if ( $need_return[4] ne "" );                        #分
  50.                         $return_time = $return_time . ":" if ( $need_return[4] ne "" && $need_return[5] ne "");        #:
  51.                         $return_time = $return_time . $time[2] if ( $need_return[5] ne "" );                        #秒
  52.                         $return_time = $return_time . " " if ( $need_return[5] ne "" && $need_return[6] ne "");        #空格
  53.                         $return_time = $return_time . $date[0] if ( $need_return[6] ne "" );                        #周
  54.                         return $return_time;
  55.                 }else{
  56.                         #否则比对时间
  57.                         my $n = 0;
  58.                         for(@need_return){
  59.                                 if( $_ ne "" ){
  60.                                         return 0 if( $_ ne $now_time[$n]);
  61.                                 }
  62.                                 $n++;
  63.                         }
  64.                         return 1;
  65.                 }
  66.         }
  67. }
复制代码

论坛徽章:
0
39 [报告]
发表于 2013-07-26 22:36 |只看该作者
回复 1# iamlimeng


    学到了很多, 非常感谢. 其实最重要的是学习了"\r"的用法. 原来真的不了解这个转义符的意思. 向楼主学习了.

论坛徽章:
0
40 [报告]
发表于 2013-07-30 10:18 |只看该作者
好强的方法,咋收藏啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP