mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-26 19:39:39 -08:00
refactor: format shell name better
This commit is contained in:
parent
f478255bbf
commit
6c17689966
|
@ -26,7 +26,7 @@ type environmentInfo interface {
|
|||
lastErrorCode() int
|
||||
getArgs() *args
|
||||
getBatteryInfo() (*battery.Battery, error)
|
||||
getParentProcess() (ps.Process, error)
|
||||
getShellName() string
|
||||
}
|
||||
|
||||
type environment struct {
|
||||
|
@ -95,9 +95,15 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
|||
return battery.Get(0)
|
||||
}
|
||||
|
||||
func (env *environment) getParentProcess() (ps.Process, error) {
|
||||
func (env *environment) getShellName() string {
|
||||
pid := os.Getppid()
|
||||
return ps.FindProcess(pid)
|
||||
p, err := ps.FindProcess(pid)
|
||||
|
||||
if err != nil {
|
||||
return "unknown"
|
||||
}
|
||||
shell := strings.Replace(p.Executable(), ".exe", "", 1)
|
||||
return strings.Trim(shell, " ")
|
||||
}
|
||||
|
||||
func cleanHostName(hostName string) string {
|
||||
|
|
6
main.go
6
main.go
|
@ -41,11 +41,7 @@ func main() {
|
|||
colorWriter := &Renderer{
|
||||
Buffer: new(bytes.Buffer),
|
||||
}
|
||||
var shell string
|
||||
if parentProcess, err := env.getParentProcess(); err != nil {
|
||||
shell = parentProcess.Executable()
|
||||
}
|
||||
colorWriter.init(shell)
|
||||
colorWriter.init(env.getShellName())
|
||||
engine := &engine{
|
||||
settings: settings,
|
||||
env: env,
|
||||
|
|
|
@ -7,7 +7,6 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/distatus/battery"
|
||||
ps "github.com/mitchellh/go-ps"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
@ -81,9 +80,9 @@ func (env *MockedEnvironment) getBatteryInfo() (*battery.Battery, error) {
|
|||
return args.Get(0).(*battery.Battery), args.Error(1)
|
||||
}
|
||||
|
||||
func (env *MockedEnvironment) getParentProcess() (ps.Process, error) {
|
||||
func (env *MockedEnvironment) getShellName() string {
|
||||
args := env.Called(nil)
|
||||
return args.Get(0).(ps.Process), args.Error(1)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func TestWorkingDir(t *testing.T) {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
package main
|
||||
|
||||
import "strings"
|
||||
|
||||
type shell struct {
|
||||
props *properties
|
||||
env environmentInfo
|
||||
|
@ -12,13 +10,7 @@ func (s *shell) enabled() bool {
|
|||
}
|
||||
|
||||
func (s *shell) string() string {
|
||||
p, err := s.env.getParentProcess()
|
||||
|
||||
if err != nil {
|
||||
return "unknown"
|
||||
}
|
||||
shell := strings.Replace(p.Executable(), ".exe", "", 1)
|
||||
return shell
|
||||
return s.env.getShellName()
|
||||
}
|
||||
|
||||
func (s *shell) init(props *properties, env environmentInfo) {
|
||||
|
|
|
@ -1,42 +1,15 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type process struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
// Pid is the process ID for this process.
|
||||
func (p *process) Pid() int {
|
||||
args := p.Called(nil)
|
||||
return args.Int(0)
|
||||
}
|
||||
|
||||
// PPid is the parent process ID for this process.
|
||||
func (p *process) PPid() int {
|
||||
args := p.Called(nil)
|
||||
return args.Int(0)
|
||||
}
|
||||
|
||||
// Executable name running this process. This is not a path to the
|
||||
// executable.
|
||||
func (p *process) Executable() string {
|
||||
args := p.Called(nil)
|
||||
return args.String(0)
|
||||
}
|
||||
|
||||
func TestWriteCurrentShell(t *testing.T) {
|
||||
expected := "zsh"
|
||||
env := new(MockedEnvironment)
|
||||
process := new(process)
|
||||
process.On("Executable", nil).Return(expected)
|
||||
env.On("getParentProcess", nil).Return(process, nil)
|
||||
env.On("getShellName", nil).Return(expected, nil)
|
||||
props := &properties{}
|
||||
s := &shell{
|
||||
env: env,
|
||||
|
@ -44,31 +17,3 @@ func TestWriteCurrentShell(t *testing.T) {
|
|||
}
|
||||
assert.Equal(t, expected, s.string())
|
||||
}
|
||||
|
||||
func TestWriteCurrentShellWindowsExe(t *testing.T) {
|
||||
expected := "pwsh"
|
||||
env := new(MockedEnvironment)
|
||||
process := new(process)
|
||||
parentProcess := expected + ".exe"
|
||||
process.On("Executable", nil).Return(parentProcess)
|
||||
env.On("getParentProcess", nil).Return(process, nil)
|
||||
props := &properties{}
|
||||
s := &shell{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
assert.Equal(t, expected, s.string())
|
||||
}
|
||||
|
||||
func TestWriteCurrentShellError(t *testing.T) {
|
||||
err := errors.New("Oh no, what shell is this?")
|
||||
env := new(MockedEnvironment)
|
||||
process := new(process)
|
||||
env.On("getParentProcess", nil).Return(process, err)
|
||||
props := &properties{}
|
||||
s := &shell{
|
||||
env: env,
|
||||
props: props,
|
||||
}
|
||||
assert.Equal(t, "unknown", s.string())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue