Skip to content

Allow _ for readability in unitized constants like 3_day or 5_gb #9111

Closed

Description

@bobhy

Related problem

〉100_000
100000
〉3.141_596_2828
3.1415962828

But not:

〉3day
3day
〉3_day
Error: nu::shell::external_command

  × External command failed
   ╭─[entry #14:1:1]
 1 │ 3_day
   · ──┬──
   ·   ╰── executable was not found
   ╰────
  help: No such file or directory (os error 2)

Describe the solution you'd like

So one could say:

let duration = 3.165_hours
let size = 1.234_567__mb

Using _ for improving readability of numeric constants is a great idea. Why not extend it to unitized constants of all types? Duration, Filesize, (what's the complete list of these?)

Describe alternatives you've considered

No response

Additional context and details

No response

Activity

  1. added
    category:enhancement
    status:needs-triage
    on May 5, 2023
  2. fdncred commented on May 5, 2023

    @fdncred
    Contributor

    I'd support this, only if it was in addition to what we have. I like what you said in another issue that there should be multiple ways to express this. e.g.

    • 3day
    • 3days
    • 3_days
    • 3da
    • 3_da
    • 3d
      etc
  3. bobhy commented on May 6, 2023

    @bobhy
    ContributorAuthor

    Yes, it could be a pure add.
    This is a good first project for an aspiring parser hacker, documented here for anyone who wants to take a crack at it.

    The routines that parse Duration and Filesize are pretty straightforward. Each has a list of suffixes and you could add more easily.
    If you only worry about underscores between the numeric and suffix part, each has just one place you'd have to do that scan. (fully general placement would allow underscores in the numeric part too, Nu allows this for ints and float literals now.)
    The hardest part would be adding enough test cases for the new syntax. I recommend rstest, it's already in the project.

  4. added
    A:accessibility
    on May 6, 2023
  5. bobhy commented on May 7, 2023

    @bobhy
    ContributorAuthor

    Just to make this an even more inviting first parser project for someone...
    The above would allow duration or filesize literals you type into Nu to include underscores.

    To have the duration print with an underscore, see TimePeriod::to_text(). If you add underscores before these duration units, they will print out just fine.

    Filesize is a bit of a puzzle, I don't have any suggestions for it yet.

  6. fdncred commented on May 7, 2023

    @fdncred
    Contributor

    It would be also be nice if plurals could be used when appropriate.

  7. removed
    status:needs-triage
    on May 7, 2023
  8. added a commit that references this issue on May 17, 2023
    0e4729b
  9. added this to the v0.81.0 milestone on May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:accessibilitycategory:enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions