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

94 lines
1.9 KiB
Go
Raw Normal View History

package cli
2022-03-12 13:04:08 -08:00
import (
"fmt"
"strings"
2022-03-12 13:04:08 -08:00
"time"
2023-01-04 11:44:29 -08:00
"github.com/jandedobbeleer/oh-my-posh/ansi"
2022-12-28 08:30:48 -08:00
"github.com/jandedobbeleer/oh-my-posh/platform"
2022-05-22 10:26:46 -07:00
color2 "github.com/gookit/color"
2022-03-12 13:04:08 -08:00
"github.com/spf13/cobra"
)
// getCmd represents the get command
var getCmd = &cobra.Command{
Use: "get [shell|millis|accent|toggles|width]",
2022-03-19 11:32:40 -07:00
Short: "Get a value from oh-my-posh",
Long: `Get a value from oh-my-posh.
2022-03-12 13:04:08 -08:00
This command is used to get the value of the following variables:
- shell
2022-05-22 10:26:46 -07:00
- millis
- accent
- toggles
- width`,
2022-03-12 13:04:08 -08:00
ValidArgs: []string{
"millis",
"shell",
2022-05-22 10:26:46 -07:00
"accent",
"toggles",
"width",
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-11-09 11:27:54 -08:00
env := &platform.Shell{
Version: cliVersion,
2022-11-09 11:27:54 -08:00
CmdFlags: &platform.Flags{
2022-09-22 23:39:27 -07:00
Shell: shellName,
},
}
env.Init()
2022-03-12 13:04:08 -08:00
defer env.Close()
switch args[0] {
case "millis":
fmt.Print(time.Now().UnixNano() / 1000000)
case "shell":
fmt.Println(env.Shell())
2022-05-22 10:26:46 -07:00
case "accent":
2023-01-04 11:44:29 -08:00
rgb, err := ansi.GetAccentColor(env)
2022-05-22 10:26:46 -07:00
if err != nil {
fmt.Println("error getting accent color:", err.Error())
return
}
accent := color2.RGB(rgb.R, rgb.G, rgb.B)
fmt.Println("#" + accent.Hex())
case "toggles":
cache := env.Cache()
togglesCache, _ := cache.Get(platform.TOGGLECACHE)
var toggles []string
if len(togglesCache) != 0 {
toggles = strings.Split(togglesCache, ",")
}
if len(toggles) == 0 {
fmt.Println("No segments are toggled off")
return
}
fmt.Println("Toggled off segments:")
for _, toggle := range toggles {
fmt.Println("- " + toggle)
}
case "width":
width, err := env.TerminalWidth()
if err != nil {
fmt.Println("error getting terminal width:", err.Error())
return
}
fmt.Println(width)
2022-03-19 11:48:37 -07:00
default:
_ = cmd.Help()
2022-03-12 13:04:08 -08:00
}
},
}
func init() { //nolint:gochecknoinits
2022-11-02 03:48:19 -07:00
RootCmd.AddCommand(getCmd)
getCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
2022-03-12 13:04:08 -08:00
}