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

Using merged columns and rows in a table #93

Closed
g3rzi opened this issue Apr 9, 2020 · 6 comments · Fixed by #116
Closed

Using merged columns and rows in a table #93

g3rzi opened this issue Apr 9, 2020 · 6 comments · Fixed by #116
Assignees
Labels
disc General discussion enhancement New feature or request

Comments

@g3rzi
Copy link

g3rzi commented Apr 9, 2020

Hi,
first, you did an amazing work with this library, this is really great :).

I didn't find it in the examples, not sure if it possible but I wanted to check.
I want to create a table with merged columns and rows, like this one:
image

Is it possible?
In the header I am trying to create a column with merged column ("RCE") that is above two cells ("EXEC" and "RUN").

The second thing is to create separated lines from the "Container" column until the end because in each Pod I have number of containers and I want them to appear in separated rows.

This is how it looks when I tried to print it:

┌────────────────────────────────────────────────────────────┐
│                            Nodes                           │
├───┬─────────┬────────┬───────────┬────────────┬──────┬─────┤
│   │ NODE IP │ PODS   │ NAMESPACE │ CONTAINERS │ RCE  │ RCE │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│   │         │        │           │            │ EXEC │ RUN │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 1 │ 1.1.1.1 │ Pod1A  │ NS1       │ container1 │ Y    │ Y   │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 2 │         │        │           │ container2 │ N    │ N   │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 3 │         │        │           │ container3 │ Y    │ N   │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 4 │         │ Pod1_B │ NS3       │ container4 │ Y    │ Y   │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 5 │         │        │           │ container5 │ N    │ N   │
├───┼─────────┼────────┼───────────┼────────────┼──────┼─────┤
│ 6 │ 2.2.2.2 │ Pod2   │ NS2       │ container6 │ Y    │ Y   │
└───┴─────────┴────────┴───────────┴────────────┴──────┴─────┘

You can see the two issues I was talking about:
image

I wanted to merge all the one I marked.
Is it possible ?

This is the code for the printing I tried:

package main

import (
	"fmt"
	"github.com/jedib0t/go-pretty/table"
	"github.com/jedib0t/go-pretty/text"
)

type nodeInfo struct {
	nodeIP string
	pods	 []Pod
}

type Pod struct {
	Name string
	Namespace string
	Containers []Container
}
type Container struct {
	name string
	rceExec string
	rceRun string
}

func tableFoo(){

	nodes := []nodeInfo{
		{
			nodeIP: "1.1.1.1",
			pods:   []Pod{
				{
					Name: "Pod1A",
					Namespace:  "NS1",
					Containers: []Container{
						{name: "container1", rceExec: "Y", rceRun: "Y",},
						{name: "container2", rceExec: "N", rceRun: "N",},
						{name: "container3", rceExec: "Y", rceRun: "N",},
					},
				},	{
					Name: "Pod1_B",
					Namespace:  "NS3",
					Containers: []Container{
						{name: "container4", rceExec: "Y", rceRun: "Y",},
						{name: "container5", rceExec: "N", rceRun: "N",},
					},
				},
			},
		},		{
			nodeIP: "2.2.2.2",
			pods:   []Pod{
				{
					Name: "Pod2",
					Namespace:  "NS2",
					Containers: []Container{
						{name: "container6", rceExec: "Y", rceRun: "Y",},
					},
				},
			},
		},
	}
	tw := table.NewWriter()
	tw.AppendHeader(table.Row{"Node IP", "Pods", "Namespace", "Containers", "RCE", "RCE"})
	tw.AppendHeader(table.Row{"", "", "", "", "EXEC", "RUN"})

	//tw1 := table.NewWriter()
	for _, node := range nodes {
		printNodeOnce := true
		nodeIpTemp := ""
		if printNodeOnce {
			nodeIpTemp = node.nodeIP
			printNodeOnce = false
		}
		for _, pod := range node.pods {
			printOnce := true

			for _, container := range pod.Containers {
				podTemp := ""
				namespaceTemp := ""
				if printOnce{
					podTemp = pod.Name
					namespaceTemp = pod.Namespace
					printOnce = false
				}

				tw.AppendRow(table.Row{nodeIpTemp, podTemp, namespaceTemp, container.name, container.rceExec, container.rceRun})
				nodeIpTemp = ""
			}

		}
	}

	tw.SetTitle("Nodes")
	tw.SetStyle(table.StyleLight)
	tw.Style().Title.Align = text.AlignCenter
	tw.Style().Options.SeparateRows = true
	tw.SetAutoIndex(true)
	fmt.Println(tw.Render())
}

