Skip to content

7. Nested loops ​

march with a bar for the time steps and a bar for the iterations of each step

Each time step of march now iterates a Newton method, ten iterations per step: a bar for the steps, a bar for the iterations. The second one goes one line below the first one, with position=1:

f90
call steps_bar%initialize(prefix_string='time step ', bracket_left_string='[', bracket_right_string=']',  &
                          partial_blocks=.true., filled_char_color_fg='cyan', add_progress_count=.true., &
                          width=30, max_value=real(steps, R8P))
call iterations_bar%initialize(prefix_string='  newton  ', bracket_left_string='[', bracket_right_string=']',  &
                               partial_blocks=.true., filled_char_color_fg='yellow', add_progress_count=.true., &
                               width=30, max_value=real(iterations, R8P), position=1)
f90
call steps_bar%start
do step = 1, steps
   call iterations_bar%start
   do iteration = 1, iterations
      call advance
      call iterations_bar%update(current=real(iteration, R8P))
   enddo
   call steps_bar%update(current=real(step, R8P))
enddo
print '(A)', 'march: done'

While running, at the sixth iteration of step 3:

$ march
time step [████████████                  ] 2/5
  newton  [██████████████████            ]  6/10

At the end:

$ march
time step [██████████████████████████████] 5/5
march: done
  • position=n draws the bar n lines below the line of the cursor, and brings the cursor back: the bar at position 0 stays where it is, the others are drawn below it. Give the outer loop position 0, the inner one position 1, the next one 2, ...
  • A bar below the current line is cleared when it completes: the iterations bar disappears at the end of each step, and is drawn again by its next start. The bar at position 0 completes as usual, and ends its line.
  • Each bar keeps its own state (timer, speed, spinner): the inner bar does not disturb the speed or the ETA of the outer one.
  • write messages go above all the bars: call the write of the bar at position 0.

In a log, where there is no line to come back to, only the bar at position 0 is written (see chapter 8).

What you learned

position for a bar per loop level; inner bars are cleared when done. Reference: The bar object.

Next: 8. Batch jobs and logs.

Released under the GPL v3, BSD 2-Clause, BSD 3-Clause or MIT License.