Skip to content

Commit 731bfca

Browse files
committed
Fix ctrl+d not working properly closes #193
1 parent a119e31 commit 731bfca

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

internal/server/commands/connect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ func attachSession(newSession ssh.Channel, currentClientSession io.ReadWriter, c
158158

159159
close := func() {
160160
newSession.Close()
161-
finished <- true // Stop the request passer on IO error
161+
close(finished) // Stop the request passer on IO error
162162
}
163163

164164
//Setup the pipes for stdin/stdout over the connections

internal/terminal/terminal.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ type Terminal struct {
124124
autoCompleteValues map[string][]*trie.Trie
125125

126126
raw bool
127+
128+
rawOverflow chan []byte
127129
}
128130

129131
func (t *Terminal) EnableRaw() {
@@ -141,7 +143,10 @@ func (t *Terminal) DisableRaw() {
141143
defer t.lock.Unlock()
142144

143145
if t.raw {
146+
t.rawOverflow = make(chan []byte, 1)
147+
144148
t.raw = false
149+
145150
t.handleWindowSize()
146151
}
147152
}
@@ -493,7 +498,6 @@ func (t *Terminal) removeDuplicates(stringsSlice []string) []string {
493498

494499
func (t *Terminal) Run() error {
495500
for {
496-
//This will break if the user does CTRL+D apparently we need to reset the whole terminal if a user does this.... so just exit instead
497501
line, err := t.ReadLine()
498502
if err != nil {
499503
return err
@@ -646,9 +650,12 @@ func (t *Terminal) Read(b []byte) (n int, err error) {
646650

647651
if t.raw {
648652
n, err := t.c.Read(b)
649-
if !t.raw {
650-
//This is a patch due to blocking reads
651-
t.addCharacterToInput(b)
653+
//This is a patch due to blocking reads
654+
if !t.raw && t.rawOverflow != nil {
655+
656+
c := make([]byte, n)
657+
copy(c, b[:n])
658+
t.rawOverflow <- c
652659
}
653660

654661
return n, err
@@ -681,10 +688,10 @@ func (t *Terminal) addCharacterToInput(characters []byte) {
681688
t.lock.Lock()
682689
defer t.lock.Unlock()
683690

684-
r, _ := bytesToKey(characters, false)
685-
t.handleKey(r)
686-
t.c.Write(t.outBuf)
687-
t.outBuf = t.outBuf[:0]
691+
log.Println("adding: ", characters)
692+
693+
n := copy(t.inBuf[:], characters[:min(len(characters)-1):256])
694+
t.remainder = t.inBuf[:n]
688695
}
689696

690697
func (t *Terminal) advanceCursor(places int) {
@@ -1111,16 +1118,29 @@ func (t *Terminal) readLine() (line string, err error) {
11111118
}
11121119

11131120
lineIsPasted := t.pasteActive
1121+
if t.rawOverflow != nil {
1122+
data, ok := <-t.rawOverflow
1123+
if ok {
1124+
n := copy(t.inBuf[:], data)
1125+
t.remainder = t.inBuf[:n]
1126+
close(t.rawOverflow)
1127+
}
1128+
t.rawOverflow = nil
1129+
}
11141130

11151131
for {
1132+
11161133
rest := t.remainder
11171134
lineOk := false
11181135
for !lineOk {
1136+
11191137
var key rune
11201138
key, rest = bytesToKey(rest, t.pasteActive)
1139+
11211140
if key == utf8.RuneError {
11221141
break
11231142
}
1143+
11241144
if !t.pasteActive {
11251145
if key == keyCtrlD {
11261146
if len(t.line) == 0 {
@@ -1152,6 +1172,7 @@ func (t *Terminal) readLine() (line string, err error) {
11521172
}
11531173

11541174
t.c.Write(t.outBuf)
1175+
11551176
t.outBuf = t.outBuf[:0]
11561177
if lineOk {
11571178
if t.echo {
@@ -1173,7 +1194,9 @@ func (t *Terminal) readLine() (line string, err error) {
11731194
var n int
11741195

11751196
t.lock.Unlock()
1197+
11761198
n, err = t.c.Read(readBuf)
1199+
11771200
t.lock.Lock()
11781201

11791202
if err != nil {

0 commit comments

Comments
 (0)