Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiChoice: refresh only if it is necessary #75

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ishell.go
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
return nil
}

_, maxRows, err := readline.GetSize(0)
stdoutFd := int(os.Stdout.Fd())
_, maxRows, err := readline.GetSize(stdoutFd)
if err != nil {
return nil
}
Expand Down Expand Up @@ -533,13 +534,17 @@ func (s *Shell) multiChoice(options []string, text string, init []int, multiResu
if multiResults {
selected = toggle(selected, cur)
}
} else {
return
}
refresh <- struct{}{}
return
}
conf.Listener = readline.FuncListener(listener)
oldconf := s.reader.scanner.SetConfig(conf)

update()

stop := make(chan struct{})
defer func() {
stop <- struct{}{}
Expand All @@ -555,7 +560,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(stdoutFd)
if maxRows != rows {
maxRows = rows
update()
Expand Down
16 changes: 12 additions & 4 deletions progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,25 @@ func (p *progressBarImpl) write(s string) error {
}

func (p *progressBarImpl) erase(n int) {
for i := 0; i < n; i++ {
p.writer.Write([]byte{'\b'})
// Don't use '\b' - it's only move cursor to back
b := make([]byte, 0, n+2)
b = append(b, '\r')
for i := 0; i < n; i ++ {
b = append(b, ' ')
}
b = append(b, '\r')
p.writer.Write(b)
}

func (p *progressBarImpl) done() {
p.wMutex.Lock()
defer p.wMutex.Unlock()

p.erase(p.writtenLen)
fmt.Fprintln(p.writer, p.final)
if p.final != "" {
p.erase(p.writtenLen)
fmt.Fprintln(p.writer, p.final)
}

}

func (p *progressBarImpl) output() string {
Expand Down