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

110 lines
3.1 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/engine"
"github.com/jandedobbeleer/oh-my-posh/src/platform"
2022-03-12 13:04:08 -08:00
"github.com/spf13/cobra"
)
var (
pwd string
pswd string
exitCode int
timing float64
stackCount int
terminalWidth int
eval bool
2023-02-05 00:06:26 -08:00
cleared bool
2022-03-12 13:04:08 -08:00
command string
shellVersion string
plain bool
noExitCode bool
2022-03-12 13:04:08 -08:00
)
// printCmd represents the prompt command
var printCmd = &cobra.Command{
2022-03-15 12:16:04 -07:00
Use: "print [debug|primary|secondary|transient|right|tooltip|valid|error]",
2022-03-12 13:04:08 -08:00
Short: "Print the prompt/context",
Long: "Print one of the prompts based on the location/use-case.",
ValidArgs: []string{
2022-03-15 12:16:04 -07:00
"debug",
2022-03-12 13:04:08 -08:00
"primary",
"secondary",
"transient",
"right",
"tooltip",
"valid",
"error",
},
Args: NoArgsOrOneValidArg,
2022-03-12 13:04:08 -08:00
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
flags := &platform.Flags{
Config: config,
PWD: pwd,
PSWD: pswd,
ErrorCode: exitCode,
ExecutionTime: timing,
StackCount: stackCount,
TerminalWidth: terminalWidth,
Eval: eval,
Shell: shellName,
ShellVersion: shellVersion,
Plain: plain,
Primary: args[0] == "primary",
2023-02-05 00:06:26 -08:00
Cleared: cleared,
NoExitCode: noExitCode,
2023-05-18 10:42:57 -07:00
Version: cliVersion,
2022-03-12 13:04:08 -08:00
}
eng := engine.New(flags)
defer eng.Env.Close()
2022-03-12 13:04:08 -08:00
switch args[0] {
2022-03-15 12:16:04 -07:00
case "debug":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.ExtraPrompt(engine.Debug))
2022-03-12 13:04:08 -08:00
case "primary":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.Primary())
2022-03-12 13:04:08 -08:00
case "secondary":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.ExtraPrompt(engine.Secondary))
2022-03-12 13:04:08 -08:00
case "transient":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.ExtraPrompt(engine.Transient))
2022-03-12 13:04:08 -08:00
case "right":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.RPrompt())
2022-03-12 13:04:08 -08:00
case "tooltip":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.Tooltip(command))
2022-03-12 13:04:08 -08:00
case "valid":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.ExtraPrompt(engine.Valid))
2022-03-12 13:04:08 -08:00
case "error":
2023-04-22 12:15:04 -07:00
fmt.Print(eng.ExtraPrompt(engine.Error))
2022-03-19 11:48:37 -07:00
default:
_ = cmd.Help()
2022-03-12 13:04:08 -08:00
}
},
}
func init() { //nolint:gochecknoinits
2022-03-12 13:04:08 -08:00
printCmd.Flags().StringVar(&pwd, "pwd", "", "current working directory")
printCmd.Flags().StringVar(&pswd, "pswd", "", "current working directory (according to pwsh)")
2022-03-21 23:41:36 -07:00
printCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
printCmd.Flags().StringVar(&shellVersion, "shell-version", "", "the shell version")
printCmd.Flags().IntVarP(&exitCode, "error", "e", 0, "last exit code")
printCmd.Flags().Float64Var(&timing, "execution-time", 0, "timing of the last command")
2022-03-12 13:04:08 -08:00
printCmd.Flags().IntVarP(&stackCount, "stack-count", "s", 0, "number of locations on the stack")
printCmd.Flags().IntVarP(&terminalWidth, "terminal-width", "w", 0, "width of the terminal")
printCmd.Flags().StringVar(&command, "command", "", "tooltip command")
printCmd.Flags().BoolVarP(&plain, "plain", "p", false, "plain text output (no ANSI)")
2023-02-05 00:06:26 -08:00
printCmd.Flags().BoolVar(&cleared, "cleared", false, "do we have a clear terminal or not")
2022-03-12 13:04:08 -08:00
printCmd.Flags().BoolVar(&eval, "eval", false, "output the prompt for eval")
printCmd.Flags().BoolVar(&noExitCode, "no-exit-code", false, "no valid exit code (cancelled or no command yet)")
2022-11-02 03:48:19 -07:00
RootCmd.AddCommand(printCmd)
2022-03-12 13:04:08 -08:00
}