CLImagic Supplemental Sample Post

Below is a sample of a part of the weekly CLImagic Supplemental series. Each weekly post contains analysis of many of the week's posts. Look for the support tiers containing the benefit "CLImagic Supplemental". Thanks.


March 17, 2:05pm - Twitter Mastodon

a=(22 28 34 40 46 47 48 49 );c=0;w=0;t=0;while :;do printf "\e[0;0H";while [[ $t -le $LINES ]];do for i in $(seq -s' ' 0 ${#a[*]});do v=${a[$(((i+w+c-1)%(${#a[*]}+1)))]};printf "\e[48;5;${v}m\n";t=$[t+1];done;w=$[w+1];done;t=0;w=0;c=$[c+1];sleep 0.06;done #HappyStPatricksDay


Whoa, went a little overboard on the pine scent air freshener there didn't you.  

Shameless disclosure, I didn't write this one on the command line. I wrote it in a  script and then condensed it to fit into 280 characters for a tweet. While it's similar to the previous tweet, I got the idea after @kabal089 said "Reminds me of A500 cracktros", which you can see at the beginning of this video. Those of you born after 1990 probably have no idea, but cracking teams that would create pirated versions of games on the Amiga, C64 and other systems would expend significant effort in creating intro demos as a flag for their cracking team. Sometimes the intros and custom music were more impressive than the actual game. 

a=(22 28 34 40 46 47 48 49 )

First we create an array to hold the color sequences that we'll use. ANSI color space can be a bit hard to work with, but essentially colors of a similar shade are 6 off from each other.

c=0;w=0;t=0;

Next initialize some counting variables

The "c" variable holds a count of the iterations.

"w" holds the wave number in this iteration

"t" is the total lines so far drawn for the current iteration

while :;do 

And then start an infinite loop. The ":" there is actually a command that is a bash internal. It does nothing successfully, and save 3 characters in a tweet. For better readablility you should use the "true" command instead.

printf "\e[0;0H";

Send the cursor to position 0,0 in the terminal with this printf statement. This is another ANSI escape sequence. Unlike the one in the rainbow command above, \e is used here as that is a sequence that printf recognizes for the escape character.  The 0;0 coordinates represent the ROW (or LINE) and COLUMN number in the terminal window, they don't represent X and Y as I've seen some documentation erroneously indicate.

while [[ $t -le $LINES ]];do 

Now we write color sequences until we reach the bottom of the terminal. $LINES is a useful bashism, but you should probably use the tput command instead.

for i in $(seq -s' ' 0 ${#a[*]});do 

This for loop iterates through the array of ANSI colors. It is passed a list of indexes to use for the "a" array from 0 to the count of array elements $(#a{ This was a silly way to do it. I should have used ${!a[*]} which just gives you the indices of an array in bash.

v=${a[$(((i+w+c-1)%(${#a[*]}+1)))]};

Now determine the color value to use for this row. Each "wave" would basically start in the next index of the array and then wrap around at the end of the array. The % operation is the modulo operator.

printf"\e[48;5;${v}m\n";

Print the ANSI sequence to create the background color for the whole line. Again, setting up 256-color mode using 48;5 and ${v} for the color value.

t=$[t+1];done;w=$[w+1];done;t=0;w=0;c=$[c+1];

Increment or reset the variables as needed.

sleep 0.06;done

Wait 0.06 seconds between redraws of the terminal. This is about 15 frames per second.

By becoming a member, you'll instantly unlock access to 54 exclusive posts
4
Images
30
Links
2
Polls
16
Writings
2
Videos
By becoming a member, you'll instantly unlock access to 54 exclusive posts
4
Images
30
Links
2
Polls
16
Writings
2
Videos