/*
What's new:
C44 - [Martin Zoubek] first public release
*/

#include <stdio.h>
#include <string.h>
#include "Progress.h"
#include "Types.h"

  uint vProgress_Total;
  uint vProgress_TitleLength,vProgress_CurrentLength;

void Progress_Init(const char *title,uint total){
  if(!title)
    title="";
  printf("%s [                         ]\x08\x08\x08\x08\x08\x08\x08\x08\x08"
    "\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08\x08",
    title);
  fflush(stdout);
  vProgress_TitleLength=strlen(title);
  vProgress_Total=total;
  vProgress_CurrentLength=0;
  }

void Progress_Done(void){
  Progress_Update(vProgress_Total);  //go to the max
  printf("]");
  vProgress_CurrentLength+=vProgress_TitleLength+3;  //clear whole line
  while(vProgress_CurrentLength--)
    printf("\x08 \x08");
  fflush(stdout);
  }

void Progress_Update(uint cur){
  uint x;
  if(cur>vProgress_Total)
    cur=vProgress_Total;
  x=(25.0*cur)/vProgress_Total;
  while(vProgress_CurrentLength<x){
    vProgress_CurrentLength++;
    printf("*");
    fflush(stdout);
    }
  }

void Progress_Restart(uint total){
  while(vProgress_CurrentLength){
    printf("\x08 \x08");
    vProgress_CurrentLength--;
    }
  fflush(stdout);
  vProgress_Total=total;
  }

