Skip to content
This repository has been archived by the owner on Feb 10, 2023. It is now read-only.

Commit

Permalink
fix: pasting workaround for windows
Browse files Browse the repository at this point in the history
adapted from rivo/tview#92
undo once gdamore/tcell#319 is fixed
  • Loading branch information
SoMuchForSubtlety committed Aug 22, 2020
1 parent 52a20ab commit 1283c2a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,14 @@ func newSession() (*viewerSession, *os.File, error) {
func (session *viewerSession) initUIWithForm() {

form := tview.NewForm().
AddInputField("username", session.username, 30, nil, session.updateUsername).
AddInputField("email", session.username, 30, nil, session.updateUsername).
AddPasswordField("password", "", 30, '*', session.updatePassword).
AddButton("test", session.testAuth).
AddButton("save", session.closeForm)

session.enablePaste(form.GetFormItem(0).(*tview.InputField), form)
session.enablePaste(form.GetFormItem(1).(*tview.InputField), form)

formTreeFlex := tview.NewFlex()
if !session.cfg.HorizontalLayout {
formTreeFlex.SetDirection(tview.FlexRow)
Expand Down
41 changes: 41 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"log"
"os/exec"
"reflect"
"regexp"
"runtime"
"strconv"
"strings"
"time"

"github.com/atotto/clipboard"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)
Expand Down Expand Up @@ -193,3 +195,42 @@ func sanitizeFileName(s string) string {
s = strings.TrimSpace(s)
return s
}

// Hacky solution for a working copy paste on Windows
// Breaks encapsulation by accessing a private member using reflect
// remove once https://github.com/gdamore/tcell/issues/319 is fixed
func (session *viewerSession) enablePaste(field *tview.InputField, form *tview.Form) {
if runtime.GOOS != "windows" {
return
}
field.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyCtrlV || event.Key() == tcell.KeyCtrlY {
session.handlePaste(field)
}
return event
})

// paste on right click
field.SetMouseCapture(func(action tview.MouseAction, event *tcell.EventMouse) (tview.MouseAction, *tcell.EventMouse) {
item, _ := form.GetFocusedItemIndex()

if item != -1 && event.Buttons()&tcell.Button2 != 0 && form.GetFormItem(item) == field {
session.handlePaste(field)
}

return action, event
})
}

func (session *viewerSession) handlePaste(field *tview.InputField) {
clipContent, err := clipboard.ReadAll()
if err != nil {
session.logError("could not paste: ", err)
}
val := reflect.ValueOf(*field)
cursorPos := val.FieldByName("cursorPos").Int()
text := field.GetText()
field.SetText(text[0:cursorPos] + clipContent + text[cursorPos:])
// TODO: why does Draw() deadlock?
session.app.ForceDraw()
}

0 comments on commit 1283c2a

Please sign in to comment.