Skip to content

Commit 30ffefb

Browse files
hdwaltersPh0enixKM
andauthored
Rename standard library functions (#639)
Co-authored-by: Phoenix Himself <[email protected]>
1 parent 3d5c47f commit 30ffefb

File tree

122 files changed

+264
-238
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

122 files changed

+264
-238
lines changed

keywords.ab

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
#! /usr/bin/env amber
2-
1+
#!/usr/bin/env amber
32
import {
43
replace,
4+
split_words,
55
starts_with,
6-
words,
76
} from "std/text"
87

9-
main (args) {
8+
main(args) {
109
let keywords = [Text]
1110
let should_append = false
1211
for line in lines("grammar.ebnf") {
1312
if not should_append and not starts_with(line, "KEYWORD"): continue
1413
should_append = true
15-
for token in words(line) {
14+
for token in split_words(line) {
1615
if {
1716
starts_with(token, "'"): keywords += [replace(token, "'", "")]
1817
token == ";": should_append = false
@@ -22,7 +21,7 @@ main (args) {
2221
let result = $ echo "\$\{{nameof keywords}[@]}" | sort $ failed {
2322
echo "Something went wrong while sorting the keywords"
2423
}
25-
for item in words(result) {
24+
for item in split_words(result) {
2625
echo item
2726
}
2827
}

setup/install.ab

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { has_failed, input } from "std/env"
2-
import { chars } from "std/text"
3-
import { includes } from "std/array"
4-
import { file_exist } from "std/fs"
1+
import { has_failed, input_prompt } from "std/env"
2+
import { split_chars } from "std/text"
3+
import { array_contains } from "std/array"
4+
import { file_exists } from "std/fs"
55
import { get_os, get_arch, get_place, get_bins_folder } from "./shared.ab"
66

77
let name = "amber"
@@ -28,7 +28,7 @@ main(args) {
2828
let os = get_os()
2929
let arch = get_arch()
3030

31-
let user_only_install = includes(args, "--user")
31+
let user_only_install = array_contains(args, "--user")
3232
let place = get_place(user_only_install)
3333
let bins_folder = get_bins_folder(user_only_install)
3434

@@ -116,7 +116,7 @@ main(args) {
116116
}
117117

118118
// Delete the previous symbolic link
119-
if file_exist("{bins_folder}/{target}") {
119+
if file_exists("{bins_folder}/{target}") {
120120
$ {sudo} rm "{bins_folder}/{target}" $ failed {
121121
echo "Failed to remove the previous amber symbol link."
122122
echo "Please make sure that root user can access {bins_folder} directory."
@@ -131,7 +131,7 @@ main(args) {
131131
exit 1
132132
}
133133

134-
let nickname = input("Would you like to help improve Amber by sharing your OS info with our developer database? Enter your GitHub nickname (or any nickname) or type `no`:")
134+
let nickname = input_prompt("Would you like to help improve Amber by sharing your OS info with our developer database? Enter your GitHub nickname (or any nickname) or type `no`:")
135135
if (nickname != "no") {
136136
// Send feedback to the server
137137
trust silent $ curl -G --data-urlencode "agent={agent}" --data-urlencode "nickname={nickname}" --data-urlencode "name=download" "https://amber-lang.com/api/visit" $

setup/shared.ab

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { has_failed } from "std/env"
2-
import { includes } from "std/array"
2+
import { array_contains } from "std/array"
33

44
pub fun get_os(): Text {
55
// Determine OS type
@@ -31,7 +31,7 @@ pub fun get_arch(): Text {
3131
exit 1
3232
}
3333

34-
let arch = includes(["arm64", "aarch64"], arch_type)
34+
let arch = array_contains(["arm64", "aarch64"], arch_type)
3535
then "aarch64"
3636
else "x86_64"
3737

setup/uninstall.ab

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
import { includes } from "std/array"
1+
import { array_contains } from "std/array"
22
import { get_arch, get_place, get_bins_folder } from "./shared.ab"
33

44
echo ""
55

66
main(args) {
77
let arch = get_arch()
88

9-
let user_only_install = includes(args, "--user")
9+
let user_only_install = array_contains(args, "--user")
1010
let place = get_place(user_only_install)
1111
let bins_folder = get_bins_folder(user_only_install)
1212

src/std/array.ab

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// Returns index of the first value found in the specified array.
22
///
33
/// If the value is not found, the function returns -1.
4-
pub fun array_first_index(array, value): Num {
4+
pub fun array_find(array, value): Num {
55
for index, element in array {
66
if value as Text == element as Text {
77
return index
@@ -11,7 +11,7 @@ pub fun array_first_index(array, value): Num {
1111
}
1212

1313
/// Searches for a value in an array and returns an array with the index of the various items.
14-
pub fun array_search(array, value): [Num] {
14+
pub fun array_find_all(array, value): [Num] {
1515
let result = [Num]
1616
for index, element in array {
1717
if value as Text == element as Text {
@@ -22,28 +22,28 @@ pub fun array_search(array, value): [Num] {
2222
}
2323

2424
/// Checks if a value is in the array.
25-
pub fun includes(array, value) {
26-
let result = array_first_index(array, value)
25+
pub fun array_contains(array, value) {
26+
let result = array_find(array, value)
2727
return result >= 0
2828
}
2929

3030
/// Returns the first element in the array; if the array is empty, the return
3131
/// value is undefined.
32-
pub fun first(array) {
32+
pub fun array_first(array) {
3333
return array[0]
3434
}
3535

3636
/// Returns the last element in the array; if the array is empty, the return
3737
/// value is undefined.
38-
pub fun last(array) {
38+
pub fun array_last(array) {
3939
let index = len(array) - 1
4040
return array[index]
4141
}
4242

4343
/// Removes an element at the index from the array; if the index is negative
4444
/// or beyond the end, the return value is undefined, but the array will be
4545
/// unchanged.
46-
pub fun remove_at(ref array: [], index: Num): Null {
46+
pub fun array_remove_at(ref array: [], index: Num): Null {
4747
let offset = index + 1
4848
let length = len(array)
4949
array = array[0..index] + array[offset..length]
@@ -52,7 +52,7 @@ pub fun remove_at(ref array: [], index: Num): Null {
5252
/// Removes an element at the index from the array, and returns it; if the
5353
/// index is negative or beyond the end, the return value is undefined, but
5454
/// the array will be unchanged.
55-
pub fun extract_at(ref array, index) {
55+
pub fun array_extract_at(ref array, index) {
5656
let element = array[index]
5757
let offset = index + 1
5858
let length = len(array)
@@ -62,7 +62,7 @@ pub fun extract_at(ref array, index) {
6262

6363
/// Removes the last element from the array, and returns it; if the array
6464
/// is empty, the return value is undefined, but the array will be unchanged.
65-
pub fun pop(ref array) {
65+
pub fun array_pop(ref array) {
6666
let length = len(array)
6767
let index = length - 1
6868
let element = array[index]
@@ -72,7 +72,7 @@ pub fun pop(ref array) {
7272

7373
/// Removes the first element from the array, and returns it; if the array
7474
/// is empty, the return value is undefined, but the array will be unchanged.
75-
pub fun shift(ref array) {
75+
pub fun array_shift(ref array) {
7676
let length = len(array)
7777
let element = array[0]
7878
array = array[1..length]

src/std/date.ab

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub fun date_posix(format: Text = "", date: Text = "", utc: Bool = false): Text?
8080

8181
/// Returns the current timestamp (seconds since the Epoch (1970-01-01 00:00 UTC)).
8282
#[allow_absurd_cast]
83-
pub fun now(): Num {
83+
pub fun date_now(): Num {
8484
return trust $ date +%s $ as Num
8585
}
8686

src/std/env.ab

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import * from "std/fs"
22
import * from "std/text"
33

44
/// Retrieves the value of an environment variable, optionally sourcing it from a file if not already set.
5-
pub fun get_env_var(var: Text, file: Text = ".env"): Text {
5+
pub fun env_var_load(var: Text, file: Text = ".env"): Text {
66
let _var = trust $ echo "\$\{!var}" $
77
if _var != "" {
88
return _var
99
}
1010

11-
if file_exist(file) {
11+
if file_exists(file) {
1212
trust $ source "{file}" $
1313
return trust $ echo "\$\{!var}" $
1414
}
@@ -17,40 +17,40 @@ pub fun get_env_var(var: Text, file: Text = ".env"): Text {
1717
}
1818

1919
/// Loads the env file in the environment, using `xargs`.
20-
pub fun load_env_file(file: Text = ".env"): Null {
20+
pub fun env_file_load(file: Text = ".env"): Null {
2121
trust $ export "\$(xargs < {file})" > /dev/null $
2222
}
2323

2424
/// Checks if a variable inside the shell session exists.
25-
pub fun shell_isset(name: Text): Bool {
25+
pub fun env_var_test(name: Text): Bool {
2626
$ [[ ! -z \$\{!{nameof name}+z} ]] $ failed {
2727
return false
2828
}
2929
return true
3030
}
3131

3232
/// Sets a constant inside the shell session.
33-
pub fun shell_constant_set(name: Text, val: Text): Null? {
33+
pub fun env_const_set(name: Text, val: Text): Null? {
3434
$ readonly \${nameof name}="\${nameof val}" 2> /dev/null $?
3535
}
3636

3737
/// Gets a constant inside the shell session.
38-
pub fun shell_constant_get(name: Text): Text? {
38+
pub fun env_const_get(name: Text): Text? {
3939
return $ echo \$\{!{nameof name}} $?
4040
}
4141

4242
/// Sets a constant inside the shell session.
43-
pub fun shell_var_set(name: Text, val: Text): Null? {
43+
pub fun env_var_set(name: Text, val: Text): Null? {
4444
$ export \${nameof name}="\${nameof val}" 2> /dev/null $?
4545
}
4646

4747
/// Gets a constant inside the shell session.
48-
pub fun shell_var_get(name: Text): Text? {
48+
pub fun env_var_get(name: Text): Text? {
4949
return $ echo \$\{!{nameof name}} $?
5050
}
5151

5252
/// Removes a variable inside the shell session.
53-
pub fun shell_unset(name: Text): Null? {
53+
pub fun env_var_unset(name: Text): Null? {
5454
$ unset {name} $?
5555
}
5656

@@ -63,7 +63,7 @@ pub fun is_command(command: Text): Bool {
6363
}
6464

6565
/// Creates a prompt and returns the value.
66-
pub fun input(prompt: Text): Text {
66+
pub fun input_prompt(prompt: Text): Text {
6767
trust $ read -p "\${nameof prompt}" $
6868
return "\$REPLY"
6969
}
@@ -80,14 +80,14 @@ pub fun input_hidden(prompt: Text): Text {
8080
/// Creates a confirm prompt (Yes/No), and returns true if the choice is Yes.
8181
///
8282
/// "No" is the default choice, set default_yes to true for "Yes" as default choice.
83-
pub fun confirm(prompt: Text, default_yes: Bool = false): Bool {
83+
pub fun input_confirm(prompt: Text, default_yes: Bool = false): Bool {
8484
let choice_default = default_yes then " [\x1b[1mY/\x1b[0mn]" else " [y/\x1b[1mN\x1b[0m]"
8585
trust {
8686
$ printf "\x1b[1m{prompt}\x1b[0m{choice_default}" $
8787
$ read -s -n 1 $
8888
$ printf "\n" $
8989
}
90-
let result = lower(trust $ echo \$REPLY $)
90+
let result = lowercase(trust $ echo \$REPLY $)
9191
return result == "y" or (result == "" and default_yes)
9292
}
9393

@@ -113,32 +113,32 @@ pub fun printf(format: Text, args: [Text] = [""]): Null {
113113
}
114114

115115
/// Escapes the text to be used with `printf`.
116-
pub fun printf_escape(text: Text): Text {
116+
pub fun escaped(text: Text): Text {
117117
return trust $ echo \${nameof text} | sed -e 's/\\\\/\\\\\\\\/g' -e "s/%/%%/g" $
118118
}
119119

120120
/// Prepares a text with formatting options for `printf`.
121-
pub fun text_shell(message: Text, style: Num, fg: Num, bg: Num): Text {
122-
return "\x1b[{style};{fg};{bg}m{printf_escape(message)}\x1b[0m"
121+
pub fun styled(message: Text, style: Num, fg: Num, bg: Num): Text {
122+
return "\x1b[{style};{fg};{bg}m{escaped(message)}\x1b[0m"
123123
}
124124

125125
/// Returns a text as bold.
126-
pub fun text_bold(message: Text): Text {
127-
return "\x1b[1m{printf_escape(message)}\x1b[0m"
126+
pub fun bold(message: Text): Text {
127+
return "\x1b[1m{escaped(message)}\x1b[0m"
128128
}
129129

130130
/// Returns a text as italic.
131-
pub fun text_italic(message: Text): Text {
132-
return "\x1b[3m{printf_escape(message)}\x1b[0m"
131+
pub fun italic(message: Text): Text {
132+
return "\x1b[3m{escaped(message)}\x1b[0m"
133133
}
134134

135135
/// Returns a text as underlined.
136-
pub fun text_underlined(message: Text): Text {
137-
return "\x1b[4m{printf_escape(message)}\x1b[0m"
136+
pub fun underlined(message: Text): Text {
137+
return "\x1b[4m{escaped(message)}\x1b[0m"
138138
}
139139

140140
/// Prints a text with a specified color.
141-
pub fun color_echo(message: Text, color: Num): Null {
141+
pub fun echo_colored(message: Text, color: Num): Null {
142142
printf("\x1b[{color as Text}m%s\x1b[0m\n", [message])
143143
}
144144

@@ -158,7 +158,7 @@ pub fun echo_warning(message: Text): Null {
158158
}
159159

160160
/// Prints a text as a error and exits if the status code is greater than 0.
161-
pub fun error(message: Text, exit_code: Num = 1): Null {
161+
pub fun echo_error(message: Text, exit_code: Num = 1): Null {
162162
printf("\x1b[1;3;97;41m%s\x1b[0m\n", [message])
163163
if exit_code > 0 : exit(exit_code)
164164
}

0 commit comments

Comments
 (0)