Skip to content

Commit a1b0829

Browse files
committed
Add Tty/Locale modules and update examples
1 parent 03e94ad commit a1b0829

12 files changed

Lines changed: 254 additions & 95 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ crate-type = ["staticlib"]
1313
# Note: we use "=version" for reproducability, cargo may silently use a more recent version if you do not use `=`.
1414
[dependencies]
1515
libc = "=0.2.180" # keep version in sync with libc workspace dep below
16+
crossterm = "0.27"
1617
roc_std_new.workspace = true
1718
roc_io_error.workspace = true
1819
roc_random.workspace = true
1920
roc_command.workspace = true
2021
memoffset = "=0.9.1"
2122

23+
[target.'cfg(not(target_os = "macos"))'.dependencies]
24+
sys-locale.workspace = true
25+
2226
[workspace]
2327
members = [
2428
"crates/roc_io_error",

build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ get_rust_triple() {
1717
esac
1818
}
1919

20+
2021
# All supported targets
2122
ALL_TARGETS="x64mac arm64mac x64musl arm64musl"
2223

ci/all_tests.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ export PATH="$(pwd)/$ROC_DIR:$PATH"
8686
echo ""
8787
echo "Using roc version: $(roc version)"
8888

89+
if [ "$(uname -s)" = "Darwin" ] && [ -z "${SDKROOT:-}" ]; then
90+
SDKROOT=$(xcrun --sdk macosx --show-sdk-path 2>/dev/null || true)
91+
if [ -n "$SDKROOT" ]; then
92+
export SDKROOT
93+
echo "Using SDKROOT: $SDKROOT"
94+
fi
95+
fi
96+
8997
# Build the platform
9098
if [ "${NO_BUILD:-}" != "1" ]; then
9199
echo ""

crates/roc_env/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ version.workspace = true
1010
[dependencies]
1111
roc_std.workspace = true
1212
roc_file.workspace = true
13+
14+
[target.'cfg(not(target_os = "macos"))'.dependencies]
1315
sys-locale.workspace = true

crates/roc_env/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,55 @@ pub fn exe_path() -> RocResult<RocList<u8>, ()> {
5858
}
5959
}
6060

61+
#[cfg(target_os = "macos")]
62+
fn locale_from_env() -> Option<String> {
63+
for key in ["LC_ALL", "LC_CTYPE", "LANG"] {
64+
if let Ok(value) = std::env::var(key) {
65+
let trimmed = value.trim();
66+
if trimmed.is_empty() {
67+
continue;
68+
}
69+
let locale = trimmed
70+
.split('.')
71+
.next()
72+
.unwrap_or(trimmed)
73+
.split('@')
74+
.next()
75+
.unwrap_or(trimmed)
76+
.trim();
77+
if !locale.is_empty() {
78+
return Some(locale.to_string());
79+
}
80+
}
81+
}
82+
83+
None
84+
}
85+
86+
#[cfg(target_os = "macos")]
87+
pub fn get_locale() -> RocResult<RocStr, ()> {
88+
locale_from_env()
89+
.map(|locale| RocResult::ok(locale.as_str().into()))
90+
.unwrap_or_else(|| RocResult::err(()))
91+
}
92+
93+
#[cfg(target_os = "macos")]
94+
pub fn get_locales() -> RocList<RocStr> {
95+
match locale_from_env() {
96+
Some(locale) => RocList::from_slice(&[RocStr::from(locale.as_str())]),
97+
None => RocList::empty(),
98+
}
99+
}
100+
101+
#[cfg(not(target_os = "macos"))]
61102
pub fn get_locale() -> RocResult<RocStr, ()> {
62103
sys_locale::get_locale().map_or_else(
63104
|| RocResult::err(()),
64105
|locale| RocResult::ok(locale.to_string().as_str().into()),
65106
)
66107
}
67108

109+
#[cfg(not(target_os = "macos"))]
68110
pub fn get_locales() -> RocList<RocStr> {
69111
const DEFAULT_MAX_LOCALES: usize = 10;
70112
let locales = sys_locale::get_locales();

crates/roc_host/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ path = "src/lib.rs"
1616
crossterm.workspace = true
1717
memmap2.workspace = true
1818
memchr.workspace = true
19-
sys-locale.workspace = true
2019
libc.workspace = true
2120
backtrace.workspace = true
2221
roc_std.workspace = true

examples/random.roc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ main! = |_args| {
1717
Err(Exit(1))
1818
}
1919
}
20-
}
20+
}

platform/Locale.roc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Locale := [].{
2+
## Returns the most preferred locale for the system or application.
3+
##
4+
## The returned [Str] is a BCP 47 language tag, like `en-US` or `fr-CA`.
5+
get! : () => Str
6+
7+
## Returns the preferred locales for the system or application.
8+
##
9+
## The returned [Str] are BCP 47 language tags, like `en-US` or `fr-CA`.
10+
all! : () => List(Str)
11+
}

platform/Tty.roc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Provides functionality to change the behaviour of the terminal.
2+
## This is useful for running an app like vim or a game in the terminal.
3+
Tty := [].{
4+
## Enable terminal [raw mode](https://en.wikipedia.org/wiki/Terminal_mode) to disable some default terminal bevahiour.
5+
##
6+
## This leads to the following changes:
7+
## - Input will not be echoed to the terminal screen.
8+
## - Input will be sent straight to the program instead of being buffered (= collected) until the Enter key is pressed.
9+
## - Special keys like Backspace and CTRL+C will not be processed by the terminal driver but will be passed to the program.
10+
enable_raw_mode! : {} => {}
11+
12+
## Revert terminal to default behaviour
13+
disable_raw_mode! : {} => {}
14+
}

0 commit comments

Comments
 (0)