Skip to content

Instantly share code, notes, and snippets.

@Hunam6
Created January 1, 2021 17:55
Show Gist options
  • Save Hunam6/abde35aca039ebc1c603546136e35795 to your computer and use it in GitHub Desktop.
Save Hunam6/abde35aca039ebc1c603546136e35795 to your computer and use it in GitHub Desktop.
import term
import rand
import os
fn main() {
mut height := 0
for {
height = os.input('Enter the christmas tree height (an integer greater than 4): ').int()
if height > 4 {
break
}
}
text := os.input('Enter a cool tagline: ')
christmas_tree(height, text)
keep_open()
}
fn keep_open() ? {
println('\n')
os.input(term.gray('> Press ${term.italic('enter')} to exit'))
}
fn christmas_tree(height int, tagline string) ? {
// Make the text uppercase
mut text := tagline.to_upper()
// Make the leaves
mut space := height
mut tree := 1
for h := 0; h < height; h++ {
for i := 0; i < space; i++ {
print(' ')
}
for i := 0; i < tree; i++ {
if tree == 1 {
print(term.bright_red('*'))
} else {
match rand.int_in_range(0, 20) {
0 { print(term.cyan('&')) }
1 { print(term.bright_red('+')) }
2 { print(term.bright_yellow('o')) }
3 { print(term.bright_magenta('@')) }
else { print(term.bright_green('^')) }
}
}
}
space--
tree += 2
println('')
}
// Make the leaves bottom
for i := 0; i < tree; i++ {
print(term.bright_blue('#'))
}
// Make the trunk
for t := 0; t < 2; t++ {
println('')
for i := 0; i < tree / 2 - 1; i++ {
print(' ')
}
print(term.bright_magenta('III'))
}
println('')
// Make the first separation
for i := 0; i < tree; i++ {
print(term.bright_magenta('~'))
}
//Check for an empty tagline
if text.len > 0 {
// Prepare the text
println('')
mut msg := tree
mut message := []string{}
for i := 0; i <= tree; i++ {
if text.len > tree {
if text[msg].str() == ' ' {
message << text[..msg]
text = text[msg + 1..]
i = 0
} else {
msg--
}
} else {
message << text
break
}
}
// Show the text
for l := 0; l < message.len; l++ {
for i := 0; i < (tree - message[l].len) / 2; i++ {
print(' ')
}
println(term.bright_red(message[l]))
}
// Make the second separation
for i := 0; i < tree; i++ {
print(term.bright_red('~'))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment