From 148e1d9420fee841cc24ebea22ac5bb02368a193 Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sat, 25 Mar 2023 22:26:38 +0700 Subject: [PATCH 1/7] fix: box result --- go.mod | 5 +++++ go.sum | 4 ++++ main.go | 11 ++++++++--- 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 34864fe..f91e102 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,8 @@ module github.com/asrul/linux-command-gpt go 1.18 + +require ( + golang.org/x/sys v0.6.0 // indirect + golang.org/x/term v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..275ee62 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= diff --git a/main.go b/main.go index 12a305c..1e4f0ce 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "time" "github.com/asrul/linux-command-gpt/gpt" + "golang.org/x/term" ) const ( @@ -58,6 +59,10 @@ func handleCommand(cmd string) int { } func main() { + width, _, err := term.GetSize(0) + if err != nil { + panic(err) + } currentUser, err := user.Current() if err != nil { panic(err) @@ -127,9 +132,9 @@ func main() { elapsed := time.Since(s).Seconds() elapsed = math.Round(elapsed*100) / 100 fmt.Printf("Completed in %v seconds\n", elapsed) - fmt.Printf("┌%s┐\n", strings.Repeat("─", len(r)+2)) - fmt.Printf("│ %s │\n", r) - fmt.Printf("└%s┘\n", strings.Repeat("─", len(r)+2)) + fmt.Println(strings.Repeat("─", width)) + fmt.Println(r) + fmt.Println(strings.Repeat("─", width)) fmt.Print("Are you sure you want to execute the command? (Y/n): ") fmt.Scanln(&c) if c != "Y" && c != "y" { From 0e50c8ec046f9b6305cc695aa287aa7157bb847e Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:25:50 +0700 Subject: [PATCH 2/7] feat: remove borders to make it easy to copy --- README.md | 6 +++--- go.mod | 5 ----- go.sum | 4 ---- main.go | 11 ++--------- 4 files changed, 5 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4c87952..dc7eaaa 100644 --- a/README.md +++ b/README.md @@ -18,9 +18,9 @@ Or you can [download lcg executable file](https://github.com/asrul10/linux-comma ```bash > lcg I want to extract linux-command-gpt.tar.gz file Completed in 0.92 seconds -┌────────────────────────────────────┐ -│ tar -xvzf linux-command-gpt.tar.gz │ -└────────────────────────────────────┘ + +tar -xvzf linux-command-gpt.tar.gz + Are you sure you want to execute the command? (Y/n): ``` diff --git a/go.mod b/go.mod index f91e102..34864fe 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,3 @@ module github.com/asrul/linux-command-gpt go 1.18 - -require ( - golang.org/x/sys v0.6.0 // indirect - golang.org/x/term v0.6.0 // indirect -) diff --git a/go.sum b/go.sum index 275ee62..e69de29 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +0,0 @@ -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= -golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= diff --git a/main.go b/main.go index 1e4f0ce..9331354 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "time" "github.com/asrul/linux-command-gpt/gpt" - "golang.org/x/term" ) const ( @@ -59,10 +58,6 @@ func handleCommand(cmd string) int { } func main() { - width, _, err := term.GetSize(0) - if err != nil { - panic(err) - } currentUser, err := user.Current() if err != nil { panic(err) @@ -131,11 +126,9 @@ func main() { c := "Y" elapsed := time.Since(s).Seconds() elapsed = math.Round(elapsed*100) / 100 - fmt.Printf("Completed in %v seconds\n", elapsed) - fmt.Println(strings.Repeat("─", width)) + fmt.Printf("Completed in %v seconds\n\n", elapsed) fmt.Println(r) - fmt.Println(strings.Repeat("─", width)) - fmt.Print("Are you sure you want to execute the command? (Y/n): ") + fmt.Print("\nAre you sure you want to execute the command? (Y/n): ") fmt.Scanln(&c) if c != "Y" && c != "y" { return From 432bfc61dbdf92e1a078eecc3f91714fd0b3e0a2 Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:30:13 +0700 Subject: [PATCH 3/7] feat: add example usage if there is no option --- main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/main.go b/main.go index 9331354..f086e67 100644 --- a/main.go +++ b/main.go @@ -31,6 +31,7 @@ Usage: lcg [options] --update-key update the API key --delete-key delete the API key +Example Usage: lcg I want to extract linux-command-gpt.tar.gz file ` VERSION = "0.1.0" From 2d6fef23aa126ea7aa5b10b9431e8370a4e16410 Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:33:53 +0700 Subject: [PATCH 4/7] chore: set default options to (N)o execute --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index f086e67..8ed6969 100644 --- a/main.go +++ b/main.go @@ -124,14 +124,14 @@ func main() { return } - c := "Y" + c := "N" elapsed := time.Since(s).Seconds() elapsed = math.Round(elapsed*100) / 100 fmt.Printf("Completed in %v seconds\n\n", elapsed) fmt.Println(r) - fmt.Print("\nAre you sure you want to execute the command? (Y/n): ") + fmt.Print("\nAre you sure you want to execute the command? (y/N): ") fmt.Scanln(&c) - if c != "Y" && c != "y" { + if c == "N" || c == "n" { return } From fbb68d2a28a4a51b2a45a764990ee1ab9c98cc9b Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:51:26 +0700 Subject: [PATCH 5/7] feat: add regenerate options --- main.go | 75 +++++++++++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 31 deletions(-) diff --git a/main.go b/main.go index 8ed6969..925ba3a 100644 --- a/main.go +++ b/main.go @@ -58,6 +58,37 @@ func handleCommand(cmd string) int { return CMD_COMPLETION } +func getCommand(gpt3 gpt.Gpt3, cmd string) (string, float64) { + gpt3.InitKey() + s := time.Now() + done := make(chan bool) + go func() { + loadingChars := []rune{'-', '\\', '|', '/'} + i := 0 + for { + select { + case <-done: + fmt.Printf("\r") + return + default: + fmt.Printf("\rLoading %c", loadingChars[i]) + i = (i + 1) % len(loadingChars) + time.Sleep(30 * time.Millisecond) + } + } + }() + + r := gpt3.Completions(cmd) + done <- true + elapsed := time.Since(s).Seconds() + elapsed = math.Round(elapsed*100) / 100 + + if r == "" { + return "", elapsed + } + return r, elapsed +} + func main() { currentUser, err := user.Current() if err != nil { @@ -99,42 +130,24 @@ func main() { return } - gpt3.InitKey() - s := time.Now() - done := make(chan bool) - go func() { - loadingChars := []rune{'-', '\\', '|', '/'} - i := 0 - for { - select { - case <-done: - fmt.Printf("\r") - return - default: - fmt.Printf("\rLoading %c", loadingChars[i]) - i = (i + 1) % len(loadingChars) - time.Sleep(30 * time.Millisecond) - } + c := "R" + r := "" + elapsed := 0.0 + for c == "R" || c == "r" { + r, elapsed = getCommand(gpt3, cmd) + 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.Scanln(&c) + if c == "N" || c == "n" { + return } - }() + } - r := gpt3.Completions(cmd) - done <- true if r == "" { return } - - c := "N" - elapsed := time.Since(s).Seconds() - elapsed = math.Round(elapsed*100) / 100 - fmt.Printf("Completed in %v seconds\n\n", elapsed) - fmt.Println(r) - fmt.Print("\nAre you sure you want to execute the command? (y/N): ") - fmt.Scanln(&c) - if c == "N" || c == "n" { - return - } - cmsplit := strings.Split(r, " ") cm := exec.Command(cmsplit[0], cmsplit[1:]...) out, err := cm.Output() From dafcaaff0fd1a66157c9405288ca66903ef556cf Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:53:11 +0700 Subject: [PATCH 6/7] docs: update example result --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dc7eaaa..d90bd92 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Completed in 0.92 seconds tar -xvzf linux-command-gpt.tar.gz -Are you sure you want to execute the command? (Y/n): +Do you want to (e)xecute, (r)egenerate, or take (N)o action on the command? (e/r/N): ``` ### Options From 7f81b1942b521d0ea4c553b72751018f299000dd Mon Sep 17 00:00:00 2001 From: asrul10 Date: Sun, 9 Apr 2023 10:54:35 +0700 Subject: [PATCH 7/7] style: update version --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 925ba3a..06f0e54 100644 --- a/main.go +++ b/main.go @@ -34,7 +34,7 @@ Usage: lcg [options] Example Usage: lcg I want to extract linux-command-gpt.tar.gz file ` - VERSION = "0.1.0" + VERSION = "0.1.3" CMD_HELP = 100 CMD_VERSION = 101 CMD_UPDATE = 102