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

Question about AutoMerge option #226

Closed
darbogx opened this issue Sep 9, 2022 · 13 comments · Fixed by #238
Closed

Question about AutoMerge option #226

darbogx opened this issue Sep 9, 2022 · 13 comments · Fixed by #238
Labels
enhancement New feature or request

Comments

@darbogx
Copy link

darbogx commented Sep 9, 2022

It is not a bug, but I have a question about generating table with auto-merge option.
I wrote following code:

package main

import (
	"fmt"

	"github.com/jedib0t/go-pretty/v6/table"
	"github.com/jedib0t/go-pretty/v6/text"
)

func main() {
	t := table.NewWriter()
	rowConfigAutoMerge := table.RowConfig{AutoMerge: true}

	t.AppendSeparator()
	t.Style().Title.Align = text.AlignCenter

	t.AppendRow(table.Row{"No", "Text1", "Text2", "Text3", "Text4"}, rowConfigAutoMerge)
	t.AppendSeparator()
	t.AppendRows([]table.Row{
		{"1", "Some Text 1", "Desctiption", "Long Text Long Text", "Long Text Long Text"},
		{"2", "Some Text 2", "Desctiption", "Text", "Text"},
		{"3", "Some Text 3", "Desctiption", "Text", "Text"},
		{"4", "Some Text 4", "Desctiption", "Text", "TextB"},
	}, rowConfigAutoMerge)

	fmt.Println(t.Render())
}

which generate output table like below:

+----+-------------+-------------+---------------------+---------------------+
| No | Text1       | Text2       | Text3               | Text4               |
+----+-------------+-------------+---------------------+---------------------+
| 1  | Some Text 1 | Desctiption |            Long Text Long Text            |
| 2  | Some Text 2 | Desctiption |                    Text                   |
| 3  | Some Text 3 | Desctiption |                    Text                   |
| 4  | Some Text 4 | Desctiption |                    Text                   |
+----+-------------+-------------+-------------------------------------------+

The combined width of Text3 and Text4 is equal to the sum of the widths of the largest elements in Text3 and Text4.
What I want to achive is more "compact" and smaller table, like below. Is there any option to merge two (Text3 and Text4) or even more columns to get width of their sum equal to the largest element in any merged row?

+----+-------------+-------------+----------+----------+
| No | Text1       | Text2       | Text3    | Text4    |
+----+-------------+-------------+----------+----------+
| 1  | Some Text 1 | Desctiption | Long Text Long Text |
| 2  | Some Text 2 | Desctiption |         Text        |
| 3  | Some Text 3 | Desctiption |         Text        |
| 4  | Some Text 4 | Desctiption |         Text        |
+----+-------------+-------------+---------------------+

I hope I explained it clearly.

@jedib0t
Copy link
Owner

jedib0t commented Sep 9, 2022

Hey yes I got what you are asking for. I should implement some sort of look ahead to figure out the column lengths after merges. Will try to work on this soon.

@jedib0t jedib0t added the enhancement New feature or request label Sep 9, 2022
@darbogx
Copy link
Author

darbogx commented Oct 24, 2022

Hi @jedib0t, I hope You are doing well. Any update to this issue?

@jedib0t
Copy link
Owner

jedib0t commented Oct 24, 2022

Hey sorry got busy with "work" work. Will try to get this done soon.

@jedib0t
Copy link
Owner

jedib0t commented Nov 4, 2022

Hey @darbogx I've made some progress with tables where some columns are merged in all the rows. Check out the unit-tests here and here. However I'm yet to address your use-case. Working on it whenever I have time away from work and kid.

Edit: now that I think about it a little more the solution might be a lot simpler than the current one I cooked up.

@darbogx
Copy link
Author

darbogx commented Nov 4, 2022

Hi @jedib0t,
Many Thanks, You did an amazing job! I did some tests with following code and it seems that something is wrong.


import (
	"fmt"
	"testing"

	"github.com/jedib0t/go-pretty/v6/text"
)

func TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom(t *testing.T) {
	rcAutoMerge := RowConfig{AutoMerge: true}

	//Column 5-8 with different String  ==> Result As I expected
	t.Run("Column 5-8 with different String", func(t *testing.T) {
		tw := NewWriter()
		tw.AppendHeader(Row{"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRW", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRH", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRY"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.SetAutoIndex(true)
		tw.SetColumnConfigs([]ColumnConfig{
			{Number: 5, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 6, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 7, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 8, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
		})
		tw.SetStyle(StyleLight)
		tw.Style().Options.SeparateRows = true

		fmt.Println(tw.Render())
	})
	//Column 5-8 with equal String  ==> Result As I expected
	t.Run("Column 5-8 with equal String", func(t *testing.T) {
		tw := NewWriter()
		tw.AppendHeader(Row{"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.SetAutoIndex(true)
		tw.SetColumnConfigs([]ColumnConfig{
			{Number: 5, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 6, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 7, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 8, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
		})
		tw.SetStyle(StyleLight)
		tw.Style().Options.SeparateRows = true

		fmt.Println(tw.Render())
	})
	//Column 5-6 and 7-8 with equal String  ==> Result As I not expected
	t.Run("Column 5-6 and 7-8 with equal String", func(t *testing.T) {
		tw := NewWriter()
		tw.AppendHeader(Row{"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRR"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.SetAutoIndex(true)
		tw.SetColumnConfigs([]ColumnConfig{
			{Number: 5, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 6, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 7, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 8, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
		})
		tw.SetStyle(StyleLight)
		tw.Style().Options.SeparateRows = true

		fmt.Println(tw.Render())
	})
	//Column 5-7 with equal String, column 8 other  ==> Result As I not expected
	t.Run("Column 5-7 with equal String, column 8 other", func(t *testing.T) {
		tw := NewWriter()
		tw.AppendHeader(Row{"Column 1", "Column 2", "Column 3", "Column 4", "Column 5", "Column 6", "Column 7", "Column 8"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 1", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DR", "4F8F5CB531E3D49A61CF417CD133792CCFA501FD8DA53EE368FED20E5FE0248C3A0B64F98A6533CEE1DA614C3A8DDEC791FF05FEE6D971D57C1348320F4EB42DRE"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.AppendRow(Row{"1.1.1.1", "Pod 1A", "NS 1A", "C 2", "Y", "Y", "Y", "Y"}, rcAutoMerge)
		tw.SetAutoIndex(true)
		tw.SetColumnConfigs([]ColumnConfig{
			{Number: 5, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 6, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 7, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
			{Number: 8, Align: text.AlignCenter, AlignFooter: text.AlignCenter, AlignHeader: text.AlignCenter, WidthMax: 24, WidthMaxEnforcer: text.WrapHard},
		})
		tw.SetStyle(StyleLight)
		tw.Style().Options.SeparateRows = true

		fmt.Println(tw.Render())
	})
}

What I want to archive is to insert long strings into table with wrapping. Above code gives me following tables:

=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_different_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │         COLUMN 5         │         COLUMN 6         │         COLUMN 7         │         COLUMN 8         │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │
│   │          │          │          │          │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │
│   │          │          │          │          │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │
│   │          │          │          │          │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │
│   │          │          │          │          │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │
│   │          │          │          │          │         0F4EB42DR        │        0F4EB42DRW        │        0F4EB42DRH        │        0F4EB42DRY        │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┴──────────────────────────┴──────────────────────────┴──────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────┘
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_equal_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │ COLUMN 5 │ COLUMN 6 │ COLUMN 7 │ COLUMN 8 │
├───┼──────────┼──────────┼──────────┼──────────┼──────────┴──────────┴──────────┴──────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │      4F8F5CB531E3D49A61CF417CD133792C     │
│   │          │          │          │          │      CFA501FD8DA53EE368FED20E5FE0248C     │
│   │          │          │          │          │      3A0B64F98A6533CEE1DA614C3A8DDEC7     │
│   │          │          │          │          │      91FF05FEE6D971D57C1348320F4EB42D     │
│   │          │          │          │          │                     R                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                     Y                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                     Y                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────┘
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-6_and_7-8_with_equal_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┬──────────────────────────┬──────────┬──────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │         COLUMN 5         │         COLUMN 6         │ COLUMN 7 │ COLUMN 8 │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┴──────────────────────────┼──────────┴──────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │               4F8F5CB531E3D49A61CF417C              │   4F8F5CB531E3D49A  │
│   │          │          │          │          │               D133792CCFA501FD8DA53EE3              │   61CF417CD133792C  │
│   │          │          │          │          │               68FED20E5FE0248C3A0B64F9              │   CFA501FD8DA53EE3  │
│   │          │          │          │          │               8A6533CEE1DA614C3A8DDEC7              │   68FED20E5FE0248C  │
│   │          │          │          │          │               91FF05FEE6D971D57C134832              │   3A0B64F98A6533CE  │
│   │          │          │          │          │                      0F4EB42DR                      │   E1DA614C3A8DDEC7  │
│   │          │          │          │          │                                                     │   91FF05FEE6D971D5  │
│   │          │          │          │          │                                                     │   7C1348320F4EB42D  │
│   │          │          │          │          │                                                     │          RR         │
├───┼──────────┼──────────┼──────────┼──────────┼─────────────────────────────────────────────────────┴─────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                     Y                                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                     Y                                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────────────────────┘
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-7_with_equal_String,_column_8_other
┌───┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │         COLUMN 5         │         COLUMN 6         │         COLUMN 7         │         COLUMN 8         │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┴──────────────────────────┴──────────────────────────┼──────────────────────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │                            4F8F5CB531E3D49A61CF417C                            │ 4F8F5CB531E3D49A61CF417C │
│   │          │          │          │          │                            D133792CCFA501FD8DA53EE3                            │ D133792CCFA501FD8DA53EE3 │
│   │          │          │          │          │                            68FED20E5FE0248C3A0B64F9                            │ 68FED20E5FE0248C3A0B64F9 │
│   │          │          │          │          │                            8A6533CEE1DA614C3A8DDEC7                            │ 8A6533CEE1DA614C3A8DDEC7 │
│   │          │          │          │          │                            91FF05FEE6D971D57C134832                            │ 91FF05FEE6D971D57C134832 │
│   │          │          │          │          │                                    0F4EB42DR                                   │        0F4EB42DRE        │
├───┼──────────┼──────────┼──────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┴──────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────┘

First 2 are ok, but with 3 and 4 I had problems. They are not rendering as I expected and I don't know if I am doing something wrong or there is other problem with the rendering?

@jedib0t
Copy link
Owner

jedib0t commented Nov 4, 2022

Pushed a simpler fix to the same branch. Here is the output now:

=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom
--- PASS: TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom (0.00s)
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_different_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┬──────────────────────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │         COLUMN 5         │         COLUMN 6         │         COLUMN 7         │         COLUMN 8         │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┼──────────────────────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │ 4F8F5CB531E3D49A61CF417C │
│   │          │          │          │          │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │ D133792CCFA501FD8DA53EE3 │
│   │          │          │          │          │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │ 68FED20E5FE0248C3A0B64F9 │
│   │          │          │          │          │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │ 8A6533CEE1DA614C3A8DDEC7 │
│   │          │          │          │          │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │ 91FF05FEE6D971D57C134832 │
│   │          │          │          │          │         0F4EB42DR        │        0F4EB42DRW        │        0F4EB42DRH        │        0F4EB42DRY        │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┴──────────────────────────┴──────────────────────────┴──────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                                     Y                                                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────┘
    --- PASS: TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_different_String (0.00s)
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_equal_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │ COLUMN 5 │ COLUMN 6 │ COLUMN 7 │ COLUMN 8 │
├───┼──────────┼──────────┼──────────┼──────────┼──────────┴──────────┴──────────┴──────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │          4F8F5CB531E3D49A61CF417C         │
│   │          │          │          │          │          D133792CCFA501FD8DA53EE3         │
│   │          │          │          │          │          68FED20E5FE0248C3A0B64F9         │
│   │          │          │          │          │          8A6533CEE1DA614C3A8DDEC7         │
│   │          │          │          │          │          91FF05FEE6D971D57C134832         │
│   │          │          │          │          │                 0F4EB42DR                 │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                     Y                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                     Y                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────┘
    --- PASS: TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-8_with_equal_String (0.00s)
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-6_and_7-8_with_equal_String
┌───┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┬──────────┬──────────────────────────┬──────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │         COLUMN 5         │ COLUMN 6 │         COLUMN 7         │ COLUMN 8 │
├───┼──────────┼──────────┼──────────┼──────────┼──────────────────────────┴──────────┼──────────────────────────┴──────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │       4F8F5CB531E3D49A61CF417C      │       4F8F5CB531E3D49A61CF417C      │
│   │          │          │          │          │       D133792CCFA501FD8DA53EE3      │       D133792CCFA501FD8DA53EE3      │
│   │          │          │          │          │       68FED20E5FE0248C3A0B64F9      │       68FED20E5FE0248C3A0B64F9      │
│   │          │          │          │          │       8A6533CEE1DA614C3A8DDEC7      │       8A6533CEE1DA614C3A8DDEC7      │
│   │          │          │          │          │       91FF05FEE6D971D57C134832      │       91FF05FEE6D971D57C134832      │
│   │          │          │          │          │              0F4EB42DR              │              0F4EB42DRR             │
├───┼──────────┼──────────┼──────────┼──────────┼─────────────────────────────────────┴─────────────────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                     Y                                     │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                                     Y                                     │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────────────────────┘
    --- PASS: TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-6_and_7-8_with_equal_String (0.00s)
=== RUN   TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-7_with_equal_String,_column_8_other
┌───┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────┬──────────────────────────┐
│   │ COLUMN 1 │ COLUMN 2 │ COLUMN 3 │ COLUMN 4 │ COLUMN 5 │ COLUMN 6 │ COLUMN 7 │         COLUMN 8         │
├───┼──────────┼──────────┼──────────┼──────────┼──────────┴──────────┴──────────┼──────────────────────────┤
│ 1 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 1      │    4F8F5CB531E3D49A61CF417C    │ 4F8F5CB531E3D49A61CF417C │
│   │          │          │          │          │    D133792CCFA501FD8DA53EE3    │ D133792CCFA501FD8DA53EE3 │
│   │          │          │          │          │    68FED20E5FE0248C3A0B64F9    │ 68FED20E5FE0248C3A0B64F9 │
│   │          │          │          │          │    8A6533CEE1DA614C3A8DDEC7    │ 8A6533CEE1DA614C3A8DDEC7 │
│   │          │          │          │          │    91FF05FEE6D971D57C134832    │ 91FF05FEE6D971D57C134832 │
│   │          │          │          │          │            0F4EB42DR           │        0F4EB42DRE        │
├───┼──────────┼──────────┼──────────┼──────────┼────────────────────────────────┴──────────────────────────┤
│ 2 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                             Y                             │
├───┼──────────┼──────────┼──────────┼──────────┼───────────────────────────────────────────────────────────┤
│ 3 │ 1.1.1.1  │ Pod 1A   │ NS 1A    │ C 2      │                             Y                             │
└───┴──────────┴──────────┴──────────┴──────────┴───────────────────────────────────────────────────────────┘
    --- PASS: TestTable_Render_AutoMerge_WithSomeColumnsCompletelyMergedCustom/Column_5-7_with_equal_String,_column_8_other (0.00s)
PASS

It can still use some work with the header columns in the 3rd case, but it looks so much better than before overall.

I'll probably commit the final fix and tag it this weekend. 🤞🏽

Edit: I meant I'll fix the issue with the headers in the third table above before tagging.

@jedib0t
Copy link
Owner

jedib0t commented Nov 5, 2022

Hey @darbogx ... I've delivered a fix to the main branch using PR #238.

Can you please verify it this works for you? Once you confirm, I can cut a tag.

@cryi
Copy link

cryi commented Nov 5, 2022

Hi @jedib0t, we are dealing with this too. I wonder whether this can be extended for columns. (If it is it, it does not in latest)
I mean we have multiple tables where each has header describing its content. E.g.

header := "Indexed Movies"
t := table.NewWriter()
t.SetStyle(table.StyleLight)
t.SetColumnConfigs([]table.ColumnConfig{{Number: 1, Align: text.AlignLeft}, {Number: 2, Align: text.AlignLeft}})
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{header, header, header, header}, table.RowConfig{AutoMerge: true})
t.AppendSeparator()
t.AppendRow(table.Row{"Title", "Director", "Minutes", "Studio"}, table.RowConfig{AutoMerge: true})
t.AppendSeparator()
for _, movie:= range movies {
   ... // add movies
}

And all columns are extended to width of Indexed Movies

@jedib0t
Copy link
Owner

jedib0t commented Nov 5, 2022

@cryi I'm not sure I understand your question.

Can you open a new issue with:

  • a better snippet of your code with real calls within the for loop
  • what is the output you expect?

Thanks!

Edit.: by the way, you don't need to call AppendSeparator between header and regular rows - they are added automatically for you.

@jedib0t
Copy link
Owner

jedib0t commented Nov 6, 2022

@cryi I think I may have an idea of what you are trying to do... Did you try using the SetTitle interface?

package main

import (
	"fmt"
	"time"

	"github.com/jedib0t/go-pretty/v6/table"
	"github.com/jedib0t/go-pretty/v6/text"
)

type Movie struct {
	Title    string
	Director string
	Minutes  time.Duration
	Studio   string
}

func main() {
	movies := []Movie{
		{Title: "Top Gun", Director: "Tony Scott", Minutes: time.Minute * 110, Studio: "Paramount Pictures"},
		{Title: "Top Gun: Maverick", Director: "Joseph Kosinski", Minutes: time.Minute * 130, Studio: "Paramount Pictures"},
	}

	rowConfig := table.RowConfig{AutoMerge: true}
	t := table.NewWriter()
	t.AppendHeader(table.Row{"Title", "Director", "Minutes", "Studio"}, rowConfig)
	for _, movie := range movies {
		t.AppendRow(table.Row{movie.Title, movie.Director, movie.Minutes, movie.Studio}, rowConfig)
	}
	t.SetTitle("Indexed Movies")
	t.SetStyle(table.StyleLight)
	t.Style().Title.Align = text.AlignCenter

	fmt.Println(t.Render())
}

Output:

┌────────────────────────────────────────────────────────────────────┐
│                           Indexed Movies                           │
├───────────────────┬─────────────────┬─────────┬────────────────────┤
│ TITLE             │ DIRECTOR        │ MINUTES │ STUDIO             │
├───────────────────┼─────────────────┼─────────┼────────────────────┤
│ Top Gun           │ Tony Scott      │ 1h50m0s │ Paramount Pictures │
│ Top Gun: Maverick │ Joseph Kosinski │ 2h10m0s │ Paramount Pictures │
└───────────────────┴─────────────────┴─────────┴────────────────────┘

@jedib0t
Copy link
Owner

jedib0t commented Nov 6, 2022

@darbogx I've cut a tag for you: https://github.com/jedib0t/go-pretty/releases/tag/v6.4.1

@cryi
Copy link

cryi commented Nov 6, 2022

Yes that is wat I was looking for. Thank you vm @jedib0t

@darbogx
Copy link
Author

darbogx commented Nov 7, 2022

Hi @jedib0t, Excellent job! Thank You so much. Now it works as I need.

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

Successfully merging a pull request may close this issue.

3 participants