go-pretty icon indicating copy to clipboard operation
go-pretty copied to clipboard

Question about AutoMerge option

Open darbogx opened this issue 1 year ago • 1 comments

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.

darbogx avatar Sep 09 '22 07:09 darbogx

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 avatar Sep 09 '22 18:09 jedib0t

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

darbogx avatar Oct 24 '22 05:10 darbogx

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

jedib0t avatar Oct 24 '22 15:10 jedib0t

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.

jedib0t avatar Nov 04 '22 02:11 jedib0t

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?

darbogx avatar Nov 04 '22 08:11 darbogx

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 avatar Nov 04 '22 16:11 jedib0t

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.

jedib0t avatar Nov 05 '22 21:11 jedib0t

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

cryi avatar Nov 05 '22 22:11 cryi

@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 avatar Nov 05 '22 22:11 jedib0t

@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 avatar Nov 06 '22 15:11 jedib0t

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

jedib0t avatar Nov 06 '22 15:11 jedib0t

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

cryi avatar Nov 06 '22 16:11 cryi

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

darbogx avatar Nov 07 '22 05:11 darbogx