From 8365aae504fe321c90243d7c64470b9d9e5c9895 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Sat, 3 Dec 2022 17:25:09 +0100 Subject: [PATCH] feat: allow block to hide based on terminal width --- src/engine/block.go | 6 ++++++ src/engine/responsive.go | 23 +++++++++++++++++++++++ src/engine/responsive_test.go | 34 ++++++++++++++++++++++++++++++++++ src/engine/segment.go | 24 ++---------------------- src/engine/segment_test.go | 31 ------------------------------- 5 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 src/engine/responsive.go create mode 100644 src/engine/responsive_test.go 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/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.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 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) - } -}