termui icon indicating copy to clipboard operation
termui copied to clipboard

migrate legacy to v3

Open ghost opened this issue 6 years ago • 2 comments

Hi guys,

Hope you are all well !

I tried to find some documentation but could not find it. How to migrate legacy termui integration to v3...

./dashboard.go:18:15: undefined: termui.NewList
./dashboard.go:24:26: undefined: termui.NewPar
./dashboard.go:30:27: undefined: termui.NewPar
./dashboard.go:36:23: undefined: termui.NewPar
./dashboard.go:42:25: undefined: termui.NewPar
./dashboard.go:48:21: undefined: termui.NewPar
./dashboard.go:54:24: undefined: termui.NewPar
./dashboard.go:60:20: undefined: termui.NewPar
./dashboard.go:66:17: undefined: termui.NewPar
./dashboard.go:120:17: undefined: termui.Body

What is replacing ? termui.NewList() termui.NewPar() termui.Body

here is the code:

package main

import (
	"fmt"
	"time"

	"github.com/gizak/termui/v3"
)

func dashboard(stopTheUI, stopTheCrawler chan bool) {
	if err := termui.Init(); err != nil {
		panic(err)
	}
	defer termui.Close()

	var snapshots []Snapshot

	logWindow := termui.NewList()
	logWindow.ItemFgColor = termui.ColorYellow
	logWindow.BorderLabel = "Log"
	logWindow.Height = 22
	logWindow.Y = 0

	totalBytesDownloaded := termui.NewPar("")
	totalBytesDownloaded.Height = 3
	totalBytesDownloaded.TextFgColor = termui.ColorWhite
	totalBytesDownloaded.BorderLabel = "Data downloaded"
	totalBytesDownloaded.BorderFg = termui.ColorCyan

	totalNumberOfRequests := termui.NewPar("")
	totalNumberOfRequests.Height = 3
	totalNumberOfRequests.TextFgColor = termui.ColorWhite
	totalNumberOfRequests.BorderLabel = "URLs crawled"
	totalNumberOfRequests.BorderFg = termui.ColorCyan

	requestsPerSecond := termui.NewPar("")
	requestsPerSecond.Height = 3
	requestsPerSecond.TextFgColor = termui.ColorWhite
	requestsPerSecond.BorderLabel = "URLs/second"
	requestsPerSecond.BorderFg = termui.ColorCyan

	averageResponseTime := termui.NewPar("")
	averageResponseTime.Height = 3
	averageResponseTime.TextFgColor = termui.ColorWhite
	averageResponseTime.BorderLabel = "Average response time"
	averageResponseTime.BorderFg = termui.ColorCyan

	numberOfWorkers := termui.NewPar("")
	numberOfWorkers.Height = 3
	numberOfWorkers.TextFgColor = termui.ColorWhite
	numberOfWorkers.BorderLabel = "Number of workers"
	numberOfWorkers.BorderFg = termui.ColorCyan

	averageSizeInBytes := termui.NewPar("")
	averageSizeInBytes.Height = 3
	averageSizeInBytes.TextFgColor = termui.ColorWhite
	averageSizeInBytes.BorderLabel = "Average response size"
	averageSizeInBytes.BorderFg = termui.ColorCyan

	numberOfErrors := termui.NewPar("")
	numberOfErrors.Height = 3
	numberOfErrors.TextFgColor = termui.ColorWhite
	numberOfErrors.BorderLabel = "Number of 4xx errors"
	numberOfErrors.BorderFg = termui.ColorCyan

	elapsedTime := termui.NewPar("")
	elapsedTime.Height = 3
	elapsedTime.TextFgColor = termui.ColorWhite
	elapsedTime.BorderLabel = "Elapsed time"
	elapsedTime.BorderFg = termui.ColorCyan

	draw := func() {

		snapshot := stats.LastSnapshot()

		// ignore empty updates
		if snapshot.Timestamp().IsZero() {
			return
		}

		// don't update if there is no new snapshot available
		if len(snapshots) > 0 {
			previousSnapShot := snapshots[len(snapshots)-1]
			if snapshot.Timestamp() == previousSnapShot.Timestamp() {
				return
			}
		}

		// capture the latest snapshot
		snapshots = append(snapshots, snapshot)

		// log messages
		logWindow.Items = stats.LastLogMessages(20)

		// total number of requests
		totalNumberOfRequests.Text = fmt.Sprintf("%d", snapshot.TotalNumberOfRequests())

		// total number of bytes downloaded
		totalBytesDownloaded.Text = formatBytes(snapshot.TotalSizeInBytes())

		// requests per second
		requestsPerSecond.Text = fmt.Sprintf("%.1f", snapshot.RequestsPerSecond())

		// average response time
		averageResponseTime.Text = fmt.Sprintf("%s", snapshot.AverageResponseTime())

		// number of workers
		numberOfWorkers.Text = fmt.Sprintf("%d", snapshot.NumberOfWorkers())

		// average request size
		averageSizeInBytes.Text = formatBytes(snapshot.AverageSizeInBytes())

		// number of errors
		numberOfErrors.Text = fmt.Sprintf("%d", snapshot.NumberOfErrors())

		// time since first snapshot
		timeSinceStart := time.Now().Sub(snapshots[0].Timestamp())
		elapsedTime.Text = fmt.Sprintf("%s", timeSinceStart)

		termui.Render(termui.Body)
	}

	termui.Body.AddRows(
		termui.NewRow(
			termui.NewCol(12, 0, logWindow),
		),
		termui.NewRow(
			termui.NewCol(3, 0, totalBytesDownloaded),
			termui.NewCol(3, 0, totalNumberOfRequests),
			termui.NewCol(3, 0, requestsPerSecond),
			termui.NewCol(3, 0, averageResponseTime),
		),
		termui.NewRow(
			termui.NewCol(3, 0, numberOfWorkers),
			termui.NewCol(3, 0, numberOfErrors),
			termui.NewCol(3, 0, averageSizeInBytes),
			termui.NewCol(3, 0, elapsedTime),
		),
	)

	termui.Body.Align()

	termui.Render(termui.Body)

	termui.Handle("/sys/wnd/resize", func(e termui.Event) {
		termui.Body.Width = termui.TermWidth()
		termui.Body.Align()
		termui.Clear()
		termui.Render(termui.Body)
	})

	termui.Handle("/sys/kbd/q", func(termui.Event) {
		stopTheCrawler <- true

		termui.StopLoop()
	})

	termui.Handle("/timer/1s", func(e termui.Event) {
		draw()
	})

	// stop when the crawler is done
	go func() {
		select {
		case <-stopTheUI:
			// wait 10 seconds before closing the ui
			time.Sleep(time.Second * 10)
			termui.StopLoop()
		}
	}()

	termui.Loop()
}

