Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default to short printing for REPL #168

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Keno
Copy link

@Keno Keno commented Jul 10, 2021

The printing choices in this package are somewhat non-standard
with respect to how Base defines printing, though that is
probably Base's fault for not documenting how these are
supposed to be used properly. In particular, this package
uses the :compact switch to decide between a parseable,
verbose printing and a compact, human-readable printing.

However, these are orthogonal concepts, and the :compact
switch isn't really supposed to switch the parsability.
Parsability vs non-parsibility is the distinction between
show(io::IO, x) (parseable) and show(io::IO, MIME"text/plain"(), x)
(non-parseable, pretty for humans). The latter is also
what is used for display in REPL contexts whereas the former
is used for repr.

I would like to suggest that the REPL printing should also
default to the human-readable version. For example, I
wanted to use intervals of units in a package, but the
default output looks very scary for first time Julia users:

julia> using Intervals, Unitful

julia> 1dB..2dB
Interval{Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Int64}, Closed, Closed}(1 dB, 2 dB)

Admittedly that is mostly because of the scary Unitful type,
but still. We have better printing, why not use it. After this PR,
the compact printing is used at the REPL:

julia> 1dB..2dB
[1 dB .. 2 dB]

The printing choices in this package are somewhat non-standard
with respect to how Base defines printing, though that is
probably Base's fault for not documenting how these are
supposed to be used properly. In particular, this package
uses the :compact switch to decide between a parseable,
verbose printing and a compact, human-readable printing.

However, these are orthogonal concepts, and the :compact
switch isn't really supposed to switch the parsability.
Parsability vs non-parsibility is the distinction between
`show(io::IO, x)` (parseable) and `show(io::IO, MIME"text/plain"(), x)`
(non-parseable, pretty for humans). The latter is also
what is used for display in REPL contexts whereas the former
is used for repr.

I would like to suggest that the REPL printing should also
default to the human-readable version. For example, I
wanted to use intervals of units in a package, but the
default output looks very scary for first time Julia users:

```
julia> using Intervals, Unitful

julia> 1dB..2dB
Interval{Gain{Unitful.LogInfo{:Decibel, 10, 10}, :?, Int64}, Closed, Closed}(1 dB, 2 dB)
```

Admittedly that is mostly because of the scary Unitful type,
but still. We have better printing, why not use it.

```
julia> 1dB..2dB
[1 dB .. 2 dB]
```
@Keno Keno requested a review from omus as a code owner July 10, 2021 01:33
Keno added a commit to Keno/Intervals.jl that referenced this pull request Jul 10, 2021
This is sort of an expansion of invenia#168. There I tried to keep the PR
fairly minimal to only fix the specific case that was bothering me.
Here I try a more extensive set of changes to show some options of
what might be possible. Some subset of these may be interesting, so
I'm opening this for discussion with the maximum set of possible
changes and we can discuss from there.

First things first, here's some demo printing:
```
julia> a = Interval{Closed, Open}(DateTime(2013, 2, 13), DateTime(2013, 2, 13, 1))
[2013-02-13T00:00:00, 2013-02-13T01:00:00)

julia> show(a)
Interval{Closed,Open}(DateTime("2013-02-13T00:00:00"), DateTime("2013-02-13T00:00:00"))

julia> show(IOContext(stdout, :compact=>true), a)
i`[DateTime("2013-02-13T00:00:00"),DateTime("2013-02-13T01:00:00"))`

julia> i`[DateTime("2013-02-13T00:00:00"),DateTime("2013-02-13T01:00:00"))`
[2013-02-13T00:00:00, 2013-02-13T01:00:00)

julia> const dB = Unitful.dB
dB

julia> 1 dB .. 2 dB
1 dB .. 2 dB

julia> i`(1dB, 2dB]`
(1 dB, 2 dB]
```

The most obvious change, is probably the introduction of the i_cmd
macro that parses back the short interval syntax and is used for
compact, parseable print. Here's the feature highlights of this scheme:

- Compact printing is changed to use the unicode double-wide brackets,
  to distinguish it from ascii brackets, which already have a different meaning

- `show(::IO, x::Interval)` is always parseable, both in compact mode
  and in non-compact mode. Note that the compact interval printing
  changed from using `..` to `,`. This is to clarify the parsing rules
  inside the macro. In particular, other than the first and last character,
  the macro uses tuple parsing rules, so you can do things like:

```
julia> i`(now(), now()+Hour(1)]`
(2021-07-10T02:51:37.780, 2021-07-10T03:51:37.780]
```

- The i-macro parses both ASCII and double-wide brackets as interval bounds

- The parseable and non-parseable printing now generally only differs in
  the printing or non-printing of the i-macro. This is quite useful to
  get something parseable back from an interval that was printed in
  non-parseable context.

- Closed intervals have special printing using the `..` operator to make the
  output match the input syntax. Of course they can also be constructed using
  the i-macro (but will still be printed as `..`).

- Even for non-compact, parseable prinitng, the long type parameter is dropped,
  in favor of only showing the Open/Closed setting, since the type should be
  redunant with the parsing of the bounds being printed.

As I said, several of these are independent, so we might only want a subset
of these, but I thought, I'd present it as one big thing.

This doesn't have any doc(test) changes yet, since I figure things will
change, and I didn't want to bother doing that twice :).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant