Skip to content

Commit

Permalink
Move global var into app struct.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Dec 20, 2020
1 parent d3c63e0 commit a582ab8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
1 change: 1 addition & 0 deletions top/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type config struct {
queryOptions query.Options // Queries' settings that might depend on Postgres version.
viewCh chan view.View // Channel used for passing view settings to stats goroutine.
logtail stat.Logfile // Logfile used for working with Postgres log file.
dialog dialogType // Remember current user-started dialog, used for selecting needed dialog handler.
}

// newConfig creates 'top' initial configuration.
Expand Down
23 changes: 11 additions & 12 deletions top/dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ const (
)

var (
// There is a prompt for every dialog.
// dialogPrompts defines prompts for user-requested actions.
// This is read-only and should not be changed during runtime.
dialogPrompts = map[dialogType]string{
dialogPgReload: "Reload configuration files (y/n): ",
dialogFilter: "Set filter: ",
Expand All @@ -39,12 +40,9 @@ var (
dialogQueryReport: "Enter the queryid: ",
dialogChangeRefresh: "Change refresh (min 1, max 300) to ",
}

// Variable-transporter, function which check user's input, uses this variable to select appropriate handler. Depending on dialog type, select appropriate function.
dialog dialogType
)

// Open 'gocui' view for the dialog.
// dialogOpen opens view for the dialog.
func dialogOpen(app *app, d dialogType) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
// some types of actions allowed only in specifics stats contexts.
Expand Down Expand Up @@ -89,7 +87,7 @@ func dialogOpen(app *app, d dialogType) func(g *gocui.Gui, v *gocui.View) error
}

// Remember the type of an opened dialog. It will be required when the dialog will be finished.
dialog = d
app.config.dialog = d
}
return nil
}
Expand All @@ -102,7 +100,7 @@ func dialogFinish(app *app) func(g *gocui.Gui, v *gocui.View) error {

printCmdline(g, "")

switch dialog {
switch app.config.dialog {
case dialogPgReload:
doReload(g, v, app.db, answer)
case dialogFilter:
Expand Down Expand Up @@ -132,11 +130,12 @@ func dialogFinish(app *app) func(g *gocui.Gui, v *gocui.View) error {
}

// Finish dialog when user presses Esc to cancel.
func dialogCancel(g *gocui.Gui, v *gocui.View) error {
dialog = dialogNone
printCmdline(g, "Do nothing. Operation canceled.")

return dialogClose(g, v)
func dialogCancel(app *app) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
app.config.dialog = dialogNone
printCmdline(g, "Do nothing. Operation canceled.")
return dialogClose(g, v)
}
}

// Close 'gocui' view object related to dialog.
Expand Down
2 changes: 1 addition & 1 deletion top/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func keybindings(app *app) error {
{"sysstat", 'A', dialogOpen(app, dialogChangeAge)},
{"sysstat", 'G', dialogOpen(app, dialogQueryReport)},
{"sysstat", 'z', dialogOpen(app, dialogChangeRefresh)},
{"dialog", gocui.KeyEsc, dialogCancel},
{"dialog", gocui.KeyEsc, dialogCancel(app)},
{"dialog", gocui.KeyEnter, dialogFinish(app)},
{"menu", gocui.KeyEsc, menuClose},
{"menu", gocui.KeyArrowUp, moveCursor(moveUp)},
Expand Down

0 comments on commit a582ab8

Please sign in to comment.