Skip to content

Commit

Permalink
Merge pull request #258 from homeport/refactor/show-cmd
Browse files Browse the repository at this point in the history
Refactor show command code logic
  • Loading branch information
HeavyWombat authored Jan 1, 2025
2 parents e2c998e + 5a11277 commit 6637d2b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
17 changes: 3 additions & 14 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions internal/img/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"image/png"
"io"
"math"
"os"
"strings"

"github.com/esimov/stackblur-go"
Expand All @@ -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

Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions internal/img/output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Binary file added test/data/expected-show-cmd.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6637d2b

Please sign in to comment.