Skip to content

Commit

Permalink
feat: color in grep mode
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsussa committed Oct 31, 2021
1 parent c38fbc9 commit c41ac18
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
46 changes: 31 additions & 15 deletions gclip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import (
"bytes"
"flag"
"fmt"
"github.com/atotto/clipboard"
"os"
"os/signal"
"strings"
"regexp"
"syscall"
)

const red = "\033[31m"
const reset = "\033[0m"

"github.com/atotto/clipboard"
"github.com/gookit/color"
)

// go install gclip.go
func main() {
grepPtr := flag.String("grep", "", "select line that contains")
flag.Parse()

grepTxt := *grepPtr

fi, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if fi.Mode() & os.ModeNamedPipe == 0 {
if fi.Mode()&os.ModeNamedPipe == 0 {
return
}

Expand All @@ -34,30 +35,45 @@ func main() {
go func() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
text := scanner.Text()
if grepPtr != nil && !strings.Contains(text, *grepPtr){
text := scanner.Text() + "\n"

if grepTxt == "" {
fmt.Print(text)
b.WriteString(text)
continue
}
b.WriteString(text)
b.WriteString("\n")
fmt.Print(text + "\n")

ok, err := regexp.MatchString(grepTxt, text)
if err != nil {
panic(err)
}

if ok {
color.Greenp(text)
b.WriteString(text)
} else {
fmt.Print(text)
}
}
endOfLine <- true
}()


c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)

select {
case <- c:
case <-c:
break
case <- endOfLine:
case <-endOfLine:
break
}

allLines := b.String()
if err := clipboard.WriteAll(allLines); err != nil {
panic(err)
}
}

func hasFlag(f *string) bool{
return f != nil && *f != ""
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module github.com/danielsussa/gclip

go 1.15
go 1.16

require github.com/atotto/clipboard v0.1.4
require (
github.com/atotto/clipboard v0.1.4
github.com/gookit/color v1.5.0
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gookit/color v1.5.0 h1:1Opow3+BWDwqor78DcJkJCIwnkviFi+rrOANki9BUFw=
github.com/gookit/color v1.5.0/go.mod h1:43aQb+Zerm/BWh2GnrgOQm7ffz7tvQXEKV6BFMl7wAo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778/go.mod h1:2MuV+tbUrU1zIOPMxZ5EncGwgmMJsa+9ucAQZXxsObs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 comments on commit c41ac18

Please sign in to comment.