From 6bef088b83c5567e92cc157e22311d2ef7a685fe Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 17 Sep 2021 08:15:06 +0200 Subject: [PATCH] feat: allow auto hiding of rprompt on Windows --- src/engine.go | 9 ++++++--- src/engine_test.go | 4 ++-- src/environment_windows.go | 14 ++++++++++++-- src/go.mod | 1 + src/go.sum | 3 +++ 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/engine.go b/src/engine.go index 1474fce8..53d16edc 100644 --- a/src/engine.go +++ b/src/engine.go @@ -33,11 +33,14 @@ func (e *engine) canWriteRPrompt() bool { } promptWidth := e.ansi.lenWithoutANSI(prompt) availableSpace := consoleWidth - promptWidth - if promptWidth > consoleWidth { - availableSpace = promptWidth - (promptWidth % consoleWidth) + // spanning multiple lines + if availableSpace < 0 { + overflow := promptWidth % consoleWidth + availableSpace = consoleWidth - overflow } promptBreathingRoom := 30 - return (availableSpace - e.ansi.lenWithoutANSI(e.rprompt)) >= promptBreathingRoom + canWrite := (availableSpace - e.ansi.lenWithoutANSI(e.rprompt)) >= promptBreathingRoom + return canWrite } func (e *engine) render() string { diff --git a/src/engine_test.go b/src/engine_test.go index 8b39d62c..b9eaf37b 100644 --- a/src/engine_test.go +++ b/src/engine_test.go @@ -22,7 +22,7 @@ func TestCanWriteRPrompt(t *testing.T) { {Case: "Terminal > Prompt enabled edge", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 70}, {Case: "Terminal > Prompt disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 100, RPromptLength: 71}, {Case: "Prompt > Terminal enabled", Expected: true, TerminalWidth: 200, PromptLength: 300, RPromptLength: 70}, - {Case: "Prompt > Terminal disabled no breathing", Expected: true, TerminalWidth: 200, PromptLength: 300, RPromptLength: 80}, + {Case: "Prompt > Terminal disabled no breathing", Expected: false, TerminalWidth: 200, PromptLength: 300, RPromptLength: 80}, {Case: "Prompt > Terminal disabled no room", Expected: true, TerminalWidth: 200, PromptLength: 400, RPromptLength: 80}, } @@ -38,6 +38,6 @@ func TestCanWriteRPrompt(t *testing.T) { engine.rprompt = strings.Repeat("x", tc.RPromptLength) engine.console.WriteString(strings.Repeat("x", tc.PromptLength)) got := engine.canWriteRPrompt() - assert.Equal(t, tc.Expected, got) + assert.Equal(t, tc.Expected, got, tc.Case) } } diff --git a/src/environment_windows.go b/src/environment_windows.go index 0fb17e83..42c68d6d 100644 --- a/src/environment_windows.go +++ b/src/environment_windows.go @@ -3,10 +3,11 @@ package main import ( - "errors" "os" + "syscall" "time" + "github.com/Azure/go-ansiterm/winterm" "golang.org/x/sys/windows" ) @@ -70,7 +71,16 @@ func (env *environment) isWsl() bool { func (env *environment) getTerminalWidth() (int, error) { defer env.tracer.trace(time.Now(), "getTerminalWidth") - return 0, errors.New("Unsupported on Windows") + handle, err := syscall.GetStdHandle(syscall.STD_OUTPUT_HANDLE) + if err != nil { + return 0, err + } + info, err := winterm.GetConsoleScreenBufferInfo(uintptr(handle)) + if err != nil { + return 0, err + } + // return int(float64(info.Size.X) * 0.57), nil + return int(info.Size.X), nil } func (env *environment) getPlatform() string { diff --git a/src/go.mod b/src/go.mod index 89085e6a..57653725 100644 --- a/src/go.mod +++ b/src/go.mod @@ -3,6 +3,7 @@ module oh-my-posh go 1.17 require ( + github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 github.com/BurntSushi/toml v0.4.1 // indirect github.com/Masterminds/sprig/v3 v3.2.2 github.com/StackExchange/wmi v1.2.1 // indirect diff --git a/src/go.sum b/src/go.sum index 83075256..d2686196 100644 --- a/src/go.sum +++ b/src/go.sum @@ -1,3 +1,5 @@ +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 h1:UQHMgLO+TxOElx5B5HZ4hJQsoJ/PvUvKRhJHDQXO8P8= +github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= @@ -159,6 +161,7 @@ golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71 h1:ikCpsnYR+Ew0vu99XlDp55lGgDJdIMx3f4a18jfse/s= golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=