mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-02-02 05:41:10 -08:00
parent
422ec3e7fe
commit
a8d4e51d54
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
|
@ -7,7 +7,7 @@
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "debug",
|
"mode": "debug",
|
||||||
"program": "${workspaceRoot}/src",
|
"program": "${workspaceRoot}/src",
|
||||||
"args": ["--config=${workspaceRoot}/themes/jandedobbeleer.omp.json"]
|
"args": ["--config=${workspaceRoot}/themes/jandedobbeleer.omp.json", "--shell=pwsh"]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "Launch tests",
|
"name": "Launch tests",
|
||||||
|
|
|
@ -25,6 +25,21 @@ func (e *engine) string() string {
|
||||||
return e.console.String()
|
return e.console.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *engine) canWriteRPrompt() bool {
|
||||||
|
prompt := e.string()
|
||||||
|
consoleWidth, err := e.env.getTerminalWidth()
|
||||||
|
if err != nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
promptWidth := e.ansi.lenWithoutANSI(prompt)
|
||||||
|
availableSpace := consoleWidth - promptWidth
|
||||||
|
if promptWidth > consoleWidth {
|
||||||
|
availableSpace = promptWidth - (promptWidth % consoleWidth)
|
||||||
|
}
|
||||||
|
promptBreathingRoom := 30
|
||||||
|
return (availableSpace - e.ansi.lenWithoutANSI(e.rprompt)) >= promptBreathingRoom
|
||||||
|
}
|
||||||
|
|
||||||
func (e *engine) render() string {
|
func (e *engine) render() string {
|
||||||
for _, block := range e.config.Blocks {
|
for _, block := range e.config.Blocks {
|
||||||
e.renderBlock(block)
|
e.renderBlock(block)
|
||||||
|
@ -151,7 +166,7 @@ func (e *engine) print() string {
|
||||||
prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)
|
prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)
|
||||||
return prompt
|
return prompt
|
||||||
case pwsh, powershell5, bash, plain:
|
case pwsh, powershell5, bash, plain:
|
||||||
if e.rprompt == "" {
|
if e.rprompt == "" || !e.canWriteRPrompt() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
e.write(e.ansi.saveCursorPosition)
|
e.write(e.ansi.saveCursorPosition)
|
||||||
|
|
43
src/engine_test.go
Normal file
43
src/engine_test.go
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestCanWriteRPrompt(t *testing.T) {
|
||||||
|
cases := []struct {
|
||||||
|
Case string
|
||||||
|
Expected bool
|
||||||
|
TerminalWidth int
|
||||||
|
TerminalWidthError error
|
||||||
|
PromptLength int
|
||||||
|
RPromptLength int
|
||||||
|
}{
|
||||||
|
{Case: "Width Error", Expected: true, TerminalWidthError: errors.New("burp")},
|
||||||
|
{Case: "Terminal > Prompt enabled", Expected: true, TerminalWidth: 200, PromptLength: 100, RPromptLength: 10},
|
||||||
|
{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 room", Expected: true, TerminalWidth: 200, PromptLength: 400, RPromptLength: 80},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range cases {
|
||||||
|
env := new(MockedEnvironment)
|
||||||
|
env.On("getTerminalWidth", nil).Return(tc.TerminalWidth, tc.TerminalWidthError)
|
||||||
|
ansi := &ansiUtils{}
|
||||||
|
ansi.init(plain)
|
||||||
|
engine := &engine{
|
||||||
|
env: env,
|
||||||
|
ansi: ansi,
|
||||||
|
}
|
||||||
|
engine.rprompt = strings.Repeat("x", tc.RPromptLength)
|
||||||
|
engine.console.WriteString(strings.Repeat("x", tc.PromptLength))
|
||||||
|
got := engine.canWriteRPrompt()
|
||||||
|
assert.Equal(t, tc.Expected, got)
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,7 @@ import (
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
"github.com/shirou/gopsutil/host"
|
"github.com/shirou/gopsutil/host"
|
||||||
"github.com/shirou/gopsutil/process"
|
"github.com/shirou/gopsutil/process"
|
||||||
|
terminal "github.com/wayneashleyberry/terminal-dimensions"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -73,6 +74,7 @@ type environmentInfo interface {
|
||||||
hasParentFilePath(path string) (fileInfo *fileInfo, err error)
|
hasParentFilePath(path string) (fileInfo *fileInfo, err error)
|
||||||
isWsl() bool
|
isWsl() bool
|
||||||
stackCount() int
|
stackCount() int
|
||||||
|
getTerminalWidth() (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type commandCache struct {
|
type commandCache struct {
|
||||||
|
@ -323,6 +325,11 @@ func (env *environment) stackCount() int {
|
||||||
return *env.args.StackCount
|
return *env.args.StackCount
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *environment) getTerminalWidth() (int, error) {
|
||||||
|
width, err := terminal.Width()
|
||||||
|
return int(width), err
|
||||||
|
}
|
||||||
|
|
||||||
func cleanHostName(hostName string) string {
|
func cleanHostName(hostName string) string {
|
||||||
garbage := []string{
|
garbage := []string{
|
||||||
".lan",
|
".lan",
|
||||||
|
|
|
@ -26,7 +26,8 @@ require (
|
||||||
github.com/stretchr/objx v0.3.0 // indirect
|
github.com/stretchr/objx v0.3.0 // indirect
|
||||||
github.com/stretchr/testify v1.7.0
|
github.com/stretchr/testify v1.7.0
|
||||||
github.com/tklauser/go-sysconf v0.3.5 // indirect
|
github.com/tklauser/go-sysconf v0.3.5 // indirect
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
|
github.com/wayneashleyberry/terminal-dimensions v1.0.0
|
||||||
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect
|
||||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
|
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb
|
||||||
golang.org/x/sys v0.0.0-20210324051608-47abb6519492
|
golang.org/x/sys v0.0.0-20210324051608-47abb6519492
|
||||||
golang.org/x/text v0.3.5
|
golang.org/x/text v0.3.5
|
||||||
|
|
|
@ -101,12 +101,14 @@ github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITn
|
||||||
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
|
github.com/tklauser/numcpus v0.2.2 h1:oyhllyrScuYI6g+h/zUvNXNp1wy7x8qQy3t/piefldA=
|
||||||
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
|
github.com/tklauser/numcpus v0.2.2/go.mod h1:x3qojaO3uyYt0i56EW/VUYs7uBvdl2fkfZFu0T9wgjM=
|
||||||
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
|
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
|
||||||
|
github.com/wayneashleyberry/terminal-dimensions v1.0.0 h1:LawtS1nqKjAfqrmKOzkcrDLAjSzh38lEhC401JPjQVA=
|
||||||
|
github.com/wayneashleyberry/terminal-dimensions v1.0.0/go.mod h1:PW2XrtV6KmKOPhuf7wbtcmw1/IFnC39mryRET2XbxeE=
|
||||||
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
|
github.com/zclconf/go-cty v1.2.0/go.mod h1:hOPWgoHbaTUnI5k4D2ld+GRpFJSCe6bCM7m1q/N4PQ8=
|
||||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||||
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||||
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 h1:It14KIkyBFYkHkwZ7k45minvA9aorojkyjGk9KJ5B/w=
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
|
||||||
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
|
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
|
||||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
|
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb h1:fqpd0EBDzlHRCjiphRR5Zo/RSWWQlWv34418dnEixWk=
|
||||||
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20210220032944-ac19c3e999fb/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
|
||||||
|
|
|
@ -142,6 +142,11 @@ func (env *MockedEnvironment) isWsl() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *MockedEnvironment) getTerminalWidth() (int, error) {
|
||||||
|
args := env.Called(nil)
|
||||||
|
return args.Int(0), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
homeBill = "/home/bill"
|
homeBill = "/home/bill"
|
||||||
homeJan = "/usr/home/jan"
|
homeJan = "/usr/home/jan"
|
||||||
|
|
Loading…
Reference in a new issue