feat: allow block to hide based on terminal width

This commit is contained in:
Jan De Dobbeleer 2022-12-03 17:25:09 +01:00 committed by Jan De Dobbeleer
parent 0d9216c62f
commit 8365aae504
5 changed files with 65 additions and 53 deletions

View file

@ -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()
}

23
src/engine/responsive.go Normal file
View file

@ -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
}

View file

@ -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)
}
}

View file

@ -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

View file

@ -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)
}
}