Переглянути джерело

Don't use tcell clipboard, only use clipboard package and own keybinds

Johan Rong 1 місяць тому
батько
коміт
711a6217e4
4 змінених файлів з 37 додано та 16 видалено
  1. 6 0
      go.mod
  2. 12 0
      go.sum
  3. 13 0
      internal/editor/action.go
  4. 6 16
      main.go

+ 6 - 0
go.mod

@@ -10,8 +10,14 @@ require (
 require (
 	github.com/clipperhouse/displaywidth v0.11.0 // indirect
 	github.com/clipperhouse/uax29/v2 v2.7.0 // indirect
+	github.com/ebitengine/purego v0.10.1 // indirect
 	github.com/gdamore/encoding v1.0.1 // indirect
 	github.com/lucasb-eyer/go-colorful v1.4.0 // indirect
+	golang.design/x/clipboard v0.8.0 // indirect
+	golang.design/x/x11 v0.2.0 // indirect
+	golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476 // indirect
+	golang.org/x/image v0.28.0 // indirect
+	golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f // indirect
 	golang.org/x/sys v0.46.0 // indirect
 	golang.org/x/term v0.43.0 // indirect
 	golang.org/x/text v0.37.0 // indirect

+ 12 - 0
go.sum

@@ -2,6 +2,8 @@ github.com/clipperhouse/displaywidth v0.11.0 h1:lBc6kY44VFw+TDx4I8opi/EtL9m20WSE
 github.com/clipperhouse/displaywidth v0.11.0/go.mod h1:bkrFNkf81G8HyVqmKGxsPufD3JhNl3dSqnGhOoSD/o0=
 github.com/clipperhouse/uax29/v2 v2.7.0 h1:+gs4oBZ2gPfVrKPthwbMzWZDaAFPGYK72F0NJv2v7Vk=
 github.com/clipperhouse/uax29/v2 v2.7.0/go.mod h1:EFJ2TJMRUaplDxHKj1qAEhCtQPW2tJSwu5BF98AuoVM=
+github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
+github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
 github.com/gdamore/encoding v1.0.1 h1:YzKZckdBL6jVt2Gc+5p82qhrGiqMdG/eNs6Wy0u3Uhw=
 github.com/gdamore/encoding v1.0.1/go.mod h1:0Z0cMFinngz9kS1QfMjCP8TY7em3bZYeeklsSDPivEo=
 github.com/gdamore/tcell/v3 v3.4.0 h1:VUym1HQZiYodA5PGQrqLxF7QwqQndcAUwQD7G7XUy5E=
@@ -11,8 +13,18 @@ github.com/lucasb-eyer/go-colorful v1.4.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i
 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
 github.com/zyedidia/rope v0.0.0-20210616205215-37fbf22eab3a h1:+VbuFCNAjzVffErUlm0TIAZClFxFZwJ1il6qtWrnIg8=
 github.com/zyedidia/rope v0.0.0-20210616205215-37fbf22eab3a/go.mod h1:IKo1js3O2gdESAUH9F3B33wlrRBnpJMJR/gXyY0a3ho=
+golang.design/x/clipboard v0.8.0 h1:6VEcH28wwcSgKc+vnxHHDWiRjrakTQIAJnPUrt3aOgg=
+golang.design/x/clipboard v0.8.0/go.mod h1:s0pwrtA3Q9fgnVtGDmP5ZK/pp55cQKB23esKsjwWhWM=
+golang.design/x/x11 v0.2.0 h1:Uiwu2guGihsJX/ZCzpoDPFz5gR/Qntm08mvoBCmRydo=
+golang.design/x/x11 v0.2.0/go.mod h1:/5q1mFkdc1rL8mvB7DsQFi6as4tIkBv4FXjcP07mrkE=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476 h1:Wdx0vgH5Wgsw+lF//LJKmWOJBLWX6nprsMqnf99rYDE=
+golang.org/x/exp/shiny v0.0.0-20250606033433-dcc06ee1d476/go.mod h1:ygj7T6vSGhhm/9yTpOQQNvuAUFziTH7RUiH74EoE2C8=
+golang.org/x/image v0.28.0 h1:gdem5JW1OLS4FbkWgLO+7ZeFzYtL3xClb97GaUzYMFE=
+golang.org/x/image v0.28.0/go.mod h1:GUJYXtnGKEUgggyzh+Vxt+AviiCcyiwpsl8iQ8MvwGY=
+golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f h1:/n+PL2HlfqeSiDCuhdBbRNlGS/g2fM4OHufalHaTVG8=
+golang.org/x/mobile v0.0.0-20250606033058-a2a15c67f36f/go.mod h1:ESkJ836Z6LpG6mTVAhA48LpfW/8fNR0ifStlH2axyfg=
 golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
 golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
 golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=

+ 13 - 0
internal/editor/action.go

@@ -5,6 +5,7 @@ import(
 	"strconv"
 	"os"
 	"github.com/zyedidia/rope"
+	"golang.design/x/clipboard"
 )
 
 type ActionManager = struct {
@@ -165,6 +166,18 @@ func DefaultActionManager() ActionManager {
 					m.BM.PaletteBuffer.Insert("?")
 				},
 			},
+			Action{
+				Binding: "Rune[v]",
+				Command: "paste",
+				AskArgs: false,
+				Callback: func(m *Model, args []string) {
+					text := clipboard.Read(clipboard.FmtText)
+
+					if string(text) != "" {
+						m.BM.Current().Insert(string(text))
+					}
+				},
+			},
 		},
 		Insert:  []Action{
 			Action{

+ 6 - 16
main.go

@@ -6,6 +6,7 @@ import (
 	"moose/internal/editor"
 	"github.com/gdamore/tcell/v3"
 	"github.com/gdamore/tcell/v3/color"
+	"golang.design/x/clipboard"
 )
 
 func main() {
@@ -19,6 +20,10 @@ func main() {
 		os.Exit(1)
 	}
 
+	err = clipboard.Init()
+	if err != nil {
+		panic(err)
+	}
 
 	style := tcell.StyleDefault.Background(color.Reset).Foreground(color.Reset)
 	s.SetStyle(style)
@@ -39,7 +44,6 @@ func main() {
 	defer quit()
 
 	isPasting := false
-	pasteBuffer := ""
 
 	s.Clear()
 	m.Render()
@@ -58,26 +62,12 @@ func main() {
 		case *tcell.EventPaste:
 			if ev.Start() {
 				isPasting = true
-				pasteBuffer = "" 
 			} else if ev.End() {
 				isPasting = false
-				m.BM.Current().Insert(pasteBuffer)
-				
-				s.Clear()
-				m.Render()
-				s.Show()
 			}
 
 		case *tcell.EventKey:
-			if isPasting {
-				if ev.Key() == tcell.KeyEnter || ev.Key() == tcell.KeyCtrlJ {
-					pasteBuffer += "\n"
-				} else if ev.Key() == tcell.KeyTab {
-					pasteBuffer += "\t"
-				} else if ev.Key() == tcell.KeyRune {
-					pasteBuffer += ev.Str()
-				}
-			} else {
+			if !isPasting {
 				m.HandleKeyInput(ev)
 				
 				s.Clear()