feat: allow segments to hide based on terminal width

This commit is contained in:
mirsella 2022-12-01 17:20:24 +01:00 committed by Jan De Dobbeleer
parent a3fb4443fa
commit 93f6576da0
5 changed files with 82 additions and 3 deletions

View file

@ -13,7 +13,7 @@ import (
// getCmd represents the get command // getCmd represents the get command
var getCmd = &cobra.Command{ var getCmd = &cobra.Command{
Use: "get [shell|millis|accent|toggles]", Use: "get [shell|millis|accent|toggles|width]",
Short: "Get a value from oh-my-posh", Short: "Get a value from oh-my-posh",
Long: `Get a value from oh-my-posh. Long: `Get a value from oh-my-posh.
@ -22,12 +22,14 @@ This command is used to get the value of the following variables:
- shell - shell
- millis - millis
- accent - accent
- toggles`, - toggles
- width`,
ValidArgs: []string{ ValidArgs: []string{
"millis", "millis",
"shell", "shell",
"accent", "accent",
"toggles", "toggles",
"width",
}, },
Args: NoArgsOrOneValidArg, Args: NoArgsOrOneValidArg,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
@ -71,6 +73,13 @@ This command is used to get the value of the following variables:
for _, toggle := range toggles { for _, toggle := range toggles {
fmt.Println("- " + toggle) fmt.Println("- " + toggle)
} }
case "width":
width, err := env.TerminalWidth()
if err != nil {
fmt.Println("error getting terminal width:", err.Error())
return
}
fmt.Println(width)
default: default:
_ = cmd.Help() _ = cmd.Help()
} }

View file

@ -36,6 +36,8 @@ type Segment struct {
Properties properties.Map `json:"properties,omitempty"` Properties properties.Map `json:"properties,omitempty"`
Interactive bool `json:"interactive,omitempty"` Interactive bool `json:"interactive,omitempty"`
Alias string `json:"alias,omitempty"` Alias string `json:"alias,omitempty"`
MaxWidth int `json:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty"`
writer SegmentWriter writer SegmentWriter
Enabled bool `json:"-"` Enabled bool `json:"-"`
@ -399,6 +401,9 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
} }
} }
} }
if segment.shouldHideForWidth() {
return
}
if segment.writer.Enabled() { if segment.writer.Enabled() {
segment.Enabled = true segment.Enabled = true
name := segment.Alias name := segment.Alias
@ -409,6 +414,26 @@ 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

View file

@ -164,3 +164,34 @@ 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)
}
}

View file

@ -318,6 +318,18 @@
"description": "https://ohmyposh.dev/docs/configuration/segment#templates", "description": "https://ohmyposh.dev/docs/configuration/segment#templates",
"enum": ["first_match", "join"] "enum": ["first_match", "join"]
}, },
"max_cols": {
"type": "integer",
"title": "if the terminal width exceeds this value, the segment will be hidden",
"description": "https://ohmyposh.dev/docs/configuration/segment#max_cols",
"default": 0
},
"min_cols": {
"type": "integer",
"title": "if the terminal width is inferior than this value, the segment will be hidden",
"description": "https://ohmyposh.dev/docs/configuration/segment#min_cols",
"default": 0
},
"properties": { "properties": {
"type": "object", "type": "object",
"title": "Segment Properties, used to change behavior/displaying", "title": "Segment Properties, used to change behavior/displaying",

View file

@ -50,7 +50,9 @@ understand how to configure a segment.
| `templates_logic` | `string` | <ul><li>`first_match`: return the first non-whitespace string and skip everything else</li><li>`join`:evaluate all templates and join all non-whitespace strings (**default**)</li></ul> | | `templates_logic` | `string` | <ul><li>`first_match`: return the first non-whitespace string and skip everything else</li><li>`join`:evaluate all templates and join all non-whitespace strings (**default**)</li></ul> |
| `properties` | `[]Property` | see [Properties][properties] below | | `properties` | `[]Property` | see [Properties][properties] below |
| `interactive` | `boolean` | when is true, the segment text is not escaped to allow the use of interactive prompt escape sequences - defaults to `false` | | `interactive` | `boolean` | when is true, the segment text is not escaped to allow the use of interactive prompt escape sequences - defaults to `false` |
| `alias` | `string` | for use with [cross segment template properties][cstp] | | `alias` | `string` | for use with [cross segment template properties][cstp] |
| `min_width` | `int` | if the terminal width exceeds this value, the segment will be hidden. For your terminal width, see `oh-my-posh get width`. Defaults to `0` (disable) |
| `max_width` | `int` | if the terminal width is smaller than this value, the segment will be hidden. For your terminal width, see `oh-my-posh get width`. Defaults to `0` (disable) |
## Style ## Style