diff --git a/src/cli/init.go b/src/cli/init.go index 75689ff0..813ed871 100644 --- a/src/cli/init.go +++ b/src/cli/init.go @@ -73,6 +73,7 @@ func runInit(shellName string) { shell.Tooltips = len(cfg.Tooltips) > 0 shell.ShellIntegration = cfg.ShellIntegration shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark) + shell.AutoUpgrade = cfg.AutoUpgrade && upgrade.Supported for i, block := range cfg.Blocks { // only fetch cursor position when relevant @@ -85,7 +86,7 @@ func runInit(shellName string) { } // allow overriding the upgrade notice from the config - if cfg.DisableNotice { + if cfg.DisableNotice || cfg.AutoUpgrade { env.Cache().Set(upgrade.CACHEKEY, "disabled", -1) } diff --git a/src/cli/upgrade.go b/src/cli/upgrade.go index 65ce2e3a..b4820653 100644 --- a/src/cli/upgrade.go +++ b/src/cli/upgrade.go @@ -19,12 +19,7 @@ var upgradeCmd = &cobra.Command{ Args: cobra.NoArgs, Run: func(_ *cobra.Command, _ []string) { if runtime.GOOS == platform.LINUX { - fmt.Print("\n ⚠️ upgrade is not supported on this platform\n\n") - return - } - - if force { - upgrade.Run() + fmt.Print("\n⚠️ upgrade is not supported on this platform\n\n") return } @@ -34,12 +29,17 @@ var upgradeCmd = &cobra.Command{ env.Init() defer env.Close() - if _, hasNotice := upgrade.Notice(env, true); !hasNotice { - fmt.Print("\n ✅ no new version available\n\n") + if force { + upgrade.Run(env) return } - upgrade.Run() + if _, hasNotice := upgrade.Notice(env, true); !hasNotice { + fmt.Print("\n✅ no new version available\n\n") + return + } + + upgrade.Run(env) }, } diff --git a/src/engine/config.go b/src/engine/config.go index 78745131..f8d6ba28 100644 --- a/src/engine/config.go +++ b/src/engine/config.go @@ -52,6 +52,7 @@ type Config struct { DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty" toml:"disable_cursor_positioning,omitempty"` PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty" toml:"patch_pwsh_bleed,omitempty"` DisableNotice bool `json:"disable_notice,omitempty" toml:"disable_notice,omitempty"` + AutoUpgrade bool `json:"auto_upgrade,omitempty" toml:"auto_upgrade,omitempty"` ITermFeatures ansi.ITermFeatures `json:"iterm_features,omitempty" toml:"iterm_features,omitempty"` // Deprecated diff --git a/src/shell/init.go b/src/shell/init.go index e480aad5..5a20b145 100644 --- a/src/shell/init.go +++ b/src/shell/init.go @@ -53,6 +53,7 @@ var ( RPrompt bool CursorPositioning bool PromptMark bool + AutoUpgrade bool ) func getExecutablePath(env platform.Environment) (string, error) { @@ -283,6 +284,7 @@ func PrintInit(env platform.Environment) string { "::CURSOR::", strconv.FormatBool(CursorPositioning), "::UPGRADE::", strconv.FormatBool(hasNotice), "::UPGRADENOTICE::", notice, + "::AUTOUPGRADE::", strconv.FormatBool(AutoUpgrade), "::PROMPT_MARK::", promptMark(), ).Replace(script) } diff --git a/src/shell/scripts/omp.bash b/src/shell/scripts/omp.bash index 61321d1e..827ec70f 100644 --- a/src/shell/scripts/omp.bash +++ b/src/shell/scripts/omp.bash @@ -76,3 +76,7 @@ fi if [ "::UPGRADE::" == "true" ]; then echo "::UPGRADENOTICE::" fi + +if [[ "::AUTOUPGRADE::" == "true" ]]; then + ::OMP:: upgrade +fi diff --git a/src/shell/scripts/omp.elv b/src/shell/scripts/omp.elv index 82899a4e..daed6292 100644 --- a/src/shell/scripts/omp.elv +++ b/src/shell/scripts/omp.elv @@ -34,3 +34,7 @@ set edit:rprompt = { if (eq '::UPGRADE::' 'true') { echo '::UPGRADENOTICE::' } + +if (eq '::AUTOUPGRADE::' 'true') { + ::OMP:: upgrade +} diff --git a/src/shell/scripts/omp.fish b/src/shell/scripts/omp.fish index 6b795fb3..41903f34 100644 --- a/src/shell/scripts/omp.fish +++ b/src/shell/scripts/omp.fish @@ -160,3 +160,7 @@ end if test "::UPGRADE::" = "true" echo "::UPGRADENOTICE::" end + +if test "::AUTOUPGRADE::" = "true" + ::OMP:: upgrade +end diff --git a/src/shell/scripts/omp.lua b/src/shell/scripts/omp.lua index 7dea9eb4..d6a111a3 100644 --- a/src/shell/scripts/omp.lua +++ b/src/shell/scripts/omp.lua @@ -335,3 +335,8 @@ end if tooltips_enabled and rl.setbinding then rl.setbinding(' ', [["luafunc:ohmyposh_space"]], 'emacs') end + +if '::AUTOUPGRADE::' == 'true' then + local prompt_exe = string.format('%s upgrade', omp_exe()) + os.execute(prompt_exe) +end diff --git a/src/shell/scripts/omp.nu b/src/shell/scripts/omp.nu index 0bfba39b..ea327ec7 100644 --- a/src/shell/scripts/omp.nu +++ b/src/shell/scripts/omp.nu @@ -44,3 +44,7 @@ if "::TRANSIENT::" == "true" { if "::UPGRADE::" == "true" { echo "::UPGRADENOTICE::" } + +if "::AUTOUPGRADE::" == "true" { + ^::OMP:: upgrade +} diff --git a/src/shell/scripts/omp.ps1 b/src/shell/scripts/omp.ps1 index e76e4570..b399c526 100644 --- a/src/shell/scripts/omp.ps1 +++ b/src/shell/scripts/omp.ps1 @@ -486,6 +486,12 @@ Example: "Enable-PoshLineError" "Export-PoshTheme" "Get-PoshThemes" + "Start-Utf8Process" "prompt" ) } | Import-Module -Global + + +if ("::AUTOUPGRADE::" -eq "true") { + & ::OMP:: upgrade +} diff --git a/src/shell/scripts/omp.py b/src/shell/scripts/omp.py index c44178de..4c33f3e5 100644 --- a/src/shell/scripts/omp.py +++ b/src/shell/scripts/omp.py @@ -27,3 +27,6 @@ notice = """::UPGRADENOTICE::""" if "::UPGRADE::" == "true": print(notice) + +if "::AUTOUPGRADE::" == "true": + ::OMP:: upgrade diff --git a/src/shell/scripts/omp.tcsh b/src/shell/scripts/omp.tcsh index d860db02..881c22a6 100644 --- a/src/shell/scripts/omp.tcsh +++ b/src/shell/scripts/omp.tcsh @@ -10,4 +10,6 @@ alias precmd "$POSH_PRECMD;$USER_PRECMD"; alias postcmd "$POSH_POSTCMD;$USER_POSTCMD"; set POSH_START_TIME = `::OMP:: get millis`; -if ("::UPGRADE::" == "true") echo "::UPGRADENOTICE::" +if ("::UPGRADE::" == "true") echo "::UPGRADENOTICE::"; + +if ("::AUTOUPGRADE::" == "true") ::OMP:: upgrade; diff --git a/src/shell/scripts/omp.zsh b/src/shell/scripts/omp.zsh index 50c33f07..056b65d6 100644 --- a/src/shell/scripts/omp.zsh +++ b/src/shell/scripts/omp.zsh @@ -165,3 +165,7 @@ fi if [[ "::UPGRADE::" = "true" ]]; then echo "::UPGRADENOTICE::" fi + +if [[ "::AUTOUPGRADE::" = "true" ]]; then + ::OMP:: upgrade +fi diff --git a/src/upgrade/cli.go b/src/upgrade/cli.go index 56055489..79539e8c 100644 --- a/src/upgrade/cli.go +++ b/src/upgrade/cli.go @@ -12,12 +12,16 @@ import ( "github.com/charmbracelet/bubbles/spinner" tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/lipgloss" + "github.com/jandedobbeleer/oh-my-posh/src/build" "github.com/jandedobbeleer/oh-my-posh/src/platform" ) var ( program *tea.Program - textStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4) + textStyle = lipgloss.NewStyle().Margin(1, 0, 2, 0) + title string + + Supported = true ) type resultMsg string @@ -30,6 +34,10 @@ const ( installing ) +func setState(message state) { + program.Send(stateMsg(message)) +} + type stateMsg state type model struct { @@ -88,7 +96,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { func (m *model) View() string { if len(m.message) > 0 { - return textStyle.Render(m.message) + return title + textStyle.Render(m.message) } var message string @@ -103,10 +111,20 @@ func (m *model) View() string { message = "Installing" } - return textStyle.Render(fmt.Sprintf("%s %s", m.spinner.View(), message)) + return title + textStyle.Render(fmt.Sprintf("%s %s", m.spinner.View(), message)) } -func Run() { +func Run(env platform.Environment) { + titleStyle := lipgloss.NewStyle().Margin(1, 0, 1, 0) + title = "📦 Upgrading Oh My Posh" + + version, err := Latest(env) + if err == nil { + title = fmt.Sprintf("%s from %s to %s", title, build.Version, version) + } + + title = titleStyle.Render(title) + program = tea.NewProgram(initialModel()) if _, err := program.Run(); err != nil { fmt.Println(err) @@ -128,7 +146,7 @@ func downloadAsset(asset string) (io.ReadCloser, error) { } if resp.StatusCode != http.StatusOK { - return nil, fmt.Errorf("Failed to download installer: %s", url) + return nil, fmt.Errorf("failed to download installer: %s", url) } return resp.Body, nil diff --git a/src/upgrade/cli_darwin.go b/src/upgrade/cli_darwin.go index 55ffd600..7473316a 100644 --- a/src/upgrade/cli_darwin.go +++ b/src/upgrade/cli_darwin.go @@ -8,10 +8,10 @@ import ( "runtime" ) -var successMsg = "Upgrade successful, restart your shell to take full advantage of the new functionality." +var successMsg = "🚀 Upgrade successful, restart your shell to take full advantage of the new functionality." func install() error { - program.Send(stateMsg(validating)) + setState(validating) executable, err := os.Executable() if err != nil { return err @@ -24,7 +24,7 @@ func install() error { defer file.Close() - program.Send(stateMsg(downloading)) + setState(downloading) asset := fmt.Sprintf("posh-%s-%s", runtime.GOOS, runtime.GOARCH) @@ -35,7 +35,7 @@ func install() error { defer data.Close() - program.Send(stateMsg(installing)) + setState(installing) _, err = io.Copy(file, data) if err != nil { diff --git a/src/upgrade/cli_other.go b/src/upgrade/cli_other.go index 973c3902..ba52736e 100644 --- a/src/upgrade/cli_other.go +++ b/src/upgrade/cli_other.go @@ -2,4 +2,10 @@ package upgrade -func Run() {} +import "github.com/jandedobbeleer/oh-my-posh/src/platform" + +func Run(_ platform.Environment) {} + +var ( + Supported = false +) diff --git a/src/upgrade/cli_windows.go b/src/upgrade/cli_windows.go index c59b579d..2a7653a9 100644 --- a/src/upgrade/cli_windows.go +++ b/src/upgrade/cli_windows.go @@ -13,7 +13,7 @@ import ( var successMsg = "Oh My Posh is installing in the background.\nRestart your shell in a minute to take full advantage of the new functionality." func install() error { - program.Send(stateMsg(downloading)) + setState(downloading) temp := os.Getenv("TEMP") if len(temp) == 0 { diff --git a/src/upgrade/notice.go b/src/upgrade/notice.go index 15a6b523..c9f06cb1 100644 --- a/src/upgrade/notice.go +++ b/src/upgrade/notice.go @@ -34,6 +34,8 @@ const ( upgradeNotice = ` A new release of Oh My Posh is available: %s → %s To upgrade, run: 'oh-my-posh upgrade' + +To enable automated upgrades, set 'auto_upgrade' to 'true' in your configuration. ` CACHEKEY = "upgrade_check" )