!character(len = 20) :: a
!character(len=1) :: bar, back
!
!back = char(8)
!bar = '='
!
!! test loop
!imax=30
!i = 1
!call print_bar()
!do i = 1,imax
!    call delete_bar()
!    call print_bar()
!    call sleep(1)
!enddo
!write(6,'(a)') '| done.'
!
!CONTAINS
SUBROUTINE print_bar(i,imax)
    implicit none
    INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(14)  ! this is double precision
    real(DP) :: imax
    integer :: i,k
    character(len=1) :: bar, back
    back = char(8)
    bar = '='
    ! print the percentage and the bar
    write(6,'(2x,1i3,1a1,2x,1a1,256a1)', advance='no') &
              100*int(i)/int(imax),'%','|', (bar, k =1,50*int(i)/int(imax))
    close(6)
    open(6)
END SUBROUTINE print_bar

SUBROUTINE delete_bar(i,imax)
    implicit none
    INTEGER, PARAMETER :: DP=SELECTED_REAL_KIND(14)  ! this is double precision
    real(DP) :: imax
    integer :: i,k
    character(len=1) :: bar, back
    back = char(8)
    bar = '='
    ! delete the bar and the percentage
    write(6,'(256a1)', advance='no') (back, k =1,(50*int(i)/int(imax))+9)
END SUBROUTINE delete_bar