Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM alpine:latest
COPY tree-EN.sh /tree-EN.sh
RUN apk add --update ncurses bash
RUN mkdir /langs
COPY langs /langs
COPY tree.sh /tree.sh
RUN apk add --update ncurses grep bash
ENV TERM=xterm-256color
CMD ["bash","/tree-EN.sh"]
CMD ["bash","/tree.sh"]
36 changes: 9 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,26 @@

You know, a Christmas tree on Bash :)

![Screenshot](./screenshot.png?raw=true)
![Video](./assets/output.gif?raw=true)

# Usage

Via cURL:
## Git clone

```sh
git clone https://github.com/sergiolepore/ChristBASHTree.git
cd ./ChristBASHTree
bash tree.sh
```
# English version
curl https://raw.githubusercontent.com/sergiolepore/ChristBASHTree/master/tree-EN.sh | bash

# Spanish version
curl https://raw.githubusercontent.com/sergiolepore/ChristBASHTree/master/tree-ES.sh | bash
```

Via Wget:

```
# English version
wget -qO- https://raw.githubusercontent.com/sergiolepore/ChristBASHTree/master/tree-EN.sh | bash

# Spanish version
wget -qO- https://raw.githubusercontent.com/sergiolepore/ChristBASHTree/master/tree-ES.sh | bash
```
## Docker

Docker:

```
```sh
docker pull sergiolepore/christbashtree:latest
docker run -it sergiolepore/christbashtree:latest
```

Git clone and execute:

```
git clone https://github.com/sergiolepore/ChristBASHTree.git && cd ./ChristBASHTree && bash tree-EN.sh
```

__Enjoy!__
**Enjoy!**

# FAQ

Expand Down
Binary file added assets/output.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions langs/en
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

MERRY_CHRISTMAS='MERRY CHRISTMAS'
AND_LOTS_OF_CODE_IN_NEW_YEAR="And lots of CODE in $(($(date +'%Y')+1))"
CODE='CODE'
5 changes: 5 additions & 0 deletions langs/es
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

MERRY_CHRISTMAS='FELICES FIESTAS'
AND_LOTS_OF_CODE_IN_NEW_YEAR="Y mucho CODIGO en $(($(date +'%Y')+1))"
CODE='CODIGO'
Binary file removed screenshot.png
Binary file not shown.
70 changes: 0 additions & 70 deletions tree-EN.sh

This file was deleted.

70 changes: 0 additions & 70 deletions tree-ES.sh

This file was deleted.

169 changes: 169 additions & 0 deletions tree.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#!/bin/bash

trap onexit 2

START_LINE=2
CROWN_HEIGHT=10
TRUNK_HEIGHT=2
MSG_LANG=en

CENTER=$(($(tput cols)/2))

function main() {

parsed_args=$(getopt -o l:Lh -l language,list-languages,help -n 'tree.sh' -- "$@")
getopt_exit_code=$?

if [ $getopt_exit_code -ne 0 ] ; then
exit 1
fi

eval set -- "$parsed_args"

while :; do
case "$1" in
-h | --help ) show_help; exit 1 ;;
-L | --list-languages ) listlangs; exit ;;
-l | --language ) MSG_LANG="$2"; shift 2 ;;
-- ) shift; break ;;
* ) break ;;
esac
done

validate_language && source ./langs/$MSG_LANG
empty_screen

show_tree_crown
show_tree_trunk
show_messages

animate

}

function listlangs() {
cat << EOF
Code Language
---- --------
es English (default)
en Spanish
EOF
}

function validate_language() {
if [ $(echo -n "$MSG_LANG" | wc -c) -ne 2 ] || [ ! -f "./langs/$MSG_LANG" ] ; then
err "language code $MSG_LANG isnt supported"
fi
}

function empty_screen() {
clear
tput civis
}

function show_tree_crown() {
tput setaf 2
tput bold

for i in $(seq 1 $CROWN_HEIGHT); do
tput cup $((START_LINE + i - 1)) $((CENTER - i + 1))
for j in $(seq 1 $(((i-1)*2 + 1))); do
echo -n '*'
done
done
}

function show_tree_trunk() {
tput sgr0
tput setaf 3

for i in $(seq 1 $TRUNK_HEIGHT); do
tput cup $((START_LINE + CROWN_HEIGHT + i - 1)) $((CENTER - 1))
echo 'mWm'
done
}

function show_messages() {
tput setaf 1
tput bold

mc_length=$(echo -n "$MERRY_CHRISTMAS" | wc -c)
loc_length=$(echo -n "$AND_LOTS_OF_CODE_IN_NEW_YEAR" | wc -c)

tput cup $((START_LINE + CROWN_HEIGHT + TRUNK_HEIGHT)) $((CENTER - mc_length / 2))
echo $MERRY_CHRISTMAS

tput cup $((START_LINE + CROWN_HEIGHT + TRUNK_HEIGHT + 1)) $((CENTER - loc_length / 2))
echo $AND_LOTS_OF_CODE_IN_NEW_YEAR
}

function animate() {

color=0
turn_off=false
code_index=$(echo "$AND_LOTS_OF_CODE_IN_NEW_YEAR" | grep "$CODE" -bo | cut -d ':' -f1)
loc_length=$(echo -n "$AND_LOTS_OF_CODE_IN_NEW_YEAR" | wc -c)
xmem=()
ymem=()

# lights and decorations
while :; do
for i in $(seq 1 36); do

if $turn_off ; then
tput setaf 2
tput bold
tput cup ${ymem[$i]} ${xmem[$i]}
unset xmem[$i]
unset ymem[$i]
echo -n '*'
fi

y=$(((RANDOM % (CROWN_HEIGHT - 1)) + 1))
x=$((((RANDOM % y) + 1) * 2 - 1))

ymem[$i]=$((START_LINE + y))
xmem[$i]=$((CENTER - y + x))

tput setaf $color
tput bold
tput cup ${ymem[$i]} ${xmem[$i]}
echo o

color=$(((color + 1) % 8))
cpos=0

for c in $(echo -n "$CODE" | grep -o .); do
tput cup $((START_LINE + CROWN_HEIGHT + TRUNK_HEIGHT + 1)) $(((CENTER - loc_length / 2) + code_index + cpos))
echo -n $c
cpos=$((cpos + 1))
sleep 0.01
done
done

$turn_off &&
turn_off=false ||
turn_off=true
done
}

function err() {
printf "tree.sh: %s\n" "$@" >&2
exit 1
}

function show_help() {
echo Usage: tree.sh [ -hL ] [ -l language_code ]
echo Options are:
echo ' -l | --language uses the specified language (english default)'
echo ' -L | --list-languages list all supported languages'
echo ' -h | --help display this message and exit'
}

function onexit() {
tput reset
tput cnorm
exit
}

main "$@"