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: next major release with performance improvements #5686

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
11 changes: 6 additions & 5 deletions .github/workflows/code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ jobs:
run:
working-directory: ${{ github.workspace }}/src
steps:
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
with:
go-version: 1.21
cache-dependency-path: src/go.sum
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: Install Go 🗳
uses: ./.github/workflows/composite/bootstrap-go
- name: Golang CI
uses: golangci/golangci-lint-action@971e284b6050e8a5849b72094c50ab08da042db8
with:
working-directory: src
- name: Fieldalignment
run: |
go install golang.org/x/tools/go/analysis/passes/fieldalignment/cmd/fieldalignment@latest
fieldalignment "./..."
- name: Unit Tests
run: go test -v ./...
7 changes: 2 additions & 5 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ jobs:
run:
working-directory: ${{ github.workspace }}/src
steps:
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
with:
go-version: 1.21
cache-dependency-path: src/go.sum
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: Install Go 🗳
uses: ./.github/workflows/composite/bootstrap-go
- name: Initialize CodeQL
uses: github/codeql-action/init@c36620d31ac7c881962c3d9dd939c40ec9434f2b
with:
Expand Down
10 changes: 4 additions & 6 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ jobs:
with:
submodules: true
persist-credentials: false
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
with:
go-version: 1.21
cache-dependency-path: src/go.sum
- uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6
- name: Install Go 🗳
uses: ./.github/workflows/composite/bootstrap-go
- name: Setup Node.js
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6
with:
node-version: 20.9.0
# Create Kind cluster to have a Kubernetes context for cloud-native-azure theme
Expand Down
7 changes: 2 additions & 5 deletions .github/workflows/gomod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ jobs:
run:
working-directory: ${{ github.workspace }}/src
steps:
- name: Install Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32
with:
go-version: 1.21
cache-dependency-path: src/go.sum
- name: Checkout code
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871
- name: Install Go 🗳
uses: ./.github/workflows/composite/bootstrap-go
- name: Check for unused dependencies
run: |
go mod tidy
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
.fleet/
src/test/umbraco/obj/
src/keys
*.prof

# Created by https://www.toptal.com/developers/gitignore/api/node,go,visualstudiocode
# Edit at https://www.toptal.com/developers/gitignore?templates=node,go,visualstudiocode
Expand Down
17 changes: 8 additions & 9 deletions src/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,19 @@ type Cache interface {
Close()
// Gets the value for a given key.
// Returns the value and a boolean indicating if the key was found.
// In case the ttl expired, the function returns false.
// In case the duration expired, the function returns false.
Get(key string) (string, bool)
// Sets a value for a given key.
// The ttl indicates how many minutes to cache the value.
Set(key, value string, ttl int)
// The duration indicates how many minutes to cache the value.
Set(key, value string, duration Duration)
// Deletes a key from the cache.
Delete(key string)
}

type Context interface {
CacheKey() (string, bool)
}

const (
FileName = "omp.cache"
)
Expand All @@ -45,11 +49,6 @@ const (
PROMPTCOUNTCACHE = "prompt_count_cache"
ENGINECACHE = "engine_cache"
FONTLISTCACHE = "font_list_cache"

ONEDAY = 1440
ONEWEEK = 10080
ONEMONTH = 43200
INFINITE = -1
)

type Entry struct {
Expand All @@ -63,5 +62,5 @@ func (c *Entry) Expired() bool {
return false
}

return time.Now().Unix() >= (c.Timestamp + int64(c.TTL)*60)
return time.Now().Unix() >= (c.Timestamp + int64(c.TTL))
}
13 changes: 13 additions & 0 deletions src/cache/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cache

type Config struct {
Duration Duration `json:"duration,omitempty" toml:"duration,omitempty"`
Strategy Strategy `json:"strategy,omitempty" toml:"strategy,omitempty"`
}

type Strategy string

const (
Folder Strategy = "folder"
Session Strategy = "session"
)
86 changes: 86 additions & 0 deletions src/cache/duration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package cache

import (
"strconv"

"github.com/jandedobbeleer/oh-my-posh/src/regex"
)

type Duration string

const INFINITE = Duration("infinite")

