bashtput

Bash function to draw array of coordinates


With a given array of underscore separated coordinates (like so: 5_2 4_5 1_3), I need a fast bash function to draw a block character at those locations on the terminal screen. Right now I have this:

function draw() {
    clear
    for i in $(echo $@); do
        y=$(echo $i | cut -d '_' -f1)
        x=$(echo $i | cut -d '_' -f2)
        tput cup $x $y && printf "█"
    done
}

This functions porperly, however it is rather slow - it takes 0.158s to execute it with 8 coordinates. Is there a better and faster way to do this?


Solution

  • I don't know that this is really a great idea, but this refactor runs about twice as fast on my box:

    draw() {
        clear
        for i; do
            y=${i%_*}
            x=${i#*_}
            tput cup $x $y && printf "█"
        done
    }