Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental clipboard feature #89

Merged
merged 4 commits into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ name: Release
on:
push:
tags:
- "v*"
- "v*"

jobs:
goreleaser:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19.x
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.19.x

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
8 changes: 3 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ name: Tests
on:
push:
tags-ignore:
- '**'
- '**'
branches:
- main
- main
pull_request:
branches:
- main
- main

jobs:

build:
name: Tests
runs-on: ubuntu-latest
Expand All @@ -30,5 +29,4 @@ jobs:

- name: Test
run: |
go install -mod=mod github.com/onsi/ginkgo/v2/ginkgo
make test
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ all: test

.PHONY: clean
clean:
@go clean -i -cache $(shell go list ./...)
rm -rf dist *.coverprofile
@go clean -i $(shell go list ./...)
@rm -rf dist *.coverprofile

.PHONY: test
test: $(sources)
@ginkgo run \
go run -mod=mod github.com/onsi/ginkgo/v2/ginkgo run \
--coverprofile=unit.coverprofile \
--randomize-all \
--randomize-suites \
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ require (
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/tools v0.10.0 // indirect
golang.org/x/tools v0.11.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 5 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ golang.org/x/image v0.9.0 h1:QrzfX26snvCM20hIhBwuHI/ThTg18b/+kcKdXHvnR+g=
golang.org/x/image v0.9.0/go.mod h1:jtrku+n79PfroUbvDdeUWMAI+heR786BofxrbiSF+J0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.11.0 h1:Gi2tvZIJyBtO9SDr1q9h5hEQCp/4L2RQ+ar0qjx2oNU=
golang.org/x/net v0.11.0/go.mod h1:2L/ixqYpgIVXmeoSA/4Lu7BzTG4KIyPIryS4IsOd1oQ=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -96,8 +96,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.10.0 h1:tvDr/iQoUqNdohiYm0LmmKcBk+q86lb9EprIUFhHHGg=
golang.org/x/tools v0.10.0/go.mod h1:UJwyiVBsOA2uwvK/e5OY3GTpDUJriEd+/YlqAwLPmyM=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
44 changes: 34 additions & 10 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ import (
"github.com/gonvenience/bunt"
"github.com/gonvenience/neat"
"github.com/gonvenience/wrap"

"github.com/homeport/termshot/internal/img"
"github.com/homeport/termshot/internal/ptexec"

"github.com/spf13/cobra"
)

// version string will be injected by automation
var version string

// saveToClipboard function will be implemented by OS specific code
var saveToClipboard func(img.Scaffold) error

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 @@ -73,9 +78,9 @@ window including all terminal colors and text decorations.
bunt.Fprintf(&buf, "Lime{➜} DimGray{%s}\n", strings.Join(args, " "))
}

bytes, runErr := ptexec.RunCommandInPseudoTerminal(args[0], args[1:]...)
if runErr != nil {
return runErr
bytes, err := ptexec.RunCommandInPseudoTerminal(args[0], args[1:]...)
if err != nil {
return err
}

buf.Write(bytes)
Expand Down Expand Up @@ -116,18 +121,31 @@ window including all terminal colors and text decorations.
return err
}

filename, runErr := cmd.Flags().GetString("filename")
if filename == "" || runErr != nil {
// save image to clipboard
//
if toClipboard, err := cmd.Flags().GetBool("clipboard"); err == nil && toClipboard {
return saveToClipboard(scaffold)
}

// save image to file
//
filename, err := cmd.Flags().GetString("filename")
if filename == "" || err != nil {
fmt.Fprintf(os.Stderr, "failed to read filename from command-line, defaulting to out.png")
filename = "out.png"
}

extension := filepath.Ext(filename)
if extension != ".png" {
return fmt.Errorf("file extension '%s' of filename '%s' is not supported, only png is supported", extension, filename)
if extension := filepath.Ext(filename); extension != ".png" {
return fmt.Errorf("file extension %q of filename %q is not supported, only png is supported", extension, filename)
}

return scaffold.SavePNG(filename)
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file: %w", err)
}

defer file.Close()
return scaffold.Write(file)
},
}

Expand Down Expand Up @@ -178,8 +196,14 @@ func executableName() string {

func init() {
rootCmd.Flags().SortFlags = false

// flags to control look
rootCmd.Flags().BoolP("edit", "e", false, "edit content before the creating screenshot")
rootCmd.Flags().BoolP("show-cmd", "c", false, "include command in screenshot")
rootCmd.Flags().BoolP("version", "v", false, "show version")

// flags for output related settings
rootCmd.Flags().StringP("filename", "f", "out.png", "filename of the screenshot")

// internals
rootCmd.Flags().BoolP("version", "v", false, "show version")
}
75 changes: 75 additions & 0 deletions internal/cmd/root_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Copyright © 2023 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package cmd

import (
"bytes"
"encoding/hex"
"fmt"
"os"
"os/exec"

"github.com/homeport/termshot/internal/img"
)

const osascript = "/usr/bin/osascript"

// hasOsascript checks if /usr/bin/osascript exists and is executable
func hasOsascript() bool {
if fi, err := os.Stat(osascript); err == nil {
return fi.Mode()&0111 != 0
}

return false
}

func init() {
if hasOsascript() {
// register tool flag to enable clipboard option
rootCmd.Flags().BoolP("clipboard", "b", false, "copy termshot to clipboard, overrules filename option")

// register function to copy image into the clipboard
saveToClipboard = func(scaffold img.Scaffold) error {
var buf bytes.Buffer

if _, err := buf.WriteString("set the clipboard to «data PICT"); err != nil {
return err
}

if err := scaffold.Write(hex.NewEncoder(&buf)); err != nil {
return err
}

if _, err := buf.WriteString("»"); err != nil {
return err
}

cmd := exec.Command(osascript, "-e", buf.String())
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Fprint(os.Stderr, string(out))
return err
}

return nil
}
}
}
17 changes: 14 additions & 3 deletions internal/img/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
package img

import (
"image"
"image/color"
"image/png"
"io"
"math"
"strings"
Expand Down Expand Up @@ -156,7 +158,7 @@ func (s *Scaffold) measureContent() (width float64, height float64) {
return width, height
}

func (s *Scaffold) SavePNG(path string) error {
func (s *Scaffold) image() (image.Image, error) {
var f = func(value float64) float64 { return s.factor * value }

var (
Expand Down Expand Up @@ -196,7 +198,7 @@ func (s *Scaffold) SavePNG(path string) error {

shadow, err := stackblur.Process(bc.Image(), uint32(s.shadowRadius))
if err != nil {
return err
return nil, err
}

dc.DrawImage(shadow, 0, 0)
Expand Down Expand Up @@ -294,5 +296,14 @@ func (s *Scaffold) SavePNG(path string) error {
x += w
}

return dc.SavePNG(path)
return dc.Image(), nil
}

func (s *Scaffold) Write(w io.Writer) error {
image, err := s.image()
if err != nil {
return err
}

return png.Encode(w, image)
}
Loading
Loading