|
From: Masatake Y. <je...@gy...> - 2004-05-16 23:12:58
|
> I have some reservations about the patch below. I'm not against giving
> percentage versus lines read in per se. But here it comes with a some
> cost and perhaps reliability. The patch assumes that "wc" and "cat"
> are available commands and are in the path. (With extra work configure
> could check this and hard-code in the paths). I don't belive the rest
> of the debugger makes use of either "cat" or "wc". Perhaps the build
> system does, but I suppose that's okay since it is for developers. I
> don't know, I may be overly fussy here.
We can check the existence of wc at runtime.
if wc -l < /dev/null >/dev/null 2>&1 ; then echo ok; else echo fail; fi
We can use wc when if it exists. cat can be replaced with "<".
> And there's something I find weird about running a "cat | wc -l" just
> to get the total number of lines in a file. Right now, For the simple
> cases where the script is under a 1,000 lines, no extra work is done
> no, wc'ing no cat'ing no line printing. In those cases where one
> needs to print out status because the file is large, running 'cat | wc
> -l' slows things down more.
I should not call "wc -l < $file" when the script is under a 1,000 lines.
If the lines over 1000, calling "wc -l < $file" once is just small overhead
against the total reading time.
BTW, I wrote a progress bar. Newer patch for bashdb will be based on this progress bar.
#!/bin/sh
progress_raw_erase() {
echo -e -n "\r\b"
}
# progress_raw_show
# $1 prefix string
# $2 max length
# $3 current length
progress_raw_show () {
local -i i=0
progress_raw_erase
echo -n "$1: ["
for (( i=0; i < $3 ; i++ )); do
echo -n "="
done
echo -n ">"
for (( i=0; i < $2 - $3 ; i++ )); do
echo -n " "
done
echo -n "]"
}
# progress_show
# $1 prefix string
# $2 max value
# $3 current value
progress_show () {
local title=$1
local -i max_value=$2
local -i current_value=$3
local -i max_length=40
local -i current_length
if (( max_value == 0 )) ; then
# Avoid dividing by 0.
current_length=${max_length}
else
current_length=$(( ${max_length} * ${current_value} / ${max_value} ))
fi
progress_raw_show "$1" ${max_length} ${current_length}
}
# progress_done
# $1 completion message
progress_done() {
progress_raw_erase
echo $1
}
title="Reading a file"
progress_show "$title" 4 1
sleep 1
progress_show "$title" 4 2
sleep 1
progress_show "$title" 4 3
sleep 1
progress_show "$title" 4 4
progress_done "file is loaded."
|