mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 06:37:46 +00:00
Infraestructure initial setup.
Some groundwork to start growing Nu's standard library. As an initial exercise we take the mantainer_time.nu script that computes the core team's table timezone so that a given date can be converted to their local timezones. While doing so, it sounded like the logic could be abstracted out and make it a subcommand of `date` instead. For that reason, that particular custom command of interested was moved to `std/date`. Another one added were basic infraestructure custom commands for the test framework, subject to change (naming, and more things). It's in `std/playground` and it's used in `tests` directory. Since this repository contains scripts, we keep the `language` directory to maintain and grow the nu std library and grow it as we see custom command candidates. For the majority of the scripts that have nothing to do with the standard language but they do exercise Nu in practice (and some use custom commands, ideally, from the very, hopefully, future nu standard library) I have expectations to make them `runnable` as well and for that reason we introduce here an `examples` directory. To run the first one, you can do: ``` nu examples/date_in_local_timezones.nu ```
This commit is contained in:
parent
fa8143b11b
commit
52f984f3ce
10 changed files with 142 additions and 18 deletions
12
assets/core_team.nu
Normal file
12
assets/core_team.nu
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
def "nu core-team" [] {
|
||||||
|
[
|
||||||
|
[ 'name', 'tz'];
|
||||||
|
[ 'andres', 'America/Guayaquil']
|
||||||
|
[ 'fdncred', 'US/Central']
|
||||||
|
[ 'gedge', 'US/Eastern']
|
||||||
|
[ 'jt', 'NZ']
|
||||||
|
[ 'wycats', 'US/Pacific']
|
||||||
|
[ 'kubouch', 'Europe/Helsinki']
|
||||||
|
['elferherrera', 'Europe/London']
|
||||||
|
]
|
||||||
|
}
|
25
examples/date_in_local_timezones.nu
Normal file
25
examples/date_in_local_timezones.nu
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
source lib/scripts.nu
|
||||||
|
source assets/core_team.nu
|
||||||
|
|
||||||
|
let next_call = ("2021-08-31 15:00:21.290597200 -05:00" | str to-datetime);
|
||||||
|
let people = (nu core-team | date local $next_call);
|
||||||
|
|
||||||
|
def say [block] {
|
||||||
|
each {|person|
|
||||||
|
do $block (
|
||||||
|
$person | merge {
|
||||||
|
[[name]; [($person.name | str capitalize)]]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
} | str collect (char newline)
|
||||||
|
}
|
||||||
|
|
||||||
|
$people | say {|person| $"($person.name)'s timezone is ($person.tz)"}
|
||||||
|
|
||||||
|
$"
|
||||||
|
|
||||||
|
for the next call happening on ($next_call | date format '%c').. in their timezones they would be:
|
||||||
|
|
||||||
|
"
|
||||||
|
|
||||||
|
$people | say {|person| $"($person.name)'s: ($person.time)"}
|
1
language/playground.nu
Normal file
1
language/playground.nu
Normal file
|
@ -0,0 +1 @@
|
||||||
|
source language/playground/lib.nu
|
55
language/playground/lib.nu
Normal file
55
language/playground/lib.nu
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
def playground [topic, block] {
|
||||||
|
with-env [N 5 REJECT slow] {
|
||||||
|
echo $topic " tests" (char newline) | str collect
|
||||||
|
|
||||||
|
do $block
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def scene [
|
||||||
|
topic: any
|
||||||
|
--tag: string
|
||||||
|
block: block
|
||||||
|
] {
|
||||||
|
$" ($topic)(char newline)"
|
||||||
|
do $block
|
||||||
|
}
|
||||||
|
|
||||||
|
def play [
|
||||||
|
topic: any
|
||||||
|
--tag: string
|
||||||
|
block: block
|
||||||
|
] {
|
||||||
|
let title = $topic;
|
||||||
|
|
||||||
|
let is_tag_empty = ($tag | empty?);
|
||||||
|
let should_run_all = ($nu.env | default RUN_ALL $false | get RUN_ALL);
|
||||||
|
|
||||||
|
if $is_tag_empty {
|
||||||
|
do $block
|
||||||
|
} {
|
||||||
|
if $tag == $nu.env.REJECT && $should_run_all {
|
||||||
|
$" ($topic) ... (ansi yellow)skipped(ansi reset) (char newline)"
|
||||||
|
} { do $block }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def expect [
|
||||||
|
actual: any
|
||||||
|
--to-be: any
|
||||||
|
] {
|
||||||
|
let are_equal = ($actual | zip { $to-be } | pivot header_names values | each {|case|
|
||||||
|
let values = $case.values;
|
||||||
|
|
||||||
|
$values.0 == $values.1
|
||||||
|
}
|
||||||
|
| all? $it) && (($actual | get | length) == ($to-be | get | length));
|
||||||
|
|
||||||
|
let line = (if $true == $are_equal {
|
||||||
|
$"(ansi green)ok(ansi reset)(char newline)"
|
||||||
|
} {
|
||||||
|
$"(ansi red)failed(ansi reset)(char newline)"
|
||||||
|
});
|
||||||
|
|
||||||
|
$" ($title) ... ($line)"
|
||||||
|
}
|
1
language/std.nu
Normal file
1
language/std.nu
Normal file
|
@ -0,0 +1 @@
|
||||||
|
source language/std/date.nu
|
7
language/std/date.nu
Normal file
7
language/std/date.nu
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
def "date local" [now] {
|
||||||
|
insert time {|value|
|
||||||
|
let converted = ($now | date to-timezone $value.tz);
|
||||||
|
|
||||||
|
$converted | date format '%c'
|
||||||
|
}
|
||||||
|
}
|
1
lib/scripts.nu
Normal file
1
lib/scripts.nu
Normal file
|
@ -0,0 +1 @@
|
||||||
|
source language/std.nu
|
|
@ -1,18 +0,0 @@
|
||||||
let m_table = (
|
|
||||||
[
|
|
||||||
['name', 'tz', 'time'];
|
|
||||||
['andres' 'America/Guayaquil' ' ']
|
|
||||||
['fdncred' 'US/Central' ' ']
|
|
||||||
['gedge' 'US/Eastern' ' ']
|
|
||||||
['jt' 'NZ' ' ']
|
|
||||||
['wycats' 'US/Pacific' ' ']
|
|
||||||
['kubouch' 'Europe/Helsinki' ' ']
|
|
||||||
['elferherrera' 'Europe/London' ' ']
|
|
||||||
]
|
|
||||||
)
|
|
||||||
let now = (date now)
|
|
||||||
$m_table | update time {
|
|
||||||
each { |name|
|
|
||||||
$now | date to-timezone ($name | get tz) | date format '%c'
|
|
||||||
}
|
|
||||||
}
|
|
38
tests/language/std/date.nu
Normal file
38
tests/language/std/date.nu
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
source lib/scripts.nu
|
||||||
|
|
||||||
|
def mock-now [] {
|
||||||
|
"2021-08-29 03:31:21.290597200 -05:00" | str to-datetime
|
||||||
|
}
|
||||||
|
|
||||||
|
def people [] {
|
||||||
|
[
|
||||||
|
[ 'name', 'tz'];
|
||||||
|
[ 'andres', 'America/Guayaquil']
|
||||||
|
[ 'fdncred', 'US/Central']
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
playground "std/date" {
|
||||||
|
|
||||||
|
scene 'command: "date local"' {
|
||||||
|
|
||||||
|
play "adds times in local timezone" {
|
||||||
|
|
||||||
|
let expected_times = [
|
||||||
|
[time];
|
||||||
|
["Sun Aug 29 03:31:21 2021"]
|
||||||
|
["Sun Aug 29 03:31:21 2021"]
|
||||||
|
];
|
||||||
|
|
||||||
|
let actual = (people |
|
||||||
|
date local (mock-now) |
|
||||||
|
select time
|
||||||
|
);
|
||||||
|
|
||||||
|
expect $actual --to-be $expected_times
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
2
tests/main.nu
Normal file
2
tests/main.nu
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
source language/playground.nu
|
||||||
|
source tests/language/std/date.nu
|
Loading…
Add table
Add a link
Reference in a new issue