Add small initial delay to screen update

To avoid flickering when the input is small
This commit is contained in:
Junegunn Choi
2015-01-07 12:46:45 +09:00
parent 3e129ac68c
commit f99f66570b

View File

@@ -31,6 +31,7 @@ type Terminal struct {
eventBox *EventBox eventBox *EventBox
mutex sync.Mutex mutex sync.Mutex
initFunc func() initFunc func()
suppress bool
} }
var _spinner []string = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`} var _spinner []string = []string{`-`, `\`, `|`, `/`, `-`, `\`, `|`, `/`}
@@ -39,11 +40,17 @@ const (
REQ_PROMPT EventType = iota REQ_PROMPT EventType = iota
REQ_INFO REQ_INFO
REQ_LIST REQ_LIST
REQ_REFRESH
REQ_REDRAW REQ_REDRAW
REQ_CLOSE REQ_CLOSE
REQ_QUIT REQ_QUIT
) )
const (
INITIAL_DELAY = 100 * time.Millisecond
SPINNER_DURATION = 200 * time.Millisecond
)
func NewTerminal(opts *Options, eventBox *EventBox) *Terminal { func NewTerminal(opts *Options, eventBox *EventBox) *Terminal {
input := []rune(opts.Query) input := []rune(opts.Query)
return &Terminal{ return &Terminal{
@@ -62,6 +69,7 @@ func NewTerminal(opts *Options, eventBox *EventBox) *Terminal {
reqBox: NewEventBox(), reqBox: NewEventBox(),
eventBox: eventBox, eventBox: eventBox,
mutex: sync.Mutex{}, mutex: sync.Mutex{},
suppress: true,
initFunc: func() { initFunc: func() {
C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse) C.Init(opts.Color, opts.Color256, opts.Black, opts.Mouse)
}} }}
@@ -79,6 +87,9 @@ func (t *Terminal) UpdateCount(cnt int, final bool) {
t.reading = !final t.reading = !final
t.mutex.Unlock() t.mutex.Unlock()
t.reqBox.Set(REQ_INFO, nil) t.reqBox.Set(REQ_INFO, nil)
if final {
t.reqBox.Set(REQ_REFRESH, nil)
}
} }
func (t *Terminal) UpdateProgress(progress float32) { func (t *Terminal) UpdateProgress(progress float32) {
@@ -158,7 +169,7 @@ func (t *Terminal) printPrompt() {
func (t *Terminal) printInfo() { func (t *Terminal) printInfo() {
t.move(1, 0, true) t.move(1, 0, true)
if t.reading { if t.reading {
duration := int64(200) * int64(time.Millisecond) duration := int64(SPINNER_DURATION)
idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration idx := (time.Now().UnixNano() % (duration * int64(len(_spinner)))) / duration
C.CPrint(C.COL_SPINNER, true, _spinner[idx]) C.CPrint(C.COL_SPINNER, true, _spinner[idx])
} }
@@ -297,8 +308,10 @@ func (t *Terminal) printAll() {
} }
func (t *Terminal) refresh() { func (t *Terminal) refresh() {
if !t.suppress {
C.Refresh() C.Refresh()
} }
}
func (t *Terminal) delChar() bool { func (t *Terminal) delChar() bool {
if len(t.input) > 0 && t.cx < len(t.input) { if len(t.input) > 0 && t.cx < len(t.input) {
@@ -350,11 +363,16 @@ func (t *Terminal) Loop() {
{ // Late initialization { // Late initialization
t.mutex.Lock() t.mutex.Lock()
t.initFunc() t.initFunc()
t.printInfo()
t.printPrompt() t.printPrompt()
t.placeCursor() t.placeCursor()
t.refresh() C.Refresh()
t.printInfo()
t.mutex.Unlock() t.mutex.Unlock()
go func() {
timer := time.NewTimer(INITIAL_DELAY)
<-timer.C
t.reqBox.Set(REQ_REFRESH, nil)
}()
} }
go func() { go func() {
@@ -370,6 +388,8 @@ func (t *Terminal) Loop() {
t.printInfo() t.printInfo()
case REQ_LIST: case REQ_LIST:
t.printList() t.printList()
case REQ_REFRESH:
t.suppress = false
case REQ_REDRAW: case REQ_REDRAW:
C.Clear() C.Clear()
t.printAll() t.printAll()