tabled icon indicating copy to clipboard operation
tabled copied to clipboard

Add `Orientation` support in collapse mode

Open zhiburt opened this issue 2 years ago • 4 comments

We have a general support of it but not in a collapse mode. It's not an easy task to do, but its scope is limited to 1 function.

The idea that we can run this with .collapse() and it will have an affect.

https://github.com/zhiburt/tabled/blob/fa313912a1da2fda40733f2db47371e1e84e2776/json_to_table/tests/test_orientation_modes.rs#L162-L178

zhiburt avatar Sep 28 '22 19:09 zhiburt

Any help on how to implement this?

gautamprikshit1 avatar Jan 08 '23 05:01 gautamprikshit1

Hi @gautamprikshit1

The description is a bit misleading here (sorry).

About Orientation

We have a Orientation abstraction which can be used to tweak a print behavior. Look how it works generally.

We have this json

{
        "key1": "value1",
        "key2": {
            "key1": 123,
            "key2": [1, 2, 3, 4, 5],
        },
        "key3": [
            {"key": 123.3},
            2,
            "asd"
        ]
}

We can tweak Orientation via set_array_mode and set_object_mode.

    let mut table = json_to_table(&json);
    table
        .set_array_mode(Orientation::Horizontal)
        .set_object_mode(Orientation::Horizontal);
vertical orientation (default). horizontal orientation
+------+-----------------------+
| key1 |  value1               |
+------+-----------------------+
| key2 | +------+---------+    |
|      | | key1 |  123    |    |
|      | +------+---------+    |
|      | | key2 | +-----+ |    |
|      | |      | |  1  | |    |
|      | |      | +-----+ |    |
|      | |      | |  2  | |    |
|      | |      | +-----+ |    |
|      | |      | |  3  | |    |
|      | |      | +-----+ |    |
|      | |      | |  4  | |    |
|      | |      | +-----+ |    |
|      | |      | |  5  | |    |
|      | |      | +-----+ |    |
|      | +------+---------+    |
+------+-----------------------+
| key3 | +-------------------+ |
|      | | +-----+---------+ | |
|      | | | key |  123.3  | | |
|      | | +-----+---------+ | |
|      | +-------------------+ |
|      | |  2                | |
|      | +-------------------+ |
|      | |  asd              | |
|      | +-------------------+ |
+------+-----------------------+
+----------+---------------------------------------------+-------------------------------+
| key1     | key2                                        | key3                          |
+----------+---------------------------------------------+-------------------------------+
|  value1  | +-------+---------------------------------+ | +-------------+-----+-------+ |
|          | | key1  | key2                            | | | +---------+ |  2  |  asd  | |
|          | +-------+---------------------------------+ | | | key     | |     |       | |
|          | |  123  | +-----+-----+-----+-----+-----+ | | | +---------+ |     |       | |
|          | |       | |  1  |  2  |  3  |  4  |  5  | | | | |  123.3  | |     |       | |
|          | |       | +-----+-----+-----+-----+-----+ | | | +---------+ |     |       | |
|          | +-------+---------------------------------+ | +-------------+-----+-------+ |
+----------+---------------------------------------------+-------------------------------+

But unfortunately this doesn't work when we set collapse view.

Collapse

.collapse method squashes tables together so you'd get this table for the json above.

    let mut table = json_to_table(&json);
    table.collapse();
+------+-------------+
| key1 | value1      |
+------+------+------+
| key2 | key1 | 123  |
|      +------+------+
|      | key2 | 1    |
|      |      +------+
|      |      | 2    |
|      |      +------+
|      |      | 3    |
|      |      +------+
|      |      | 4    |
|      |      +------+
|      |      | 5    |
+------+-----++------+
| key3 | key | 123.3 |
|      +-----+-------+
|      | 2           |
|      +-------------+
|      | asd         |
+------+-------------+

But we CAN'T yet set a orientation when being in this view. The issue is to add a support for it.

So it would look something like this.

    let mut table = json_to_table(&json);
    table
        .set_array_mode(Orientation::Horizontal)
        .set_object_mode(Orientation::Horizontal)
        .collapse();
+-----------+--------------------------+-----------------+
|    key1   |             key2         |   key3          |
+-----------+------+-------------------+-------+---+-----+
|  value1   | key1 |        key2       |   key | 2 | asd |
|           +------+---+---+---+---+---+-------+   |     |
|           | 123  | 1 | 2 | 3 | 4 | 5 | 123.3 |   |     |
+-----------+------+---+---+---+---+---+-------+---+-----+

zhiburt avatar Jan 08 '23 09:01 zhiburt

From code perspective you'd need to create an analogues function to json_to_table_r to print a horizontal mode for a serde_json::Value.

https://github.com/zhiburt/tabled/blob/64d68081580e05e7efe25945daa431a691cf2cda/json_to_table/src/table.rs#L198-L202

PS: To be honest even though it requires to create only 1 function is PRETTY messy thing. (I mean it's relatively complex function)

zhiburt avatar Jan 08 '23 09:01 zhiburt

#240 for example much easier to do.

zhiburt avatar Jan 08 '23 19:01 zhiburt