-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cache): remove segment specific caching
- Loading branch information
1 parent
a45fb39
commit 4fe6efc
Showing
44 changed files
with
289 additions
and
982 deletions.
There are no files selected for viewing
This file contains 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 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,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") | ||
} |
This file contains 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,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) | ||
} | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.