Skip to content

Commit

Permalink
Make sure to return func to close
Browse files Browse the repository at this point in the history
  • Loading branch information
nakabonne committed May 6, 2020
1 parent bcc0a31 commit 444b255
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 16 deletions.
33 changes: 23 additions & 10 deletions pkg/gui/gui.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,26 @@ func (g *Gui) switchPage(prev, next string) {
g.pages.RemovePage(prev).ShowPage(next)
}

func (g *Gui) modal(p tview.Primitive, width, height int) *tview.Grid {
return tview.NewGrid().
SetColumns(0, width, 0).
SetRows(0, height, 0).
AddItem(p, 1, 1, 1, 1, 0, 0, true)
// showWarn shows the given message as a modal.
// doneLabel param is used for the name of button to close.
func (g *Gui) showWarn(message, doneLabel string) {
g.showModal(message, doneLabel, 60, 150, tcell.ColorBlack)
}

// message shows the given message as a modal.
func (g *Gui) message(message, doneLabel string) {
// showLoading shows the given message as a modal.
// It doesn't provide any close buttons, instead it returns the function to close itself.
func (g *Gui) showLoading(message string) func() {
g.showModal(message, "", 2, 100, tcell.ColorBlack)
close := func() {
g.switchPage(modalPageName, mainPageName)
}
return close
}

func (g *Gui) showModal(message, doneLabel string, rowWidth, colWidth int, backGroundColor tcell.Color) {
modal := tview.NewModal().
SetText(message).
SetBackgroundColor(tcell.ColorBlack)
SetBackgroundColor(backGroundColor)

if doneLabel != "" {
modal.AddButtons([]string{doneLabel}).
Expand All @@ -134,7 +142,12 @@ func (g *Gui) message(message, doneLabel string) {
}
})
}
g.pages.AddAndSwitchToPage(modalPageName, g.modal(modal, 150, 60), true).ShowPage(mainPageName)

grid := tview.NewGrid().
SetColumns(0, colWidth, 0).
SetRows(0, rowWidth, 0).
AddItem(modal, 1, 1, 1, 1, 0, 0, true)
g.pages.AddAndSwitchToPage(modalPageName, grid, true).ShowPage(mainPageName)
}

// registerPath adds path to golangci-lint runner as an arg.
Expand All @@ -155,7 +168,7 @@ func (g *Gui) enableLinter(node *tview.TreeNode, linter *config.Linter) {

func (g *Gui) disableLinter(node *tview.TreeNode, linter *config.Linter) {
if err := g.runner.DisableLinter(linter.Name()); err != nil {
g.message(err.Error(), "Enter")
g.showWarn(err.Error(), "Enter")
return
}
node.SetColor(item.DefaultLinterColor)
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/item/linters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

const (
DefaultLinterColor = tcell.ColorRed
DefaultLinterColor = tcell.ColorSilver
EnabledLinterColor = tcell.ColorLime
)

Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/item/navi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func NewNavi() *Navi {
}

const (
globalNavi = "[yellow::b]r[white]: run, [yellow::b]j[white]: move down, [yellow]k[white]: move up, [yellow]q[white]: quit"
globalNavi = "[aqua::b]r[white]: run, [aqua::b]j[white]: move down, [aqua]k[white]: move up, [aqua]q[white]: quit"
)

func (n *Navi) Update(p tview.Primitive) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/gui/item/source_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
DefaultDirColor = tcell.ColorRed
DefaultDirColor = tcell.ColorSilver
SelectedDirColor = tcell.ColorLime
)

Expand Down
6 changes: 3 additions & 3 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ func (g *Gui) grobalKeybind(event *tcell.EventKey) {
case 'q':
g.application.Stop()
case 'r':
g.message("running linters...", "")
close := g.showLoading("running linters...")

go func() {
issues, err := g.runner.Run()
g.switchPage(modalPageName, mainPageName)
close()
if err != nil {
g.resultsItem.ShowMessage(err.Error())
g.showWarn(err.Error(), "Close")
g.application.Draw()
return
}
Expand Down

0 comments on commit 444b255

Please sign in to comment.