MacroMan
MacroMan

Reputation: 2458

How to print carriage return (\r) only on OSX

I have a script (it's php actually, but the concept is the same as bash) that prints a progress bar in the terminal. I use carriage return \r to put the carriage back at the beginning of a line.

Unfortunately, printing \r on a OSX produces a newline.

Is there any other character or simple way around just moving the carriage on OSX in terminal?

Upvotes: 0

Views: 1870

Answers (2)

user4453924
user4453924

Reputation:

Not sure of all the options for tput on OSX but this may work

Example

while [[ x -lt 100 ]];do
   ((x+=10))
   tput sc
   echo -n $x%
   sleep 1
   tput el1
   tput rc
done

Explanation

tput sc 

Save the cursor position

tput el1

Clears line to the left

tput rc

Returns cursor position

Upvotes: 1

anishsane
anishsane

Reputation: 20980

You can perhaps use ANSI escape characters for cursor movement.

printf $'\033[s'
progress=0
print_progress() { printf "%#$(($1))s" " " | tr ' ' '#' ; }
while [ $progress -lt 100 ]; do 
    print_progress $progress
    printf $'\033[u'
    sleep 0.1
    ((progress++))
done
echo

Upvotes: 1

Related Questions