jless icon indicating copy to clipboard operation
jless copied to clipboard

feature request: abbreviated truncated views

Open brewingcode opened this issue 3 years ago • 2 comments

Would it be possible to add a command line option to render the truncated strings as just a count (key-value pairs for objects, array elements for arrays)? For example, here's what jless currently says with https://api.github.com/repos/PaulJuliusMartinez/jless/commits:

▽ [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, …]
  ▽ [0]: {sha: "cdcb02038730f0cd3f5ccea594511e3cfb37887a", node_id: "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0N…", …}
      sha: "cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      node_id: "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0NTExZTNjZmIzNzg4N2E"
    ▷ commit: {author: {…}, committer: {…}, message: "Upgrade arg parsing to clap v3 (#17)\n\nSwitches arg parsing to st…", …}
      url: "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      html_url: "https://github.com/PaulJuliusMartinez/jless/commit/cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      comments_url: "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a/…"
    ▷ author: {login: "tranzystorek-io", id: 5671049, node_id: "MDQ6VXNlcjU2NzEwNDk=", avatar_url: "https://avatars.gith…", …}
    ▷ committer: {login: "PaulJuliusMartinez", id: 3301004, node_id: "MDQ6VXNlcjMzMDEwMDQ=", avatar_url: "https://avatar…", …}
    ▷ parents: [{sha: "619f74697d1c0277b07d685bcd061262010485f2", url: "https://api.github.com/repos/PaulJuliusMartinez…", …}]
  ▷ [1]: {sha: "619f74697d1c0277b07d685bcd061262010485f2", node_id: "C_kwDOFYWjXtoAKDYxOWY3NDY5N2QxYzAyNzdiMDdkNjg1YmNkM…", …}
  ▷ [2]: {sha: "ba2e6e7dc7855bf32852bd8a113619e91f016e3d", node_id: "C_kwDOFYWjXtoAKGJhMmU2ZTdkYzc4NTViZjMyODUyYmQ4YTExM…", …}
  ▷ [3]: {sha: "bf449651685c8709a2669e360bb3902f9f68afbd", node_id: "C_kwDOFYWjXtoAKGJmNDQ5NjUxNjg1Yzg3MDlhMjY2OWUzNjBiY…", …}
  ▷ [4]: {sha: "b3f21a215a9f3b12156f542cc78f330624d5faf1", node_id: "C_kwDOFYWjXtoAKGIzZjIxYTIxNWE5ZjNiMTIxNTZmNTQyY2M3O…", …}
  ▷ [5]: {sha: "814ac066ae52ad052f2bc685d358821c00f41fea", node_id: "C_kwDOFYWjXtoAKDgxNGFjMDY2YWU1MmFkMDUyZjJiYzY4NWQzN…", …}

I have a tool of my own for viewing JSON that shows the same structure like so:

[
  {
    "sha": "cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "node_id": "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0NTExZTNjZmIzNzg4N2E",
    "commit": { 7 },
    "url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "html_url": "https://github.com/PaulJuliusMartinez/jless/commit/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "comments_url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a/co
mments",
    "author": { 18 },
    "committer": { 18 },
    "parents": [ 1 ]
  },
  { 9 },
  { 9 },
  { 9 },
  { 9 },
  { 9 },
  { 9 },

While I don't need an exact match, I figure that jless could (optionally) produce something like:

▽ [ 30 ]
  ▽ [0]: { 9 }
      sha: "cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      node_id: "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0NTExZTNjZmIzNzg4N2E"
    ▷ commit: { 7 }
      url: "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      html_url: "https://github.com/PaulJuliusMartinez/jless/commit/cdcb02038730f0cd3f5ccea594511e3cfb37887a"
      comments_url: "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a/…"
    ▷ author: { 18 }
    ▷ committer: { 18 }
    ▷ parents: [ 1 ]
  ▷ [1]: { 9 }
  ▷ [2]: { 9 }
  ▷ [3]: { 9 }
  ▷ [4]: { 9 }
  ▷ [5]: { 9 }

I'm happy to give this a try if you could point me to a starting place. I know zero rust, but am familiar with lots of other languages. It looks like lineprinter.rs:fill_in_value() is where these labels are generated, and maybe from there into truncatedstrview.rs, but I quickly got lost in there.

brewingcode avatar Feb 16 '22 01:02 brewingcode

Since the count might be confused with an actual value, what if you appended items to the counts?

[
  {
    "sha": "cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "node_id": "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0NTExZTNjZmIzNzg4N2E",
    "commit": { 7 items },
    "url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "html_url": "https://github.com/PaulJuliusMartinez/jless/commit/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "comments_url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a/co
mments",
    "author": { 18 item },
    "committer": { 18 items },
    "parents": [ 1 item ] // <---- without 'item', it would be easy to confuse it with a value 
  },
  { 9 items },
  { 9 items },
  { 9 items },
  { 9 items },
  { 9 items },
  { 9 items },

Another approach would be to show the count next to the braces e.g

[
  {
    "sha": "cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "node_id": "C_kwDOFYWjXtoAKGNkY2IwMjAzODczMGYwY2QzZjVjY2VhNTk0NTExZTNjZmIzNzg4N2E",
    "commit": { … }, // 7 items
    "url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "html_url": "https://github.com/PaulJuliusMartinez/jless/commit/cdcb02038730f0cd3f5ccea594511e3cfb37887a",
    "comments_url": "https://api.github.com/repos/PaulJuliusMartinez/jless/commits/cdcb02038730f0cd3f5ccea594511e3cfb37887a/co
mments",
    "author": { … }, // 1 item
    "committer": { … }, // 18 items
    "parents": [ … ] // 1 item
  },
  { … }, // 9 items
  { … }, // 9 items
  { … }, // 9 items
  { … }, // 9 items
  { … }, // 9 items
  { … }, // 9 items

The latter approach is how the JSON Formatter extension for chrome handles collapsed nodes.

ducaale avatar Feb 18 '22 16:02 ducaale

Hmmm, I initially didn't like the added busy-ness of having "items" all over the place, and also the terminal coloring makes it very clear what is a preview and what is not. But I don't like assuming terminal coloring will fix everything, and I did stumble across one case where it is ambiguous without terminal coloring, and that's in line mode, which renders collapsed arrays as previews on a single line:

        },
        "geometry": {
          "type": "Point",
          "coordinates": [ 3 ]
        }
      },

So I'll add "item"/"items". Good suggestion, thanks @ducaale.

In fact, since line mode relies on single-line previews, I've disabled Preview::None in line mode, too. Otherwise, collapsed nodes in line mode show nothing, which is super bad:

        },
        "geometry": {
          "type": "Point",
          "coordinates":            // nothing here, oops
        }

brewingcode avatar Feb 18 '22 18:02 brewingcode