This repository has been archived by the owner on Feb 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adapted from rivo/tview#92 undo once gdamore/tcell#319 is fixed
- Loading branch information
1 parent
52a20ab
commit 83467c4
Showing
3 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |