[GH-ISSUE #304] how to rerun app ? #229

Closed
opened 2026-03-04 01:03:10 +03:00 by kerem · 1 comment
Owner

Originally created by @xinity on GitHub (Jun 13, 2019).
Original GitHub issue: https://github.com/rivo/tview/issues/304

using modal i'd like to add a feature on which with the "OK" button is trigger, it will get me to the previous page.

i'm adding below a crapy code i'm trying with tview:
where i'm added the " // ?? reload the app ??" comment is where i'd like to trigger the "redraw of the app.

If anyone have a clue, let me know :)

package main

import (
	"github.com/rivo/tview"
	"github.com/sirupsen/logrus"
	"os"
	"os/exec"
)

var log = logrus.New()



func main() {
	// You could set this to any `io.Writer` such as a file
	log.Out = os.Stdout
	file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
	if err == nil {
		log.Out = file
	} else {
		log.Info("Failed to log to file, using default stderr")
	}
	app := tview.NewApplication()
	modal := tview.NewModal().
		SetText("Bienvenu dans le gestionnaire d'installation du manager XXX	").
		AddButtons([]string{"Ok"}).
		SetDoneFunc(func(buttonIndex int, buttonLabel string) {
			if buttonLabel == "Ok" {
				app.Stop()
			}
		})
	if err := app.SetRoot(modal, false).Run(); err != nil {
		panic(err)
	}
	form := tview.NewForm().
		AddButton("update system", func() {
			cmd := exec.Command("sh", "-c", "apt-get update -qqy")
			out, err := cmd.CombinedOutput()
			if err != nil {
				modal := tview.NewModal().
					SetText("An error has occured , please check logs ?").
					AddButtons([]string{"OK"}).
					SetDoneFunc(func(buttonIndex int, buttonLabel string) {
						if buttonLabel == "OK" {
							app.Stop()
						}
					log.Fatalf("combined out:%s", string(out))
				})
				if err := app.SetRoot(modal, false).Run(); err != nil {
					panic(err)
				}
			}
		modal := tview.NewModal().
			SetText("Update done , please proceed to next stage").
			AddButtons([]string{"OK"}).
			SetDoneFunc(func(buttonIndex int, buttonLabel string) {
				if buttonLabel == "OK" {
					log.Infof("combined out:%s", string(out))
					app.Stop()
				}
				// ?? reload the app ??
			})
		if err := app.SetRoot(modal, false).Run(); err != nil {
			panic(err)
		}

		}).
		AddButton("Install Docker", func() {
			cmd := exec.Command("sh", "-c", "apt-get install docker.io ansible -qqy")
			out, err := cmd.CombinedOutput()
			if err != nil {
				log.Errorf("combined out:\n%s\n", string(out))
				modal := tview.NewModal().
					SetText("An error has occured , please check logs ?").
					AddButtons([]string{"OK"}).
					SetDoneFunc(func(buttonIndex int, buttonLabel string) {
						if buttonLabel == "OK" {
							app.Stop()
						}
					log.Fatalf("combined out:%s", string(out))
				})
				if err := app.SetRoot(modal, false).Run(); err != nil {
					panic(err)
				}
			}

		}).
		AddButton("Quit", func() {
			app.Stop()
		})
	form.SetBorder(false).SetTitle("Enter some data").SetTitleAlign(tview.AlignCenter)
	if err := app.SetRoot(form, true).Run(); err != nil {
		panic(err)
	}
}
Originally created by @xinity on GitHub (Jun 13, 2019). Original GitHub issue: https://github.com/rivo/tview/issues/304 using modal i'd like to add a feature on which with the "OK" button is trigger, it will get me to the previous page. i'm adding below a crapy code i'm trying with tview: where i'm added the " // ?? reload the app ??" comment is where i'd like to trigger the "redraw of the app. If anyone have a clue, let me know :) ``` package main import ( "github.com/rivo/tview" "github.com/sirupsen/logrus" "os" "os/exec" ) var log = logrus.New() func main() { // You could set this to any `io.Writer` such as a file log.Out = os.Stdout file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) if err == nil { log.Out = file } else { log.Info("Failed to log to file, using default stderr") } app := tview.NewApplication() modal := tview.NewModal(). SetText("Bienvenu dans le gestionnaire d'installation du manager XXX "). AddButtons([]string{"Ok"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonLabel == "Ok" { app.Stop() } }) if err := app.SetRoot(modal, false).Run(); err != nil { panic(err) } form := tview.NewForm(). AddButton("update system", func() { cmd := exec.Command("sh", "-c", "apt-get update -qqy") out, err := cmd.CombinedOutput() if err != nil { modal := tview.NewModal(). SetText("An error has occured , please check logs ?"). AddButtons([]string{"OK"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonLabel == "OK" { app.Stop() } log.Fatalf("combined out:%s", string(out)) }) if err := app.SetRoot(modal, false).Run(); err != nil { panic(err) } } modal := tview.NewModal(). SetText("Update done , please proceed to next stage"). AddButtons([]string{"OK"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonLabel == "OK" { log.Infof("combined out:%s", string(out)) app.Stop() } // ?? reload the app ?? }) if err := app.SetRoot(modal, false).Run(); err != nil { panic(err) } }). AddButton("Install Docker", func() { cmd := exec.Command("sh", "-c", "apt-get install docker.io ansible -qqy") out, err := cmd.CombinedOutput() if err != nil { log.Errorf("combined out:\n%s\n", string(out)) modal := tview.NewModal(). SetText("An error has occured , please check logs ?"). AddButtons([]string{"OK"}). SetDoneFunc(func(buttonIndex int, buttonLabel string) { if buttonLabel == "OK" { app.Stop() } log.Fatalf("combined out:%s", string(out)) }) if err := app.SetRoot(modal, false).Run(); err != nil { panic(err) } } }). AddButton("Quit", func() { app.Stop() }) form.SetBorder(false).SetTitle("Enter some data").SetTitleAlign(tview.AlignCenter) if err := app.SetRoot(form, true).Run(); err != nil { panic(err) } } ```
kerem closed this issue 2026-03-04 01:03:10 +03:00
Author
Owner

