Skip to content

Commit 49cf736

Browse files
authored
Merge pull request #87 from Napche/issue-46-windows-multichoice-bug
Replace all readlineGetSize(0) instances with os.Stdout.Fd
2 parents dcb76b0 + a1801e5 commit 49cf736

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

Diff for: ishell.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -481,24 +481,17 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
481481
cur = selected[len(selected)-1]
482482
}
483483

484-
_, curRow, err := getPosition()
484+
fd := int(os.Stdout.Fd())
485+
_, maxRows, err := readline.GetSize(fd)
485486
if err != nil {
486487
return nil
487488
}
488489

489-
_, maxRows, err := readline.GetSize(0)
490-
if err != nil {
491-
return nil
492-
}
493-
494-
// allocate some space to be at the top of the screen
495-
s.Printf("\033[%dS", curRow)
496-
497490
// move cursor to the top
498491
// TODO it happens on every update, however, some trash appears in history without this line
499492
s.Print("\033[0;0H")
500493

501-
offset := 0
494+
offset := fd
502495

503496
update := func() {
504497
strs := buildOptionsStrings(options, selected, cur)
@@ -521,7 +514,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
521514
offset++
522515
}
523516
if cur >= len(options) {
524-
offset = 0
517+
offset = fd
525518
cur = 0
526519
}
527520
} else if key == -1 {
@@ -533,7 +526,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
533526
if len(options) > maxRows-1 {
534527
offset = len(options) - maxRows + 1
535528
} else {
536-
offset = 0
529+
offset = fd
537530
}
538531
cur = len(options) - 1
539532
}
@@ -563,7 +556,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
563556
case <-refresh:
564557
update()
565558
case <-t.C:
566-
_, rows, _ := readline.GetSize(0)
559+
_, rows, _ := readline.GetSize(fd)
567560
if maxRows != rows {
568561
maxRows = rows
569562
update()
@@ -659,11 +652,12 @@ func copyShellProgressBar(s *Shell) ProgressBar {
659652
}
660653

661654
func getPosition() (int, int, error) {
662-
state, err := readline.MakeRaw(0)
655+
fd := int(os.Stdout.Fd())
656+
state, err := readline.MakeRaw(fd)
663657
if err != nil {
664658
return 0, 0, err
665659
}
666-
defer readline.Restore(0, state)
660+
defer readline.Restore(fd, state)
667661
fmt.Printf("\033[6n")
668662
var out string
669663
reader := bufio.NewReader(os.Stdin)

0 commit comments

Comments
 (0)