mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 04:54:03 -08:00
feat(notice): disable/enable upgrade notice
This commit is contained in:
parent
9f70320525
commit
71f81a0b79
27
src/cli/disable.go
Normal file
27
src/cli/disable.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// getCmd represents the get command
|
||||
var disableCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf(toggleUse, "disable"),
|
||||
Short: "Disable a feature",
|
||||
Long: fmt.Sprintf(toggleLong, "Disable", "disable"),
|
||||
ValidArgs: toggleArgs,
|
||||
Args: NoArgsOrOneValidArg,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
_ = cmd.Help()
|
||||
return
|
||||
}
|
||||
toggleFeature(cmd, args[0], false)
|
||||
},
|
||||
}
|
||||
|
||||
func init() { //nolint:gochecknoinits
|
||||
RootCmd.AddCommand(disableCmd)
|
||||
}
|
63
src/cli/enable.go
Normal file
63
src/cli/enable.go
Normal file
|
@ -0,0 +1,63 @@
|
|||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
toggleUse = "%s [notice]"
|
||||
toggleLong = `%s a feature
|
||||
|
||||
This command is used to %s one of the following features:
|
||||
|
||||
- notice`
|
||||
toggleArgs = []string{
|
||||
"notice",
|
||||
}
|
||||
)
|
||||
|
||||
// getCmd represents the get command
|
||||
var enableCmd = &cobra.Command{
|
||||
Use: fmt.Sprintf(toggleUse, "enable"),
|
||||
Short: "Enable a feature",
|
||||
Long: fmt.Sprintf(toggleLong, "Enable", "enable"),
|
||||
ValidArgs: toggleArgs,
|
||||
Args: NoArgsOrOneValidArg,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if len(args) == 0 {
|
||||
_ = cmd.Help()
|
||||
return
|
||||
}
|
||||
toggleFeature(cmd, args[0], true)
|
||||
},
|
||||
}
|
||||
|
||||
func init() { //nolint:gochecknoinits
|
||||
RootCmd.AddCommand(enableCmd)
|
||||
}
|
||||
|
||||
func toggleFeature(cmd *cobra.Command, feature string, enable bool) {
|
||||
env := &platform.Shell{
|
||||
CmdFlags: &platform.Flags{
|
||||
Shell: shellName,
|
||||
Version: cliVersion,
|
||||
},
|
||||
}
|
||||
env.Init()
|
||||
defer env.Close()
|
||||
switch feature {
|
||||
case "notice":
|
||||
if enable {
|
||||
env.Cache().Delete(upgrade.CACHEKEY)
|
||||
return
|
||||
}
|
||||
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
|
||||
default:
|
||||
_ = cmd.Help()
|
||||
}
|
||||
}
|
|
@ -50,7 +50,7 @@ var (
|
|||
)
|
||||
|
||||
func init() { //nolint:gochecknoinits
|
||||
RootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config (required)")
|
||||
RootCmd.PersistentFlags().StringVarP(&config, "config", "c", "", "config file path")
|
||||
RootCmd.Flags().BoolVarP(&initialize, "init", "i", false, "init (deprecated)")
|
||||
RootCmd.Flags().BoolVar(&displayVersion, "version", false, "version")
|
||||
RootCmd.Flags().StringVarP(&shellName, "shell", "s", "", "shell (deprecated)")
|
||||
|
|
|
@ -42,3 +42,8 @@ func (_m *MockedCache) Init(home string) {
|
|||
func (_m *MockedCache) Set(key, value string, ttl int) {
|
||||
_m.Called(key, value, ttl)
|
||||
}
|
||||
|
||||
// delete provides a mock function with given fields: key
|
||||
func (_m *MockedCache) Delete(key string) {
|
||||
_m.Called(key)
|
||||
}
|
||||
|
|
|
@ -85,3 +85,9 @@ func (fc *fileCache) Set(key, value string, ttl int) {
|
|||
})
|
||||
fc.dirty = true
|
||||
}
|
||||
|
||||
// delete the key from the cache
|
||||
func (fc *fileCache) Delete(key string) {
|
||||
fc.cache.Delete(key)
|
||||
fc.dirty = true
|
||||
}
|
||||
|
|
|
@ -28,6 +28,12 @@ func (c *ConcurrentMap) Get(key string) (interface{}, bool) {
|
|||
return "", false
|
||||
}
|
||||
|
||||
func (c *ConcurrentMap) Delete(key string) {
|
||||
c.RLock()
|
||||
defer c.RUnlock()
|
||||
delete(c.values, key)
|
||||
}
|
||||
|
||||
func (c *ConcurrentMap) List() map[string]interface{} {
|
||||
return c.values
|
||||
}
|
||||
|
|
|
@ -98,6 +98,8 @@ type Cache interface {
|
|||
// Sets a value for a given key.
|
||||
// The ttl indicates how may minutes to cache the value.
|
||||
Set(key, value string, ttl int)
|
||||
// Deletes a key from the cache.
|
||||
Delete(key string)
|
||||
}
|
||||
|
||||
type HTTPRequestModifier func(request *http.Request)
|
||||
|
|
|
@ -39,7 +39,7 @@ https://ohmyposh.dev/docs/installation/windows#update`
|
|||
unix = "To upgrade, use your favorite package manager or, if you used Homebrew to install, run: 'brew upgrade oh-my-posh'"
|
||||
darwin = "To upgrade, run: 'brew upgrade oh-my-posh'"
|
||||
|
||||
UPGRADECACHEKEY = "upgrade_check"
|
||||
CACHEKEY = "upgrade_check"
|
||||
)
|
||||
|
||||
func getLatestVersion(env platform.Environment) (string, error) {
|
||||
|
@ -59,7 +59,7 @@ func getLatestVersion(env platform.Environment) (string, error) {
|
|||
// The upgrade check is only performed every other week.
|
||||
func Notice(env platform.Environment) (string, bool) {
|
||||
// do not check when last validation was < 1 week ago
|
||||
if _, OK := env.Cache().Get(UPGRADECACHEKEY); OK {
|
||||
if _, OK := env.Cache().Get(CACHEKEY); OK {
|
||||
return "", false
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ func Notice(env platform.Environment) (string, bool) {
|
|||
}
|
||||
|
||||
oneWeek := 10080
|
||||
env.Cache().Set(UPGRADECACHEKEY, latest, oneWeek)
|
||||
env.Cache().Set(CACHEKEY, latest, oneWeek)
|
||||
|
||||
version := fmt.Sprintf("v%s", env.Flags().Version)
|
||||
if latest == version {
|
||||
|
|
|
@ -35,7 +35,7 @@ func TestCanUpgrade(t *testing.T) {
|
|||
Version: tc.CurrentVersion,
|
||||
})
|
||||
cache := &mock.MockedCache{}
|
||||
cache.On("Get", UPGRADECACHEKEY).Return("", tc.Cache)
|
||||
cache.On("Get", CACHEKEY).Return("", tc.Cache)
|
||||
cache.On("Set", mock2.Anything, mock2.Anything, mock2.Anything)
|
||||
env.On("Cache").Return(cache)
|
||||
env.On("GOOS").Return(tc.GOOS)
|
||||
|
|
|
@ -9,6 +9,20 @@ import TabItem from "@theme/TabItem";
|
|||
|
||||
Before validating anything, make sure you're on the [latest version][latest] of Oh My Posh and your terminal and shell are up-to-date.
|
||||
|
||||
### I want to disable the upgrade notice
|
||||
|
||||
You can disable the upgrade notice using the following command once:
|
||||
|
||||
```bash
|
||||
oh-my-posh disable notice
|
||||
```
|
||||
|
||||
If you want to enable it again, run:
|
||||
|
||||
```bash
|
||||
oh-my-posh enable notice
|
||||
```
|
||||
|
||||
### The prompt is slow (delay in showing the prompt between commands)
|
||||
|
||||
:::tip Windows
|
||||
|
|
Loading…
Reference in a new issue