func (d Duration) Seconds() int {
if d == INFINITE {
return -1
}

re := `(?P<AMOUNT>[0-9]*)(?P<UNIT>.*)`
match := regex.FindNamedRegexMatch(re, string(d))
if len(match) < 2 {
return 0
}

amount := match["AMOUNT"]
unit := match["UNIT"]

if len(amount) == 0 {
return 0
}

amountInt, err := strconv.Atoi(amount)
if err != nil {
return 0
}

var multiplier int

switch unit {
case "second", "seconds":
multiplier = 1
case "minute", "minutes":
multiplier = 60
case "hour", "hours":
multiplier = 3600
case "day", "days":
multiplier = 86400
case "week", "weeks":
multiplier = 604800
case "month", "months":
multiplier = 2592000
}

return amountInt * multiplier
}

func (d Duration) IsEmpty() bool {
return len(d) == 0
}

func ToDuration(seconds int) Duration {
if seconds == 0 {
return ""
}

if seconds == -1 {
return "infinite"
}

if seconds%604800 == 0 {
return Duration(strconv.Itoa(seconds/604800) + "weeks")
}

if seconds%86400 == 0 {
return Duration(strconv.Itoa(seconds/86400) + "days")
}

if seconds%3600 == 0 {
return Duration(strconv.Itoa(seconds/3600) + "hours")
}

if seconds%60 == 0 {
return Duration(strconv.Itoa(seconds/60) + "minutes")
}

return Duration(strconv.Itoa(seconds) + "seconds")
}
95 changes: 95 additions & 0 deletions src/cache/duration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package cache

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestSeconds(t *testing.T) {
cases := []struct {
Case string
Duration Duration
Expected int
}{
{
Case: "1 second",
Duration: "1second",
Expected: 1,
},
{
Case: "2 seconds",
Duration: "2seconds",
Expected: 2,
},
{
Case: "1 minute",
Duration: "1minute",
Expected: 60,
},
{
Case: "2 minutes",
Duration: "2minutes",
Expected: 120,
},
{
Case: "1 hour",
Duration: "1hour",
Expected: 3600,
},
{
Case: "2 hours",
Duration: "2hours",
Expected: 7200,
},
{
Case: "1 day",
Duration: "1day",
Expected: 86400,
},
{
Case: "2 days",
Duration: "2days",
Expected: 172800,
},
{
Case: "1 week",
Duration: "1week",
Expected: 604800,
},
{
Case: "2 weeks",
Duration: "2weeks",
Expected: 1209600,
},
{
Case: "1 month",
Duration: "1month",
Expected: 2592000,
},
{
Case: "2 months",
Duration: "2month",
Expected: 5184000,
},
{
Case: "invalid",
Duration: "foo",
Expected: 0,
},
{
Case: "1 fortnight",
Duration: "1fortnight",
Expected: 0,
},
{
Case: "infinite",
Duration: "infinite",
Expected: -1,
},
}
for _, tc := range cases {
got := tc.Duration.Seconds()
assert.Equal(t, tc.Expected, got, tc.Case)
}
}
8 changes: 4 additions & 4 deletions src/cache/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (fc *File) Close() {
}

// returns the value for the given key as long as
// the TTL (minutes) is not expired
// the duration is not expired
func (fc *File) Get(key string) (string, bool) {
val, found := fc.cache.Get(key)
if !found {
Expand All @@ -73,12 +73,12 @@ func (fc *File) Get(key string) (string, bool) {
return "", false
}

// sets the value for the given key with a TTL (minutes)
func (fc *File) Set(key, value string, ttl int) {
// sets the value for the given key with a duration
func (fc *File) Set(key, value string, duration Duration) {
fc.cache.Set(key, &Entry{
Value: value,
Timestamp: time.Now().Unix(),
TTL: ttl,
TTL: duration.Seconds(),
})

fc.dirty = true
Expand Down
10 changes: 7 additions & 3 deletions src/cache/mock/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package mock

import mock "github.com/stretchr/testify/mock"
import (
"github.com/jandedobbeleer/oh-my-posh/src/cache"
mock "github.com/stretchr/testify/mock"
)

type Cache struct {
mock.Mock
Expand Down Expand Up @@ -34,8 +37,9 @@ func (_m *Cache) Get(key string) (string, bool) {
return r0, r1
}

func (_m *Cache) Set(key, value string, ttl int) {
_m.Called(key, value, ttl)
// set provides a mock function with given fields: key, value, ttl
func (_m *Cache) Set(key, value string, duration cache.Duration) {
_m.Called(key, value, duration)
}

func (_m *Cache) Delete(key string) {
Expand Down
Loading