mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 04:54:03 -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
|
lastErrorCode() int
|
||||||
getArgs() *args
|
getArgs() *args
|
||||||
getBatteryInfo() (*battery.Battery, error)
|
getBatteryInfo() (*battery.Battery, error)
|
||||||
getParentProcess() (ps.Process, error)
|
getShellName() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type environment struct {
|
type environment struct {
|
||||||
|
@ -95,9 +95,15 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
||||||
return battery.Get(0)
|
return battery.Get(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (env *environment) getParentProcess() (ps.Process, error) {
|
func (env *environment) getShellName() string {
|
||||||
pid := os.Getppid()
|
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 {
|
func cleanHostName(hostName string) string {
|
||||||
|
|
6
main.go
6
main.go
|
@ -41,11 +41,7 @@ func main() {
|
||||||
colorWriter := &Renderer{
|
colorWriter := &Renderer{
|
||||||
Buffer: new(bytes.Buffer),
|
Buffer: new(bytes.Buffer),
|
||||||
}
|
}
|
||||||
var shell string
|
colorWriter.init(env.getShellName())
|
||||||
if parentProcess, err := env.getParentProcess(); err != nil {
|
|
||||||
shell = parentProcess.Executable()
|
|
||||||
}
|
|
||||||
colorWriter.init(shell)
|
|
||||||
engine := &engine{
|
engine := &engine{
|
||||||
settings: settings,
|
settings: settings,
|
||||||
env: env,
|
env: env,
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
ps "github.com/mitchellh/go-ps"
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/mock"
|
"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)
|
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)
|
args := env.Called(nil)
|
||||||
return args.Get(0).(ps.Process), args.Error(1)
|
return args.String(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestWorkingDir(t *testing.T) {
|
func TestWorkingDir(t *testing.T) {
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "strings"
|
|
||||||
|
|
||||||
type shell struct {
|
type shell struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
|
@ -12,13 +10,7 @@ func (s *shell) enabled() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *shell) string() string {
|
func (s *shell) string() string {
|
||||||
p, err := s.env.getParentProcess()
|
return s.env.getShellName()
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return "unknown"
|
|
||||||
}
|
|
||||||
shell := strings.Replace(p.Executable(), ".exe", "", 1)
|
|
||||||
return shell
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *shell) init(props *properties, env environmentInfo) {
|
func (s *shell) init(props *properties, env environmentInfo) {
|
||||||
|
|
|
@ -1,42 +1,15 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"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) {
|
func TestWriteCurrentShell(t *testing.T) {
|
||||||
expected := "zsh"
|
expected := "zsh"
|
||||||
env := new(MockedEnvironment)
|
env := new(MockedEnvironment)
|
||||||
process := new(process)
|
env.On("getShellName", nil).Return(expected, nil)
|
||||||
process.On("Executable", nil).Return(expected)
|
|
||||||
env.On("getParentProcess", nil).Return(process, nil)
|
|
||||||
props := &properties{}
|
props := &properties{}
|
||||||
s := &shell{
|
s := &shell{
|
||||||
env: env,
|
env: env,
|
||||||
|
@ -44,31 +17,3 @@ func TestWriteCurrentShell(t *testing.T) {
|
||||||
}
|
}
|
||||||
assert.Equal(t, expected, s.string())
|
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