-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor(config): use generic cache for SegmentWriter #7316
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
Open
Mertozturkk
wants to merge
3
commits into
JanDeDobbeleer:main
Choose a base branch
from
Mertozturkk:refactor/generic-segment-writer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/jandedobbeleer/oh-my-posh/src/cache" | ||
| "github.com/jandedobbeleer/oh-my-posh/src/maps" | ||
| "github.com/jandedobbeleer/oh-my-posh/src/runtime/mock" | ||
| "github.com/jandedobbeleer/oh-my-posh/src/segments" | ||
| "github.com/jandedobbeleer/oh-my-posh/src/template" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestSegmentCache(t *testing.T) { | ||
| template.Cache = &cache.Template{ | ||
| Segments: maps.NewConcurrent[any](), | ||
| } | ||
| defer cache.DeleteAll(cache.Device) | ||
|
|
||
| env := new(mock.Environment) | ||
| env.On("Pwd").Return("/tmp") | ||
Mertozturkk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| segment := &Segment{ | ||
| Type: TEXT, | ||
| Cache: &Cache{ | ||
| Strategy: Folder, | ||
| Duration: cache.Duration("10m"), | ||
| }, | ||
| Alias: "my_text_segment", | ||
| env: env, | ||
| } | ||
|
|
||
| textWriter := &segments.Text{} | ||
| textWriter.Init(nil, nil) | ||
| textWriter.SetText("Hello, Cache!") | ||
| segment.writer = textWriter | ||
| segment.name = "my_text_segment" | ||
|
|
||
| segment.setCache() | ||
|
|
||
| newSegment := &Segment{ | ||
| Type: TEXT, | ||
| Cache: &Cache{ | ||
| Strategy: Folder, | ||
| Duration: cache.Duration("10m"), | ||
| }, | ||
| Alias: "my_text_segment", | ||
| env: env, | ||
| } | ||
| newSegment.name = "my_text_segment" | ||
|
|
||
| newTextWriter := &segments.Text{} | ||
| newTextWriter.Init(nil, nil) | ||
| newSegment.writer = newTextWriter | ||
|
|
||
| restored := newSegment.restoreCache() | ||
|
|
||
| assert.True(t, restored, "Cache should be restored") | ||
| assert.NotNil(t, newSegment.writer, "Writer should be restored") | ||
| assert.IsType(t, &segments.Text{}, newSegment.writer, "Writer should be of type *segments.Text") | ||
| if newSegment.writer != nil { | ||
| assert.Equal(t, "Hello, Cache!", newSegment.writer.Text(), "Restored text should match") | ||
| } | ||
|
|
||
| // Test legacy cache (string value) | ||
| legacySegment := &Segment{ | ||
| Type: TEXT, | ||
| Cache: &Cache{ | ||
| Strategy: Device, | ||
| Duration: cache.Duration("10m"), | ||
| }, | ||
| Alias: "legacy_segment", | ||
| env: env, | ||
| } | ||
| legacySegment.name = "legacy_segment" | ||
|
|
||
| // Initialize writer so cacheKeyAndStore can compute the key | ||
| legacyWriter := &segments.Text{} | ||
| legacyWriter.Init(nil, nil) | ||
| legacySegment.writer = legacyWriter | ||
|
|
||
| // Derive the key using the same logic as production code | ||
| legacyKey, legacyStore := legacySegment.cacheKeyAndStore() | ||
| cache.Set(legacyStore, legacyKey, "legacy_json_string", cache.Duration("10m")) | ||
|
|
||
| // Should return false and delete the key | ||
| restoredLegacy := legacySegment.restoreCache() | ||
| assert.False(t, restoredLegacy, "Legacy cache should not be restored") | ||
|
|
||
| // Verify key is gone | ||
| _, found := cache.Get[string](legacyStore, legacyKey) | ||
| assert.False(t, found, "Legacy key should be removed") | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package segments | ||
|
|
||
| import ( | ||
| "strings" | ||
|
|
||
| c "golang.org/x/text/cases" | ||
| "golang.org/x/text/language" | ||
|
|
||
| "github.com/jandedobbeleer/oh-my-posh/src/log" | ||
| "github.com/jandedobbeleer/oh-my-posh/src/segments/options" | ||
| ) | ||
|
|
||
| // Taskwarrior option constants | ||
| const ( | ||
| TaskwarriorCommand options.Option = "command" | ||
| TaskwarriorCommands options.Option = "commands" | ||
| ) | ||
|
|
||
| // Taskwarrior displays task counts and context from Taskwarrior. | ||
| // The Commands field is a map from capitalized command name to the raw output | ||
| // of the corresponding Taskwarrior invocation. Each entry in the config map | ||
| // has the command name as key and a full Taskwarrior argument string as value. | ||
| type Taskwarrior struct { | ||
| Base | ||
|
|
||
| // Commands holds the raw output of each configured command, keyed by name | ||
| // with the first letter uppercased. | ||
| Commands map[string]string | ||
| } | ||
|
|
||
| func (t *Taskwarrior) Template() string { | ||
| return " \uf4a0 {{ range $k, $v := .Commands }}{{ $k }}:{{ $v }} {{ end }}" | ||
| } | ||
|
|
||
| func (t *Taskwarrior) Enabled() bool { | ||
| cmd := t.options.String(TaskwarriorCommand, "task") | ||
|
|
||
| if !t.env.HasCommand(cmd) { | ||
| return false | ||
| } | ||
|
|
||
| defaultCommands := map[string]string{ | ||
| "due": "+PENDING due.before:tomorrow count", | ||
| "scheduled": "+PENDING scheduled.before:tomorrow count", | ||
| "waiting": "+WAITING count", | ||
| "context": "_get rc.context", | ||
| } | ||
|
|
||
| configuredCommands := t.options.KeyValueMap(TaskwarriorCommands, defaultCommands) | ||
|
|
||
| t.Commands = make(map[string]string, len(configuredCommands)) | ||
|
|
||
| for name, args := range configuredCommands { | ||
| key := c.Title(language.English).String(name) | ||
| t.Commands[key] = t.runCommand(cmd, args) | ||
| } | ||
|
|
||
| return true | ||
| } | ||
|
|
||
| func (t *Taskwarrior) runCommand(cmd, args string) string { | ||
| output, err := t.env.RunCommand(cmd, strings.Fields(args)...) | ||
| if err != nil { | ||
| log.Error(err) | ||
| return "" | ||
| } | ||
|
|
||
| return strings.TrimSpace(output) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.