/
screen.go
84 lines (62 loc) · 1.46 KB
/
screen.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"os"
"golang.org/x/crypto/ssh/terminal"
"fmt"
"strings"
"unicode/utf8"
)
type Screen interface {
Draw(i *ClipboardItem) error
}
type TerminalScreen struct {
TerminalWidth int
}
func NewTerminalScreen() (*TerminalScreen, error) {
s := &TerminalScreen{}
s.TerminalWidth = s.getWindowWidth()
s.init()
return s, nil
}
func (s *TerminalScreen) init() {
sideLength := (s.TerminalWidth - len(AppName)) / 2
fmt.Printf("%s%s%s\n", strings.Repeat("=", sideLength), "Goppy", strings.Repeat("=", sideLength))
}
func (s *TerminalScreen) Draw(i *ClipboardItem) error {
if ! utf8.Valid(i.Contents) {
fmt.Printf("<Cannot display binary data>\n")
} else {
fmt.Printf("%s\n", i.Contents)
}
fmt.Printf("%s\n", strings.Repeat("=", s.TerminalWidth))
return nil
}
func (s *TerminalScreen) getWindowWidth() int {
w, _, err := terminal.GetSize(int(os.Stdin.Fd()))
if err != nil {
w = 0
}
return MaxInt(w, HeaderLength)
}
type NoScreen struct {}
func (NoScreen) Draw(i *ClipboardItem) error {
// Pass
return nil
}
func truncateString(s string, maxLen, maxLines int) string {
originalLength := len(s)
splitLines := strings.Split(s, "\n")[:maxLines]
s = strings.Join(splitLines, "\n")
maxLen = maxAcceptableLength(s, maxLen)
// Nothing was truncated
if len(s) == originalLength {
return s
}
return s[:maxLen] + TruncatedSuffix
}
func maxAcceptableLength(s string, maxLen int) int {
if len(s) > maxLen {
return maxLen
}
return len(s)
}