From 9538b0fed5790042c27aa0877c2f700e5a998ee1 Mon Sep 17 00:00:00 2001 From: asrul10 Date: Tue, 19 Dec 2023 10:09:26 +0700 Subject: [PATCH] feat: copy to clipboard --- README.md | 4 +++- go.mod | 2 ++ go.sum | 2 ++ main.go | 18 +++++++++--------- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index d90bd92..fdb0fd2 100644 --- a/README.md +++ b/README.md @@ -21,9 +21,11 @@ Completed in 0.92 seconds tar -xvzf linux-command-gpt.tar.gz -Do you want to (e)xecute, (r)egenerate, or take (N)o action on the command? (e/r/N): +Do you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): ``` +To use the "copy to clipboard" feature, you need to install either the `xclip` or `xsel` package. + ### Options ```bash > lcg [options] diff --git a/go.mod b/go.mod index 34864fe..a846ac1 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/asrul/linux-command-gpt go 1.18 + +require github.com/atotto/clipboard v0.1.4 diff --git a/go.sum b/go.sum index e69de29..244b67e 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= +github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= diff --git a/main.go b/main.go index 06f0e54..509777f 100644 --- a/main.go +++ b/main.go @@ -4,12 +4,12 @@ import ( "fmt" "math" "os" - "os/exec" "os/user" "strings" "time" "github.com/asrul/linux-command-gpt/gpt" + "github.com/atotto/clipboard" ) const ( @@ -138,8 +138,10 @@ func main() { c = "N" fmt.Printf("Completed in %v seconds\n\n", elapsed) fmt.Println(r) - fmt.Print("\nDo you want to (e)xecute, (r)egenerate, or take (N)o action on the command? (e/r/N): ") + fmt.Print("\nDo you want to (c)opy, (r)egenerate, or take (N)o action on the command? (c/r/N): ") fmt.Scanln(&c) + + // No action if c == "N" || c == "n" { return } @@ -148,13 +150,11 @@ func main() { if r == "" { return } - cmsplit := strings.Split(r, " ") - cm := exec.Command(cmsplit[0], cmsplit[1:]...) - out, err := cm.Output() - if err != nil { - fmt.Println(err.Error()) + + // Copy to clipboard + if c == "C" || c == "c" { + clipboard.WriteAll(r) + fmt.Println("\033[33mCopied to clipboard") return } - - fmt.Println(string(out)) }