Repository navigation
Description
Problem
There are many places in Nushell where values are turned into strings for one reason or another. Over time, this has led to many different implementations and behaviors. Most recently, this issue was brought up in #14086. Going foward, we should standardize the process(es) for turning values into strings and document it somewhere.
My current hypothesis (I need to go through the codebase and confirm) is that there are two or three main use cases for turning a value into a string:
- The value needs to be displayed in a human-readable format (e.g., in errors, for
table, or forformat). This should probably respect the current locale and formats specified in the config. - The value needs to be converted to string data for some other programmatic purpose (e.g.,
into string). This should probably have the same output regardless of config settings or environment variables. - The value needs to be sent to an external source. E.g., as an external command argument, converted to an OS environment variable, or as input to an external command. This is similar to
2)except that I think certain conversions should be disallowed as they are most likely an error, and having different behavior for others types makes more sense for this use case. To differentiate it frominto string,to textcan take up this role.
I think we can create a separate issue for 1) if necessary. I would rather start with 2) and 3), as I think 2) currently has the least uniform behavior and 3) is related to 2).
Rough Proposal
For 2), my current proposal is that we, for the most part, use to nuon. The difference being that all values are first converted to strings and then outputted as such (e.g., binary values are not formatted as binary literals). I.e., there is no guarantee that type information is preserved. In particular, types should be converted as follows:
| Type | Conversion |
|---|---|
| bool | Use Rust's built in Display implementation |
| int | Display |
| float | Display |
| filesize | Automatically detect unit precision. Leaning towards non-metric units so that decimal places can be used without losing precision. E.g., (1MB + 1B) | into string == '1.000001MB' |
| duration | Use ISO 8601 durations but also add a negative sign at the start if necessary. |
| date | Use .to_rfc3339() from chrono. This follows RFC 3339 but also allows out of range years as specified by ISO 8601 (otherwise some valid date values would not be representable as strings). To be precise, automatic subsecond precision is used and +00:00 is used instead of Z. |
| string | Simply used as is. |
| glob | Used as is. |
| binary | Used as is if valid utf-8. |
| cellpath | Prefixed with $. and with necessary quoting/escaping for each path member. |
| closure | Error/disallowed |
| range | [start]..[end] or [start]..<next>..[end] |
| nothing | null |
| list | [ val1, val2 ], adding quotes either where necessary or always (for strings, globs, and binary). |
| record | { foo: bar, baz: val }, adding quotes where necessary for columns/keys. |
Then, I propose 3) should be based off of 2) but with the following differences:
- Non-utf-8 binary values are allowed.
- Cellpaths are disallowed? These are nushell-specific data types, so probably does not make sense to convert it to a string for the purpose of sending to an external source.
- Similarly, ranges should probably be disallowed? Our range format is most likely not going to be interpreted properly by an external source.
- Records are disallowed too. Same as the above two, especially since records have escaping concerns and allow arbitrary nesting. Instead, users should use
to jsonor some othertocommand. - Lists that contain none of the disallowed types above and also do not contain other lists are allowed. Each value is converted to a string based on
2)and a new line is added after each element. For conversion to OS environment variables, the OS specific path separator is used instead (I believe;for windows and:for unix?). - Filesizes and durations I think can be converted the same as in
2)... - Nothing/
nullis a whole separate issue that might be worth discussing.
If you made it this far, thanks for reading lol. Would love to hear everybody's thoughts.
Activity
IanManske commentedon Oct 15, 2024
Another related but probably separate issue: the
to {format}commands should probably error on data types not supported by the data format (e.g., JSON has no concept of dates). Instead, there should be a--lossyflag that falls back to using2)to convert the unsupported types to string. The--lossyflag means that round-trip conversions are not value preserving. E.g.,($some_date | to json --lossy | from json) != $some_date.fdncred commentedon Oct 15, 2024
I think we need to be sure that we're not losing functionality. It sounds like 3) could be losing some but I can't really tell without testing things.
I definitely want a better "to string" api where we don't have to decide which of ~5 functions to use.
I figure something like to_abbreviated is still needed since we represent nested tables, records, and lists as strings but maybe that can just be inner workings and not be public?
I also wonder if closures should work in a way similar to how
view sourcecan display them.Thanks for writing this up. I agree that something is definitely needed. I look forward to seeing how this progresses.
132ikl commentedon Oct 15, 2024
I'm a bit confused on the distinction between
to nuonand what you're proposing here, could you explain a bit more? The rules from your table seem to be similar to whatto nuondoes.IanManske commentedon Oct 16, 2024
No problem, I should have given an example.
So, I guess besides binary values, my proposal is the same as
to nuon. However, we now have glob types without a corresponding syntax. If we add a syntax for glob literals and add it to the nuon format, then globs would also be different under this proposal.Improve `CellPath` display output (#14197)
str joinandinto stringhandle dates differently #11265132ikl commentedon May 15, 2025
We talked a little about this at the meeting yesterday, and I've been thinking about it a bit since. I like Ian's rough proposal, and I think the three noted use cases are accurate (although I think we should be a bit stricter on 3 than Ian suggests, but that's a topic for #14452). Also, there is a TLDR at the bottom of this comment
String conversion methods
I'm not as concerned with the specifics of each value representation right now so much as mapping Nushell commands/behaviors to the use-cases described. I think as a first step we can replace our current string conversion functions (
to_expanded_string,to_abbreviated_string,coerce_into_string) with:to_human_string(but can bikeshed later)to_programmatic_stringHow we would actually implement these methods
Here's a list of the primary areas in Nushell where we use string conversions, and how I think they should behave using the above approach:
to textto_human_stringfunction. This would meanto textmaintains its function of being a simple way to convert any Nushell value to a human-readable string.to textis currently commonly used as an alternative tostr join (char nl)would no longer work. If we decide to do this, I think we should bite the bullet on this one and add ato linesor something, since it seems like an anti-pattern to be usingto textwhen you actually just want to join lines. If we really don't want to change this, we could add another command to print the human-readable version of a string.render/tableto textshould instead be used for that.to_human_string(with the exception of binary values since we have the fancy binary value display)tabledtables, and inner values useto_human_string. This way, scalar values are displayed the same within a table and outside of a table, which is not the current behavior.into stringto_programmatic_stringfor scalars.toandformatsubcommandsto_programmatic_stringas needed, but should not useto_human_stringto_programmatic_string. For example,to nuonwould probably useto_programmatic_stringfor most values, but would need special handling for binary values (as noted by Ian)to textto convert inner tables to human readable strings if needed.format datecommand when given no arguments will output a human readable string. This is the onlyformatcommand which works like this, and I think we should remove this functionality. Then, allformatcommands will be programmatic conversions only.to textcan instead used to produce a (config respecting) human readable string from a date.str join(probably others, but I don't initially see anything?). Whether we want to change this is a different topic, so for the sake of not derailing us, I will assume the current behavior. I think we should useto_programmatic_stringfor these cases, as they are programmatic operations. You can always useto textif you want a human-readable string.Miscellanea:
to_programmatic_stringas needed.render/tableandexplore, but move that to a specific helper function for use by those commands specifically. This should make it clear that it's not a general-purpose conversion.table(sodisplay_outputhook set to{||}), I think we should useto_human_stringTLDR
I think string conversions should fit into two main categories, with a corresponding conversion method on
Value:to text,render/table, andexploreCommands which convert to a specific format, like the
toandformatsubcommands can use programmatic strings, but apply further strictness or additional formatting as needed by that format. They should not use human readable strings.To implement this, we would need to make following changes:
to textwould become the primary way to do general-purpose conversions to human readable strings. Therefore, we might want lists and records to look a little more like their literals to make it clear that the value is a list or record (as opposed to the current behavior of joining list items with newlines and printing key-value pairs for records)str join (char nl)use-case ofto text, such asto linesformat datewould be removed. The format specifier argument would be made mandatory. You should useto textorinto stringinstead.render/tablewould render values inside of a list/table and outside of a list/table in the same way (with the exception of binary values, since they have a special display outside of a list/table)to htmlandto mdinstead being converted to a[list 1 item]string)str join) would use programmatic strings.