Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language): allow tweaking version cache #5859

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cache/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,17 @@ type Duration string

const (
INFINITE = Duration("infinite")
NONE = Duration("none")
ONEWEEK = Duration("168h")
ONEDAY = Duration("24h")
TWOYEARS = Duration("17520h")
)

func (d Duration) Seconds() int {
if d == NONE {
return 0
}

if d == INFINITE {
return -1
}
Expand Down
8 changes: 7 additions & 1 deletion src/cache/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,16 @@ func (fc *File) Get(key string) (string, bool) {

// sets the value for the given key with a duration
func (fc *File) Set(key, value string, duration Duration) {
seconds := duration.Seconds()

if seconds == 0 {
return
}

fc.cache.Set(key, &Entry{
Value: value,
Timestamp: time.Now().Unix(),
TTL: duration.Seconds(),
TTL: seconds,
})

fc.dirty = true
Expand Down
5 changes: 4 additions & 1 deletion src/config/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ func (segment *Segment) migrate(version int) {
return
}

// Cache settings
// Cache settings, the default is now 24h so we have to respect this being disabled previously
if !segment.Properties.GetBool("cache_version", false) {
segment.Properties[properties.CacheDuration] = cache.NONE
}
delete(segment.Properties, "cache_version")

segment.IncludeFolders = segment.migrateFolders(includeFolders)
Expand Down
3 changes: 2 additions & 1 deletion src/segments/language.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ func (l *language) setVersion() error {
l.version.Executable = command.executable

if marchalled, err := json.Marshal(l.version); err == nil {
l.env.Cache().Set(cacheKey, string(marchalled), cache.ONEWEEK)
duration := l.props.GetString(properties.CacheDuration, string(cache.ONEWEEK))
l.env.Cache().Set(cacheKey, string(marchalled), cache.Duration(duration))
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
"type": "string",
"title": "Cache duration",
"description": "The duration for which the segment will be cached. This is parsed using the `time.ParseDuration` function from the Go standard library (see https://pkg.go.dev/time#ParseDuration for details).",
"pattern": "^(infinite|([0-9]+(h|m|s))+)$"
"pattern": "^(none|infinite|([0-9]+(h|m|s))+)$"
},
"filler": {
"type": "string",
Expand Down
10 changes: 6 additions & 4 deletions website/docs/configuration/segment.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@ The cache property allows you to control how often a segment is refreshed. This
generate or when you want to avoid fetching information too often. The cache property is an object with the following
properties:

| Name | Type | Description |
| ---------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `duration` | `string` | the duration for which the segment will be cached. The duration is a string in the format `1h2m3s`. The duration is parsed using the [time.ParseDuration](https://golang.org/pkg/time/#ParseDuration) function from the Go standard library |
| `strategy` | `string` | the strategy to use to identify if we should show the segment's cache value. See below for more information on strategy |
| Name | Type | Description |
| ---------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `duration` | `string` | the duration for which the segment will be cached. The duration is a string in the format `1h2m3s`. The duration is parsed using the [time.ParseDuration] function from the Go standard library. To disable the cache, use `none` |
| `strategy` | `string` | the strategy to use to identify if we should show the segment's cache value. See below for more information on strategy |

<Config
data={{
Expand Down Expand Up @@ -182,6 +182,7 @@ You can also combine these properties:
/>

:::info

- Oh My Posh will accept both `/` and `\` as path separators for a folder and will match regardless of which
is used by the current operating system.
- Because the strings are evaluated as regular expressions, if you want to use a backslash (`\`) in a Windows
Expand Down Expand Up @@ -236,3 +237,4 @@ To list the currently toggled segments, use `oh-my-posh get toggles`.
[cstp]: templates.mdx#cross-segment-template-properties
[cache]: #cache
[include-exclude]: #include--exclude-folders
[time.ParseDuration]: https://golang.org/pkg/time/#ParseDuration
14 changes: 8 additions & 6 deletions website/docs/segments/cli/angular.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import Config from "@site/src/components/Config.js";

## Properties

| Name | Type | Default | Description |
| Name | Type | Default | Description |
| ---------------------- | :--------: | :------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the angular version |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the angular version |
| `cache_duration` | `string` | `24h` | the duration for which the version will be cached. The duration is a string in the format `1h2m3s` and is parsed using the [time.ParseDuration] function from the Go standard library. To disable the cache, use `none` |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `extensions` | `[]string` | `angular.json` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |

Expand Down Expand Up @@ -59,3 +60,4 @@ import Config from "@site/src/components/Config.js";
[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[angular-cli-docs]: https://angular.io/cli
[time.ParseDuration]: https://golang.org/pkg/time/#ParseDuration
14 changes: 8 additions & 6 deletions website/docs/segments/cli/aurelia.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import Config from "@site/src/components/Config.js";

## Properties

| Name | Type | Default | Description |
| Name | Type | Default | Description |
| ---------------------- | :--------: | :------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the aurelia version |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the aurelia version |
| `cache_duration` | `string` | `24h` | the duration for which the version will be cached. The duration is a string in the format `1h2m3s` and is parsed using the [time.ParseDuration] function from the Go standard library. To disable the cache, use `none` |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `extensions` | `[]string` | `package.json` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |

Expand Down Expand Up @@ -59,3 +60,4 @@ import Config from "@site/src/components/Config.js";
[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[aurelia]: https://docs.aurelia.io/
[time.ParseDuration]: https://golang.org/pkg/time/#ParseDuration
Loading