Say I have this basic bash script for installing Vim plugins:
#!/usr/bin/env bash
plugins=(
tpope/vim-endwise
tpope/vim-fugitive
tpope/vim-surround
tpope/vim-unimpaired
)
rm -rf $HOME/.vim/pack/bundle/*
mkdir $HOME/.vim/pack/bundle/start
installplugin() {
plugin=”$(echo “$1" | sed -e ‘s/.*[\/]//’)”
git clone –depth=1 -q https://github.com/$1.git \
$HOME/.vim/pack/bundle/start/$plugin
rm -rf $HOME/.vim/pack/bundle/start/$plugin/.git*
echo $plugin installed!
}
for repo in ${plugins[@]}; do
installplugin “$repo” &
done
wait
It clones each repository in the array plugins to ~/.vim/pack/bundle/start, and does so asynchronously.
Ignoring the async element of the script (the & at the end of installplugin “$repo” &) for the moment, how would I add a . to the end of a line such as Installing $plugin (which would show at the start of the installation process of a given plugin) every second or so, as a form of progress bar, perhaps outputting Done. on the same line after completion of that plugin, then moving on to the next plugin?
Here is my stab at the issue:
#!/usr/bin/env bash
plugins=(
tpope/vim-endwise
tpope/vim-fugitive
tpope/vim-surround
tpope/vim-unimpaired
)
rm -rf $HOME/.vim/pack/bundle/*
mkdir $HOME/.vim/pack/bundle/start
progressbar() {
until [ $installed -eq 1 ]; do
sleep 0.1
echo -n '.'
done
}
installplugin() {
installed=0
echo -n "Installing $plugin"
progressbar &
plugin=”$(echo “$1" | sed -e ‘s/.*[\/]//’)”
git clone –depth=1 -q https://github.com/$1.git \
$HOME/.vim/pack/bundle/start/$plugin
rm -rf $HOME/.vim/pack/bundle/start/$plugin/.git*
installed=1
echo ' Done.'
}
for repo in ${plugins[@]}; do
installplugin “$repo”
done
wait
It does not work, but I do not understand why.
What I assume is a simple thing to solve becomes much more complicated if you remember that the original script was asynchronous, as you would have to remember how many lines up the output of each Installing message is from the bottom, and then update the lines with dots until their respective plugin has been installed. I need to modify the original script to show the basic form of progress bar I described earlier?