I always look for interesting codes or scripts that are designed to be screensavers in terminal, or at least possibly to use as one.

For some time, there is one thing simple that I never found a screensaver for it. That is painting blocks into terminal, colorful, rectangular, randomly, block, block, block, …. There might be some randomly printing 2-by-1 spaced tiny blocks, but it’s not as big as I want, too small to be even remotely spectacular.

I want something bigger, so I present you the blocky.sh, a colorfully blocky thingy.

https://bytebucket.org/livibetter/blocky.sh/raw/tip/images/blocky.sh.b750.t0.01.gif

blocky.sh -b 750 -t 0.01

As of the version 0.1.0, the algorithm of the rendering is simple, using recursion:

  1. fill whole screen with random color

  2. divide the area

    At first, there is a chance to end, this is to provide a possibility of bigger blocks. In other way to look at this is an escape chance, so to speak. It escapes if the following it true:

    10000WH SWSH<RANDOM b M

    Where W and H is the width and height of the area, SW and SH is screen width and height, RANDOM is Bash’s $RANDOM, b is the variable of choice, M is the upper bound of random number plus 1, in this case, it’s 32767+1.

    Basically, the left part is the percentage of the area size to the screen size, in integer, 0% to 100% onto the interval [0,10000]. The right part is a random outcome on the interval [0,b-1]. In short, the bigger b, the higher chance to escape, therefore more bigger blocks.

    As long as the width and/or height has enough to be divided, it will recursively call itself with one or two of divided areas after fill the first divided area with random color.

    The division could be either horizontally (width) or vertically (height), equal chance. The first divided area is on the interval [min,min+w], where w is a uniform random number on the interval [0,W-2min], and rest of area goes to the second divided area.

    If either can be divided, then it’s end condition.

As I said, it’s simple, but I am sure it can be done clearer and I can think of some improvements, for example:

  1. More responsive q or any key. At current state, it has to wait for recursions to end, before responding to the inputs.

  2. More responsive to window resize, same as previous point.

  3. Better fill

    You can use -T to get more animated feeling, however, it’s up-to-down filling, although it doesn’t look strange for horizontal division, but it would be nicer to have left-to-right filling.

  4. Better algorithm for more organic look

    Sometimes, the output looks to me a bit too artificial. I am sure there actually some study on this kind of stuff, but this algorithm is all I can think of.

  5. Coloring

    Those coloring problems with neighbors could be something for this, but it could become complex or harder to code since blocky.sh is written in Bash.

    However, coloring without considering neighbors actually produce some shapes other than rectangles.

I can’t say it’s perfect, but it’s the right angle for me.