[#] Terminalowy znacznik postępu

( Ostatnio zmieniony wt., 17/06/2008 - 14:32 )
 

C

/* Program to draw progress indicators on screen */
/* USAGE: p_ind [0|1|2|3|4] where 0,1,2,3,4 correspond   */
/* to defines below.                             */
/* Call from scripts thusly...
    #================================================================
        #!/bin/bash

        # the trap is incase we press ^C during
        # p_ind's execution (otherwise it'd be a runaway process),
        # and 2> /dev/null so we don't see error if it's already killed
        trap 'kill $BG_PID 2> /dev/null' 1 2 3 15

        echo "Some time consuming task....."
        p_ind 3 &
        BG_PID=$!

    # ENTER TIME CONSUMING CODE HERE
        sleep 20

        # incase it's already killed, 2> /dev/null
        kill $BG_PID 2> /dev/null
        echo -e "\nAll Done"
        exit 0
        #================================================================

        (C) 2004 Kevin Waldron
        Released under the terms of the GNU General
        Public Licence
        http://www.zazzybob.com
*/

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#define DOT_TYPE 0
#define LINE_TYPE 1
#define STAR_TYPE 2
#define ROTATE 3
#define ROTATE_2 4

#ifndef TRUE
#define TRUE 1
#endif

void draw_dots(void);
void draw_line(void);
void draw_stars(void);
void draw_rotate(void);
void draw_rotate2(void);

int main( int argc, char *argv[])
{
  extern int errno;
  void (*pDrawFunc)(void);
  int type, i;

  if ( argc != 2 ) {
     fprintf( stderr, "USAGE: %s type\n", argv[0] );
     exit( EXIT_FAILURE );
  }

  /* check that all chars in argv[1] are digits */
  for ( i = 0; i < strlen(argv[1]); i++ )
  {
     if ( !(isdigit( argv[1][i] ) ) ) {
         fprintf( stderr, "%s: Error in argument\n", argv[0] );
         exit( EXIT_FAILURE );
     }
  }

  type = atoi( argv[1] );

  switch ( type )
  {
     case DOT_TYPE:
        pDrawFunc = draw_dots;
        pDrawFunc();
        break;
     case LINE_TYPE:
        pDrawFunc = draw_line;
        pDrawFunc();
        break;
     case STAR_TYPE:
        pDrawFunc = draw_stars;
        pDrawFunc();
        break;
     case ROTATE:
        pDrawFunc = draw_rotate;
        pDrawFunc();
        break;
     case ROTATE_2:
        pDrawFunc = draw_rotate2;
        pDrawFunc();
        break;
     default:
        fprintf( stderr, "%s: Type %d not supported\n",
                 argv[0], type );
        exit( EXIT_FAILURE );
        break;
  }

  return 0;
}

void draw_dots(void)
{
   while ( TRUE ) {
      printf( "." );
      fflush(stdout);
      sleep(1);
   }
}

void draw_line(void)
{
   while ( TRUE ) {
      printf( "-" );
      fflush(stdout);
      sleep(1);
   }
}

void draw_stars(void)
{
   while ( TRUE ) {
      printf( "*" );
      fflush(stdout);
      sleep(1);
   }
}

void draw_rotate(void)
{
   int count = 0;
   while ( TRUE ) {
      switch(count++) {
         case 0:
            printf( "\b-" );
            fflush(stdout);
            sleep(1);
            break;
         case 1:
            printf( "\b\\" );
            fflush(stdout);
            sleep(1);
            break;
         case 2:
            printf( "\b|" );
            fflush(stdout);
            sleep(1);
            break;
         case 3:
            printf( "\b/" );
            fflush(stdout);
            sleep(1);
            break;
         default:
            count = 0;
            break;
      }
   }
}

void draw_rotate2(void)
{
   int count = 0;
   while ( TRUE ) {
      switch(count++) {
         case 0:
            printf( "\b\b- " );
            fflush(stdout);
            sleep(1);
            break;
         case 1:
            printf( "\b\b\\ " );
            fflush(stdout);
            sleep(1);
            break;
         case 2:
            printf( "\b\b| " );
            fflush(stdout);
            sleep(1);
            break;
         case 3:
            printf( "\b\b/ " );
            fflush(stdout);
            sleep(1);
            break;
         default:
            count = 0;
            break;
      }
   }
}

SH

#!/bin/bash
#
# anim.sh
# print a progress wheel while test_pad returns 0
#
# $ANIM  : wheel
# $PAD   : on what $ANIM moves
# $SLEEP : timer
#
# Author : Nicolas Marciniak <nmarciniak@gmail.com>

function test_pad() {
  return 0
}

function reverse() {
  string=$1
  len=$(echo -n $string | wc -c)
  while test $len -gt 0
  do
    rev=$rev$(echo $string | cut -c $len)
    len=$(( len - 1 ))
  done
  echo $rev
}

ANIM="//--\\||"
PAD="                                                                  "
SLEEP="0.1"

ANIM_L="$(reverse $ANIM)"
PAD_LN=${#PAD}
ANIM_LN=${#ANIM}
u=0; re=0; l=0

while test_pad; do
  if [ $re -eq 1 ]; then L=$ANIM_L; else L=$ANIM; fi
  if [ $re -eq 1 ]; then p=$(( $PAD_LN - $u )); else p=$u; fi
  echo -ne "${PAD:0:$p} ${L:$l:1} ${PAD:$p:$PAD_LN} \r"
  if [ $u -eq $PAD_LN ]; then u=0; if [ $re -eq 1 ]; then re=0; else re=1; fi; fi
  u=$(( u + 1 )); l=$(( l + 1 ))
  if [ $l -eq $ANIM_LN ]; then l=0; fi
  sleep $SLEEP
done
exit 0
Twoja ocena: Brak Średnio: 4 (2 głosy)

Dodaj nową odpowiedź

Informacja:

Osoby zamieszczające wypowiedzi naruszające prawo lub prawem chronione dobra osób trzecich, mogą ponieść z tego tytułu odpowiedzialność karną lub cywilną. Upewnij się, że twoja wypowiedź nie godzi w niczyje mienie.

  • Internal paths in single or double quotes, written as "internal:node/99", for example, are replaced with the appropriate absolute URL or path. Paths to files in single or double quotes, written as "files:somefile.ext", for example, are replaced with the appropriate URL that can be used to download the file.
  • Adresy internetowe są automatycznie zamieniane w odnośniki, które można kliknąć.
  • Dozwolone znaczniki HTML: <strong> <blockquote> <code>
  • Znaki końca linii i akapitu dodawane są automatycznie.

Więcej informacji na temat formatowania

Łapirobot
Proszę odpowiedzieć
8
4
M
7
Y
Q
s
1
U
S
Enter the code without spaces and pay attention to upper/lower case.