Repository: JanDeDobbeleer/oh-my-posh
Pull request #3162 (2022-12-03 16:25:44)

feat: allow block to hide based on terminal width
### Prerequisites

- [x] I have read and understood the [contributing guide][CONTRIBUTING.md]
- [x] The commit message follows the [conventional commits][cc] guidelines
- [x] Tests for the changes have been added (for bug fixes/features)
- [x] Docs have been added/updated (for bug fixes/features)

<!---

Tips:

If you're not comfortable with working with Git, we're working a guide (https://ohmyposh.dev/docs/contributing_git) to help you out.
Oh My Posh advises GitKraken (https://www.gitkraken.com/invite/nQmDPR9D) as your preferred cross platform Git GUI power tool.

-->

[CONTRIBUTING.md]: https://github.com/JanDeDobbeleer/oh-my-posh/blob/main/CONTRIBUTING.md
[cc]: https://www.conventionalcommits.org/en/v1.0.0/#summary



Patch:
diff --git a/src/engine/block.go b/src/engine/block.go
index 71e96b2c..dab34afa 100644
--- a/src/engine/block.go
+++ b/src/engine/block.go
@@ -47,6 +47,9 @@ type Block struct {
 	HorizontalOffset int `json:"horizontal_offset,omitempty"`
 	VerticalOffset   int `json:"vertical_offset,omitempty"`
 
+	MaxWidth int `json:"max_width,omitempty"`
+	MinWidth int `json:"min_width,omitempty"`
+
 	env                   platform.Environment
 	writer                color.Writer
 	ansi                  *color.Ansi
@@ -77,6 +80,9 @@ func (b *Block) executeSegmentLogic() {
 	if b.env.Flags().Debug {
 		return
 	}
+	if shouldHideForWidth(b.env, b.MinWidth, b.MaxWidth) {
+		return
+	}
 	b.setEnabledSegments()
 	b.setSegmentsText()
 }
diff --git a/src/engine/responsive.go b/src/engine/responsive.go
new file mode 100644
index 00000000..e21b8eca
--- /dev/null
+++ b/src/engine/responsive.go
@@ -0,0 +1,23 @@
+package engine
+
+import "oh-my-posh/platform"
+
+func shouldHideForWidth(env platform.Environment, minWidth, maxWidth int) bool {
+	if maxWidth == 0 && minWidth == 0 {
+		return false
+	}
+	width, err := env.TerminalWidth()
+	if err != nil {
+		return false
+	}
+	if minWidth > 0 && maxWidth > 0 {
+		return width < minWidth || width > maxWidth
+	}
+	if maxWidth > 0 && width > maxWidth {
+		return true
+	}
+	if minWidth > 0 && width < minWidth {
+		return true
+	}
+	return false
+}
diff --git a/src/engine/segment.go b/src/engine/segment.go
index 026e28b7..ef29927c 100644
--- a/src/engine/segment.go
+++ b/src/engine/segment.go
@@ -39,10 +39,10 @@ type Segment struct {
 	MaxWidth            int            `json:"max_width,omitempty"`
 	MinWidth            int            `json:"min_width,omitempty"`
 
+	env             platform.Environment
 	writer          SegmentWriter
 	Enabled         bool `json:"-"`
 	text            string
-	env             platform.Environment
 	backgroundCache string
 	foregroundCache string
 }
@@ -401,7 +401,7 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
 			}
 		}
 	}
-	if segment.shouldHideForWidth() {
+	if shouldHideForWidth(segment.env, segment.MinWidth, segment.MaxWidth) {
 		return
 	}
 	if segment.writer.Enabled() {
@@ -414,26 +414,6 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
 	}
 }
 