func main(){
	tableFoo()
}

/disc /enhancement

@jedib0t jedib0t self-assigned this May 7, 2020
@jedib0t jedib0t added the enhancement New feature or request label May 7, 2020
@jedib0t
Copy link
Owner

jedib0t commented May 7, 2020

Hey thanks for the suggestion. I've always wanted to implement such a feature. Will look into it.

@jedib0t jedib0t added the disc General discussion label May 9, 2020
@jedib0t
Copy link
Owner

jedib0t commented May 9, 2020

Hey @g3rzi... I was able to implement auto-merging cells vertically. The code is currently in the table-automerge branch with unit-tests which has examples. The example is also attached to the open PR #107. Can you try using the new code from that branch and lemme know if it works for you?

Merging cells horizontally is not supported yet - I'm yet to figure out how to implement it.

@jedib0t
Copy link
Owner

jedib0t commented May 9, 2020

I've figured out a way to possibly support horizontal cell merges, but this may need some deep changes to the Table rendering logic and may take a lot more time. I'm gonna merge #107 for now.

Will work on the changes needed for horizontal cell merges, but a PR for that will appear at a later point of time. Will keep this issue open until then.

@jedib0t
Copy link
Owner

jedib0t commented May 13, 2020

Hey @g3rzi... after some trial and error, I'm afraid I won't be able to support horizontal cell merges as of now. With the way the code is structured currently, supporting horizontal merges would make the interfaces complicated and hard to understand.

Here is a sample output from my testing code:

┌───┬─────────┬────────┬───────────┬───────────┬──────┬─────┐
│   │ NODE IP │ PODS   │ NAMESPACE │ CONTAINER │ RCE        │
├   ┤         │        │           │           ├──────┼─────┤
│   │         │        │           │           │ EXEC │ RUN │
├───┼─────────┼────────┼───────────┼───────────┼──────┼─────┤
│ 1 │ 1.1.1.1 │ Pod 1A │ NS 1A     │ C 1       │   Y        │
├───┤         │        │           ├───────────┼──────┼─────┤
│ 2 │         │        │           │ C 2       │   Y  │  N  │
├───┤         │        ├───────────┼───────────┼──────┼─────┤
│ 3 │         │        │ NS 1B     │ C 3       │   N        │
├───┤         ├────────┼───────────┼───────────┼──────┼─────┤
│ 4 │         │ Pod 1B │ NS 2      │ C 4       │   Y        │
├───┤         │        │           ├───────────┼──────┼─────┤
│ 5 │         │        │           │ C 5       │   Y  │  N  │
├───┼─────────┼────────┼───────────┼───────────┼──────┼─────┤
│ 6 │ 2.2.2.2 │ Pod 2  │ NS 3      │ C 6       │   Y        │
├───┤         │        │           ├───────────┼──────┼─────┤
│ 7 │         │        │           │ C 7       │   Y  │  N  │
└───┴─────────┴────────┴───────────┴───────────┴──────┴─────┘

I'll address this at a later point of time if I can make some deep structural changes to the rendering logic.

@jedib0t
Copy link
Owner

jedib0t commented May 15, 2020

Hey @g3rzi I've supported both horizontal and vertical auto-merges. Please take a look at the latest release and the documentation @ https://github.com/jedib0t/go-pretty/blob/master/table/README.md#auto-merge

@g3rzi
Copy link
Author

g3rzi commented May 16, 2020

@jedib0t this is great!! 😃
Thank you for the quick response, I will check it and update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disc General discussion enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants