-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from j0hax/asynchronous-loading
Implement asynchronous loading with fancy new status bar
- Loading branch information
Showing
6 changed files
with
190 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package statusbar | ||
|
||
import ( | ||
"github.com/gdamore/tcell/v2" | ||
"github.com/rivo/tview" | ||
) | ||
|
||
// Blur implements tview.Primitive. | ||
func (s *StatusBar) Blur() { | ||
s.field.Blur() | ||
} | ||
|
||
// Draw implements tview.Primitive. | ||
func (s *StatusBar) Draw(screen tcell.Screen) { | ||
s.field.Draw(screen) | ||
} | ||
|
||
// Focus implements tview.Primitive. | ||
func (s *StatusBar) Focus(delegate func(p tview.Primitive)) { | ||
s.field.Focus(delegate) | ||
} | ||
|
||
// GetRect implements tview.Primitive. | ||
func (s *StatusBar) GetRect() (int, int, int, int) { | ||
return s.field.GetRect() | ||
} | ||
|
||
// HasFocus implements tview.Primitive. | ||
func (s *StatusBar) HasFocus() bool { | ||
return s.field.HasFocus() | ||
} | ||
|
||
// InputHandler implements tview.Primitive. | ||
func (s *StatusBar) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) { | ||
return s.field.InputHandler() | ||
} | ||
|
||
// MouseHandler implements tview.Primitive. | ||
func (s *StatusBar) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) { | ||
return s.field.MouseHandler() | ||
} | ||
|
||
// SetRect implements tview.Primitive. | ||
func (s *StatusBar) SetRect(x int, y int, width int, height int) { | ||
s.field.SetRect(x, y, width, height) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package statusbar | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"github.com/rivo/tview" | ||
) | ||
|
||
type StatusBar struct { | ||
app *tview.Application | ||
field *tview.InputField | ||
loadingDone chan bool | ||
mutex sync.Mutex | ||
} | ||
|
||
func NewStatusBar(application *tview.Application) *StatusBar { | ||
i := tview.NewInputField() | ||
i.SetDisabled(true) | ||
return &StatusBar{ | ||
app: application, | ||
field: i, | ||
loadingDone: make(chan bool), | ||
} | ||
} | ||
|
||
func (f *StatusBar) setLabel(s string) { | ||
f.mutex.Lock() | ||
defer f.mutex.Unlock() | ||
f.field.SetLabelWidth(len(s) + 1) | ||
f.field.SetLabel(s) | ||
} | ||
|
||
func (f *StatusBar) setMessage(items ...string) { | ||
message := strings.Join(items, " ") | ||
|
||
f.mutex.Lock() | ||
defer f.mutex.Unlock() | ||
f.field.SetText(message) | ||
} | ||
|
||
// StartLoading displays a loading animation with a specified message. | ||
// | ||
// The animation stops as soon as DoneLoading is called. | ||
func (f *StatusBar) StartLoading(message string) { | ||
f.setLabel("Loading") | ||
go func() { | ||
count := 0 | ||
ticker := time.NewTicker(100 * time.Millisecond) | ||
for { | ||
select { | ||
case <-f.loadingDone: | ||
ticker.Stop() | ||
f.setLabel("Finished Loading") | ||
f.setMessage(message) | ||
f.app.Draw() | ||
return | ||
case <-ticker.C: | ||
lbl := fmt.Sprintf("%s%s", message, strings.Repeat(".", count%4)) | ||
f.setMessage(lbl) | ||
f.app.Draw() | ||
count += 1 | ||
} | ||
} | ||
}() | ||
} | ||
|
||
// DoneLoading is called after calling StartLoading to indicate that the loading process has finished. | ||
func (f *StatusBar) DoneLoading() { | ||
select { | ||
case f.loadingDone <- true: | ||
return | ||
default: | ||
panic("Did not call StartLoading()!") | ||
} | ||
} |