mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-12-28 12:29:40 -08:00
feat: ignore folders for segments
This commit is contained in:
parent
91b962a49a
commit
9ffa132174
15
engine.go
15
engine.go
|
@ -62,16 +62,9 @@ func (e *engine) renderDiamondSegment(text string) {
|
||||||
e.renderer.write(Transparent, e.activeSegment.Background, e.activeSegment.TrailingDiamond)
|
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) {
|
func (e *engine) renderText(text string) {
|
||||||
prefix := e.getStringProperty(Prefix, " ")
|
prefix := e.activeSegment.getValue(Prefix, " ")
|
||||||
postfix := e.getStringProperty(Postfix, " ")
|
postfix := e.activeSegment.getValue(Postfix, " ")
|
||||||
e.renderer.write(e.activeSegment.Background, e.activeSegment.Foreground, fmt.Sprintf("%s%s%s", prefix, text, 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 {
|
func (e *engine) renderBlockSegments(block *Block) string {
|
||||||
defer e.reset()
|
defer e.reset()
|
||||||
e.activeBlock = block
|
e.activeBlock = block
|
||||||
|
cwd, _ := e.env.getwd()
|
||||||
for _, segment := range block.Segments {
|
for _, segment := range block.Segments {
|
||||||
|
if segment.hasValue(IgnoreFolders, cwd) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
props, err := segment.mapSegmentWithWriter(e.env)
|
props, err := segment.mapSegmentWithWriter(e.env)
|
||||||
if err != nil || !segment.enabled() {
|
if err != nil || !segment.enabled() {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "regexp"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
)
|
||||||
|
|
||||||
//Property defines one property of a segment for context
|
//Property defines one property of a segment for context
|
||||||
type Property string
|
type Property string
|
||||||
|
@ -15,6 +18,8 @@ const (
|
||||||
Postfix Property = "postfix"
|
Postfix Property = "postfix"
|
||||||
//ColorBackground color the background or foreground when a specific color is set
|
//ColorBackground color the background or foreground when a specific color is set
|
||||||
ColorBackground Property = "color_background"
|
ColorBackground Property = "color_background"
|
||||||
|
//IgnoreFolders folders to ignore and not run the segment logic
|
||||||
|
IgnoreFolders Property = "ignore_folders"
|
||||||
)
|
)
|
||||||
|
|
||||||
type properties struct {
|
type properties struct {
|
||||||
|
@ -73,3 +78,15 @@ func (p *properties) getBool(property Property, defaultValue bool) bool {
|
||||||
}
|
}
|
||||||
return boolValue
|
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
|
||||||
|
}
|
||||||
|
|
20
segment.go
20
segment.go
|
@ -72,6 +72,26 @@ func (segment *Segment) enabled() bool {
|
||||||
return segment.writer.enabled()
|
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) {
|
func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties, error) {
|
||||||
functions := map[SegmentType]SegmentWriter{
|
functions := map[SegmentType]SegmentWriter{
|
||||||
Session: &session{},
|
Session: &session{},
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
@ -26,3 +27,29 @@ func TestMapSegmentWriterCannotMap(t *testing.T) {
|
||||||
assert.Nil(t, props)
|
assert.Nil(t, props)
|
||||||
assert.Error(t, err)
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue