mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: add shell info segment
This commit is contained in:
parent
2791b180b2
commit
5c4af19d90
10
Gopkg.lock
generated
10
Gopkg.lock
generated
|
@ -26,6 +26,15 @@
|
||||||
revision = "cd55aeac72a19c6d5826061d5125d72b252a12eb"
|
revision = "cd55aeac72a19c6d5826061d5125d72b252a12eb"
|
||||||
version = "v0.3.11"
|
version = "v0.3.11"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
digest = "1:69530a15507094162be4eb3180c1d68b787177e8c9bbe76d4da40d44d6586f6f"
|
||||||
|
name = "github.com/mitchellh/go-ps"
|
||||||
|
packages = ["."]
|
||||||
|
pruneopts = "UT"
|
||||||
|
revision = "147ff83818ae939913b2e20b91ae3cd6c391771c"
|
||||||
|
source = "github.com/mitchellh/go-ps"
|
||||||
|
version = "v1.0.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
|
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
|
||||||
name = "github.com/pmezard/go-difflib"
|
name = "github.com/pmezard/go-difflib"
|
||||||
|
@ -113,6 +122,7 @@
|
||||||
input-imports = [
|
input-imports = [
|
||||||
"github.com/distatus/battery",
|
"github.com/distatus/battery",
|
||||||
"github.com/imdario/mergo",
|
"github.com/imdario/mergo",
|
||||||
|
"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",
|
||||||
"golang.org/x/sys/windows",
|
"golang.org/x/sys/windows",
|
||||||
|
|
|
@ -47,6 +47,11 @@
|
||||||
version = "v0.1.1"
|
version = "v0.1.1"
|
||||||
source = "github.com/stretchr/objx"
|
source = "github.com/stretchr/objx"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/mitchellh/go-ps"
|
||||||
|
version = "v1.0.0"
|
||||||
|
source = "github.com/mitchellh/go-ps"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/distatus/battery"
|
name = "github.com/distatus/battery"
|
||||||
branch = "master"
|
branch = "master"
|
||||||
|
|
|
@ -70,6 +70,15 @@
|
||||||
"postfix": " "
|
"postfix": " "
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "shell",
|
||||||
|
"style": "powerline",
|
||||||
|
"foreground": "#ffffff",
|
||||||
|
"background": "#0077c2",
|
||||||
|
"properties": {
|
||||||
|
"prefix": " "
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "exit",
|
"type": "exit",
|
||||||
"style": "diamond",
|
"style": "diamond",
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/distatus/battery"
|
"github.com/distatus/battery"
|
||||||
|
ps "github.com/mitchellh/go-ps"
|
||||||
)
|
)
|
||||||
|
|
||||||
type environmentInfo interface {
|
type environmentInfo interface {
|
||||||
|
@ -25,6 +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)
|
||||||
}
|
}
|
||||||
|
|
||||||
type environment struct {
|
type environment struct {
|
||||||
|
@ -93,6 +95,11 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
|
||||||
return battery.Get(0)
|
return battery.Get(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *environment) getParentProcess() (ps.Process, error) {
|
||||||
|
pid := os.Getppid()
|
||||||
|
return ps.FindProcess(pid)
|
||||||
|
}
|
||||||
|
|
||||||
func cleanHostName(hostName string) string {
|
func cleanHostName(hostName string) string {
|
||||||
garbage := []string{
|
garbage := []string{
|
||||||
".lan",
|
".lan",
|
||||||
|
|
25
segment.go
25
segment.go
|
@ -48,6 +48,8 @@ const (
|
||||||
Battery SegmentType = "battery"
|
Battery SegmentType = "battery"
|
||||||
//Spotify writes the Spotify status for Mac
|
//Spotify writes the Spotify status for Mac
|
||||||
Spotify SegmentType = "spotify"
|
Spotify SegmentType = "spotify"
|
||||||
|
//ShellInfo writes which shell we're currently in
|
||||||
|
ShellInfo SegmentType = "shell"
|
||||||
//Powerline writes it Powerline style
|
//Powerline writes it Powerline style
|
||||||
Powerline SegmentStyle = "powerline"
|
Powerline SegmentStyle = "powerline"
|
||||||
//Plain writes it without ornaments
|
//Plain writes it without ornaments
|
||||||
|
@ -66,17 +68,18 @@ func (segment *Segment) enabled() bool {
|
||||||
|
|
||||||
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) *properties {
|
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) *properties {
|
||||||
functions := map[SegmentType]SegmentWriter{
|
functions := map[SegmentType]SegmentWriter{
|
||||||
Session: &session{},
|
Session: &session{},
|
||||||
Path: &path{},
|
Path: &path{},
|
||||||
Git: &git{},
|
Git: &git{},
|
||||||
Exit: &exit{},
|
Exit: &exit{},
|
||||||
Venv: &venv{},
|
Venv: &venv{},
|
||||||
Root: &root{},
|
Root: &root{},
|
||||||
Text: &text{},
|
Text: &text{},
|
||||||
Time: &tempus{},
|
Time: &tempus{},
|
||||||
Cmd: &command{},
|
Cmd: &command{},
|
||||||
Battery: &batt{},
|
Battery: &batt{},
|
||||||
Spotify: &spotify{},
|
Spotify: &spotify{},
|
||||||
|
ShellInfo: &shell{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
|
@ -7,6 +7,7 @@ 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"
|
||||||
)
|
)
|
||||||
|
@ -80,6 +81,11 @@ 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) {
|
||||||
|
args := env.Called(nil)
|
||||||
|
return args.Get(0).(ps.Process), args.Error(1)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWorkingDir(t *testing.T) {
|
func TestWorkingDir(t *testing.T) {
|
||||||
want := "/usr/err"
|
want := "/usr/err"
|
||||||
env := new(MockedEnvironment)
|
env := new(MockedEnvironment)
|
||||||
|
|
24
segment_shell.go
Normal file
24
segment_shell.go
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type shell struct {
|
||||||
|
props *properties
|
||||||
|
env environmentInfo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *shell) enabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *shell) string() string {
|
||||||
|
p, err := s.env.getParentProcess()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
return p.Executable()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *shell) init(props *properties, env environmentInfo) {
|
||||||
|
s.props = props
|
||||||
|
s.env = env
|
||||||
|
}
|
59
segment_shell_test.go
Executable file
59
segment_shell_test.go
Executable file
|
@ -0,0 +1,59 @@
|
||||||
|
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)
|
||||||
|
props := &properties{}
|
||||||
|
s := &shell{
|
||||||
|
env: env,
|
||||||
|
props: props,
|
||||||
|
}
|
||||||
|
assert.Equal(t, "zsh", 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