Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Oct 12, 2022
0 parents commit 35a1eb2
Show file tree
Hide file tree
Showing 8 changed files with 302 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: build

on:
push:
pull_request:
branches:
- master

jobs:
build:
strategy:
matrix:
go-version: [^1]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
env:
GO111MODULE: "on"
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- name: Download Go modules
run: go mod download
- name: Build
run: go build -v ./...
- name: Test
run: go test ./...
35 changes: 35 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: release

on:
push:
tags:
- v*.*.*

jobs:
release:
permissions:
contents: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v3
with:
go-version: ^1
- uses: actions/cache@v3
with:
path: ~/go/pkg/mod
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
- uses: docker/setup-qemu-action@v2
- uses: docker/setup-buildx-action@v2
- uses: goreleaser/goreleaser-action@v3
with:
version: latest
distribution: goreleaser
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HOMEBREW_TAP_GITHUB_TOKEN: ${{ secrets.HOMEBREW_TAP_GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dist
shcopy
85 changes: 85 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
project_name: shcopy
env:
- GO111MODULE=on
- CGO_ENABLED=0
before:
hooks:
- go mod tidy
builds:
-
ldflags: -s -w -X main.ProjectName={{ .ProjectName }} -X main.Version=v{{ .Version }} -X main.CommitSHA={{ .ShortCommit }}
goos:
- linux
- darwin
- windows
- freebsd
- openbsd
- netbsd
goarch:
- amd64
- arm64
- "386"
- arm
goarm:
- "7"
- "6"
ignore:
- goos: windows
goarch: arm64
- goos: windows
goarm: "7"
- goos: windows
goarm: "6"

archives:
-
format_overrides:
- goos: windows
format: zip
replacements:
windows: Windows
darwin: Darwin
386: i386
amd64: x86_64
files:
- README*
- LICENSE*

nfpms:
-
vendor: aymanbagabas
homepage: "https://aymanbagabas.com/"
maintainer: "Ayman Bagabas <ayman.bagabas@gmail.com>"
description: "Copy text to clipboard from anywhere using ANSI OSC 52 sequence"
license: MIT
formats:
- deb
- rpm
bindir: /usr/bin

brews:
- tap:
owner: 'aymanbagabas'
name: homebrew-tap
token: "{{ .Env.HOMEBREW_TAP_GITHUB_TOKEN }}"
commit_author:
name: 'Ayman Bagabas'
email: 'ayman.bagabas@gmail.com'
homepage: 'https://aymanbagabas.com/'
description: 'Copy text to clipboard from anywhere using ANSI OSC 52 sequence'

checksum:
name_template: 'checksums.txt'

source:
enabled: true

snapshot:
name_template: "{{ incpatch .Version }}-snapshot"

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Ayman Bagabas

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.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/aymanbagabas/shcopy

go 1.19

replace github.com/aymanbagabas/go-osc52 => ../go-osc52

require (
github.com/aymanbagabas/go-osc52 v1.0.3
github.com/spf13/pflag v1.0.5
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
111 changes: 111 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package main

import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"

"github.com/aymanbagabas/go-osc52"
"github.com/spf13/pflag"
)

var (
ProjectName = "shcopy"
Version = "unknown"
CommitSHA = "build from source"

term = pflag.StringP("term", "t", os.Getenv("TERM"), "Terminal type: screen, tmux, etc.")
clear = pflag.BoolP("clear", "c", false, "Clear the clipboard.")
primary = pflag.BoolP("primary", "p", false, "Use the primary clipboard instead system clipboard.")
version = pflag.BoolP("version", "v", false, "Print version and exit.")
help = pflag.BoolP("help", "h", false, "Print help and exit.")
debug = pflag.BoolP("debug", "d", false, "Print debug information.")
)

func usage(isError bool) {
out := os.Stdout
if isError {
out = os.Stderr
}
fmt.Fprintf(out, `Usage:
%[1]s [options] [text]
%[1]s [options] < [file]
Copy text to the system clipboard from any supported terminal using
ANSI OSC 52 sequence.
Options:
`, ProjectName)
pflag.PrintDefaults()
}

func main() {
pflag.Usage = func() {
usage(true)
}
pflag.Parse()

if *version {
fmt.Printf("%s version %s (%s)", ProjectName, Version, CommitSHA)
os.Exit(0)
}

if *help {
usage(false)
os.Exit(0)
}

var str string
args := pflag.Args()
// read from stdin if no arguments are provided and we are not clearing the
// clipboard
if len(args) == 0 && !*clear {
reader := bufio.NewReader(os.Stdin)
var b strings.Builder

for {
r, _, err := reader.ReadRune()
if err != nil && err == io.EOF {
break
}
_, err = b.WriteRune(r)
if err != nil {
fmt.Printf("Failed to write rune: %v", err)
os.Exit(1)
}
}

// input
str = b.String()
} else {
str = strings.Join(args, " ")
}

if *debug {
log.Printf("Input: %q", str)
}
term := strings.ToLower(*term)
clip := osc52.ClipboardC
if *primary {
clip = osc52.ClipboardP
}
if *debug {
log.Printf("Clipboard: %v", clip)
}
if strings.Contains(term, "kitty") {
// Flush the keyboard before copying, this is required for
// Kitty < 0.22.0.
os.Stdout.WriteString(osc52.Clear(term, clip))
}
// the sequence string to be sent to the terminal
seq := osc52.Sequence(str, term, clip)
if *debug {
log.Printf("Sequence: %q", seq)
}

// send the sequence to the terminal
os.Stdout.WriteString(seq)
}

0 comments on commit 35a1eb2

Please sign in to comment.