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

feat(#58): add copy to clipboard flag --copy #97

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

AlejandroSuero
Copy link

@AlejandroSuero AlejandroSuero commented May 19, 2024

Changes made

With this addition the user can choose in interactive mode, or with the flag --copy, to copy the created image to their system's clipboard. This was achieved with the cross-platform clipboard package by golang-design.

Tests

  • Tested via make test:
    • In MacOS.
    • In Windows.
  • Manually tested via ./freeze --config user --lines 23,30 config.go --copy to get this image:
    image

Closes #58.

@Moulick
Copy link

Moulick commented May 25, 2024

@AlejandroSuero Not working, I cloned your fork, checked out your branch and compiled. --copy is not copying to the clipboard. Also make test fails. I am on MacOS (14.5 (23F79))

❯ make test
go test ./...
?   	github.com/charmbracelet/freeze/font	[no test files]
?   	github.com/charmbracelet/freeze/input	[no test files]
?   	github.com/charmbracelet/freeze/svg	[no test files]
?   	github.com/charmbracelet/freeze/test/input	[no test files]
--- FAIL: TestFreezeCopy (0.04s)
    freeze_test.go:81: clipboard is empty
FAIL
FAIL	github.com/charmbracelet/freeze	2.748s
FAIL
make: *** [test] Error 1

@AlejandroSuero
Copy link
Author

AlejandroSuero commented May 27, 2024

@Moulick I am using the same version of MacOS and it builds correctly with go build.

add-copy-to-clipboard-branch-demo.mp4
  • Result from using ./freeze --config user --lines 23,30 config.go --copy after being built:
    image

As for the test part, I committed a fix (eba42f9), apparently it was reading from the clipboard but not writing it, for example: If I have an image on the clipboard it will pass the test but it will fail otherwise.

  • Image from make test:
    image

@Moulick
Copy link

Moulick commented May 29, 2024

@AlejandroSuero now the test works, and the image generated by the make test appears in my clipboard. But running ./freeze --lines 23,30 config.go --copy still writes the image to the disk rather than clipboard. Attaching the compiled binary from my laptop

freeze.zip

@AlejandroSuero
Copy link
Author

After unzip freeze.zip then ./freeze --execute "exa -l" --copy I get this (recently powered up MacOS machine with nothing in the clipboard):

image

But running ./freeze --lines 23,30 config.go --copy still writes the image to the disk rather than clipboard

With these, are you referring to the fact that it writes the output image as well as copying it to the clipboard?

When running ./freeze --lines 23,30 config.go --copy with the downloaded freeze.zip I get the image freeze.png as well as it being copied to the clipboard:

image

@Moulick
Copy link

Moulick commented May 30, 2024

@AlejandroSuero not sure if there is something wrong with my machine but for me --copy is writing the freeze.png to disk and nothing to clipboard. Maybe we need someone else to try test

@AlejandroSuero
Copy link
Author

freezezip-just-powered-up-machine.mp4

@Moulick this is how it works for me just powering up the machine and having the clipboard empty.

png.go Outdated
Comment on lines 98 to 104
if copy {
err = clipboard.Init()
if err != nil {
return err
}
clipboard.Write(clipboard.FmtImage, png)
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am going to add clipboard.Read(clipboard.FmtImage) to see if it works that way for @Moulick.

@AlejandroSuero
Copy link
Author

With the changes in c57172d, It still passes the tests and works the same way manually for me.

@maaslalani
Copy link
Member

Hey @AlejandroSuero, instead of introducing a new flag I wonder if we should simply allow --output clipboard or --output copy to copy to the clipboard, what do you think?

@meowgorithm
Copy link
Member

No opinions, but you could also introduce the option to print to stdout so you could freeze --output stdout | pbcopy.

@maaslalani
Copy link
Member

maaslalani commented May 31, 2024

No opinions, but you could also introduce the option to print to stdout so you could freeze --output stdout | pbcopy.

In that case I would rather simply detect if stdout is a tty and send to stdout if it's not a tty.

freeze | pbcopy

@AlejandroSuero
Copy link
Author

@maaslalani the problem I contemplated when I thought about it in the first place was that if for example you want to write an output name like artichoke_example.png and still you want to copy the image to the clipboard it won't be possible.

But I would like to hear your thoughts about that scenario.

@AlejandroSuero
Copy link
Author

@meowgorithm as for the stdout @Moulick mentioned this in the issue linked to this PR:

@Moulick
Copy link

Moulick commented May 31, 2024

  1. It looks like something is broken on my system, as I ran a new MacOS VM and it works fine there. I have no clue what's broken on my system but probably a myriad of third-party applications I am running that might be interfering with the clipboard. ¯\_(ツ)_/¯
  2. --output clipboard does sound like a better idea. I don't think anyone want to copy to clipboard and write to disk at the same time. Even on Android/iOS, the option when taking a screenshot is copy to clipboard and delete.
  3. pbcopy does not handle putting images into the clipboard correctly. So freeze | pbcopy cannot work.
    1. freeze --output stdout or detecting stdout tty should also be done, so we can use other programs (like image compression) in the workflow but probably in a separate PR.

@AlejandroSuero
Copy link
Author

AlejandroSuero commented Jun 2, 2024

@maaslalani @Moulick lets see if you can help me figure out why freeze --output clipboard isn't working.

I made the following changes:

  • On png.go, refactored if copyClipboard for:
  switch output {
  case "clipboard":
    err = clipboard.Init()
    if err != nil {
      return err
    }
    clipboard.Write(clipboard.FmtImage, png)
    clipboard.Read(clipboard.FmtImage)
  break
  default:
    err = os.WriteFile(output, png, 0644)
    if err != nil {
      return err
    }
  }

But it does not copy to clipboard it just writes file clipboard.

Screenshot 2024-06-02 at 16 34 56

Note

I removed all instances of config.Copy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

put result in clipboard
4 participants