#!/bin/sh

cat > loop.py <<EOF
from mercurial import commands

def loop(ui, loops, **opts):
    loops = int(loops)
    total = None
    if loops >= 0:
        total = loops
    loops = abs(loops)

    for i in range(loops):
        ui.progress('loop', i, 'loop.%d' % i, 'loopnum', total)
    ui.progress('loop', None, 'loop.done', 'loopnum', total)

commands.norepo += " loop"

cmdtable = {
    "loop": (loop, [], 'hg loop LOOPS'),
}
EOF

cat > filtercr.py <<EOF
import sys, re
for line in sys.stdin:
    line = re.sub(r'\r+[^\n]', lambda m: '\n' + m.group()[-1:], line)
    sys.stdout.write(line)
EOF

echo "[extensions]" >> $HGRCPATH
echo "progress=" >> $HGRCPATH
echo "loop=" `pwd`/loop.py >> $HGRCPATH
echo "[ui]" >> $HGRCPATH
echo "interactive=1" >> $HGRCPATH

echo '% test default params, display nothing because of delay'
hg -y loop 3 | python filtercr.py

echo "[progress]" >> $HGRCPATH
echo "delay=0" >> $HGRCPATH
echo "refresh=0" >> $HGRCPATH

echo '% test with delay=0, refresh=0'
hg -y loop 3 | python filtercr.py

echo '% test refresh is taken in account'
hg -y --config progress.refresh=100 loop 3 | python filtercr.py

echo '% test format options 1'
hg -y --config 'progress.format=number topic item+2' loop 2 | python filtercr.py

echo '% test format options 2'
hg -y --config 'progress.format=number item-3 bar' loop 2 | python filtercr.py

echo '% test format options and indeterminate progress'
hg -y --config 'progress.format=number item bar' loop -- -2 | python filtercr.py

echo '% test immediate progress completion'
hg -y loop 0 | python filtercr.py
