Skip to content

Commit

Permalink
Merge pull request #87 from Napche/issue-46-windows-multichoice-bug
Browse files Browse the repository at this point in the history
Replace all readlineGetSize(0) instances with os.Stdout.Fd
  • Loading branch information
abiosoft authored Apr 10, 2018
2 parents dcb76b0 + a1801e5 commit 49cf736
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions ishell.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,24 +481,17 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
cur = selected[len(selected)-1]
}

_, curRow, err := getPosition()
fd := int(os.Stdout.Fd())
_, maxRows, err := readline.GetSize(fd)
if err != nil {
return nil
}

_, maxRows, err := readline.GetSize(0)
if err != nil {
return nil
}

// allocate some space to be at the top of the screen
s.Printf("\033[%dS", curRow)

// move cursor to the top
// TODO it happens on every update, however, some trash appears in history without this line
s.Print("\033[0;0H")

offset := 0
offset := fd

update := func() {
strs := buildOptionsStrings(options, selected, cur)
Expand All @@ -521,7 +514,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
offset++
}
if cur >= len(options) {
offset = 0
offset = fd
cur = 0
}
} else if key == -1 {
Expand All @@ -533,7 +526,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
if len(options) > maxRows-1 {
offset = len(options) - maxRows + 1
} else {
offset = 0
offset = fd
}
cur = len(options) - 1
}
Expand Down Expand Up @@ -563,7 +556,7 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
case <-refresh:
update()
case <-t.C:
_, rows, _ := readline.GetSize(0)
_, rows, _ := readline.GetSize(fd)
if maxRows != rows {
maxRows = rows
update()
Expand Down Expand Up @@ -659,11 +652,12 @@ func copyShellProgressBar(s *Shell) ProgressBar {
}

func getPosition() (int, int, error) {
state, err := readline.MakeRaw(0)
fd := int(os.Stdout.Fd())
state, err := readline.MakeRaw(fd)
if err != nil {
return 0, 0, err
}
defer readline.Restore(0, state)
defer readline.Restore(fd, state)
fmt.Printf("\033[6n")
var out string
reader := bufio.NewReader(os.Stdin)
Expand Down

0 comments on commit 49cf736

Please sign in to comment.