[#1223] lens/tui: Refactor UI
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
parent
8ddea4b8b2
commit
72fae1434d
2 changed files with 39 additions and 22 deletions
|
@ -12,7 +12,7 @@ import (
|
|||
|
||||
type RecordsView struct {
|
||||
*tview.Box
|
||||
ui *UI
|
||||
|
||||
onUnmount func()
|
||||
|
||||
bucket *Bucket
|
||||
|
@ -24,14 +24,15 @@ type RecordsView struct {
|
|||
lastRecordIndex int
|
||||
selectedRecordIndex int
|
||||
|
||||
ui *UI
|
||||
filter *Filter
|
||||
}
|
||||
|
||||
func NewRecordsView(ui *UI, bucket *Bucket, filter *Filter) *RecordsView {
|
||||
return &RecordsView{
|
||||
Box: tview.NewBox(),
|
||||
ui: ui,
|
||||
bucket: bucket,
|
||||
ui: ui,
|
||||
filter: filter,
|
||||
}
|
||||
}
|
||||
|
@ -83,33 +84,49 @@ func (v *RecordsView) Unmount() {
|
|||
v.onUnmount = nil
|
||||
}
|
||||
|
||||
func (v *RecordsView) Update(_ context.Context) error {
|
||||
// TODO: make update `commit or rollback`
|
||||
_, _, _, recordsPerPage := v.GetInnerRect()
|
||||
func (v *RecordsView) Update(ctx context.Context) error {
|
||||
var newRecords []*Record
|
||||
|
||||
newLastRecordIndex := v.firstRecordIndex + recordsPerPage
|
||||
ready := make(chan struct{})
|
||||
go func() {
|
||||
close(ready)
|
||||
|
||||
// The terminal's been resized and become shorter.
|
||||
if v.lastRecordIndex > newLastRecordIndex {
|
||||
v.lastRecordIndex = newLastRecordIndex
|
||||
_, _, _, recordsPerPage := v.GetInnerRect()
|
||||
newLastRecordIndex := v.firstRecordIndex + recordsPerPage
|
||||
|
||||
// If the selected record is invisible, select the last one visible.
|
||||
v.selectedRecordIndex = min(v.selectedRecordIndex, v.lastRecordIndex-1)
|
||||
return nil
|
||||
}
|
||||
// The terminal was resized and became shorter.
|
||||
if v.lastRecordIndex > newLastRecordIndex {
|
||||
v.lastRecordIndex = newLastRecordIndex
|
||||
|
||||
for len(v.records) < newLastRecordIndex {
|
||||
record, ok := <-v.buffer
|
||||
if !ok {
|
||||
break
|
||||
// If the selected record is invisible, select the last one visible.
|
||||
v.selectedRecordIndex = min(v.selectedRecordIndex, v.lastRecordIndex-1)
|
||||
return
|
||||
}
|
||||
v.records = append(v.records, record)
|
||||
|
||||
for len(v.records) < newLastRecordIndex {
|
||||
record, ok := <-v.buffer
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
newRecords = append(newRecords, record)
|
||||
}
|
||||
v.lastRecordIndex = min(len(v.records), newLastRecordIndex)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
case <-ready:
|
||||
v.records = append(v.records, newRecords...)
|
||||
}
|
||||
v.lastRecordIndex = min(len(v.records), newLastRecordIndex)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *RecordsView) GetInnerRect() (int, int, int, int) {
|
||||
x, y, width, height := v.Box.GetInnerRect()
|
||||
return x + 3, y + 1, width - 3, height - 1
|
||||
}
|
||||
|
||||
func (v *RecordsView) Draw(screen tcell.Screen) {
|
||||
x, y, width, height := v.GetInnerRect()
|
||||
|
||||
|
@ -133,7 +150,7 @@ func (v *RecordsView) Draw(screen tcell.Screen) {
|
|||
text := result.String()
|
||||
|
||||
if index == v.selectedRecordIndex {
|
||||
text = fmt.Sprintf("[:white]%s[:black]", text)
|
||||
text = fmt.Sprintf("[:white]%s[:-]", text)
|
||||
tview.Print(screen, text, x, y, width, tview.AlignLeft, tview.Styles.PrimitiveBackgroundColor)
|
||||
} else {
|
||||
tview.Print(screen, text, x, y, width, tview.AlignLeft, tview.Styles.PrimaryTextColor)
|
||||
|
|
|
@ -311,7 +311,7 @@ func (ui *UI) mountAndUpdate(ctx context.Context) {
|
|||
}
|
||||
|
||||
x, y, width, height := ui.GetInnerRect()
|
||||
ui.pageToMount.SetRect(x, y, width, height)
|
||||
ui.pageToMount.SetRect(x, y, width, height-1)
|
||||
|
||||
s = loadOp(ctx, ui.pageToMount.Update)
|
||||
if s.err != nil {
|
||||
|
@ -332,7 +332,7 @@ func (ui *UI) mountAndUpdate(ctx context.Context) {
|
|||
|
||||
func (ui *UI) update(ctx context.Context) {
|
||||
x, y, width, height := ui.GetInnerRect()
|
||||
ui.mountedPage.SetRect(x, y, width, height)
|
||||
ui.mountedPage.SetRect(x, y, width, height-1)
|
||||
|
||||
s := loadOp(ctx, ui.mountedPage.Update)
|
||||
if s.err != nil {
|
||||
|
|
Loading…
Reference in a new issue