feat: memory segment

This commit is contained in:
Jan0660 2021-09-03 20:54:19 +02:00 committed by Jan De Dobbeleer
parent 817725f0e7
commit daf91dc85a
7 changed files with 95 additions and 1 deletions

View file

@ -57,6 +57,7 @@ module.exports = {
"text",
"time",
"ytm",
"memory",
],
},
{

View file

@ -20,6 +20,7 @@ require (
github.com/huandu/xstrings v1.3.2 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.1
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
github.com/sergi/go-diff v1.2.0 // indirect
github.com/shirou/gopsutil v3.21.8+incompatible
github.com/smartystreets/goconvey v1.6.4 // indirect

View file

@ -90,6 +90,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 h1:onHthvaw9LFnH4t2DcNVpwGmV9E1BkGknEliJkfwQj0=
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58/go.mod h1:DXv8WO4yhMYhSNPKjeNKa5WY9YCIEBRbNzFFPJbWO6Y=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=

View file

@ -123,6 +123,8 @@ const (
Rust SegmentType = "rust"
// OWM writes the weather coming from openweatherdata
OWM SegmentType = "owm"
// Memory writes used memory percentage
Memory SegmentType = "memory"
)
func (segment *Segment) string() string {
@ -264,6 +266,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
Dart: &dart{},
Nbgv: &nbgv{},
Rust: &rust{},
Memory: &memory{},
}
if writer, ok := functions[segment.Type]; ok {
props := &properties{

34
src/segment_memory.go Normal file
View file

@ -0,0 +1,34 @@
package main
import (
"strconv"
mem "github.com/pbnjay/memory"
)
type memory struct {
props *properties
env environmentInfo
TotalMemory uint64
FreeMemory uint64
}
const (
Precision Property = "precision"
)
func (n *memory) enabled() bool {
return true
}
func (n *memory) string() string {
newText := strconv.FormatFloat(100.0/float64(n.TotalMemory)*float64(n.TotalMemory-n.FreeMemory), 'f', n.props.getInt(Precision, 0), 64)
return newText
}
func (n *memory) init(props *properties, env environmentInfo) {
n.props = props
n.env = env
n.TotalMemory = mem.TotalMemory()
n.FreeMemory = mem.FreeMemory()
}

View file

@ -0,0 +1,29 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMemory(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Memory memory
Precision int
}{
{Case: "50", ExpectedString: "50", Memory: memory{FreeMemory: 50, TotalMemory: 100}},
{Case: "50.0", ExpectedString: "50.0", Memory: memory{FreeMemory: 50, TotalMemory: 100}, Precision: 1},
}
for _, tc := range cases {
tc.Memory.env = new(MockedEnvironment)
tc.Memory.props = &properties{
values: map[Property]interface{}{
Precision: tc.Precision,
},
}
assert.Equal(t, tc.ExpectedString, tc.Memory.string(), tc.Case)
}
}

View file

@ -168,7 +168,8 @@
"crystal",
"dart",
"rust",
"owm"
"owm",
"memory"
]
},
"style": {
@ -1637,6 +1638,29 @@
"title": "Posh-Git Segment",
"description": "https://ohmyposh.dev/docs/poshgit"
}
},
{
"if": {
"properties": {
"type": { "const": "memory" }
}
},
"then": {
"title": "Get used memory percentage",
"description": "https://ohmyposh.dev/docs/memory",
"properties": {
"properties": {
"properties": {
"precision": {
"type": "integer",
"title": "Precision",
"description": "number of decimal places to show",
"default": 0
}
}
}
}
}
}
]
}