mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-09-20 05:27:31 -07:00
feat: allow segments to hide based on terminal width
This commit is contained in:
parent
a3fb4443fa
commit
93f6576da0
|
@ -13,7 +13,7 @@ import (
|
|||
|
||||
// getCmd represents the get 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",
|
||||
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
|
||||
- millis
|
||||
- accent
|
||||
- toggles`,
|
||||
- toggles
|
||||
- width`,
|
||||
ValidArgs: []string{
|
||||
"millis",
|
||||
"shell",
|
||||
"accent",
|
||||
"toggles",
|
||||
"width",
|
||||
},
|
||||
Args: NoArgsOrOneValidArg,
|
||||
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 {
|
||||
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:
|
||||
_ = cmd.Help()
|
||||
}
|
||||
|
|
|
@ -36,6 +36,8 @@ type Segment struct {
|
|||
Properties properties.Map `json:"properties,omitempty"`
|
||||
Interactive bool `json:"interactive,omitempty"`
|
||||
Alias string `json:"alias,omitempty"`
|
||||
MaxWidth int `json:"max_width,omitempty"`
|
||||
MinWidth int `json:"min_width,omitempty"`
|
||||
|
||||
writer SegmentWriter
|
||||
Enabled bool `json:"-"`
|
||||
|
@ -399,6 +401,9 @@ func (segment *Segment) SetEnabled(env platform.Environment) {
|
|||
}
|
||||
}
|
||||
}
|
||||
if segment.shouldHideForWidth() {
|
||||
return
|
||||
}
|
||||
if segment.writer.Enabled() {
|
||||
segment.Enabled = true
|
||||
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() {
|
||||
if !segment.Enabled {
|
||||
return
|
||||
|
|
|
@ -164,3 +164,34 @@ 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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -318,6 +318,18 @@
|
|||
"description": "https://ohmyposh.dev/docs/configuration/segment#templates",
|
||||
"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": {
|
||||
"type": "object",
|
||||
"title": "Segment Properties, used to change behavior/displaying",
|
||||
|
|
|
@ -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> |
|
||||
| `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` |
|
||||
| `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
|
||||
|
||||
|
|
Loading…
Reference in a new issue