gizak / termui

Golang terminal dashboard
MIT License
13.06k stars 783 forks source link

How to update grid? #264

Closed GideonWolfe closed 4 years ago

GideonWolfe commented 4 years ago

I have a program where I want the user to use a tab bar to select between multiple pages.

I am new to go and this framework so I am pretty sure I'm doing something wrong.

Currently my setup is I have a "master grid", which only contains the top navigation tab bar, and another row to contain another grid, which the user will interact with.

What this grid will be should be a result of the users navbar selection.

My overall confusion is with the "rendering" of these grids and buffers, and how to do it optimally and correctly.

Currently, I declare the main objects like so, using my createDashboardGrid() function to return a ui.Grid object with some placeholder widgets, as well as createMainTabPane() to return a tabpane with a generated list of selections.

 // declare master grid and set terminal dimensions
masterGrid := ui.NewGrid()
termWidth, termHeight := ui.TerminalDimensions()
masterGrid.SetRect(0, 0, termWidth, termHeight)

// navigation tab widget
tabpane := createMainTabPane(courses)

// Grid that should be reassigned based on user choice
contentGrid := createDashboardGrid("Initial Dashboard")

Then I use a function updateMasterGrid() which sets the properties (rows, columns, etc) of the master grid. The idea is that updateMasterGrid() assigns the grid contentGrid() to the second row slot in masterGrid().

func updateMasterGrid(masterGrid *ui.Grid, tabpane *widgets.TabPane, contentGrid *ui.Grid) {
  ui.Clear()
  // defining master grid layout
  masterGrid.Set(
    ui.NewRow(1.0/20,
      ui.NewCol(1.0, tabpane),
    ),
    ui.NewRow(19.0/20,
      ui.NewCol(1.0/1, contentGrid),
    ),
  )
  ui.Render(masterGrid)
}

Then I use the eventhandler loop to wait for input. If the user presses Enter, I want the contentGrid variable to be changed to reflect the users choice. Then I try to call updateMasterGrid() to try and load the new contentGrid into masterGrid.

Am I going about this in the wrong way? Currently the rendered grids are getting layered and the more I press Enter, the slower it gets. I must be missing the proper way to EDIT a grid, and I'm just creating new ones?

GideonWolfe commented 4 years ago

Figured it out.

contentGrid = &courseMasterGrids[tabpane.ActiveTabIndex-1] // Select the correct grid to edit
contentGrid.Items[1].Entry = &courseOverviewGrids[tabpane.ActiveTabIndex-1] // Change item in subgrid based on user choice
masterGrid.Items[1].Entry = contentGrid // update subgrid