Last active
December 24, 2024 20:53
-
-
Save rimelek/3fe8a0bc8da72903824296835e9ae5dd to your computer and use it in GitHub Desktop.
Shell script for Docker Desktop users to draw a Christmas tree in Docker Desktop using container names on the Containers tab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Video: https://youtu.be/_wMgTx9pRVg | |
set -eu -o pipefail | |
tree_width=40 | |
tree_character="X" | |
tree_trunk_character="M" | |
# console chars | |
empty_character=".." | |
border_character="I" | |
console_width=$(( tree_width + 10 )) | |
console_height="12" | |
console_line_ids=() | |
console_line_number=1 | |
label="hu.rimelek.docker-christmas" | |
function console_render_line() { | |
local line="$1" | |
local delay_ms="${2:-300}" | |
local line_count="${#console_line_ids[@]}" | |
if (( line_count == console_height )); then | |
docker rm -f "${console_line_ids[0]}" | |
console_line_ids=("${console_line_ids[@]:1}") | |
fi | |
line="$(printf "%03d" $console_line_number)${empty_character}$line" | |
console_line_ids+=( | |
"$(docker create \ | |
--name "$line" \ | |
--label hu.rimelek.docker-christmas \ | |
bash)" | |
) | |
(( ++console_line_number )) | |
} | |
function console_padding() { | |
local text="$1" | |
local text_length="${#text}" | |
local align="${2:-right}" | |
local padding_size="$(( console_width - text_length ))" | |
case "$align" in | |
center) | |
local padding_left_size="$(( padding_size / 2 ))" | |
local padding_right_size="$(( console_width - (padding_left_size + text_length) ))" | |
if [[ "$padding_left_size" != "0" ]]; then | |
echo -n "$(printf -- "${empty_character}%.0s" $(seq 1 "$padding_left_size"))" | |
fi | |
echo -n "${text}" | |
if [[ "$padding_right_size" != "0" ]]; then | |
echo -n "$(printf -- "${empty_character}%.0s" $(seq 1 "$padding_right_size"))" | |
fi | |
;; | |
left) | |
if [[ "$padding_size" != "0" ]]; then | |
echo -n "$(printf -- "${empty_character}%.0s" $(seq 1 "$padding_size"))" | |
fi | |
echo -n "${text}" | |
;; | |
*) | |
echo -n "${text}" | |
if [[ "$padding_size" != "0" ]]; then | |
echo -n "$(printf -- "${empty_character}%.0s" $(seq 1 "$padding_size"))" | |
fi | |
;; | |
esac | |
} | |
function text_align() { | |
local text="${1:-}" | |
local align="${2:-left}" | |
local align_map | |
declare -A align_map | |
align_map["left"]=right | |
align_map["center"]=center | |
align_map["right"]=left | |
echo -n "$(console_padding "$text" "${align_map[$align]}")" | |
} | |
function console_writeln() { | |
local text="${1:-}" | |
local align="${2:-left}" | |
console_render_line "${border_character}$(text_align "$text" "$align")${border_character}" | |
} | |
function draw_tree_top() { | |
local number_start=1 | |
local number_end=$(( tree_width / 4 )) | |
local line_width | |
local i | |
i=$number_start | |
while (( i <= number_end )); do | |
line_width=$(( i * 4 )) | |
console_writeln "$(printf -- "${tree_character}%.0s" $(seq 1 $line_width))" "center" | |
sleep 0.2 | |
(( ++i )) | |
done | |
} | |
function draw_tree_bottom() { | |
local number_start=1 | |
local number_end=2 | |
local i | |
i=$number_start | |
while (( i <= number_end )); do | |
console_writeln "$(printf -- "${tree_trunk_character}%.0s" $(seq 1 4))" "center" | |
(( ++i )) | |
sleep 0.2 | |
done | |
} | |
function draw_merry_christmas() { | |
console_writeln "" | |
console_writeln "M_E_R_R_Y__C_H_R_I_S_T_M_A_S" "center" | |
console_writeln "D_O_C_K_E_R__U_S_E_R_S" "center" | |
sleep 1 | |
console_writeln "O_R" "center" | |
sleep 1 | |
console_writeln "J_U_S_T__A__H_A_P_P_Y__D_A_Y" "center" | |
console_writeln "" | |
sleep 3 | |
} | |
for i in $(seq 1 3); do | |
draw_tree_top | |
draw_tree_bottom | |
draw_merry_christmas | |
done | |
console_writeln "" | |
console_writeln "" | |
console_writeln "CONTAINERS__WILL__BE__DELETED" "center" | |
console_writeln "IN__20__SECONDS" "center" | |
sleep 20 | |
docker container ls -a -q --filter label="$label" | xargs -- docker container rm -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment