-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathterminal_windows.go
More file actions
84 lines (67 loc) · 1.34 KB
/
terminal_windows.go
File metadata and controls
84 lines (67 loc) · 1.34 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
//go:build windows
// +build windows
/* SPDX-License-Identifier: MIT */
/*
* Author: Jianhui Zhao <zhaojh329@gmail.com>
*/
package main
import (
"context"
"sync"
"sync/atomic"
conpty "github.com/qsocket/conpty-go"
)
type Terminal struct {
pty *conpty.ConPty
wait_ack atomic.Int32
cond *sync.Cond
ack_block int32
closeOnce sync.Once
}
func NewTerminal(username string) (*Terminal, error) {
pty, err := conpty.Start("cmd.exe")
if err != nil {
return nil, err
}
t := &Terminal{
pty: pty,
ack_block: 4096,
cond: sync.NewCond(&sync.Mutex{}),
}
go func() {
pty.Wait(context.Background())
t.Close()
}()
return t, nil
}
func (t *Terminal) Read(buf []byte) (int, error) {
return t.pty.Read(buf)
}
func (t *Terminal) Write(data []byte) (int, error) {
return t.pty.Write(data)
}
func (t *Terminal) SetWinSize(cols, rows uint16) error {
return t.pty.Resize(int(cols), int(rows))
}
func (t *Terminal) Close() error {
t.closeOnce.Do(func() {
t.wait_ack.Store(0)
t.cond.Signal()
t.pty.Close()
})
return nil
}
func (t *Terminal) Ack(n uint16) {
t.wait_ack.Add(-int32(n))
t.cond.Signal()
}
func (t *Terminal) WaitAck(len int) {
newWaitAck := t.wait_ack.Add(int32(len))
if newWaitAck > t.ack_block {
t.cond.L.Lock()
for t.wait_ack.Load() > t.ack_block {
t.cond.Wait()
}
t.cond.L.Unlock()
}
}