Skip to content

Commit

Permalink
Moves editor logic to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Oct 16, 2023
1 parent a5a7aa8 commit 389c9c8
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 112 deletions.
118 changes: 6 additions & 112 deletions pkg/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"log"
"os"
"os/user"
"strings"

"github.com/eiannone/keyboard"
"github.com/ksysoev/wsget/pkg/formater"
Expand All @@ -26,8 +25,8 @@ const (

type CLI struct {
formater *formater.Formater
history *History
wsConn *ws.Connection
editor *Editor
}

func NewCLI(wsConn *ws.Connection) *CLI {
Expand All @@ -38,9 +37,11 @@ func NewCLI(wsConn *ws.Connection) *CLI {

homeDir := currentUser.HomeDir

history := NewHistory(homeDir+"/"+HistoryFilename, HistoryLimit)

return &CLI{
formater: formater.NewFormatter(),
history: NewHistory(homeDir+"/"+HistoryFilename, HistoryLimit),
editor: NewEditor(history),
wsConn: wsConn,
}
}
Expand All @@ -51,7 +52,7 @@ func (c *CLI) Run(outputFile *os.File) error {
}
defer keyboard.Close()
defer func() {
err := c.history.SaveToFile()
err := c.editor.History.SaveToFile()
if err != nil {
fmt.Println("Fail to save history:", err)
}
Expand All @@ -74,7 +75,7 @@ func (c *CLI) Run(outputFile *os.File) error {
case keyboard.KeyEsc:
fmt.Println("Request Mode: Type your API request and press Ctrl+S to send it. Press ESC to cancel request")

req, err := c.requestMode(keysEvents)
req, err := c.editor.EditRequest(keysEvents, "")
if err != nil {
if err.Error() == "interrupted" {
return nil
Expand Down Expand Up @@ -114,110 +115,3 @@ func (c *CLI) Run(outputFile *os.File) error {
}
}
}

func (c *CLI) requestMode(keyStream <-chan keyboard.KeyEvent) (string, error) {
buffer := ""

historyIndex := 0

for e := range keyStream {
if e.Err != nil {
return buffer, e.Err
}

switch e.Key {
case keyboard.KeyCtrlC, keyboard.KeyCtrlD:
return buffer, fmt.Errorf("interrupted")
case keyboard.KeyCtrlS:
if buffer == "" {
return buffer, fmt.Errorf("cannot send empty request")
}

requet := strings.TrimSpace(buffer)

c.history.AddRequest(requet)

return requet, nil
case keyboard.KeyEsc:
return "", nil

case keyboard.KeySpace:
fmt.Print(" ")

buffer += " "
case keyboard.KeyEnter:
fmt.Print("\n")

buffer += "\n"
case keyboard.KeyBackspace, keyboard.KeyDelete, MacOSDeleteKey:
if buffer == "" {
continue
}

if buffer[len(buffer)-1] == '\n' {
buffer = buffer[:len(buffer)-1]

fmt.Print(LineUp)

startPrevLine := strings.LastIndex(buffer, "\n")
if startPrevLine == -1 {
startPrevLine = 0
} else {
startPrevLine++
}

fmt.Print(buffer[startPrevLine:])
} else {
fmt.Print("\b \b")
buffer = buffer[:len(buffer)-1]
}
case keyboard.KeyArrowUp:
historyIndex++
req := c.history.GetRequst(historyIndex)

if req == "" {
historyIndex--
continue
}

c.clearInput(buffer)

fmt.Print(req)
buffer = req
case keyboard.KeyArrowDown:
historyIndex--
req := c.history.GetRequst(historyIndex)

if req == "" {
historyIndex++
continue
}

c.clearInput(buffer)

fmt.Print(req)
buffer = req
default:
if e.Key > 0 {
continue
}

fmt.Print(string(e.Rune))

buffer += string(e.Rune)
}
}

return buffer, fmt.Errorf("keyboard stream was unexpectably closed")
}

func (c *CLI) clearInput(buffer string) {
for i := 0; i < len(buffer); i++ {
if buffer[i] == '\n' {
fmt.Print(LineUp)
fmt.Print(LineClear)
} else {
fmt.Print("\b \b")
}
}
}
123 changes: 123 additions & 0 deletions pkg/cli/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package cli

import (
"fmt"
"strings"

"github.com/eiannone/keyboard"
)

type Editor struct {
History *History
}

func NewEditor(history *History) *Editor {
return &Editor{
History: history,
}
}

func (ed *Editor) EditRequest(keyStream <-chan keyboard.KeyEvent, buffer string) (string, error) {
historyIndex := 0

for e := range keyStream {
if e.Err != nil {
return buffer, e.Err
}

switch e.Key {
case keyboard.KeyCtrlC, keyboard.KeyCtrlD:
return buffer, fmt.Errorf("interrupted")
case keyboard.KeyCtrlS:
if buffer == "" {
return buffer, fmt.Errorf("cannot send empty request")
}

requet := strings.TrimSpace(buffer)

ed.History.AddRequest(requet)

return requet, nil
case keyboard.KeyEsc:
return "", nil

case keyboard.KeySpace:
fmt.Print(" ")

buffer += " "
case keyboard.KeyEnter:
fmt.Print("\n")

buffer += "\n"
case keyboard.KeyBackspace, keyboard.KeyDelete, MacOSDeleteKey:
if buffer == "" {
continue
}

if buffer[len(buffer)-1] == '\n' {
buffer = buffer[:len(buffer)-1]

fmt.Print(LineUp)

startPrevLine := strings.LastIndex(buffer, "\n")
if startPrevLine == -1 {
startPrevLine = 0
} else {
startPrevLine++
}

fmt.Print(buffer[startPrevLine:])
} else {
fmt.Print("\b \b")
buffer = buffer[:len(buffer)-1]
}
case keyboard.KeyArrowUp:
historyIndex++
req := ed.History.GetRequst(historyIndex)

if req == "" {
historyIndex--
continue
}

ed.clearInput(buffer)

fmt.Print(req)
buffer = req
case keyboard.KeyArrowDown:
historyIndex--
req := ed.History.GetRequst(historyIndex)

if req == "" {
historyIndex++
continue
}

ed.clearInput(buffer)

fmt.Print(req)
buffer = req
default:
if e.Key > 0 {
continue
}

fmt.Print(string(e.Rune))

buffer += string(e.Rune)
}
}

return buffer, fmt.Errorf("keyboard stream was unexpectably closed")
}

func (ed *Editor) clearInput(buffer string) {
for i := 0; i < len(buffer); i++ {
if buffer[i] == '\n' {
fmt.Print(LineUp)
fmt.Print(LineClear)
} else {
fmt.Print("\b \b")
}
}
}

0 comments on commit 389c9c8

Please sign in to comment.