Skip to content

Commit

Permalink
Correctly use struct for state variables
Browse files Browse the repository at this point in the history
  • Loading branch information
iBug committed Sep 1, 2024
1 parent e469293 commit 1747b5b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func runWithConfig(cmd *cobra.Command, args []string, config analyze.AnalyzerCon
}

if !config.Analyze && !config.Daemon {
go tui.Tui(analyzer)
go tui.New(analyzer).Run()
}
if config.Daemon {
if err := systemd.NotifyReady(); err != nil {
Expand Down
57 changes: 31 additions & 26 deletions pkg/tui/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
import (
"fmt"
"net/netip"
"sync/atomic"
"time"

"github.com/taoky/ayano/pkg/analyze"
Expand All @@ -15,35 +16,39 @@ const (
Total
)

func Tui(a *analyze.Analyzer) {
type TuiStatus struct {
// displayRecord is used to remember latest accesses time by specific IP
displayRecord map[netip.Prefix]time.Time
serverFilter string
mode ShowMode
ticker *time.Ticker
refreshChan chan struct{}
inputChan chan byte
}
type Tui struct {
analyzer analyze.Analyzer
// displayRecord is used to remember latest accesses time by specific IP
displayRecord map[netip.Prefix]time.Time
serverFilter string
mode ShowMode
ticker *time.Ticker
refreshChan chan struct{}
inputChan chan byte
noPrint atomic.Bool
}

status := TuiStatus{
func New(a *analyze.Analyzer) *Tui {
return &Tui{
displayRecord: make(map[netip.Prefix]time.Time),
serverFilter: "",
mode: TopValues,
ticker: time.NewTicker(time.Duration(a.Config.RefreshSec) * time.Second),
refreshChan: make(chan struct{}),
inputChan: make(chan byte),
}
}

go timerRoutine(status.ticker, status.refreshChan)
go waitForOneByte(status.inputChan)
func (t *Tui) Run() {
a := t.analyzer

Check failure on line 42 in pkg/tui/interface.go

View workflow job for this annotation

GitHub Actions / Build

assignment copies lock value to a: github.com/taoky/ayano/pkg/analyze.Analyzer contains sync.Mutex

Check failure on line 42 in pkg/tui/interface.go

View workflow job for this annotation

GitHub Actions / Build

assignment copies lock value to a: github.com/taoky/ayano/pkg/analyze.Analyzer contains sync.Mutex
go t.timerRoutine()
go t.waitForOneByte()

for {
select {
case k := <-status.inputChan:
case k := <-t.inputChan:
switch k {
case 'S', 's':
noPrint.Store(true)
t.noPrint.Store(true)
servers := a.GetCurrentServers()
if len(servers) == 1 {
serverFmt := ""
Expand All @@ -64,14 +69,14 @@ func Tui(a *analyze.Analyzer) {
if n != 0 {
fmt.Println("Failed to get input:", err)
} else {
status.serverFilter = ""
t.serverFilter = ""
}
} else {
found := false
for _, str := range servers {
if str == input {
found = true
status.serverFilter = input
t.serverFilter = input
break
}
}
Expand All @@ -80,13 +85,13 @@ func Tui(a *analyze.Analyzer) {
}
}
}
noPrint.Store(false)
t.noPrint.Store(false)
case 'T', 't':
if status.mode == TopValues {
status.mode = Total
if t.mode == TopValues {
t.mode = Total
fmt.Println("Switched to showing total")
} else {
status.mode = TopValues
t.mode = TopValues
fmt.Println("Switched to showing top values")
}
case '?':
Expand All @@ -98,10 +103,10 @@ func Tui(a *analyze.Analyzer) {
}
// This shall always run after input is handled.
// Don't write "continue" above!
go waitForOneByte(status.inputChan)
case <-status.refreshChan:
if status.mode == TopValues {
a.PrintTopValues(status.displayRecord, "size", status.serverFilter)
go t.waitForOneByte()
case <-t.refreshChan:
if t.mode == TopValues {
a.PrintTopValues(t.displayRecord, "size", t.serverFilter)
} else {
a.PrintTotal()
}
Expand Down
16 changes: 6 additions & 10 deletions pkg/tui/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,17 @@ package tui
import (
"log"
"os"
"sync/atomic"
"time"
)

var noPrint atomic.Bool

func timerRoutine(ticker *time.Ticker, refreshChan chan<- struct{}) {
for range ticker.C {
if !noPrint.Load() {
refreshChan <- struct{}{}
func (t *Tui) timerRoutine() {
for range t.ticker.C {
if !t.noPrint.Load() {
t.refreshChan <- struct{}{}
}
}
}

func waitForOneByte(inputChan chan<- byte) {
func (t *Tui) waitForOneByte() {
oldState, err := makeRaw(int(os.Stdin.Fd()))
if err != nil {
log.Fatal(err)
Expand All @@ -36,5 +32,5 @@ func waitForOneByte(inputChan chan<- byte) {
if n == 0 {
return
}
inputChan <- b[0]
t.inputChan <- b[0]
}

0 comments on commit 1747b5b

Please sign in to comment.