Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add color echo functions to stdlib #308

Merged
merged 17 commits into from
Jul 23, 2024
Merged
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
51 changes: 51 additions & 0 deletions src/std/env.ab
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * from "std/fs"
import * from "std/text"

pub fun get_env_var(var: Text, file: Text = ".env"): Text {
let _var = unsafe $echo "\$\{!var}"$
Expand Down Expand Up @@ -74,3 +75,53 @@ pub fun is_root(): Bool {

return false
}

pub fun printf(format: Text, args: [Text] = [""]): Null {
unsafe ${nameof args}=("{format}" "\$\{{nameof args}[@]}")$
unsafe $printf "\$\{{nameof args}[@]}"$
}

pub fun printf_escape(text: Text): Text {
return unsafe $echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g"$
}

pub fun text_shell(message: Text, style: Num, fg: Num, bg: Num): Text {
return "\x1b[{style};{fg};{bg}m{printf_escape(message)}\x1b[0m"
}

pub fun text_bold(message: Text): Text {
return "\x1b[1m{printf_escape(message)}\x1b[0m"
}

pub fun text_italic(message: Text): Text {
return "\x1b[3m{printf_escape(message)}\x1b[0m"
}

pub fun text_underlined(message: Text): Text {
return "\x1b[4m{printf_escape(message)}\x1b[0m"
}

pub fun color_echo(message: Text, color: Num): Null {
printf("\x1b[{color as Text}m%s\x1b[0m\n", [message])
}

pub fun echo_info(message: Text): Null {
printf("\x1b[1;3;97;44m %s \x1b[0m\n", [message])
}

pub fun echo_success(message: Text): Null {
printf("\x1b[1;3;97;42m %s \x1b[0m\n", [message])
}

pub fun echo_warning(message: Text): Null {
printf("\x1b[1;3;97;43m %s \x1b[0m\n", [message])
}

pub fun error(message: Text, exit_code: Num = 1): Null {
printf("\x1b[1;3;97;41m %s \x1b[0m\n", [message])
if exit_code > 0 : exit(exit_code)
}




4 changes: 4 additions & 0 deletions src/tests/stdlib/color_echo.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
color_echo("Hello Amber!", 33)
}
1 change: 1 addition & 0 deletions src/tests/stdlib/color_echo.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello Amber!
4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_info.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo_info("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/echo_info.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 Hello Amber! 
4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_success.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo_success("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/echo_success.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 Hello Amber! 
4 changes: 4 additions & 0 deletions src/tests/stdlib/echo_warning.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo_warning("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/echo_warning.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 Hello Amber! 
4 changes: 4 additions & 0 deletions src/tests/stdlib/error.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
error("Hello Amber!", 0)
}
1 change: 1 addition & 0 deletions src/tests/stdlib/error.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
 Hello Amber! 
5 changes: 5 additions & 0 deletions src/tests/stdlib/printf.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * from "std/env"

main {
printf("%s,%s!\n", ["Hello", " Amber"])
}
1 change: 1 addition & 0 deletions src/tests/stdlib/printf.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, Amber!
5 changes: 5 additions & 0 deletions src/tests/stdlib/printf_escape.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * from "std/env"

main {
echo printf_escape("Hello \v Amber %T")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/printf_escape.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello \\v Amber %%T
4 changes: 4 additions & 0 deletions src/tests/stdlib/text_bold.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo text_bold("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/text_bold.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\x1b[1mHello Amber!\x1b[0m
4 changes: 4 additions & 0 deletions src/tests/stdlib/text_italic.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo text_italic("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/text_italic.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\x1b[3mHello Amber!\x1b[0m
4 changes: 4 additions & 0 deletions src/tests/stdlib/text_shell.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo text_shell("Hello Amber!", 31, 1, 42)
}
1 change: 1 addition & 0 deletions src/tests/stdlib/text_shell.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\x1b[31;1;42mHello Amber!\x1b[0m
4 changes: 4 additions & 0 deletions src/tests/stdlib/text_underlined.ab
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import * from "std/env"
main {
echo text_underlined("Hello Amber!")
}
1 change: 1 addition & 0 deletions src/tests/stdlib/text_underlined.output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\x1b[4mHello Amber!\x1b[0m
Loading