feat: ignore folders for segments

This commit is contained in:
Jan De Dobbeleer 2020-10-02 16:58:25 +02:00 committed by Jan De Dobbeleer
parent 91b962a49a
commit 9ffa132174
4 changed files with 71 additions and 10 deletions

View file

@ -62,16 +62,9 @@ func (e *engine) renderDiamondSegment(text string) {
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.TrailingDiamond)
}
func (e *engine) getStringProperty(property Property, defaultValue string) string {
if value, ok := e.activeSegment.Properties[property]; ok {
return parseString(value, defaultValue)
}
return defaultValue
}
func (e *engine) renderText(text string) {
prefix := e.getStringProperty(Prefix, " ")
postfix := e.getStringProperty(Postfix, " ")
prefix := e.activeSegment.getValue(Prefix, " ")
postfix := e.activeSegment.getValue(Postfix, " ")
e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, postfix))
}
@ -90,7 +83,11 @@ func (e *engine) renderSegmentText(text string) {
func (e *engine) renderBlockSegments(block *Block) string {
defer e.reset()
e.activeBlock = block
cwd, _ := e.env.getwd()
for _, segment := range block.Segments {
if segment.hasValue(IgnoreFolders, cwd) {
continue
}
props, err := segment.mapSegmentWithWriter(e.env)
if err != nil || !segment.enabled() {
continue

View file

@ -1,6 +1,9 @@
package main
import "regexp"
import (
"fmt"
"regexp"
)
//Property defines one property of a segment for context
type Property string
@ -15,6 +18,8 @@ const (
Postfix Property = "postfix"
//ColorBackground color the background or foreground when a specific color is set
ColorBackground Property = "color_background"
//IgnoreFolders folders to ignore and not run the segment logic
IgnoreFolders Property = "ignore_folders"
)
type properties struct {
@ -73,3 +78,15 @@ func (p *properties) getBool(property Property, defaultValue bool) bool {
}
return boolValue
}
func parseStringArray(value interface{}) []string {
expectedValue, ok := value.([]interface{})
if !ok {
return []string{}
}
list := make([]string, len(expectedValue))
for i, v := range expectedValue {
list[i] = fmt.Sprint(v)
}
return list
}

View file

@ -72,6 +72,26 @@ func (segment *Segment) enabled() bool {
return segment.writer.enabled()
}
func (segment *Segment) getValue(property Property, defaultValue string) string {
if value, ok := segment.Properties[property]; ok {
return parseString(value, defaultValue)
}
return defaultValue
}
func (segment *Segment) hasValue(property Property, match string) bool {
if value, ok := segment.Properties[property]; ok {
list := parseStringArray(value)
for _, element := range list {
if element == match {
return true
}
}
return false
}
return false
}
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties, error) {
functions := map[SegmentType]SegmentWriter{
Session: &session{},

View file

@ -1,6 +1,7 @@
package main
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
@ -26,3 +27,29 @@ func TestMapSegmentWriterCannotMap(t *testing.T) {
assert.Nil(t, props)
assert.Error(t, err)
}
func TestParseTestSettings(t *testing.T) {
segmentJSON :=
`
{
"type": "path",
"style": "powerline",
"powerline_symbol": "",
"foreground": "#ffffff",
"background": "#61AFEF",
"properties": {
"prefix": "  ",
"style": "folder",
"ignore_folders": [
"go-my-psh"
]
}
}
`
segment := &Segment{}
err := json.Unmarshal([]byte(segmentJSON), segment)
assert.NoError(t, err)
expected := "go-my-psh"
got := segment.hasValue(IgnoreFolders, expected)
assert.True(t, got)
}