File: multi.go

package info (click to toggle)
micro 2.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: sh: 265; makefile: 77; xml: 53
file content (63 lines) | stat: -rw-r--r-- 1,313 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package clipboard

import (
	"bytes"
)

// For storing multi cursor clipboard contents
type multiClipboard map[Register][]string

var multi multiClipboard

func (c multiClipboard) getAllText(r Register) string {
	content := c[r]
	if content == nil {
		return ""
	}

	buf := &bytes.Buffer{}
	for _, s := range content {
		buf.WriteString(s)
	}
	return buf.String()
}

func (c multiClipboard) getText(r Register, num int) string {
	content := c[r]
	if content == nil || len(content) <= num {
		return ""
	}

	return content[num]
}

// isValid checks if the text stored in this multi-clipboard is the same as the
// text stored in the system clipboard (provided as an argument), and therefore
// if it is safe to use the multi-clipboard for pasting instead of the system
// clipboard.
func (c multiClipboard) isValid(r Register, clipboard string, ncursors int) bool {
	content := c[r]
	if content == nil || len(content) != ncursors {
		return false
	}

	return clipboard == c.getAllText(r)
}

func (c multiClipboard) writeText(text string, r Register, num int, ncursors int) {
	content := c[r]
	if content == nil || len(content) != ncursors {
		content = make([]string, ncursors, ncursors)
		c[r] = content
	}

	if num >= ncursors {
		return
	}

	content[num] = text
}

func init() {
	multi = make(multiClipboard)
}