Skip to content

Commit

Permalink
feat: memory segment
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan0660 authored and JanDeDobbeleer committed Sep 3, 2021
1 parent 817725f commit daf91dc
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/sidebars.js
Expand Up @@ -57,6 +57,7 @@ module.exports = {
"text",
"time",
"ytm",
"memory",
],
},
{
Expand Down
1 change: 1 addition & 0 deletions src/go.mod
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/go.sum
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions src/segment.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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{
Expand Down
34 changes: 34 additions & 0 deletions src/segment_memory.go
@@ -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 changes: 29 additions & 0 deletions src/segment_memory_test.go
@@ -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)
}
}
26 changes: 25 additions & 1 deletion themes/schema.json
Expand Up @@ -168,7 +168,8 @@
"crystal",
"dart",
"rust",
"owm"
"owm",
"memory"
]
},
"style": {
Expand Down Expand Up @@ -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
}
}
}
}
}
}
]
}
Expand Down

0 comments on commit daf91dc

Please sign in to comment.