mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 20:39:40 -08:00
feat: allow block to hide based on terminal width
This commit is contained in:
parent
0d9216c62f
commit
8365aae504
|
@ -47,6 +47,9 @@ type Block struct {
|
||||||
HorizontalOffset int `json:"horizontal_offset,omitempty"`
|
HorizontalOffset int `json:"horizontal_offset,omitempty"`
|
||||||
VerticalOffset int `json:"vertical_offset,omitempty"`
|
VerticalOffset int `json:"vertical_offset,omitempty"`
|
||||||
|
|
||||||
|
MaxWidth int `json:"max_width,omitempty"`
|
||||||
|
MinWidth int `json:"min_width,omitempty"`
|
||||||
|
|
||||||
env platform.Environment
|
env platform.Environment
|
||||||
writer color.Writer
|
writer color.Writer
|
||||||
ansi *color.Ansi
|
ansi *color.Ansi
|
||||||
|
@ -77,6 +80,9 @@ func (b *Block) executeSegmentLogic() {
|
||||||
if b.env.Flags().Debug {
|
if b.env.Flags().Debug {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if shouldHideForWidth(b.env, b.MinWidth, b.MaxWidth) {
|
||||||
|
return
|
||||||
|
}
|
||||||
b.setEnabledSegments()
|
b.setEnabledSegments()
|
||||||
b.setSegmentsText()
|
b.setSegmentsText()
|
||||||
}
|
}
|
||||||
|
|
23
src/engine/responsive.go
Normal file
23
src/engine/responsive.go
Normal 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
|
||||||
|
}
|
34
src/engine/responsive_test.go
Normal file
34
src/engine/responsive_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -39,10 +39,10 @@ type Segment struct {
|
||||||
MaxWidth int `json:"max_width,omitempty"`
|
MaxWidth int `json:"max_width,omitempty"`
|
||||||
MinWidth int `json:"min_width,omitempty"`
|
MinWidth int `json:"min_width,omitempty"`
|
||||||
|
|
||||||
|
env platform.Environment
|
||||||
writer SegmentWriter
|
writer SegmentWriter
|
||||||
Enabled bool `json:"-"`
|
Enabled bool `json:"-"`
|
||||||
text string
|
text string
|
||||||
env platform.Environment
|
|
||||||
backgroundCache string
|
backgroundCache string
|
||||||
foregroundCache 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
|
return
|
||||||
}
|
}
|
||||||
if segment.writer.Enabled() {
|
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() {
|
func (segment *Segment) SetText() {
|
||||||
if !segment.Enabled {
|
if !segment.Enabled {
|
||||||
return
|
return
|
||||||
|
|
|
@ -164,34 +164,3 @@ func TestGetColors(t *testing.T) {
|
||||||
assert.Equal(t, tc.ExpectedColor, color, tc.Case)
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue