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

Stdlib documentation #339

Merged
merged 29 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
4c6f14b
feat(doc): stdlib first incomplete version
Mte90 Jul 23, 2024
5c3560a
feat(doc): stdlib first incomplete version
Mte90 Jul 23, 2024
c94b242
feat(review): by @CymDeveloppement
Mte90 Jul 23, 2024
51560f2
feat(review): by @CymDeveloppement
Mte90 Jul 23, 2024
0f06bdd
Update src/std/array.ab
Mte90 Jul 23, 2024
24b17dd
Update src/std/text.ab
Mte90 Jul 23, 2024
57aa42a
Update src/std/text.ab
Mte90 Jul 23, 2024
95c4d9d
Update src/std/text.ab
Mte90 Jul 23, 2024
20495c2
Update src/std/env.ab
Mte90 Jul 23, 2024
3a72721
Update src/std/env.ab
Mte90 Jul 23, 2024
d11470f
Update src/std/fs.ab
Mte90 Jul 23, 2024
707f245
Update src/std/fs.ab
Mte90 Jul 23, 2024
8cbc342
Update src/std/http.ab
Mte90 Jul 23, 2024
095984e
Update src/std/text.ab
Mte90 Jul 23, 2024
cfac531
Update src/std/text.ab
Mte90 Jul 23, 2024
b9f1cac
Update src/std/text.ab
Mte90 Jul 23, 2024
6a09b1a
feat(review): by @krosfire
Mte90 Jul 23, 2024
69e3633
feat(review): by @krosfire
Mte90 Jul 23, 2024
c513e03
feat(review): text and remove in_array
Mte90 Jul 24, 2024
267379e
feat(review): by @CymDeveloppement
Mte90 Jul 25, 2024
9cf6922
Merge branch 'master' into stdlib-doc
Mte90 Jul 29, 2024
baa0bde
Merge branch 'master' into stdlib-doc
Mte90 Aug 6, 2024
c623e69
fix(std): remove dependence so we can merge the documentation in the …
Mte90 Aug 30, 2024
67c6f8c
fix(test): wip
Mte90 Aug 30, 2024
34a0852
fix(test): wip
Mte90 Aug 30, 2024
7c2cd27
fix(test): wip
Mte90 Aug 30, 2024
f065caa
fix(test): wip
Mte90 Aug 30, 2024
e3aba47
fix(test): wip
Mte90 Aug 30, 2024
0223e08
Update text.ab
Mte90 Sep 3, 2024
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
4 changes: 4 additions & 0 deletions src/std/array.ab
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Get the index of the first value found in the array specified
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun array_first_index(array, value): Num {
loop index, element in array {
if value as Text == element as Text {
Expand All @@ -7,6 +8,7 @@ pub fun array_first_index(array, value): Num {
return -1
}

/// Get the total of the values found in the array
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun array_search(array, value): [Num] {
let result = [Num]
loop index, element in array {
Expand All @@ -17,11 +19,13 @@ pub fun array_search(array, value): [Num] {
return result
}

/// Check if the value is in the array
pub fun in_array(array, value): Bool {
let result = array_first_index(array, value)
return result >= 0
}

/// Check if the value is in the array
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun includes(arr, value) {
loop v in arr {
if v == value {
Expand Down
28 changes: 24 additions & 4 deletions src/std/env.ab
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * from "std/fs"
import * from "std/text"

/// Get the variable from the environment, otherwise check from the input file
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun get_env_var(var: Text, file: Text = ".env"): Text {
let _var = unsafe $echo "\$\{!var}"$
if _var != "" {
Expand All @@ -15,59 +16,71 @@ pub fun get_env_var(var: Text, file: Text = ".env"): Text {
return ""
}
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

/// Load the env file in the environment
pub fun load_env_file(file: Text = ".env"): Null {
unsafe $export "\$(xargs < {file})" > /dev/null$
}

/// ?
pub fun shell_isset(name: Text): Bool {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
$[[ ! -z \$\{!{nameof name}+z} ]]$ failed {
return false
}
return true
}

/// Set a constant
pub fun shell_constant_set(name: Text, val: Text): Null {
$readonly \${nameof name}="\${nameof val}" 2> /dev/null$?
}

/// Get a constant
pub fun shell_constant_get(name: Text): Text {
return $echo \$\{!{nameof name}}$?
}

/// Set a variable?
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun shell_var_set(name: Text, val: Text): Null {
$export \${nameof name}="\${nameof val}" 2> /dev/null$?
}

/// Get a variable?
pub fun shell_var_get(name: Text): Text {
return $echo \$\{!{nameof name}}$?
}

/// Remove a variable?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds right to me

pub fun shell_unset(name: Text): Null {
$unset {name}$?
}

/// Check if the command exist
pub fun is_command(command: Text): Bool {
$[ -x "\$(command -v {command})" ]$ failed {
return false
}
return true
}

/// Create a prompt and return the value
pub fun input(prompt: Text): Text {
unsafe $printf "\${nameof prompt}"$
unsafe $read$
return "\$REPLY"
}

/// Check if the command passed fails
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun has_failed(command: Text): Bool {
unsafe silent $eval {command}$
return status != 0
}

/// Close the script
pub fun exit(code: Num): Null {
unsafe $exit "{code}"$
}

/// Check if the script is running with a user with root permission
pub fun is_root(): Bool {
if unsafe $id -u$ == "0" {
return true
Expand All @@ -76,52 +89,59 @@ pub fun is_root(): Bool {
return false
}

/// ?
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun printf(format: Text, args: [Text] = [""]): Null {
unsafe ${nameof args}=("{format}" "\$\{{nameof args}[@]}")$
unsafe $printf "\$\{{nameof args}[@]}"$
}

/// ?
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun printf_escape(text: Text): Text {
return unsafe $echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g"$
}

/// Print a string with formatting options
pub fun text_shell(message: Text, style: Num, fg: Num, bg: Num): Text {
return "\x1b[{style};{fg};{bg}m{printf_escape(message)}\x1b[0m"
}
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

/// Print a string as bold
pub fun text_bold(message: Text): Text {
return "\x1b[1m{printf_escape(message)}\x1b[0m"
}
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

/// Print a string as italic
pub fun text_italic(message: Text): Text {
return "\x1b[3m{printf_escape(message)}\x1b[0m"
}
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

/// Print a string as underlined
pub fun text_underlined(message: Text): Text {
return "\x1b[4m{printf_escape(message)}\x1b[0m"
}
Mte90 marked this conversation as resolved.
Show resolved Hide resolved

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

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

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

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

/// Print a string as Error
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
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)
}




15 changes: 13 additions & 2 deletions src/std/fs.ab
Original file line number Diff line number Diff line change
@@ -1,55 +1,66 @@
import * from "std/env"

/// Check if directory exists
pub fun dir_exist(path) {
$[ -d "{path}" ]$ failed {
return false
}
return true
}

/// Check if file exists
pub fun file_exist(path) {
$[ -f "{path}" ]$ failed {
return false
}
return true
}

/// Get the file content
pub fun file_read(path) {
return $< "{path}"$?
}

/// Write the content to the file, doesn't check if the file exist
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun file_write(path, content) {
return $echo "{content}" > "{path}"$?
}

/// Append the content to the file, doesn't check if the file exist
pub fun file_append(path, content) {
return $echo "{content}" >> "{path}"$?
}

/// Create a symbolic link, if the file doens't exist return a boolean and print a message
pub fun create_symbolic_link(origin: Text, destination: Text): Bool {
if file_exist(origin) {
unsafe $ln -s "{origin}" "{destination}"$
return true
}

echo "The file {origin} doesn't exist!"
error("The file {origin} doesn't exist!")
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
return false
}

/// Create a directory with the full path
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun create_dir(path: Text): Null {
if not dir_exist(path) {
unsafe $mkdir -p "{path}"$
}
}

/// Set the file as executable, if the file doesn't exist return a boolean and print a message
pub fun make_executable(path: Text): Bool {
if file_exist(path) {
unsafe $chmod +x "{path}"$
return true
}

echo "The file {path} doesn't exist!"
error("The file {path} doesn't exist!")
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
return false
}

/// Change the owner of the file, if the file doesn't exist return a boolean
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun change_owner(user: Text, path: Text): Bool {
if file_exist(path) or dir_exist(path) {
unsafe $chown -R "{user}" "{path}"$
Expand Down
1 change: 1 addition & 0 deletions src/std/http.ab
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * from "std/env"

/// It will download the file using (if present) in the order: `curl`, `wget`, `aria2c`
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun download(url: Text, path: Text): Bool {
if {
is_command("curl") {
Expand Down
1 change: 1 addition & 0 deletions src/std/math.ab
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/// Sum the array content
#[allow_absurd_cast]
pub fun sum(list: [Num]): Num {
return unsafe $echo "{list}" | awk '\{s=0; for (i=1; i<=NF; i++) s+=\$i; print s}'$ as Num
Expand Down
21 changes: 17 additions & 4 deletions src/std/text.ab
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
/// Replace the content just once
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun replace_once(source, pattern, replacement) {
return unsafe $echo "\$\{source/{pattern}/{replacement}}"$
}

// Replace the content multiple times
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun replace(source, pattern, replacement) {
return unsafe $echo "\$\{source//{pattern}/{replacement}}"$
}

/// Replace with regex using `sed`
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun replace_regex(source: Text, pattern: Text, replacement: Text): Text {
return unsafe $echo "{source}" | sed -e "s/{pattern}/{replacement}/g"$
}


/// Split the string and return an array
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun split(text: Text, delimiter: Text): [Text] {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
let result = [Text]
unsafe $IFS="{delimiter}" read -rd '' -a {nameof result} < <(printf %s "\${nameof text}")$
return result
}

/// Split a string with newlines
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun lines(text: Text): [Text] {
let result = [Text]
unsafe $IFS=\$'\n' read -rd '' -a {nameof result} <<<"\${nameof text}"$
return result
return split(text, "\n")
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Split by space separator
pub fun words(text: Text): [Text] {
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
return split(text, " ")
}

/// Merge a string using the delimeter
pub fun join(list: [Text], delimiter: Text): Text {
return unsafe $IFS="{delimiter}" ; echo "\$\{{nameof list}[*]}"$
}

/// Trim the spaces at top of the string using `sed`
pub fun trim_left(text: Text): Text {
return unsafe $echo "{text}" | sed -e 's/^[[:space:]]*//'$
}

/// Trim the spaces at end of the string using `sed`
pub fun trim_right(text: Text): Text {
return unsafe $echo "{text}" | sed -e 's/[[:space:]]*\$//'$
}

/// Trim the spaces from the string input
pub fun trim(text: Text): Text {
return trim_left(trim_right(text))
}

/// Lowercase the string input using `tr`
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun lower(text: Text): Text {
return unsafe $echo "{text}" | tr '[:upper:]' '[:lower:]'$
}

/// Lowercase the string input using `tr`
pub fun upper(text: Text): Text {
return unsafe $echo "{text}" | tr '[:lower:]' '[:upper:]'$
}

/// ?
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
#[allow_absurd_cast]
pub fun parse(text: Text): Num {
$[ -n "{text}" ] && [ "{text}" -eq "{text}" ] 2>/dev/null$?
return text as Num
}

/// ?
Mte90 marked this conversation as resolved.
Show resolved Hide resolved
pub fun chars(text: Text): [Text] {
let chars = [Text]
unsafe $for ((i=0; i<\$\{#{nameof text}}; i++)); do
Expand All @@ -65,6 +76,7 @@ pub fun chars(text: Text): [Text] {
return chars
}

/// Get the string length
#[allow_absurd_cast]
pub fun len(value): Num {
unsafe {
Expand All @@ -75,6 +87,7 @@ pub fun len(value): Num {
}
}

/// Check if string contain the value
pub fun contains(text: Text, phrase: Text): Bool {
let result = unsafe $if [[ "{text}" == *"{phrase}"* ]]; then
echo 1
Expand Down
Loading