Skip to content

Commit

Permalink
added copy and paste shortcuts to multiline mode
Browse files Browse the repository at this point in the history
changed clipboard library

added copy and paste shortcuts to multiline mode

changed clipboard library

reverted

removed deps

removed old code
  • Loading branch information
user authored and aandrew-me committed Feb 1, 2024
1 parent 92cb9b9 commit 3296073
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/aandrew-me/tgpt/v2
go 1.20

require (
github.com/atotto/clipboard v0.1.4
github.com/bogdanfinn/fhttp v0.5.24
github.com/bogdanfinn/tls-client v1.6.1
github.com/c-bata/go-prompt v0.2.6
Expand All @@ -16,7 +17,6 @@ require (

require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/bogdanfinn/utls v1.5.16 // indirect
github.com/charmbracelet/lipgloss v0.8.0 // indirect
Expand Down
12 changes: 7 additions & 5 deletions helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ImgResponse struct {
Images []string `json:"images"`
}

func getData(input string, isInteractive bool, prevMessages string) string {
func getDataResponseTxt(input string, isInteractive bool, prevMessages string) string {
// Receiving response
resp, err := providers.NewRequest(input, structs.Params{ApiKey: *apiKey, ApiModel: *apiModel, Provider: *provider, Max_length: *max_length, Temperature: *temperature, Top_p: *top_p, Preprompt: *preprompt}, prevMessages)

Expand Down Expand Up @@ -71,7 +71,11 @@ func getData(input string, isInteractive bool, prevMessages string) string {
}

// Handling each part
responseTxt := handleEachPart(resp)
return handleEachPart(resp)
}

func getData(input string, isInteractive bool, prevMessages string) (string, string) {
responseTxt := getDataResponseTxt(input, isInteractive, prevMessages)
safeResponse, _ := json.Marshal(responseTxt)

fmt.Print("\n\n")
Expand All @@ -86,7 +90,7 @@ func getData(input string, isInteractive bool, prevMessages string) string {
},
`, string(safeInput), string(safeResponse))

return msgObject
return msgObject, responseTxt
}

func loading(stop *bool) {
Expand Down Expand Up @@ -691,8 +695,6 @@ func downloadImage(url string, destDir string, filename string) error {
}
defer file.Close()



_, err = io.Copy(file, response.Body)
if err != nil {
return err
Expand Down
47 changes: 39 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"syscall"

"github.com/atotto/clipboard"
Prompt "github.com/c-bata/go-prompt"
"github.com/charmbracelet/bubbles/textarea"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -28,6 +29,7 @@ var codeText = color.New(color.BgBlack, color.FgGreen, color.Bold)
var stopSpin = false
var programLoop = true
var userInput = ""
var lastResponse = ""
var executablePath = ""
var provider *string
var apiModel *string
Expand Down Expand Up @@ -195,8 +197,10 @@ func main() {
}
os.Exit(0)
}
previousMessages += getData(input, true, previousMessages)
responseJson, responseTxt := getData(input, true, previousMessages)
previousMessages += responseJson
history = append(history, input)
lastResponse = responseTxt
}
}
}
Expand All @@ -205,9 +209,11 @@ func main() {
/////////////////////
// Multiline interactive
/////////////////////

fmt.Print("\nPress Tab to submit and Ctrl + C to exit.\n")

previousMessages := ""
history := []string{}

for programLoop {
fmt.Print("\n")
Expand All @@ -219,7 +225,10 @@ func main() {
os.Exit(1)
}
if len(userInput) > 0 {
previousMessages += getData(userInput, true, previousMessages)
responseJson, responseTxt := getData(userInput, true, previousMessages)
previousMessages += responseJson
history = append(history, userInput)
lastResponse = responseTxt
}

}
Expand Down Expand Up @@ -307,10 +316,35 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.textarea.Blur()
return m, tea.Quit
}

default:
if !m.textarea.Focused() {
cmd = m.textarea.Focus()
if m.textarea.Focused() {
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
}
}

// Command mode
if !m.textarea.Focused() {
switch msg.String() {
case "i":
m.textarea.Focus()
case "y":
if len(lastResponse) == 0 {
break
}
err := clipboard.WriteAll(lastResponse)
if err != nil {
fmt.Println("Could not write to clipboard")
}
case "p":
m.textarea.Focus()
clip, err := clipboard.ReadAll()
msg.Runes = []rune(clip)
if err != nil {
fmt.Println("Could not read from clipboard")
}
userInput = clip
m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
}
}
Expand All @@ -321,8 +355,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
}

m.textarea, cmd = m.textarea.Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}

Expand Down Expand Up @@ -385,7 +417,6 @@ func showHelpMessage() {
bold.Println("\nProvider: phind")
fmt.Println("Uses Phind Model. Great for developers")


boldBlue.Println("\nExamples:")
fmt.Println(`tgpt "What is internet?"`)
fmt.Println(`tgpt -m`)
Expand Down

0 comments on commit 3296073

Please sign in to comment.