/
main.go
171 lines (151 loc) · 3.56 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"io"
"log"
"os"
"syscall"
"github.com/gliderlabs/ssh"
"github.com/kr/pty"
"github.com/progrium/tcell"
"github.com/mattn/go-runewidth"
)
var defStyle tcell.Style
type sshDriver struct {
winWidth int
winHeight int
winCh chan os.Signal
ptmx *os.File
}
func (d *sshDriver) Init(winCh chan os.Signal) (*os.File, *os.File, error) {
ptmx, tty, err := pty.Open()
if err != nil {
return nil, nil, err
}
d.ptmx = ptmx
d.winCh = winCh
return tty, tty, nil
}
func (d *sshDriver) Fini() {}
func (d *sshDriver) WinSize() (int, int, error) {
return d.winWidth, d.winHeight, nil
}
func emitStr(s tcell.Screen, x, y int, style tcell.Style, str string) {
for _, c := range str {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
for col := x1; col <= x2; col++ {
s.SetContent(col, y1, tcell.RuneHLine, nil, style)
s.SetContent(col, y2, tcell.RuneHLine, nil, style)
}
for row := y1 + 1; row < y2; row++ {
s.SetContent(x1, row, tcell.RuneVLine, nil, style)
s.SetContent(x2, row, tcell.RuneVLine, nil, style)
}
if y1 != y2 && x1 != x2 {
// Only add corners if we need to
s.SetContent(x1, y1, tcell.RuneULCorner, nil, style)
s.SetContent(x2, y1, tcell.RuneURCorner, nil, style)
s.SetContent(x1, y2, tcell.RuneLLCorner, nil, style)
s.SetContent(x2, y2, tcell.RuneLRCorner, nil, style)
}
for row := y1 + 1; row < y2; row++ {
for col := x1 + 1; col < x2; col++ {
s.SetContent(col, row, r, nil, style)
}
}
}
type DriverSetter interface {
SetDriver(tcell.TermDriver)
}
func main() {
ssh.Handle(func(s ssh.Session) {
sshPty, winCh, isPty := s.Pty()
if !isPty {
return
}
driver := &sshDriver{}
driver.winWidth = sshPty.Window.Width
driver.winHeight = sshPty.Window.Height
screen, e := tcell.NewTerminfoScreen()
if e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
return
}
dset, ok := screen.(DriverSetter)
if !ok {
log.Fatal("Unable to set tcell driver")
return
}
dset.SetDriver(driver)
if e := screen.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
return
}
go termLoop(screen, s)
go func() {
for win := range winCh {
driver.winWidth = win.Width
driver.winHeight = win.Height
driver.winCh <- syscall.SIGWINCH
}
}()
go func() { _, _ = io.Copy(driver.ptmx, s) }()
_, _ = io.Copy(s, driver.ptmx)
})
log.Fatal(ssh.ListenAndServe(":2222", nil))
}
func termLoop(s tcell.Screen, sess ssh.Session) {
defStyle = tcell.StyleDefault.
Background(tcell.ColorBlack).
Foreground(tcell.ColorWhite)
s.SetStyle(defStyle)
s.Clear()
white := tcell.StyleDefault.
Foreground(tcell.ColorWhite).Background(tcell.ColorRed)
w, h := s.Size()
for {
drawBox(s, 1, 1, 42, 6, white, ' ')
emitStr(s, 2, 2, white, "Press ESC to exit, C to clear.")
s.Show()
ev := s.PollEvent()
st := tcell.StyleDefault.Background(tcell.ColorRed)
w, h = s.Size()
switch ev := ev.(type) {
case *tcell.EventResize:
s.Sync()
s.SetContent(w-1, h-1, 'R', nil, st)
case *tcell.EventKey:
s.SetContent(w-2, h-2, ev.Rune(), nil, st)
s.SetContent(w-1, h-1, 'K', nil, st)
if ev.Key() == tcell.KeyEscape {
s.Fini()
sess.Exit(0)
} else if ev.Key() == tcell.KeyCtrlL {
s.Sync()
} else {
if ev.Rune() == 'C' || ev.Rune() == 'c' {
s.Clear()
}
}
default:
s.SetContent(w-1, h-1, 'X', nil, st)
}
}
}