gizak / gizak/termui

migrate legacy to v3

Open
#256 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Go
Stars
13.6k
Forks
819
PR merge metrics
No merged PRs in 30d

Description

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...

```bash
./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:
```go
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

Contributor guide

No contributing guide indexed for this repository

Research direction

The affected entry point is dashboard.go, especially lines 18-120, and the missing symbols are termui.NewList, termui.NewPar, and termui.Body. Start by checking the v3 package API for replacements and comparing each use in the supplied example. Done means a migration note identifies the v3 equivalents and shows an updated example that builds.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
cli
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.