nushell/crates/nu-std
Bob Hyman bc437da5c7
std dt datetime-diff: fix uninitialized field ref when borrowing (#10466)
fixes #10455 

@KAAtheWiseGit, I'm sorry, I didn't mean to block your first PR #10461,
didn't see you had submitted it till I got around to submitting this. If
you want to incoporate useful ideas from this PR into yours, I do not
mind deferring to you.


# Description
Changes made in `datetime-diff`:
* Initialize millisecond and microsecond fields in `$current`, to fix
the error when borrow needs to refer to them.
* Fix `borrow_nanoseconds` to borrow from seconds, not from (unused)
microseconds.
* Added error check to insist that first argument is >= second argument.
`datetime-diff` doesn't represent negative durations correctly (it tries
to borrow out of the year, resulting in negative year and positive all
other fields). We don't currently have a use case requiring negative
durations.
* Add comments so help is a bit clearer (I was surprised that the first
argument, named `$from` was actually supposed to be the *later*
datetime. The order of arguments is reasonable (reminiscent of <later>
<minus> <earlier>), so I just changed the param name to match its
purpose.

Changes made in `pretty-print-duration`:
* changed type of argument from `duration` to `record`. (it's not clear
why Nu was not complaining about this!)
* changed test for skipping a clause from `> 0` to `!= 0`. Even though
`datetime-diff` won't present a negative field in the record, user might
call `pretty-print-duration` with one, might as well handle it. (but I
think `hour:-2` will be rendered as `-2hr`, not `-2hrs`...).
* added help and an example.

# User-Facing Changes
none requiring code changes.

# Tests + Formatting
- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`
- 
- # After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
2023-09-24 10:53:56 +02:00
..
src add 'from ndjson' into standard library (#10283) 2023-09-11 14:59:07 +02:00
std std dt datetime-diff: fix uninitialized field ref when borrowing (#10466) 2023-09-24 10:53:56 +02:00
tests std dt datetime-diff: fix uninitialized field ref when borrowing (#10466) 2023-09-24 10:53:56 +02:00
Cargo.toml Bump to 0.85.1 development version (#10431) 2023-09-20 18:38:42 +12:00
CONTRIBUTING.md fix the std test commands calls in dev documents (#9535) 2023-07-12 18:26:47 +02:00
LICENSE add LICENSE to nu-std (#8803) 2023-04-07 13:39:21 -07:00
README.md refactor the CONTRIBUTING.md guidelines for nu-std (#8912) 2023-04-19 22:21:27 +02:00

Welcome to the standard library of `nushell`!

The standard library is a pure-nushell collection of custom commands which provide interactive utilities and building blocks for users writing casual scripts or complex applications.

To see what's here:

> use std
> help commands | select name usage | where name =~ "std "
╭────┬─────────────────────────────┬────────────────────────────────────────────────────────────────╮
│  # │            name             │                                usage                           │
├────┼─────────────────────────────┼────────────────────────────────────────────────────────────────┤
│  0 │ std assert                  │ Universal assert command                                       │
│  1 │ std assert equal            │ Assert $left == $right                                         │
           . . .
│ 11 │ std clip                    │ put the end of a pipe into the system clipboard.               │
│ 12 │ std dirs add                │ Add one or more directories to the list.                       │
           . . .
├────┼─────────────────────────────┼────────────────────────────────────────────────────────────────┤
│  # │            name             │                                usage                           │
╰────┴─────────────────────────────┴────────────────────────────────────────────────────────────────╯

🧰 Using the standard library in the REPL or in scripts

All commands in the standard library must be "imported" into the running environment (the interactive read-execute-print-loop (REPL) or a .nu script) using the use command.

You can choose to import the whole module, but then must refer to individual commands with a std prefix, e.g:

use std

std log debug "Running now"
std assert (1 == 2)

Or you can enumerate the specific commands you want to import and invoke them without the std prefix.

use std ["log debug" assert]

log debug "Running again"
assert (2 == 1)

This is probably the form of import you'll want to add to your env.nu for interactive use.

✏️ contribute to the standard library

You're invited to contribute to the standard library! See CONTRIBUTING.md for details