Cut/copy/paste
This commit is contained in:
parent
c8729aebb8
commit
e1ebc29ac1
53
clipboard.go
Normal file
53
clipboard.go
Normal file
@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import "github.com/zyedidia/clipboard"
|
||||
|
||||
type ClipMethod uint8
|
||||
|
||||
const (
|
||||
ClipExternal ClipMethod = iota
|
||||
_
|
||||
ClipInternal
|
||||
)
|
||||
|
||||
var ClipCurrentMethod ClipMethod
|
||||
|
||||
var internalClipboard string
|
||||
|
||||
// ClipInitialize will initialize the clipboard for the given method first,
|
||||
// and if that fails, an internal method will be chosen, instead. The Method
|
||||
// chosen is returned along with any error that may have occurred while
|
||||
// selecting the method. The error is not fatal because an internal method
|
||||
// is used.
|
||||
func ClipInitialize(m ClipMethod) (ClipMethod, error) {
|
||||
err := clipboard.Initialize()
|
||||
if err != nil {
|
||||
ClipCurrentMethod = ClipInternal
|
||||
return ClipInternal, err
|
||||
}
|
||||
ClipCurrentMethod = ClipExternal
|
||||
return ClipExternal, nil
|
||||
}
|
||||
|
||||
// ClipRead receives the clipboard contents using the ClipCurrentMethod.
|
||||
func ClipRead() (string, error) {
|
||||
switch ClipCurrentMethod {
|
||||
case ClipExternal:
|
||||
return clipboard.ReadAll("clipboard")
|
||||
case ClipInternal:
|
||||
return internalClipboard, nil
|
||||
}
|
||||
panic("How did execution get here?")
|
||||
}
|
||||
|
||||
// ClipWrite sets the clipboard contents using the ClipCurrentMethod.
|
||||
func ClipWrite(content string) error {
|
||||
switch ClipCurrentMethod {
|
||||
case ClipExternal:
|
||||
return clipboard.WriteAll(content, "clipboard")
|
||||
case ClipInternal:
|
||||
internalClipboard = content
|
||||
return nil
|
||||
}
|
||||
panic("How did execution get here?")
|
||||
}
|
5
go.mod
5
go.mod
@ -2,4 +2,7 @@ module github.com/fivemoreminix/diesel
|
||||
|
||||
go 1.12
|
||||
|
||||
require github.com/gdamore/tcell/v2 v2.2.0
|
||||
require (
|
||||
github.com/gdamore/tcell/v2 v2.2.0
|
||||
github.com/zyedidia/clipboard v1.0.3 // indirect
|
||||
)
|
||||
|
2
go.sum
2
go.sum
@ -8,6 +8,8 @@ github.com/mattn/go-runewidth v0.0.10 h1:CoZ3S2P7pvtP45xOtBw+/mDL2z0RKI576gSkzRR
|
||||
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
|
||||
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||
github.com/zyedidia/clipboard v1.0.3 h1:F/nCDVYMdbDWTmY8s8cJl0tnwX32q96IF09JHM14bUI=
|
||||
github.com/zyedidia/clipboard v1.0.3/go.mod h1:zykFnZUXX0ErxqvYLUFEq7QDJKId8rmh2FgD0/Y8cjA=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf h1:MZ2shdL+ZM/XzY3ZGOnh4Nlpnxz5GSOhOmtHo3iPU6M=
|
||||
|
45
main.go
45
main.go
@ -34,19 +34,17 @@ func main() {
|
||||
}
|
||||
defer s.Fini() // Useful for handling panics
|
||||
|
||||
// defer func() {
|
||||
// if err := recover(); err != nil {
|
||||
// s.Fini()
|
||||
// fmt.Fprintln(os.Stderr, err)
|
||||
// }
|
||||
// }()
|
||||
|
||||
sizex, sizey := s.Size()
|
||||
|
||||
tabContainer := ui.NewTabContainer(&theme)
|
||||
tabContainer.SetPos(0, 1)
|
||||
tabContainer.SetSize(sizex, sizey-1)
|
||||
|
||||
_, err := ClipInitialize(ClipExternal)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// Load files from command-line arguments
|
||||
if len(os.Args) > 1 {
|
||||
for _, path := range os.Args[1:] {
|
||||
@ -148,8 +146,37 @@ func main() {
|
||||
os.Exit(0)
|
||||
}}}))
|
||||
|
||||
bar.AddMenu(ui.NewMenu("Edit", &theme, []ui.Item{&ui.ItemEntry{Name: "New", Callback: func() {
|
||||
s.Beep()
|
||||
bar.AddMenu(ui.NewMenu("Edit", &theme, []ui.Item{&ui.ItemEntry{Name: "Cut", Callback: func() {
|
||||
if tabContainer.GetTabCount() > 0 {
|
||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||
te := tab.Child.(*ui.TextEdit)
|
||||
selectedStr := te.GetSelectedString()
|
||||
if selectedStr != "" { // If something is selected...
|
||||
te.Delete(false) // Delete the selection
|
||||
// TODO: better error handling within editor
|
||||
_ = ClipWrite(selectedStr) // Add the selectedStr to clipboard
|
||||
}
|
||||
}
|
||||
}}, &ui.ItemEntry{Name: "Copy", Callback: func() {
|
||||
if tabContainer.GetTabCount() > 0 {
|
||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||
te := tab.Child.(*ui.TextEdit)
|
||||
selectedStr := te.GetSelectedString()
|
||||
if selectedStr != "" { // If there is something selected...
|
||||
_ = ClipWrite(selectedStr) // Add selectedStr to clipboard
|
||||
}
|
||||
}
|
||||
}}, &ui.ItemEntry{Name: "Paste", Callback: func() {
|
||||
if tabContainer.GetTabCount() > 0 {
|
||||
tab := tabContainer.GetTab(tabContainer.GetSelectedTabIdx())
|
||||
te := tab.Child.(*ui.TextEdit)
|
||||
|
||||
contents, err := ClipRead()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
te.Insert(contents)
|
||||
}
|
||||
}}}))
|
||||
|
||||
bar.AddMenu(ui.NewMenu("Search", &theme, []ui.Item{&ui.ItemEntry{Name: "New", Callback: func() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user