Skip to content

Commit

Permalink
Move menu-related global variables into config.
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Dec 20, 2020
1 parent 4a1b4e9 commit a865f64
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 77 deletions.
1 change: 1 addition & 0 deletions top/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type config struct {
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.
menu menuStyle // When working with menus, keep properties of the menu.
}

// newConfig creates 'top' initial configuration.
Expand Down
10 changes: 5 additions & 5 deletions top/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func keybindings(app *app) error {
{"sysstat", 'a', switchViewTo(app, "activity")},
{"sysstat", 'x', switchViewTo(app, "statements")},
{"sysstat", 'Q', resetStat(app.db)},
{"sysstat", 'E', menuOpen(menuConfStyle, false)},
{"sysstat", 'X', menuOpen(menuPgssStyle, app.postgresProps.ExtPGSSAvail)},
{"sysstat", 'P', menuOpen(menuProgressStyle, false)},
{"sysstat", 'E', menuOpen(menuConf, app.config, false)},
{"sysstat", 'X', menuOpen(menuPgss, app.config, app.postgresProps.ExtPGSSAvail)},
{"sysstat", 'P', menuOpen(menuProgress, app.config, false)},
{"sysstat", 'l', showPgLog(app.db, app.postgresProps.VersionNum, app.doExit)},
{"sysstat", 'C', showPgConfig(app.db, app.doExit)},
{"sysstat", '~', runPsql(app.db, app.doExit)},
Expand All @@ -59,8 +59,8 @@ func keybindings(app *app) error {
{"dialog", gocui.KeyEsc, dialogCancel(app)},
{"dialog", gocui.KeyEnter, dialogFinish(app)},
{"menu", gocui.KeyEsc, menuClose},
{"menu", gocui.KeyArrowUp, moveCursor(moveUp)},
{"menu", gocui.KeyArrowDown, moveCursor(moveDown)},
{"menu", gocui.KeyArrowUp, moveCursor(moveUp, app.config)},
{"menu", gocui.KeyArrowDown, moveCursor(moveDown, app.config)},
{"menu", gocui.KeyEnter, menuSelect(app)},
{"sysstat", 'h', showHelp},
{"sysstat", gocui.KeyF1, showHelp},
Expand Down
149 changes: 77 additions & 72 deletions top/menu.go
Original file line number Diff line number Diff line change
@@ -1,105 +1,108 @@
// Menus used in case when user should make a choice from the list of similar items.

package top

import (
"fmt"
"github.com/jroimartin/gocui"
)

// Type of the menu
// menuType defines a type of the used menu.
type menuType int

// Direction of user choice
// direction defines direction of user choice in the used menu.
type direction int

// Particular menu types
const (
menuNone menuType = iota
menuPgss
menuProgress
menuConf
)

// Directions - user allowed to move up and down.
const (
moveUp direction = iota
moveDown
// Available menu types.
menuNone menuType = iota // no active menu
menuPgss // menu with pg_stat_statements stats
menuProgress // menu with pg_stat_progress_* stats
menuConf // menu with configuration files

// Directions allowed when working with menu.
moveUp direction = iota // move up
moveDown // move down
)

// Describes menu and its details
// menuStyle describes menu properties.
type menuStyle struct {
menuType // Type of a menu
menuTitle string // Title
menuItems []string // List of items
menuType // Type of a menu
title string // Title
items []string // List of items
}

var (
// pg_stat_statements menu
menuPgssStyle = menuStyle{
menuType: menuPgss,
menuTitle: " Choose pg_stat_statements mode (Enter to choose, Esc to exit): ",
menuItems: []string{
" pg_stat_statements timings",
" pg_stat_statements general",
" pg_stat_statements input/output",
" pg_stat_statements temp files input/output",
" pg_stat_statements temp tables (local) input/output",
},
}

// pg_stat_progress_* menu
menuProgressStyle = menuStyle{
menuType: menuProgress,
menuTitle: " Choose pg_stat_progress_* view (Enter to choose, Esc to exit): ",
menuItems: []string{
" pg_stat_progress_vacuum",
" pg_stat_progress_cluster",
" pg_stat_progress_create_index",
},
}

// edit configuration files
menuConfStyle = menuStyle{
menuType: menuConf,
menuTitle: " Edit configuration file (Enter to edit, Esc to exit): ",
menuItems: []string{
" postgresql.conf",
" pg_hba.conf",
" pg_ident.conf",
" recovery.conf",
},
// selectMenuStyle returns selected menuStyle properties.
func selectMenuStyle(t menuType) menuStyle {
var s menuStyle

switch t {
case menuPgss:
s = menuStyle{
menuType: menuPgss,
title: " Choose pg_stat_statements mode (Enter to choose, Esc to exit): ",
items: []string{
" pg_stat_statements timings",
" pg_stat_statements general",
" pg_stat_statements input/output",
" pg_stat_statements temp files input/output",
" pg_stat_statements temp tables (local) input/output",
},
}
case menuProgress:
s = menuStyle{
menuType: menuProgress,
title: " Choose pg_stat_progress_* view (Enter to choose, Esc to exit): ",
items: []string{
" pg_stat_progress_vacuum",
" pg_stat_progress_cluster",
" pg_stat_progress_create_index",
},
}
case menuConf:
s = menuStyle{
menuType: menuConf,
title: " Edit configuration file (Enter to edit, Esc to exit): ",
items: []string{
" postgresql.conf",
" pg_hba.conf",
" pg_ident.conf",
" recovery.conf",
},
}
default:
s = menuStyle{
menuType: menuNone,
}
}

// Variable-transporter, function which check user's choice, uses this variable to select appropriate handler. Depending on menu type, select appropriate function.
menu menuType
items []string
)
return s
}

// Open 'gocui' view object and display menu items depending on passed menu type.
func menuOpen(m menuStyle, pgssAvail bool) func(g *gocui.Gui, _ *gocui.View) error {
// menuOpen opens UI view object for menu.
func menuOpen(m menuType, config *config, pgssAvail bool) func(g *gocui.Gui, _ *gocui.View) error {
return func(g *gocui.Gui, _ *gocui.View) error {
s := selectMenuStyle(m)

// in case of opening menu for switching to pg_stat_statements and if it isn't available - it's unnecessary to open menu, just notify user and do nothing
if !pgssAvail && m.menuType == menuPgss {
if !pgssAvail && s.menuType == menuPgss {
printCmdline(g, msgPgStatStatementsUnavailable)
return nil
}

v, err := g.SetView("menu", 0, 5, 72, 6+len(m.menuItems))
v, err := g.SetView("menu", 0, 5, 72, 6+len(s.items))
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = m.menuTitle
v.Title = s.title
}
if _, err := g.SetCurrentView("menu"); err != nil {
return err
}

menu = m.menuType
items = m.menuItems
menuDraw(v, s.items)

menuDraw(v)
// Save menu properties in config.
config.menu = s

return nil
}
Expand All @@ -110,7 +113,7 @@ func menuSelect(app *app) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor() /* cy point to an index of the entry, use it to switch to a context */

switch menu {
switch app.config.menu.menuType {
case menuPgss:
switch cy {
case 0:
Expand Down Expand Up @@ -152,6 +155,8 @@ func menuSelect(app *app) func(g *gocui.Gui, v *gocui.View) error {
/* do nothing */
}

// When menu item has been selected, close menu and reset menu properties from config.
app.config.menu = selectMenuStyle(menuNone)
return menuClose(g, v)
}
}
Expand All @@ -168,10 +173,10 @@ func menuClose(g *gocui.Gui, v *gocui.View) error {
return nil
}

func menuDraw(v *gocui.View) {
func menuDraw(v *gocui.View, items []string) {
_, cy := v.Cursor()
v.Clear()
/* print menu items */
// print menu items
for i, item := range items {
if i == cy {
fmt.Fprintln(v, "\033[30;47m"+item+"\033[0m")
Expand All @@ -182,17 +187,17 @@ func menuDraw(v *gocui.View) {
}

// Move cursor to one item up or down.
func moveCursor(d direction) func(g *gocui.Gui, v *gocui.View) error {
func moveCursor(d direction, config *config) func(g *gocui.Gui, v *gocui.View) error {
return func(g *gocui.Gui, v *gocui.View) error {
if v != nil {
cx, cy := v.Cursor()
switch d {
case moveDown:
v.SetCursor(cx, cy+1) /* errors don't make sense here */
menuDraw(v)
menuDraw(v, config.menu.items)
case moveUp:
v.SetCursor(cx, cy-1) /* errors don't make sense here */
menuDraw(v)
menuDraw(v, config.menu.items)
}
}
return nil
Expand Down

0 comments on commit a865f64

Please sign in to comment.