-func (segment *Segment) shouldHideForWidth() bool {
-	if segment.MaxWidth == 0 && segment.MinWidth == 0 {
-		return false
-	}
-	width, err := segment.env.TerminalWidth()
-	if err != nil {
-		return false
-	}
-	if segment.MinWidth > 0 && segment.MaxWidth > 0 {
-		return width < segment.MinWidth || width > segment.MaxWidth
-	}
-	if segment.MaxWidth > 0 && width > segment.MaxWidth {
-		return true
-	}
-	if segment.MinWidth > 0 && width < segment.MinWidth {
-		return true
-	}
-	return false
-}
-
 func (segment *Segment) SetText() {
 	if !segment.Enabled {
 		return


Test patch:
diff --git a/src/engine/responsive_test.go b/src/engine/responsive_test.go
new file mode 100644
index 00000000..7a21fd7a
--- /dev/null
+++ b/src/engine/responsive_test.go
@@ -0,0 +1,34 @@
+package engine
+
+import (
+	"oh-my-posh/mock"
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestShouldHideForWidth(t *testing.T) {
+	cases := []struct {
+		Case     string
+		MinWidth int
+		MaxWidth int
+		Width    int
+		Error    error
+		Expected bool
+	}{
+		{Case: "No settings"},
+		{Case: "Min cols - hide", MinWidth: 10, Width: 9, Expected: true},
+		{Case: "Min cols - show", MinWidth: 10, Width: 20, Expected: false},
+		{Case: "Max cols - hide", MaxWidth: 10, Width: 11, Expected: true},
+		{Case: "Max cols - show", MaxWidth: 10, Width: 8, Expected: false},
+		{Case: "Min & Max cols - hide", MinWidth: 10, MaxWidth: 20, Width: 21, Expected: true},
+		{Case: "Min & Max cols - hide 2", MinWidth: 10, MaxWidth: 20, Width: 8, Expected: true},
+		{Case: "Min & Max cols - show", MinWidth: 10, MaxWidth: 20, Width: 11, Expected: false},
+	}
+	for _, tc := range cases {
+		env := new(mock.MockedEnvironment)
+		env.On("TerminalWidth").Return(tc.Width, tc.Error)
+		got := shouldHideForWidth(env, tc.MinWidth, tc.MaxWidth)
+		assert.Equal(t, tc.Expected, got, tc.Case)
+	}
+}
diff --git a/src/engine/segment_test.go b/src/engine/segment_test.go
index 28bbd5d6..f9efd14f 100644
--- a/src/engine/segment_test.go
+++ b/src/engine/segment_test.go
@@ -164,34 +164,3 @@ func TestGetColors(t *testing.T) {
 		assert.Equal(t, tc.ExpectedColor, color, tc.Case)
 	}
 }
-
-func TestShouldHideForCols(t *testing.T) {
-	cases := []struct {
-		Case     string
-		MinWidth int
-		MaxWidth int
-		Width    int
-		Error    error
-		Expected bool
-	}{
-		{Case: "No settings"},
-		{Case: "Min cols - hide", MinWidth: 10, Width: 9, Expected: true},
-		{Case: "Min cols - show", MinWidth: 10, Width: 20, Expected: false},
-		{Case: "Max cols - hide", MaxWidth: 10, Width: 11, Expected: true},
-		{Case: "Max cols - show", MaxWidth: 10, Width: 8, Expected: false},
-		{Case: "Min & Max cols - hide", MinWidth: 10, MaxWidth: 20, Width: 21, Expected: true},
-		{Case: "Min & Max cols - hide 2", MinWidth: 10, MaxWidth: 20, Width: 8, Expected: true},
-		{Case: "Min & Max cols - show", MinWidth: 10, MaxWidth: 20, Width: 11, Expected: false},
-	}
-	for _, tc := range cases {
-		env := new(mock.MockedEnvironment)
-		env.On("TerminalWidth").Return(tc.Width, tc.Error)
-		segment := &Segment{
-			env:      env,
-			MaxWidth: tc.MaxWidth,
-			MinWidth: tc.MinWidth,
-		}
-		got := segment.shouldHideForWidth()
-		assert.Equal(t, tc.Expected, got, tc.Case)
-	}
-}