-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclipty.go
More file actions
89 lines (74 loc) · 1.49 KB
/
Copy pathclipty.go
File metadata and controls
89 lines (74 loc) · 1.49 KB
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
85
86
87
88
89
package clipty
import (
"context"
"net/url"
"os"
"github.com/creack/pty"
)
type Std struct {
Stdin *os.File
Stdout *os.File
Stderr *os.File
}
// MainFunc is CLI main loop function.
type MainFunc func(ctx context.Context, params url.Values, std Std)
type clipty struct {
params map[string][]string
pty *os.File
tty *os.File
cancel context.CancelFunc
}
func newClipty(params url.Values, mf MainFunc) (*clipty, error) {
pty, tty, err := pty.Open()
if err != nil {
return nil, err
}
ctx, cancel := context.WithCancel(context.Background())
c := &clipty{
params: params,
pty: pty,
tty: tty,
cancel: cancel,
}
// When the process is closed by the user,
// close pty so that Read() on the pty breaks with an EOF.
go func() {
defer func() {
tty.Sync()
c.Close()
}()
mf(ctx, params, Std{tty, tty, tty})
}()
return c, nil
}
func (c *clipty) Read(p []byte) (n int, err error) {
return c.pty.Read(p)
}
func (c *clipty) Write(p []byte) (n int, err error) {
return c.pty.Write(p)
}
func (c *clipty) Close() error {
c.cancel()
c.tty.Close()
return c.pty.Close()
}
func (c *clipty) WindowTitleVariables() map[string]interface{} {
return map[string]interface{}{
//"command": c.command,
"params": c.params,
}
}
func (c *clipty) ResizeTerminal(width int, height int) error {
window := pty.Winsize{
Rows: uint16(height),
Cols: uint16(width),
X: 0,
Y: 0,
}
err := pty.Setsize(c.pty, &window)
if err != nil {
return err
} else {
return nil
}
}