]> git.lizzy.rs Git - micro.git/blob - internal/clipboard/multi.go
Merge
[micro.git] / internal / clipboard / multi.go
1 package clipboard
2
3 import (
4         "bytes"
5 )
6
7 // For storing multi cursor clipboard contents
8 type multiClipboard map[Register][]string
9
10 var multi multiClipboard
11
12 func (c multiClipboard) getAllText(r Register) string {
13         content := c[r]
14         if content == nil {
15                 return ""
16         }
17
18         buf := &bytes.Buffer{}
19         for _, s := range content {
20                 buf.WriteString(s)
21         }
22         return buf.String()
23 }
24
25 func (c multiClipboard) getText(r Register, num int) string {
26         content := c[r]
27         if content == nil || len(content) <= num {
28                 return ""
29         }
30
31         return content[num]
32 }
33
34 // isValid checks if the text stored in this multi-clipboard is the same as the
35 // text stored in the system clipboard (provided as an argument), and therefore
36 // if it is safe to use the multi-clipboard for pasting instead of the system
37 // clipboard.
38 func (c multiClipboard) isValid(r Register, clipboard string, ncursors int) bool {
39         content := c[r]
40         if content == nil || len(content) != ncursors {
41                 return false
42         }
43
44         return clipboard == c.getAllText(r)
45 }
46
47 func (c multiClipboard) writeText(text string, r Register, num int, ncursors int) {
48         content := c[r]
49         if content == nil || len(content) != ncursors {
50                 content = make([]string, ncursors, ncursors)
51                 c[r] = content
52         }
53
54         if num >= ncursors {
55                 return
56         }
57
58         content[num] = text
59 }
60
61 func init() {
62         multi = make(multiClipboard)
63 }