diff --git a/internal/cmd/root.go b/internal/cmd/root.go index b9f4fe3..e280f20 100644 --- a/internal/cmd/root.go +++ b/internal/cmd/root.go @@ -42,15 +42,6 @@ var version string // saveToClipboard function will be implemented by OS specific code var saveToClipboard func(img.Scaffold) error -// commandIndicator is the string to be used to indicate the command in the screenshot -var commandIndicator = func() string { - if val, ok := os.LookupEnv("TS_COMMAND_INDICATOR"); ok { - return val - } - - return "➜" -}() - var rootCmd = &cobra.Command{ Use: fmt.Sprintf("%s [%s flags] [--] command [command flags] [command arguments] [...]", executableName(), executableName()), Short: "Creates a screenshot of terminal command output", @@ -111,11 +102,9 @@ window including all terminal colors and text decorations. // Optional: Prepend command line arguments to output content // if includeCommand, err := cmd.Flags().GetBool("show-cmd"); err == nil && includeCommand { - // #nosec G104 - bunt.Fprintf(&buf, "Lime{%s} DimGray{%s}\n", - commandIndicator, - strings.Join(args, " "), - ) + if err := scaffold.AddCommand(args...); err != nil { + return err + } } // Run the provided command in a pseudo terminal and capture diff --git a/internal/img/output.go b/internal/img/output.go index 52c4fb6..18f025f 100644 --- a/internal/img/output.go +++ b/internal/img/output.go @@ -27,6 +27,7 @@ import ( "image/png" "io" "math" + "os" "strings" "github.com/esimov/stackblur-go" @@ -49,6 +50,15 @@ const ( defaultFontDPI = 144 ) +// commandIndicator is the string to be used to indicate the command in the screenshot +var commandIndicator = func() string { + if val, ok := os.LookupEnv("TS_COMMAND_INDICATOR"); ok { + return val + } + + return "➜" +}() + type Scaffold struct { content bunt.String @@ -138,6 +148,15 @@ func (s *Scaffold) GetFixedColumns() int { return columns } +func (s *Scaffold) AddCommand(args ...string) error { + return s.AddContent(strings.NewReader( + bunt.Sprintf("Lime{%s} DimGray{%s}\n", + commandIndicator, + strings.Join(args, " "), + ), + )) +} + func (s *Scaffold) AddContent(in io.Reader) error { parsed, err := bunt.ParseStream(in) if err != nil { diff --git a/internal/img/output_test.go b/internal/img/output_test.go index f48c8d0..b67cb79 100644 --- a/internal/img/output_test.go +++ b/internal/img/output_test.go @@ -75,6 +75,13 @@ var _ = Describe("Creating images", func() { Expect(scaffold).To(LookLike(testdata("expected-wrapping.png"))) }) + It("should show the command when configured", func() { + scaffold := NewImageCreator() + Expect(scaffold.AddCommand("echo", "foobar")).To(Succeed()) + Expect(scaffold.AddContent(strings.NewReader("foobar"))).To(Succeed()) + Expect(scaffold).To(LookLike(testdata("expected-show-cmd.png"))) + }) + It("should write a PNG stream based on provided input with ANSI sequences", func() { var buf bytes.Buffer _, _ = Fprintf(&buf, "Text with emphasis, like *bold*, _italic_, _*bold/italic*_ or ~underline~.\n\n") diff --git a/test/data/expected-show-cmd.png b/test/data/expected-show-cmd.png new file mode 100644 index 0000000..ab05300 Binary files /dev/null and b/test/data/expected-show-cmd.png differ