mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: memory segment
This commit is contained in:
parent
817725f0e7
commit
daf91dc85a
|
@ -57,6 +57,7 @@ module.exports = {
|
||||||
"text",
|
"text",
|
||||||
"time",
|
"time",
|
||||||
"ytm",
|
"ytm",
|
||||||
|
"memory",
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
@ -20,6 +20,7 @@ require (
|
||||||
github.com/huandu/xstrings v1.3.2 // indirect
|
github.com/huandu/xstrings v1.3.2 // indirect
|
||||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.4.1
|
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/sergi/go-diff v1.2.0 // indirect
|
||||||
github.com/shirou/gopsutil v3.21.8+incompatible
|
github.com/shirou/gopsutil v3.21.8+incompatible
|
||||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||||
|
|
|
@ -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 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 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
|
||||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
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 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
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=
|
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
|
||||||
|
|
|
@ -123,6 +123,8 @@ const (
|
||||||
Rust SegmentType = "rust"
|
Rust SegmentType = "rust"
|
||||||
// OWM writes the weather coming from openweatherdata
|
// OWM writes the weather coming from openweatherdata
|
||||||
OWM SegmentType = "owm"
|
OWM SegmentType = "owm"
|
||||||
|
// Memory writes used memory percentage
|
||||||
|
Memory SegmentType = "memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (segment *Segment) string() string {
|
func (segment *Segment) string() string {
|
||||||
|
@ -264,6 +266,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) error {
|
||||||
Dart: &dart{},
|
Dart: &dart{},
|
||||||
Nbgv: &nbgv{},
|
Nbgv: &nbgv{},
|
||||||
Rust: &rust{},
|
Rust: &rust{},
|
||||||
|
Memory: &memory{},
|
||||||
}
|
}
|
||||||
if writer, ok := functions[segment.Type]; ok {
|
if writer, ok := functions[segment.Type]; ok {
|
||||||
props := &properties{
|
props := &properties{
|
||||||
|
|
34
src/segment_memory.go
Normal file
34
src/segment_memory.go
Normal 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()
|
||||||
|
}
|
29
src/segment_memory_test.go
Normal file
29
src/segment_memory_test.go
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -168,7 +168,8 @@
|
||||||
"crystal",
|
"crystal",
|
||||||
"dart",
|
"dart",
|
||||||
"rust",
|
"rust",
|
||||||
"owm"
|
"owm",
|
||||||
|
"memory"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"style": {
|
"style": {
|
||||||
|
@ -1637,6 +1638,29 @@
|
||||||
"title": "Posh-Git Segment",
|
"title": "Posh-Git Segment",
|
||||||
"description": "https://ohmyposh.dev/docs/poshgit"
|
"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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue