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 8, 2020
1 parent 52a20ab commit 83467c4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func (session *viewerSession) initUIWithForm() {
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
12 changes: 12 additions & 0 deletions util_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// +build !windows

package main

import (
"github.com/rivo/tview"
)

// do nothing for non windows
// remove once https://github.com/gdamore/tcell/issues/319 is fixed
func (session *viewerSession) enablePaste(field *tview.InputField, form *tview.Form) {
}
47 changes: 47 additions & 0 deletions util_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// +build windows

package main

import (
"reflect"

"github.com/atotto/clipboard"
"github.com/gdamore/tcell"
"github.com/rivo/tview"
)

// 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) {
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 83467c4

Please sign in to comment.