Skip to content

Commit

Permalink
Return error on update callback
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelmota committed Dec 30, 2019
1 parent 38187de commit ca1987a
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 26 deletions.
10 changes: 6 additions & 4 deletions cointop/chart.go
Expand Up @@ -91,13 +91,14 @@ func (ct *Cointop) UpdateChart() error {
}
}

ct.Update(func() {
ct.Update(func() error {
if ct.Views.Chart.Backing() == nil {
return
return nil
}

ct.Views.Chart.Backing().Clear()
fmt.Fprint(ct.Views.Chart.Backing(), ct.colorscheme.Chart(body))
return nil
})

return nil
Expand Down Expand Up @@ -424,14 +425,15 @@ func (ct *Cointop) ToggleCoinChart() error {
// ShowChartLoader shows chart loading indicator
func (ct *Cointop) ShowChartLoader() error {
ct.debuglog("ShowChartLoader()")
ct.Update(func() {
ct.Update(func() error {
if ct.Views.Chart.Backing() == nil {
return
return nil
}

content := "\n\nLoading..."
ct.Views.Chart.Backing().Clear()
fmt.Fprint(ct.Views.Chart.Backing(), ct.colorscheme.Chart(content))
return nil
})

return nil
Expand Down
10 changes: 6 additions & 4 deletions cointop/conversion.go
Expand Up @@ -174,14 +174,15 @@ func (ct *Cointop) updateConvertMenu() {
}

content := fmt.Sprintf("%s%s%s", header, helpline, body)
ct.Update(func() {
ct.Update(func() error {
if ct.Views.ConvertMenu.Backing() == nil {
return
return nil
}

ct.Views.ConvertMenu.Backing().Clear()
ct.Views.ConvertMenu.Backing().Frame = true
fmt.Fprintln(ct.Views.ConvertMenu.Backing(), content)
return nil
})
}

Expand Down Expand Up @@ -230,14 +231,15 @@ func (ct *Cointop) hideConvertMenu() error {
ct.State.convertMenuVisible = false
ct.SetViewOnBottom(ct.Views.ConvertMenu.Name())
ct.SetActiveView(ct.Views.Table.Name())
ct.Update(func() {
ct.Update(func() error {
if ct.Views.ConvertMenu.Backing() == nil {
return
return nil
}

ct.Views.ConvertMenu.Backing().Clear()
ct.Views.ConvertMenu.Backing().Frame = false
fmt.Fprintln(ct.Views.ConvertMenu.Backing(), "")
return nil
})
return nil
}
Expand Down
10 changes: 6 additions & 4 deletions cointop/help.go
Expand Up @@ -58,14 +58,15 @@ func (ct *Cointop) updateHelp() {
versionline := pad.Left(fmt.Sprintf("v%s", ct.Version()), ct.maxTableWidth-5, " ")
content := header + infoline + body + versionline

ct.Update(func() {
ct.Update(func() error {
if ct.Views.Help.Backing() == nil {
return
return nil
}

ct.Views.Help.Backing().Clear()
ct.Views.Help.Backing().Frame = true
fmt.Fprintln(ct.Views.Help.Backing(), content)
return nil
})
}

Expand All @@ -82,14 +83,15 @@ func (ct *Cointop) hideHelp() error {
ct.State.helpVisible = false
ct.SetViewOnBottom(ct.Views.Help.Name())
ct.SetActiveView(ct.Views.Table.Name())
ct.Update(func() {
ct.Update(func() error {
if ct.Views.Help.Backing() == nil {
return
return nil
}

ct.Views.Help.Backing().Clear()
ct.Views.Help.Backing().Frame = false
fmt.Fprintln(ct.Views.Help.Backing(), "")
return nil
})
return nil
}
Expand Down
5 changes: 3 additions & 2 deletions cointop/marketbar.go
Expand Up @@ -143,13 +143,14 @@ func (ct *Cointop) updateMarketbar() error {
content = pad.Right(content, maxX, " ")
content = ct.colorscheme.Marketbar(content)

ct.Update(func() {
ct.Update(func() error {
if ct.Views.Marketbar.Backing() == nil {
return
return nil
}

ct.Views.Marketbar.Backing().Clear()
fmt.Fprintln(ct.Views.Marketbar.Backing(), content)
return nil
})

return nil
Expand Down
9 changes: 6 additions & 3 deletions cointop/portfolio.go
Expand Up @@ -81,12 +81,13 @@ func (ct *Cointop) updatePortfolioUpdateMenu() {
label := fmt.Sprintf(" Enter holdings for %s %s", ct.colorscheme.MenuLabel(coin.Name), current)
content := fmt.Sprintf("%s\n%s\n\n%s%s\n\n\n [Enter] %s [ESC] Cancel", header, label, strings.Repeat(" ", 29), coin.Symbol, submitText)

ct.Update(func() {
ct.Update(func() error {
ct.Views.PortfolioUpdateMenu.Backing().Clear()
ct.Views.PortfolioUpdateMenu.Backing().Frame = true
fmt.Fprintln(ct.Views.PortfolioUpdateMenu.Backing(), content)
fmt.Fprintln(ct.Views.Input.Backing(), value)
ct.Views.Input.Backing().SetCursor(len(value), 0)
return nil
})
}

Expand All @@ -111,9 +112,9 @@ func (ct *Cointop) hidePortfolioUpdateMenu() error {
ct.SetViewOnBottom(ct.Views.PortfolioUpdateMenu.Name())
ct.SetViewOnBottom(ct.Views.Input.Name())
ct.SetActiveView(ct.Views.Table.Name())
ct.Update(func() {
ct.Update(func() error {
if ct.Views.PortfolioUpdateMenu.Backing() == nil {
return
return nil
}

ct.Views.PortfolioUpdateMenu.Backing().Clear()
Expand All @@ -122,7 +123,9 @@ func (ct *Cointop) hidePortfolioUpdateMenu() error {

ct.Views.Input.Backing().Clear()
fmt.Fprintln(ct.Views.Input.Backing(), "")
return nil
})

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion cointop/statusbar.go
Expand Up @@ -63,8 +63,9 @@ func (ct *Cointop) UpdateStatusbar(s string) error {

str = str[:end] + v

ct.Update(func() {
ct.Update(func() error {
ct.Views.Statusbar.Update(str)
return nil
})

return nil
Expand Down
5 changes: 3 additions & 2 deletions cointop/table.go
Expand Up @@ -193,9 +193,9 @@ func (ct *Cointop) RefreshTable() error {
ct.highlightRow(currentrow)
}

ct.Update(func() {
ct.Update(func() error {
if ct.Views.Table.Backing() == nil {
return
return nil
}

ct.Views.Table.Backing().Clear()
Expand All @@ -204,6 +204,7 @@ func (ct *Cointop) RefreshTable() error {
go ct.UpdateTableHeader()
go ct.updateMarketbar()
go ct.UpdateChart()
return nil
})

return nil
Expand Down
5 changes: 3 additions & 2 deletions cointop/table_header.go
Expand Up @@ -89,12 +89,13 @@ func (ct *Cointop) UpdateTableHeader() {
headers = append(headers, str)
}

ct.Update(func() {
ct.Update(func() error {
if ct.Views.TableHeader.Backing() == nil {
return
return nil
}

ct.Views.TableHeader.Backing().Clear()
fmt.Fprintln(ct.Views.TableHeader.Backing(), strings.Join(headers, ""))
return nil
})
}
6 changes: 2 additions & 4 deletions cointop/update.go
Expand Up @@ -7,16 +7,14 @@ import (
)

// Update takes a callback which updates the view
func (ct *Cointop) Update(f func()) {
func (ct *Cointop) Update(f func() error) {
ct.debuglog(fmt.Sprintf("Update()"))

if ct.g == nil {
panic("gocui is not initialized")
}

ct.g.Update(func(g *gocui.Gui) error {
f()

return nil
return f()
})
}

0 comments on commit ca1987a

Please sign in to comment.