oh-my-posh/src/cli/init.go

101 lines
2.3 KiB
Go
Raw Normal View History

package cli
2022-03-12 13:04:08 -08:00
import (
"fmt"
2022-12-28 08:30:48 -08:00
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
"github.com/jandedobbeleer/oh-my-posh/src/engine"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/shell"
"github.com/jandedobbeleer/oh-my-posh/src/upgrade"
2022-03-12 13:04:08 -08:00
"github.com/spf13/cobra"
)
var (
2023-03-20 04:38:31 -07:00
printOutput bool
strict bool
manual bool
2022-03-12 13:04:08 -08:00
initCmd = &cobra.Command{
Use: "init [bash|zsh|fish|powershell|pwsh|cmd|nu|tcsh|elvish|xonsh]",
Short: "Initialize your shell and config",
Long: `Initialize your shell and config.
See the documentation to initialize your shell: https://ohmyposh.dev/docs/installation/prompt.`,
2022-03-12 13:04:08 -08:00
ValidArgs: []string{
"bash",
"zsh",
"fish",
"powershell",
"pwsh",
"cmd",
2022-04-10 10:33:13 -07:00
"nu",
2023-02-17 01:11:44 -08:00
"tcsh",
2023-02-18 04:49:10 -08:00
"elvish",
2023-02-18 07:46:31 -08:00
"xonsh",
2022-03-12 13:04:08 -08:00
},
Args: NoArgsOrOneValidArg,
2022-03-12 13:04:08 -08:00
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
2022-03-12 13:04:08 -08:00
runInit(args[0])
},
}
)
2024-05-03 23:02:16 -07:00
func init() {
2023-03-20 04:38:31 -07:00
initCmd.Flags().BoolVarP(&printOutput, "print", "p", false, "print the init script")
initCmd.Flags().BoolVarP(&strict, "strict", "s", false, "run in strict mode")
initCmd.Flags().BoolVarP(&manual, "manual", "m", false, "enable/disable manual mode")
2022-03-12 13:04:08 -08:00
_ = initCmd.MarkPersistentFlagRequired("config")
2022-11-02 03:48:19 -07:00
RootCmd.AddCommand(initCmd)
2022-03-12 13:04:08 -08:00
}
2022-03-21 23:41:36 -07:00
func runInit(shellName string) {
2022-11-09 11:27:54 -08:00
env := &platform.Shell{
CmdFlags: &platform.Flags{
2023-07-02 11:53:50 -07:00
Shell: shellName,
Config: config,
Strict: strict,
Manual: manual,
2022-03-12 13:04:08 -08:00
},
}
env.Init()
2022-03-12 13:04:08 -08:00
defer env.Close()
cfg := engine.LoadConfig(env)
shell.Transient = cfg.TransientPrompt != nil
shell.ErrorLine = cfg.ErrorLine != nil || cfg.ValidLine != nil
shell.Tooltips = len(cfg.Tooltips) > 0
shell.ShellIntegration = cfg.ShellIntegration
shell.PromptMark = shellName == shell.FISH && cfg.ITermFeatures != nil && cfg.ITermFeatures.Contains(ansi.PromptMark)
for i, block := range cfg.Blocks {
// only fetch cursor position when relevant
if !cfg.DisableCursorPositioning && (i == 0 && block.Newline) {
shell.CursorPositioning = true
}
if block.Type == engine.RPrompt {
shell.RPrompt = true
}
}
// allow overriding the upgrade notice from the config
if cfg.DisableNotice {
env.Cache().Set(upgrade.CACHEKEY, "disabled", -1)
}
2023-03-20 04:38:31 -07:00
if printOutput {
2022-03-21 23:41:36 -07:00
init := shell.PrintInit(env)
2022-03-12 13:04:08 -08:00
fmt.Print(init)
return
}
2022-03-21 23:41:36 -07:00
init := shell.Init(env)
2022-03-12 13:04:08 -08:00
fmt.Print(init)
}