@rivo commented on GitHub (Jul 9, 2019):

Application.Run() should only be run once in your application. To switch between different primitives of your component (e.g. Modal and Form), use the Pages primitive. Here's an example:

package main

import "github.com/rivo/tview"

func main() {
	pages := tview.NewPages()

	form := tview.NewForm().
		AddInputField("Name:", "", 20, nil, nil)
	pages.AddPage("form", form, true, false)

	modal := tview.NewModal().
		SetText("Click Ok to go to the form").
		AddButtons([]string{"Ok"}).
		SetDoneFunc(func(index int, label string) {
			pages.SwitchToPage("form")
		})
	pages.AddPage("modal", modal, false, true)

	if err := tview.NewApplication().SetRoot(pages, true).Run(); err != nil {
		panic(err)
	}
}

Let me know if this answers your question.

<!-- gh-comment-id:509618345 --> @rivo commented on GitHub (Jul 9, 2019): `Application.Run()` should only be run once in your application. To switch between different primitives of your component (e.g. `Modal` and `Form`), use the `Pages` primitive. Here's an example: ```go package main import "github.com/rivo/tview" func main() { pages := tview.NewPages() form := tview.NewForm(). AddInputField("Name:", "", 20, nil, nil) pages.AddPage("form", form, true, false) modal := tview.NewModal(). SetText("Click Ok to go to the form"). AddButtons([]string{"Ok"}). SetDoneFunc(func(index int, label string) { pages.SwitchToPage("form") }) pages.AddPage("modal", modal, false, true) if err := tview.NewApplication().SetRoot(pages, true).Run(); err != nil { panic(err) } } ``` Let me know if this answers your question.
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#229
No description provided.