[GH-ISSUE #443] Demo code for menu type code #319

Closed
opened 2026-03-04 01:03:56 +03:00 by kerem · 4 comments
Owner

Originally created by @richardltc on GitHub (May 8, 2020).
Original GitHub issue: https://github.com/rivo/tview/issues/443

Hi,

I am trying to build on the grid demo, by adding different entries to the menu section, but an not getting how I can do that.

Any pointers would be very much appreciated.

Thank you in advance.

Richard

Originally created by @richardltc on GitHub (May 8, 2020). Original GitHub issue: https://github.com/rivo/tview/issues/443 Hi, I am trying to build on the grid demo, by adding different entries to the menu section, but an not getting how I can do that. Any pointers would be very much appreciated. Thank you in advance. Richard
kerem closed this issue 2026-03-04 01:03:56 +03:00
Author
Owner

@gnojus commented on GitHub (May 14, 2020):

Hi,
general guidelines should be covered in package documentation: grid.AddItem.
If you need more specific information, you should explain further what you want to achieve, then we may be able to help you.

<!-- gh-comment-id:628759637 --> @gnojus commented on GitHub (May 14, 2020): Hi, general guidelines should be covered in package documentation: [grid.AddItem](https://pkg.go.dev/github.com/rivo/tview?tab=doc#Grid.AddItem). If you need more specific information, you should explain further what you want to achieve, then we may be able to help you.
Author
Owner

@richardltc commented on GitHub (May 15, 2020):

Hi,

Thanks for replying.
So, I am starting a new project off, and am using the code example from the existing demo project "grid", but I can't work out how I can add "menu entries" to the left side section called "menu"?

Any pointers would be great.

Thanks in advance.

Richard

<!-- gh-comment-id:629057557 --> @richardltc commented on GitHub (May 15, 2020): Hi, Thanks for replying. So, I am starting a new project off, and am using the code example from the existing demo project "grid", but I can't work out how I can add "menu entries" to the left side section called "menu"? Any pointers would be great. Thanks in advance. Richard
Author
Owner

@gnojus commented on GitHub (May 15, 2020):

The rectangle "menu" is not actually a menu, it's just a TextView with text 'menu'. I'm not sure what you mean by

how I can add "menu entries"

if you want a menu in that tile, you will need to use something else, e. g. a List. Anyway, here is the demo code with the list:

package main

import (
	"fmt"

	"github.com/rivo/tview"
)

func main() {
	newPrimitive := func(text string) tview.Primitive {
		return tview.NewTextView().
			SetTextAlign(tview.AlignCenter).
			SetText(text)
	}
	// creating a list
	menu := tview.NewList().ShowSecondaryText(false)
	// Ading items
	for i := 0; i < 5; i++ {
		text := fmt.Sprintf("Menu option %d", i+1)
		// List item with text only
		menu.AddItem(text, "", 0, nil)
	}
	main := newPrimitive("Main content")
	sideBar := newPrimitive("Side Bar")

	grid := tview.NewGrid().
		SetRows(3, 0, 3).
		SetColumns(30, 0, 30).
		SetBorders(true).
		AddItem(newPrimitive("Header"), 0, 0, 1, 3, 0, 0, false).
		AddItem(newPrimitive("Footer"), 2, 0, 1, 3, 0, 0, false)

	// Layout for screens narrower than 100 cells (menu and side bar are hidden).
	grid.AddItem(menu, 0, 0, 0, 0, 0, 0, false).
		AddItem(main, 1, 0, 1, 3, 0, 0, false).
		AddItem(sideBar, 0, 0, 0, 0, 0, 0, false)

	// Layout for screens wider than 100 cells.
	grid.AddItem(menu, 1, 0, 1, 1, 0, 100, false).
		AddItem(main, 1, 1, 1, 1, 0, 100, false).
		AddItem(sideBar, 1, 2, 1, 1, 0, 100, false)

	// Focusing the menu so that it captures users input (key presses)
	if err := tview.NewApplication().SetRoot(grid, true).SetFocus(menu).Run(); err != nil {
		panic(err)
	}
}
<!-- gh-comment-id:629178214 --> @gnojus commented on GitHub (May 15, 2020): The rectangle "menu" is not actually a menu, it's just a `TextView` with text 'menu'. I'm not sure what you mean by > how I can add "menu entries" if you want a menu in that tile, you will need to use something else, e. g. a `List`. Anyway, here is the demo code with the list: ```go package main import ( "fmt" "github.com/rivo/tview" ) func main() { newPrimitive := func(text string) tview.Primitive { return tview.NewTextView(). SetTextAlign(tview.AlignCenter). SetText(text) } // creating a list menu := tview.NewList().ShowSecondaryText(false) // Ading items for i := 0; i < 5; i++ { text := fmt.Sprintf("Menu option %d", i+1) // List item with text only menu.AddItem(text, "", 0, nil) } main := newPrimitive("Main content") sideBar := newPrimitive("Side Bar") grid := tview.NewGrid(). SetRows(3, 0, 3). SetColumns(30, 0, 30). SetBorders(true). AddItem(newPrimitive("Header"), 0, 0, 1, 3, 0, 0, false). AddItem(newPrimitive("Footer"), 2, 0, 1, 3, 0, 0, false) // Layout for screens narrower than 100 cells (menu and side bar are hidden). grid.AddItem(menu, 0, 0, 0, 0, 0, 0, false). AddItem(main, 1, 0, 1, 3, 0, 0, false). AddItem(sideBar, 0, 0, 0, 0, 0, 0, false) // Layout for screens wider than 100 cells. grid.AddItem(menu, 1, 0, 1, 1, 0, 100, false). AddItem(main, 1, 1, 1, 1, 0, 100, false). AddItem(sideBar, 1, 2, 1, 1, 0, 100, false) // Focusing the menu so that it captures users input (key presses) if err := tview.NewApplication().SetRoot(grid, true).SetFocus(menu).Run(); err != nil { panic(err) } } ```
Author
Owner

@rivo commented on GitHub (Sep 15, 2020):

Please let us know if this helped you so I can close this issue.

<!-- gh-comment-id:692617577 --> @rivo commented on GitHub (Sep 15, 2020): Please let us know if this helped you so I can close this issue.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#319
No description provided.