Skip to content

Commit

Permalink
refactor: move exclude/include folders to segment root
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Nov 3, 2024
1 parent a20d0e9 commit 9578349
Show file tree
Hide file tree
Showing 17 changed files with 203 additions and 241 deletions.
4 changes: 2 additions & 2 deletions src/cache/duration.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cache

import (
"strconv"
"time"
)

Expand Down Expand Up @@ -38,5 +37,6 @@ func ToDuration(seconds int) Duration {
return INFINITE
}

return Duration(strconv.Itoa(seconds) + "s")
duration := time.Duration(seconds) * time.Second
return Duration(duration.String())
}
52 changes: 47 additions & 5 deletions src/config/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package config

import (
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)

const (
includeFolders = properties.Property("include_folders")
excludeFolders = properties.Property("exclude_folders")
cacheTimeout = properties.Property("cache_timeout")
)

func (cfg *Config) Migrate() {
Expand All @@ -23,11 +30,46 @@ func (segment *Segment) migrate(version int) {

// Cache settings
delete(segment.Properties, "cache_version")
cacheTimeout := segment.Properties.GetInt("cache_timeout", 0)
if cacheTimeout != 0 {
segment.Cache = &cache.Config{
Duration: cache.ToDuration(cacheTimeout * 60),
Strategy: cache.Folder,
segment.Cache = segment.migrateCache()

segment.IncludeFolders = segment.migrateFolders(includeFolders)
segment.ExcludeFolders = segment.migrateFolders(excludeFolders)
}

func (segment *Segment) hasProperty(property properties.Property) bool {
for key := range segment.Properties {
if key == property {
return true
}
}
return false
}

func (segment *Segment) migrateCache() *cache.Config {
if !segment.hasProperty(cacheTimeout) {
return nil
}

timeout := segment.Properties.GetInt(cacheTimeout, 0)
delete(segment.Properties, cacheTimeout)

if timeout == 0 {
return nil
}

return &cache.Config{
Duration: cache.ToDuration(timeout * 60),
Strategy: cache.Folder,
}
}

func (segment *Segment) migrateFolders(property properties.Property) []string {
if !segment.hasProperty(property) {
return []string{}
}

array := segment.Properties.GetStringArray(property, []string{})
delete(segment.Properties, property)

return array
}
55 changes: 21 additions & 34 deletions src/config/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Segment struct {
env runtime.Environment
Properties properties.Map `json:"properties,omitempty" toml:"properties,omitempty"`
Cache *cache.Config `json:"cache,omitempty" toml:"cache,omitempty"`
Alias string `json:"alias,omitempty" toml:"alias,omitempty"`
LeadingPowerlineSymbol string `json:"leading_powerline_symbol,omitempty" toml:"leading_powerline_symbol,omitempty"`
Style SegmentStyle `json:"style,omitempty" toml:"style,omitempty"`
styleCache SegmentStyle
name string
LeadingDiamond string `json:"leading_diamond,omitempty" toml:"leading_diamond,omitempty"`
TrailingDiamond string `json:"trailing_diamond,omitempty" toml:"trailing_diamond,omitempty"`
Expand All @@ -52,20 +52,22 @@ type Segment struct {
Background color.Ansi `json:"background" toml:"background"`
Filler string `json:"filler,omitempty" toml:"filler,omitempty"`
Type SegmentType `json:"type,omitempty" toml:"type,omitempty"`
Style SegmentStyle `json:"style,omitempty" toml:"style,omitempty"`
styleCache SegmentStyle
ForegroundTemplates template.List `json:"foreground_templates,omitempty" toml:"foreground_templates,omitempty"`
Tips []string `json:"tips,omitempty" toml:"tips,omitempty"`
BackgroundTemplates template.List `json:"background_templates,omitempty" toml:"background_templates,omitempty"`
Templates template.List `json:"templates,omitempty" toml:"templates,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
Duration time.Duration `json:"-" toml:"-"`
NameLength int `json:"-" toml:"-"`
Interactive bool `json:"interactive,omitempty" toml:"interactive,omitempty"`
Enabled bool `json:"-" toml:"-"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty" toml:"invert_powerline,omitempty"`
Alias string `json:"alias,omitempty" toml:"alias,omitempty"`
LeadingPowerlineSymbol string `json:"leading_powerline_symbol,omitempty" toml:"leading_powerline_symbol,omitempty"`
ForegroundTemplates template.List `json:"foreground_templates,omitempty" toml:"foreground_templates,omitempty"`
Tips []string `json:"tips,omitempty" toml:"tips,omitempty"`
BackgroundTemplates template.List `json:"background_templates,omitempty" toml:"background_templates,omitempty"`
Templates template.List `json:"templates,omitempty" toml:"templates,omitempty"`
ExcludeFolders []string `json:"exclude_folders,omitempty" toml:"exclude_folders,omitempty"`
IncludeFolders []string `json:"include_folders,omitempty" toml:"include_folders,omitempty"`
Duration time.Duration `json:"-" toml:"-"`
NameLength int `json:"-" toml:"-"`
MaxWidth int `json:"max_width,omitempty" toml:"max_width,omitempty"`
MinWidth int `json:"min_width,omitempty" toml:"min_width,omitempty"`
Interactive bool `json:"interactive,omitempty" toml:"interactive,omitempty"`
Enabled bool `json:"-" toml:"-"`
Newline bool `json:"newline,omitempty" toml:"newline,omitempty"`
InvertPowerline bool `json:"invert_powerline,omitempty" toml:"invert_powerline,omitempty"`
}

func (segment *Segment) Name() string {
Expand Down Expand Up @@ -299,28 +301,13 @@ func (segment *Segment) shouldIncludeFolder() bool {
}

func (segment *Segment) cwdIncluded() bool {
value, ok := segment.Properties[properties.IncludeFolders]
if !ok {
// IncludeFolders isn't specified, everything is included
if len(segment.IncludeFolders) == 0 {
return true
}

list := properties.ParseStringArray(value)

if len(list) == 0 {
// IncludeFolders is an empty array, everything is included
return true
}

return segment.env.DirMatchesOneOf(segment.env.Pwd(), list)
return segment.env.DirMatchesOneOf(segment.env.Pwd(), segment.IncludeFolders)
}

func (segment *Segment) cwdExcluded() bool {
value, ok := segment.Properties[properties.ExcludeFolders]
if !ok {
return false
}

list := properties.ParseStringArray(value)
return segment.env.DirMatchesOneOf(segment.env.Pwd(), list)
return segment.env.DirMatchesOneOf(segment.env.Pwd(), segment.ExcludeFolders)
}
19 changes: 8 additions & 11 deletions src/config/segment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"testing"

"github.com/jandedobbeleer/oh-my-posh/src/color"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/jandedobbeleer/oh-my-posh/src/segments"
Expand Down Expand Up @@ -46,11 +45,11 @@ func TestParseTestConfig(t *testing.T) {
"foreground": "#ffffff",
"background": "#61AFEF",
"properties": {
"style": "folder",
"exclude_folders": [
"/super/secret/project"
]
}
"style": "folder"
},
"exclude_folders": [
"/super/secret/project"
]
}
`
segment := &Segment{}
Expand Down Expand Up @@ -78,11 +77,9 @@ func TestShouldIncludeFolder(t *testing.T) {
env.On("DirMatchesOneOf", cwd, []string{"Projects/oh-my-posh"}).Return(tc.Included)
env.On("DirMatchesOneOf", cwd, []string{"Projects/nope"}).Return(tc.Excluded)
segment := &Segment{
Properties: properties.Map{
properties.IncludeFolders: []string{"Projects/oh-my-posh"},
properties.ExcludeFolders: []string{"Projects/nope"},
},
env: env,
IncludeFolders: []string{"Projects/oh-my-posh"},
ExcludeFolders: []string{"Projects/nope"},
env: env,
}
got := segment.shouldIncludeFolder()
assert.Equal(t, tc.Expected, got, tc.Case)
Expand Down
4 changes: 0 additions & 4 deletions src/properties/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ type Property string
const (
// Style indicates the style to use
Style Property = "style"
// IncludeFolders indicates folders to be included for the segment logic
IncludeFolders Property = "include_folders"
// ExcludeFolders indicates folders to be excluded for the segment logic
ExcludeFolders Property = "exclude_folders"
// FetchVersion decides whether to fetch the version number or not
FetchVersion Property = "fetch_version"
// AlwaysEnabled decides whether or not to always display the info
Expand Down
4 changes: 0 additions & 4 deletions src/segments/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,6 @@ func (g *Git) shouldDisplay() bool {
return false
}

if g.shouldIgnoreRootRepository(gitdir.ParentFolder) {
return false
}

g.setDir(gitdir.Path)

if !gitdir.IsDir {
Expand Down
4 changes: 0 additions & 4 deletions src/segments/mercurial.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ func (hg *Mercurial) shouldDisplay() bool {
return false
}

if hg.shouldIgnoreRootRepository(hgdir.ParentFolder) {
return false
}

hg.setDir(hgdir.ParentFolder)

hg.workingDir = hgdir.Path
Expand Down
4 changes: 0 additions & 4 deletions src/segments/plastic.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,6 @@ func (p *Plastic) Enabled() bool {
return false
}

if p.shouldIgnoreRootRepository(wkdir.ParentFolder) {
return false
}

if !wkdir.IsDir {
return false
}
Expand Down
4 changes: 0 additions & 4 deletions src/segments/sapling.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,6 @@ func (sl *Sapling) shouldDisplay() bool {
return false
}

if sl.shouldIgnoreRootRepository(slDir.ParentFolder) {
return false
}

sl.workingDir = slDir.Path
sl.rootDir = slDir.Path
// convert the worktree file path to a windows one when in a WSL shared folder
Expand Down
14 changes: 1 addition & 13 deletions src/segments/sapling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ func TestShouldDisplay(t *testing.T) {
HasSapling bool
InRepo bool
Expected bool
Excluded bool
}{
{
Case: "Sapling not installed",
Expand All @@ -134,12 +133,6 @@ func TestShouldDisplay(t *testing.T) {
Case: "Sapling installed, not in repo",
HasSapling: true,
},
{
Case: "Sapling installed, in repo but ignored",
HasSapling: true,
InRepo: true,
Excluded: true,
},
{
Case: "Sapling installed, in repo",
HasSapling: true,
Expand All @@ -158,19 +151,14 @@ func TestShouldDisplay(t *testing.T) {
env.On("InWSLSharedDrive").Return(false)
env.On("GOOS").Return(runtime.LINUX)
env.On("Home").Return("/usr/home/sapling")
env.On("DirMatchesOneOf", fileInfo.ParentFolder, []string{"/sapling/repo"}).Return(tc.Excluded)
if tc.InRepo {
env.On("HasParentFilePath", ".sl", false).Return(fileInfo, nil)
} else {
env.On("HasParentFilePath", ".sl", false).Return(&runtime.FileInfo{}, errors.New("error"))
}

props := &properties.Map{
properties.ExcludeFolders: []string{"/sapling/repo"},
}

sl := &Sapling{}
sl.Init(props, env)
sl.Init(&properties.Map{}, env)

got := sl.shouldDisplay()
assert.Equal(t, tc.Expected, got, tc.Case)
Expand Down
8 changes: 0 additions & 8 deletions src/segments/scm.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,6 @@ func (s *scm) formatBranch(branch string) string {
return string(runes[0:maxLength]) + truncateSymbol
}

func (s *scm) shouldIgnoreRootRepository(rootDir string) bool {
excludedFolders := s.props.GetStringArray(properties.ExcludeFolders, []string{})
if len(excludedFolders) == 0 {
return false
}
return s.env.DirMatchesOneOf(rootDir, excludedFolders)
}

func (s *scm) FileContents(folder, file string) string {
return strings.Trim(s.env.FileContent(folder+"/"+file), " \r\n")
}
Expand Down
4 changes: 0 additions & 4 deletions src/segments/svn.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ func (s *Svn) shouldDisplay() bool {
return false
}

if s.shouldIgnoreRootRepository(Svndir.ParentFolder) {
return false
}

if Svndir.IsDir {
s.workingDir = Svndir.Path
s.rootDir = Svndir.Path
Expand Down
40 changes: 19 additions & 21 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -450,27 +450,7 @@
"type": "object",
"title": "Segment Properties, used to change behavior/displaying",
"description": "https://ohmyposh.dev/docs/configuration/segment#properties",
"default": {},
"properties": {
"include_folders": {
"type": "array",
"title": "If specified, segment will only render in these folders",
"description": "https://ohmyposh.dev/docs/configuration/segment#include--exclude-folders",
"default": [],
"items": {
"type": "string"
}
},
"exclude_folders": {
"type": "array",
"title": "Exclude rendering in these folders",
"description": "https://ohmyposh.dev/docs/configuration/segment#include--exclude-folders",
"default": [],
"items": {
"type": "string"
}
}
}
"default": {}
},
"interactive": {
"type": "boolean",
Expand All @@ -484,6 +464,24 @@
"description": "https://ohmyposh.dev/docs/configuration/segment",
"default": ""
},
"include_folders": {
"type": "array",
"title": "If specified, segment will only render in these folders",
"description": "https://ohmyposh.dev/docs/configuration/segment#include--exclude-folders",
"default": [],
"items": {
"type": "string"
}
},
"exclude_folders": {
"type": "array",
"title": "Exclude rendering in these folders",
"description": "https://ohmyposh.dev/docs/configuration/segment#include--exclude-folders",
"default": [],
"items": {
"type": "string"
}
},
"cache": {
"type": "object",
"title": "Cache settings",
Expand Down
2 changes: 1 addition & 1 deletion website/docs/configuration/block.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import Config from "@site/src/components/Config.js";
}}
/>

## Properties
## Settings

| Name | Type |
| ------------------ | --------- |
Expand Down
Loading

0 comments on commit 9578349

Please sign in to comment.