Merge pull request #10 from asrul10/feat/copy-clipboard

(feat) Copy to clipboard
This commit is contained in:
asrul10
2023-12-19 10:12:35 +07:00
committed by GitHub
5 changed files with 20 additions and 14 deletions

View File

@@ -21,9 +21,11 @@ Completed in 0.92 seconds
tar -xvzf linux-command-gpt.tar.gz 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 ### Options
```bash ```bash
> lcg [options] > lcg [options]

2
go.mod
View File

@@ -1,3 +1,5 @@
module github.com/asrul/linux-command-gpt module github.com/asrul/linux-command-gpt
go 1.18 go 1.18
require github.com/atotto/clipboard v0.1.4

2
go.sum
View File

@@ -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=

View File

@@ -4,7 +4,7 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@@ -88,7 +88,7 @@ func (gpt3 *Gpt3) loadApiKey() bool {
if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) { if _, err := os.Stat(apiKeyFile); os.IsNotExist(err) {
return false return false
} }
apiKey, err := ioutil.ReadFile(apiKeyFile) apiKey, err := os.ReadFile(apiKeyFile)
if err != nil { if err != nil {
return false return false
} }
@@ -144,7 +144,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
if err != nil { if err != nil {
panic(err) panic(err)
} }
req.Body = ioutil.NopCloser(bytes.NewBuffer(payloadJson)) req.Body = io.NopCloser(bytes.NewBuffer(payloadJson))
client := &http.Client{} client := &http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)
@@ -153,7 +153,7 @@ func (gpt3 *Gpt3) Completions(ask string) string {
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := io.ReadAll(resp.Body)
if err != nil { if err != nil {
panic(err) panic(err)
} }

18
main.go
View File

@@ -4,12 +4,12 @@ import (
"fmt" "fmt"
"math" "math"
"os" "os"
"os/exec"
"os/user" "os/user"
"strings" "strings"
"time" "time"
"github.com/asrul/linux-command-gpt/gpt" "github.com/asrul/linux-command-gpt/gpt"
"github.com/atotto/clipboard"
) )
const ( const (
@@ -138,8 +138,10 @@ func main() {
c = "N" c = "N"
fmt.Printf("Completed in %v seconds\n\n", elapsed) fmt.Printf("Completed in %v seconds\n\n", elapsed)
fmt.Println(r) 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) fmt.Scanln(&c)
// No action
if c == "N" || c == "n" { if c == "N" || c == "n" {
return return
} }
@@ -148,13 +150,11 @@ func main() {
if r == "" { if r == "" {
return return
} }
cmsplit := strings.Split(r, " ")
cm := exec.Command(cmsplit[0], cmsplit[1:]...) // Copy to clipboard
out, err := cm.Output() if c == "C" || c == "c" {
if err != nil { clipboard.WriteAll(r)
fmt.Println(err.Error()) fmt.Println("\033[33mCopied to clipboard")
return return
} }
fmt.Println(string(out))
} }