Skip to content

Commit a7a73c3

Browse files
committed
fix: disabled bold decoration when IBMCLOUD_COLOR is unset
1 parent d31af25 commit a7a73c3

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

bluemix/terminal/table.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ func (t *PrintableTable) Print() {
127127
tbl.Style().Options.SeparateHeader = false
128128
tbl.Style().Options.SeparateRows = false
129129
tbl.Style().Format.Header = text.FormatDefault
130-
tbl.Style().Color.Header = text.Colors{text.Bold}
130+
if ColorsEnabled() {
131+
tbl.Style().Color.Header = text.Colors{text.Bold}
132+
}
131133

132134
headerRow, rows := t.createPrettyRowsAndHeaders()
133135
columnConfig := t.createColumnConfigs()

bluemix/terminal/table_test.go

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestPrintTableSimple(t *testing.T) {
2424
testTable.Print()
2525
assert.Contains(t, buf.String(), "test2")
2626
assert.Contains(t, buf.String(), "row1")
27-
assert.Equal(t, bold+"test1 "+reset+bold+"test2 "+reset+"\nrow1 row2\n", buf.String())
27+
assert.Equal(t, "test1 test2\nrow1 row2\n", buf.String())
2828
}
2929

3030
func TestPrintTableJson(t *testing.T) {
@@ -44,7 +44,7 @@ func TestEmptyHeaderTable(t *testing.T) {
4444
testTable.Add("row1", "row2")
4545
testTable.Print()
4646
assert.Contains(t, buf.String(), "row1")
47-
assert.Equal(t, bold+" "+reset+bold+" "+reset+"\nrow1 row2\n", buf.String())
47+
assert.Equal(t, "\nrow1 row2\n", buf.String())
4848
}
4949

5050
func TestEmptyHeaderTableJson(t *testing.T) {
@@ -85,7 +85,7 @@ func TestNotEnoughRowEntires(t *testing.T) {
8585
testTable.Add("", "row2")
8686
testTable.Print()
8787
assert.Contains(t, buf.String(), "row1")
88-
assert.Equal(t, bold+"col1 "+reset+bold+"col2 "+reset+"\nrow1\n row2\n", buf.String())
88+
assert.Equal(t, "col1 col2\nrow1\n row2\n", buf.String())
8989
}
9090

9191
func TestMoreColThanTerminalWidth(t *testing.T) {
@@ -95,8 +95,9 @@ func TestMoreColThanTerminalWidth(t *testing.T) {
9595
testTable.Add("row1", "row2")
9696
testTable.Print()
9797
assert.Contains(t, buf.String(), "row1")
98-
assert.Equal(t, bold+"col1 "+reset+bold+" "+reset+"\nrow1 row2\n", buf.String())
99-
os.Unsetenv("TEST_TERMINAL_WIDTH")
98+
assert.Equal(t, "col1\nrow1 row2\n", buf.String())
99+
err := os.Unsetenv("TEST_TERMINAL_WIDTH")
100+
assert.Nil(t, err)
100101
}
101102

102103
func TestWideHeaderNames(t *testing.T) {
@@ -105,7 +106,7 @@ func TestWideHeaderNames(t *testing.T) {
105106
testTable.Add("col1", "col2")
106107
testTable.Print()
107108
assert.Contains(t, buf.String(), "Lorem ipsum dolor sit amet, consectetu")
108-
assert.Equal(t, bold+"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt u "+reset+bold+"NAME "+reset+"\ncol1 col2\n", buf.String())
109+
assert.Equal(t, "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt u NAME\ncol1 col2\n", buf.String())
109110
}
110111

111112
func TestWidestColumn(t *testing.T) {
@@ -115,7 +116,7 @@ func TestWidestColumn(t *testing.T) {
115116
testTable.Add(id, "row2")
116117
testTable.Print()
117118
assert.Contains(t, buf.String(), id)
118-
assert.Equal(t, bold+"ID "+reset+bold+"Name "+reset+"\nABCDEFG-9b8babbd-f2ed-4371-b817-a839e4130332 row2\n", buf.String())
119+
assert.Equal(t, buf.String(), "ID Name\nABCDEFG-9b8babbd-f2ed-4371-b817-a839e4130332 row2\n")
119120
}
120121

121122
func TestMultiWideColumns(t *testing.T) {
@@ -126,7 +127,7 @@ func TestMultiWideColumns(t *testing.T) {
126127
testTable.Add(id, desc, "col3")
127128
testTable.Print()
128129
result := strings.Split(buf.String(), "\n")
129-
assert.Equal(t, result[0], bold+"ID "+reset+bold+"Description "+reset+bold+"Name "+reset)
130+
assert.Equal(t, result[0], "ID "+"Description "+"Name")
130131
assert.Equal(t, result[1], "ABCDEFG-9b8babbd-f2ed-4371-b817-a839e4130332 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut col3")
131132
}
132133

@@ -138,7 +139,7 @@ func TestMultiWideColumnsWithoutDescription(t *testing.T) {
138139
testTable.Add(id, desc, "col3", "Column The fourth", "Yet another column value", "ZZZZZZ")
139140
testTable.Print()
140141
result := strings.Split(buf.String(), "\n")
141-
assert.Equal(t, result[0], "\x1b[1mAA \x1b[0m\x1b[1mBB \x1b[0m\x1b[1mCC \x1b[0m\x1b[1mDD \x1b[0m\x1b[1mFF \x1b[0m\x1b[1mZZ \x1b[0m")
142+
assert.Equal(t, result[0], "AA BB CC DD FF ZZ")
142143
assert.Equal(t, result[1], "ABCDEFG-9b8babbd-f2ed-4371-b817-a839e4130332 Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. col3 Column The fourth Yet another column value ZZZZZZ")
143144
}
144145

@@ -197,3 +198,20 @@ func TestEmptyTable(t *testing.T) {
197198
assert.Equal(t, err, nil)
198199
assert.Equal(t, len(strings.TrimSpace(buf.String())), 0)
199200
}
201+
202+
func TestColorEnabled(t *testing.T) {
203+
setErr := os.Setenv("IBMCLOUD_COLOR", "true")
204+
assert.Nil(t, setErr)
205+
buf := bytes.Buffer{}
206+
testTable := NewTable(&buf, []string{"test1", "test2"})
207+
testTable.Add("row1", "row2")
208+
testTable.Print()
209+
assert.Contains(t, buf.String(), bold+"test1")
210+
assert.Contains(t, buf.String(), bold+"test2")
211+
assert.Contains(t, buf.String(), "row1")
212+
assert.Equal(t, bold+"test1 "+reset+bold+"test2 "+reset+"\nrow1 row2\n", buf.String())
213+
214+
unsetErr := os.Unsetenv("IBMCLOUD_COLOR")
215+
assert.Nil(t, unsetErr)
216+
217+
}

0 commit comments

Comments
 (0)