2022-03-12 22:43:32 -08:00
|
|
|
package cli
|
2022-03-12 13:04:08 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-11-17 11:52:41 -08:00
|
|
|
"strings"
|
2022-03-12 13:04:08 -08:00
|
|
|
"time"
|
|
|
|
|
2023-01-05 12:57:38 -08:00
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/ansi"
|
|
|
|
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
2022-12-28 08:30:48 -08:00
|
|
|
|
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{
|
2022-12-01 08:20:24 -08:00
|
|
|
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-05-05 02:26:20 -07:00
|
|
|
|
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
|
2022-11-17 11:52:41 -08:00
|
|
|
- accent
|
2022-12-01 08:20:24 -08:00
|
|
|
- toggles
|
|
|
|
- width`,
|
2022-03-12 13:04:08 -08:00
|
|
|
ValidArgs: []string{
|
|
|
|
"millis",
|
|
|
|
"shell",
|
2022-05-22 10:26:46 -07:00
|
|
|
"accent",
|
2022-11-17 11:52:41 -08:00
|
|
|
"toggles",
|
2022-12-01 08:20:24 -08:00
|
|
|
"width",
|
2022-03-12 13:04:08 -08:00
|
|
|
},
|
2022-05-05 02:26:20 -07:00
|
|
|
Args: NoArgsOrOneValidArg,
|
2022-03-12 13:04:08 -08:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-03-20 22:41:04 -07:00
|
|
|
if len(args) == 0 {
|
|
|
|
_ = cmd.Help()
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 11:27:54 -08:00
|
|
|
env := &platform.Shell{
|
2022-03-16 00:47:39 -07:00
|
|
|
Version: cliVersion,
|
2022-11-09 11:27:54 -08:00
|
|
|
CmdFlags: &platform.Flags{
|
2022-09-22 23:39:27 -07:00
|
|
|
Shell: shellName,
|
|
|
|
},
|
2022-03-16 00:47:39 -07:00
|
|
|
}
|
2022-05-07 01:12:22 -07:00
|
|
|
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())
|
2022-11-17 11:52:41 -08:00
|
|
|
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)
|
|
|
|
}
|
2022-12-01 08:20:24 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-08-04 23:33:02 -07:00
|
|
|
func init() { //nolint:gochecknoinits
|
2022-11-02 03:48:19 -07:00
|
|
|
RootCmd.AddCommand(getCmd)
|
2022-09-23 01:16:19 -07:00
|
|
|
getCmd.Flags().StringVar(&shellName, "shell", "", "the shell to print for")
|
2022-03-12 13:04:08 -08:00
|
|
|
}
|