Cheers, X

ghost avatar Jan 16 '20 13:01 ghost

Help !!! please...

ghost avatar Jan 18 '20 16:01 ghost

I just opened up an 🕸️ old Go project and found some changes need to me made from the old version to the new.

package github.com/gizak/termui: no Go files in /code/go/src/github.com/gizak/termui

Adding /v3 to the end of the import statements brought in the new code:

"github.com/gizak/termui"
"github.com/gizak/termui/v3"

Unfortunately there seem to have been some API changes in v3:

ui/interface.go:35:2: undefined: termui.Handle
ui/interface.go:37:2: undefined: termui.Loop
ui/keyboard.go:6:11: e.Data undefined (type termui.Event has no field or method Data)
ui/keyboard.go:15:3: undefined: termui.StopLoop
ui/layout.go:5:22: undefined: termui.Par
ui/layout.go:6:9: undefined: termui.List
ui/layout.go:12:6: undefined: termui.NewPar
ui/layout.go:15:15: undefined: termui.Attribute
ui/layout.go:17:7: undefined: termui.NewList
ui/layout.go:20:16: undefined: termui.Attribute
ui/layout.go:20:16: too many errors

Is there possibly a 'quick and easy' way of upgrade versions or should I go through and rewrite it using the v3 docs as reference?

missinglink avatar Aug 05 '20 09:08 missinglink