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

98 lines
2.7 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/color"
"github.com/jandedobbeleer/oh-my-posh/engine"
"github.com/jandedobbeleer/oh-my-posh/platform"
"github.com/jandedobbeleer/oh-my-posh/shell"
2022-03-12 13:04:08 -08:00
"github.com/spf13/cobra"
)
var (
author string
cursorPadding int
rPromptOffset int
bgColor string
2022-05-05 09:06:31 -07:00
outputImage string
2022-03-12 13:04:08 -08:00
)
// imageCmd represents the image command
var imageCmd = &cobra.Command{
Use: "image",
Short: "Export your config to an image",
Long: `Export your config to an image.
2022-03-12 13:04:08 -08:00
You can tweak the output by using additional flags:
- author: displays the author below the prompt
- cursor-padding: the padding of the prompt cursor
- rprompt-offset: the offset of the right prompt
- background-color: the background color of the image
Example usage:
> oh-my-posh config export image --config ~/myconfig.omp.json
Exports the config to an image file called myconfig.png in the current working directory.
2022-05-05 09:06:31 -07:00
> oh-my-posh config export image --config ~/myconfig.omp.json --output ~/mytheme.png
Exports the config to an image file ~/mytheme.png.
> oh-my-posh config export image --config ~/myconfig.omp.json --author "John Doe"
Exports the config to an image file using customized output options.`,
Args: cobra.NoArgs,
2022-03-12 13:04:08 -08:00
Run: func(cmd *cobra.Command, args []string) {
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-03-12 13:04:08 -08:00
Config: config,
Shell: shell.GENERIC,
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)
2022-10-13 11:01:51 -07:00
writerColors := cfg.MakeColors()
2022-03-12 13:04:08 -08:00
writer := &color.AnsiWriter{
2022-03-21 23:41:36 -07:00
TerminalBackground: shell.ConsoleBackgroundColor(env, cfg.TerminalBackground),
2022-03-12 13:04:08 -08:00
AnsiColors: writerColors,
}
eng := &engine.Engine{
Config: cfg,
Env: env,
Writer: writer,
2022-03-12 13:04:08 -08:00
}
prompt := eng.PrintPrimary()
imageCreator := &engine.ImageRenderer{
AnsiString: prompt,
Author: author,
CursorPadding: cursorPadding,
RPromptOffset: rPromptOffset,
BgColor: bgColor,
Ansi: writer,
2022-03-12 13:04:08 -08:00
}
2022-05-05 09:06:31 -07:00
if outputImage != "" {
imageCreator.Path = cleanOutputPath(outputImage, env)
}
2022-03-12 13:04:08 -08:00
imageCreator.Init(env.Flags().Config)
err := imageCreator.SavePNG()
if err != nil {
fmt.Print(err.Error())
}
},
}
func init() { //nolint:gochecknoinits
2022-03-12 13:04:08 -08:00
imageCmd.Flags().StringVar(&author, "author", "", "config author")
imageCmd.Flags().StringVar(&bgColor, "background-color", "", "image background color")
imageCmd.Flags().IntVar(&cursorPadding, "cursor-padding", 0, "prompt cursor padding")
imageCmd.Flags().IntVar(&rPromptOffset, "rprompt-offset", 0, "right prompt offset")
2022-05-05 09:06:31 -07:00
imageCmd.Flags().StringVarP(&outputImage, "output", "o", "", "image file (.png) to export to")
2022-03-12 13:04:08 -08:00
exportCmd.AddCommand(imageCmd)
}