feat: add shell info segment

This commit is contained in:
Jan De Dobbeleer 2020-09-15 13:44:53 +02:00
parent 2791b180b2
commit 5c4af19d90
8 changed files with 134 additions and 11 deletions

10
Gopkg.lock generated
View file

@ -26,6 +26,15 @@
revision = "cd55aeac72a19c6d5826061d5125d72b252a12eb"
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]]
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
name = "github.com/pmezard/go-difflib"
@ -113,6 +122,7 @@
input-imports = [
"github.com/distatus/battery",
"github.com/imdario/mergo",
"github.com/mitchellh/go-ps",
"github.com/stretchr/testify/assert",
"github.com/stretchr/testify/mock",
"golang.org/x/sys/windows",

View file

@ -47,6 +47,11 @@
version = "v0.1.1"
source = "github.com/stretchr/objx"
[[constraint]]
name = "github.com/mitchellh/go-ps"
version = "v1.0.0"
source = "github.com/mitchellh/go-ps"
[[constraint]]
name = "github.com/distatus/battery"
branch = "master"

View file

@ -70,6 +70,15 @@
"postfix": " "
}
},
{
"type": "shell",
"style": "powerline",
"foreground": "#ffffff",
"background": "#0077c2",
"properties": {
"prefix": "  "
}
},
{
"type": "exit",
"style": "diamond",

View file

@ -9,6 +9,7 @@ import (
"strings"
"github.com/distatus/battery"
ps "github.com/mitchellh/go-ps"
)
type environmentInfo interface {
@ -25,6 +26,7 @@ type environmentInfo interface {
lastErrorCode() int
getArgs() *args
getBatteryInfo() (*battery.Battery, error)
getParentProcess() (ps.Process, error)
}
type environment struct {
@ -93,6 +95,11 @@ func (env *environment) getBatteryInfo() (*battery.Battery, error) {
return battery.Get(0)
}
func (env *environment) getParentProcess() (ps.Process, error) {
pid := os.Getppid()
return ps.FindProcess(pid)
}
func cleanHostName(hostName string) string {
garbage := []string{
".lan",

View file

@ -48,6 +48,8 @@ const (
Battery SegmentType = "battery"
//Spotify writes the Spotify status for Mac
Spotify SegmentType = "spotify"
//ShellInfo writes which shell we're currently in
ShellInfo SegmentType = "shell"
//Powerline writes it Powerline style
Powerline SegmentStyle = "powerline"
//Plain writes it without ornaments
@ -66,17 +68,18 @@ func (segment *Segment) enabled() bool {
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) *properties {
functions := map[SegmentType]SegmentWriter{
Session: &session{},
Path: &path{},
Git: &git{},
Exit: &exit{},
Venv: &venv{},
Root: &root{},
Text: &text{},
Time: &tempus{},
Cmd: &command{},
Battery: &batt{},
Spotify: &spotify{},
Session: &session{},
Path: &path{},
Git: &git{},
Exit: &exit{},
Venv: &venv{},
Root: &root{},
Text: &text{},
Time: &tempus{},
Cmd: &command{},
Battery: &batt{},
Spotify: &spotify{},
ShellInfo: &shell{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

View file

@ -7,6 +7,7 @@ import (
"testing"
"github.com/distatus/battery"
ps "github.com/mitchellh/go-ps"
"github.com/stretchr/testify/assert"
"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)
}
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) {
want := "/usr/err"
env := new(MockedEnvironment)

24
segment_shell.go Normal file
View 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
View 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())
}